sourceServerStatusRegistryDirectoryEvents.test.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { describe, expect, it, vi } from "vitest";
  2. import { Emitter } from "@zcode/rpc";
  3. import type { ISourceServerManagementService } from "@zcode/services";
  4. import { getSourceServerStatusRegistry } from "@/store/sourceServerStatusRegistry.js";
  5. /**
  6. * 合同 §8.1.4 消费方双订阅:removeSourceServer 只产生 Directory 写入(onDidChangeDirectory),
  7. * status registry 必须据此权威重读并清掉已删设备的状态,否则陈旧 online 态会残留。
  8. */
  9. describe("Source Server status registry directory events", () => {
  10. it("drops the removed device state after a directory delta without any state change", async () => {
  11. const stateEmitter = new Emitter<never>();
  12. const directoryEmitter = new Emitter<{ generation: number }>();
  13. const withDevice = {
  14. generation: 1,
  15. profiles: [
  16. {
  17. profileId: "p-1",
  18. transport: "docker",
  19. target: { kind: "docker", container: "dev" },
  20. sourceServerId: "source-a",
  21. createdAt: 1,
  22. },
  23. ],
  24. servers: [
  25. {
  26. sourceServerId: "source-a",
  27. displayName: "Server A",
  28. installState: "installed",
  29. },
  30. ],
  31. workspaceHistory: [],
  32. states: [
  33. {
  34. profileId: "p-1",
  35. sourceServerId: "source-a",
  36. state: "online",
  37. updateAvailability: "none",
  38. updatedAt: 1,
  39. },
  40. ],
  41. failedPathCount: 0,
  42. abandonedPathCount: 0,
  43. migrationCounts: [],
  44. };
  45. const removed = {
  46. ...withDevice,
  47. generation: 2,
  48. profiles: [],
  49. servers: [],
  50. states: [],
  51. };
  52. const getSnapshot = vi
  53. .fn()
  54. .mockResolvedValueOnce(withDevice)
  55. .mockResolvedValueOnce(removed);
  56. const management = {
  57. getSnapshot,
  58. onDidChangeState: stateEmitter.event,
  59. onDidChangeDirectory: directoryEmitter.event,
  60. } as unknown as ISourceServerManagementService;
  61. const registry = getSourceServerStatusRegistry(management);
  62. const dispose = registry.subscribe(vi.fn());
  63. await vi.waitFor(() =>
  64. expect(registry.getSnapshot().get("source-a")).toBe("online"),
  65. );
  66. directoryEmitter.fire({ generation: 2 });
  67. await vi.waitFor(() =>
  68. expect(registry.getSnapshot().has("source-a")).toBe(false),
  69. );
  70. expect(getSnapshot).toHaveBeenCalledTimes(2);
  71. dispose();
  72. });
  73. });