tracker.test.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // 修订号 tracker(spec §4 第 4 类):apps/zcode-cli/docs/design/v2/model/context-composition.md §3.4。
  2. import { describe, expect, it } from "vitest";
  3. import { V4_NOTIFICATIONS } from "@zcode/shared/zcode-protocol-v4";
  4. import {
  5. ContextCompositionRevisionTracker,
  6. getContextCompositionTracker,
  7. subscribeSessionContextComposition,
  8. } from "../../src/zcode-protocol/context-composition/index.js";
  9. const flushMicrotasks = () => new Promise<void>((resolve) => setImmediate(resolve));
  10. describe("context composition revision tracker", () => {
  11. it("coalesces changes in one turn of the event loop into one revision and one notification", async () => {
  12. const emitted: Array<{ sessionId: string; revision: number }> = [];
  13. const tracker = new ContextCompositionRevisionTracker((event) => emitted.push(event));
  14. tracker.markChanged("a");
  15. tracker.markChanged("a");
  16. tracker.markChanged("b");
  17. // 修订号在第一次变更时就加一:同一轮里的查询读到的已是新值。
  18. expect(tracker.revision("a")).toBe(1);
  19. expect(emitted).toEqual([]);
  20. await flushMicrotasks();
  21. expect(emitted).toEqual([
  22. { sessionId: "a", revision: 1 },
  23. { sessionId: "b", revision: 1 },
  24. ]);
  25. tracker.markChanged("a");
  26. await flushMicrotasks();
  27. expect(emitted.at(-1)).toEqual({ sessionId: "a", revision: 2 });
  28. expect(tracker.revision("a")).toBe(2);
  29. expect(tracker.revision("never-changed")).toBe(0);
  30. });
  31. it("wires runtime changes to v4 notifications and stops after unsubscribe", async () => {
  32. const notifications: Array<{ method: string; params: unknown }> = [];
  33. const context = {
  34. contextCompositionTracker: undefined,
  35. notify: (notification: { method: string; params?: unknown }) =>
  36. notifications.push({ method: notification.method, params: notification.params }),
  37. };
  38. const listeners = new Set<(change: { reason: string }) => void>();
  39. const runtime = {
  40. subscribeContextCompositionChanged: (listener: (change: { reason: string }) => void) => {
  41. listeners.add(listener);
  42. return () => listeners.delete(listener);
  43. },
  44. };
  45. const unsubscribe = subscribeSessionContextComposition(
  46. context as never,
  47. "s1",
  48. runtime as never,
  49. );
  50. for (const listener of listeners) {
  51. listener({ reason: "request" });
  52. listener({ reason: "request_usage" });
  53. }
  54. await flushMicrotasks();
  55. expect(notifications).toEqual([
  56. {
  57. method: V4_NOTIFICATIONS.conversationContextCompositionChanged,
  58. params: { sessionId: "s1", revision: 1 },
  59. },
  60. ]);
  61. expect(getContextCompositionTracker(context as never).revision("s1")).toBe(1);
  62. unsubscribe();
  63. expect(listeners.size).toBe(0);
  64. // 测试桩 runtime 没有订阅方法时不接线,也不抛错。
  65. expect(() =>
  66. subscribeSessionContextComposition(context as never, "s2", {} as never)(),
  67. ).not.toThrow();
  68. });
  69. });