windowHostSessionsIndexObserver.test.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { Emitter } from "@zcode/rpc";
  2. import type { IZCodeAgentService, ZCodeAgentRuntimeLifecycleEvent } from "@zcode/services";
  3. import type {
  4. SessionsIndexTopicWireCandidate,
  5. SessionSummary,
  6. } from "@zcode/shared/zcode-protocol-v4";
  7. import { describe, expect, it, vi } from "vitest";
  8. import { createWindowHostSessionsIndexObserver } from "../src/host/windowHostSessionsIndexObserver.js";
  9. function summary(overrides: Partial<SessionSummary> = {}): SessionSummary {
  10. return {
  11. sessionId: "task-1",
  12. workspaceId: "/work/demo",
  13. title: "Task",
  14. phase: "running",
  15. sessionEnded: false,
  16. hasBackgroundWork: false,
  17. lastActivityAt: 2,
  18. createdAt: 1,
  19. ...overrides,
  20. };
  21. }
  22. describe("WindowHostSessionsIndexObserver", () => {
  23. it("runtime available 在 subscribe ACK 前到达时不会递归创建第二条订阅", async () => {
  24. const frames = new Emitter<SessionsIndexTopicWireCandidate>();
  25. const lifecycle = new Emitter<ZCodeAgentRuntimeLifecycleEvent>();
  26. const subscribeSessionsIndexV4 = vi.fn(async () => {
  27. lifecycle.fire({
  28. workspaceKey: "/work/demo",
  29. workspacePath: "/work/demo",
  30. state: "available",
  31. runtimeIdentity: {
  32. workspaceKey: "/work/demo",
  33. generation: 1,
  34. identity: "runtime-1",
  35. },
  36. });
  37. return { ack: { subscriptionId: "sub-1", mode: "snapshot" as const, logEpoch: "e1" } };
  38. });
  39. const agentService = {
  40. onDynamicSessionsIndexFrame: vi.fn(() => frames.event),
  41. onAgentRuntimeLifecycle: lifecycle.event,
  42. onAgentRuntimeRestarted: vi.fn(() => ({ dispose: vi.fn() })),
  43. subscribeSessionsIndexV4,
  44. resyncSessionsIndexV4: vi.fn(),
  45. unsubscribeSessionsIndexV4: vi.fn(async () => undefined),
  46. } as unknown as IZCodeAgentService;
  47. const observer = createWindowHostSessionsIndexObserver({
  48. agentService,
  49. target: { workspacePath: "/work/demo" },
  50. onSessionsChange: vi.fn(),
  51. });
  52. await observer.start();
  53. expect(subscribeSessionsIndexV4).toHaveBeenCalledTimes(1);
  54. observer.dispose();
  55. });
  56. it("ACK 前暂存 initial frame,并按 delta 连续更新内存 summary", async () => {
  57. const frames = new Emitter<SessionsIndexTopicWireCandidate>();
  58. const changes: SessionSummary[][] = [];
  59. const unsubscribeSessionsIndexV4 = vi.fn(async () => undefined);
  60. const agentService = {
  61. onDynamicSessionsIndexFrame: vi.fn(() => frames.event),
  62. onAgentRuntimeRestarted: vi.fn(() => ({ dispose: vi.fn() })),
  63. subscribeSessionsIndexV4: vi.fn(async () => {
  64. frames.fire({
  65. wireVersion: 3,
  66. kind: "complete",
  67. deliveryKind: "initial",
  68. logicalFrameId: "logical-initial",
  69. logicalFrameOrdinal: 1,
  70. topic: "sessions-index//work/demo",
  71. subscriptionId: "sub-1",
  72. frame: {
  73. topic: "sessions-index//work/demo",
  74. subscriptionId: "sub-1",
  75. fromSeq: 0,
  76. toSeq: 1,
  77. sentAt: 1,
  78. payload: {
  79. kind: "snapshot",
  80. snapshot: {
  81. protocolVersion: 1,
  82. workspaceId: "/work/demo",
  83. logEpoch: "epoch-1",
  84. sessions: [summary()],
  85. },
  86. },
  87. },
  88. });
  89. return { ack: { subscriptionId: "sub-1", mode: "snapshot", logEpoch: "epoch-1" } };
  90. }),
  91. resyncSessionsIndexV4: vi.fn(),
  92. unsubscribeSessionsIndexV4,
  93. } as unknown as IZCodeAgentService;
  94. const observer = createWindowHostSessionsIndexObserver({
  95. agentService,
  96. target: { workspacePath: "/work/demo" },
  97. onSessionsChange: (sessions) => changes.push(sessions),
  98. });
  99. await observer.start();
  100. expect(agentService.subscribeSessionsIndexV4).toHaveBeenCalledWith(
  101. expect.objectContaining({
  102. subscriberScope: "window-controller",
  103. runtimePolicy: "existing-only",
  104. visibility: "background",
  105. }),
  106. );
  107. expect(changes.at(-1)).toEqual([expect.objectContaining({ phase: "running" })]);
  108. frames.fire({
  109. wireVersion: 3,
  110. kind: "complete",
  111. deliveryKind: "online",
  112. logicalFrameId: "logical-delta",
  113. logicalFrameOrdinal: 2,
  114. topic: "sessions-index//work/demo",
  115. subscriptionId: "sub-1",
  116. frame: {
  117. topic: "sessions-index//work/demo",
  118. subscriptionId: "sub-1",
  119. fromSeq: 1,
  120. toSeq: 2,
  121. sentAt: 2,
  122. payload: {
  123. kind: "deltas",
  124. deltas: [{ op: "session.upserted", session: summary({ phase: "completedSuccess" }) }],
  125. },
  126. },
  127. });
  128. expect(changes.at(-1)).toEqual([expect.objectContaining({ phase: "completedSuccess" })]);
  129. observer.dispose();
  130. expect(unsubscribeSessionsIndexV4).toHaveBeenCalledWith(
  131. expect.objectContaining({ subscriptionId: "sub-1", runtimePolicy: "existing-only" }),
  132. );
  133. });
  134. });