persistent-command-index.test.ts 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { describe, expect, it, vi } from "vitest";
  2. import { PersistentCommandIndex } from "../src/zcode-protocol-v4/persistent-command-index.js";
  3. const ack = (commandId: string, revisionAtDecision = 0) => ({
  4. commandId,
  5. status: "accepted" as const,
  6. revisionAtDecision,
  7. });
  8. describe("PersistentCommandIndex", () => {
  9. it("同 session 惰性加载一次,四个来源复用同一索引", async () => {
  10. const loadSession = vi.fn(async () => ({
  11. workspacePath: "/repo",
  12. facts: {
  13. transcript: [ack("transcript")],
  14. timeline: [ack("timeline")],
  15. },
  16. }));
  17. const index = new PersistentCommandIndex({ loadSession });
  18. expect(
  19. await index.lookup("transcript", { sessionId: "session-1", commandId: "transcript" }),
  20. ).toEqual(ack("transcript"));
  21. expect(
  22. await index.lookup("timeline", { sessionId: "session-1", commandId: "timeline" }),
  23. ).toEqual(ack("timeline"));
  24. expect(loadSession).toHaveBeenCalledTimes(1);
  25. });
  26. it("workspaceIdentity 优先于 workspacePath;foreign incremental record 明确拒绝", async () => {
  27. const index = new PersistentCommandIndex({
  28. loadSession: async () => ({
  29. workspacePath: "/same/path",
  30. workspaceIdentity: "remote:ssh:host-a:/same/path",
  31. }),
  32. });
  33. await expect(
  34. index.record(
  35. { workspacePath: "/same/path", workspaceIdentity: "remote:ssh:host-b:/same/path" },
  36. "session-1",
  37. "child",
  38. ack("fork"),
  39. ),
  40. ).rejects.toThrow("fault.command.queryForeignWorkspace");
  41. });
  42. it("incremental record 不触发第二次全量 load", async () => {
  43. const loadSession = vi.fn(async () => ({ workspacePath: "/repo" }));
  44. const index = new PersistentCommandIndex({ loadSession });
  45. await index.record({ workspacePath: "/repo" }, "session-1", "discarded", {
  46. commandId: "discarded",
  47. status: "failed",
  48. reasonCode: "fault.command.inputDiscardedOnRestart",
  49. revisionAtDecision: 0,
  50. });
  51. expect(
  52. await index.lookup("discarded", { sessionId: "session-1", commandId: "discarded" }),
  53. ).toMatchObject({ status: "failed" });
  54. expect(loadSession).toHaveBeenCalledTimes(1);
  55. });
  56. it("读取失败不缓存,恢复后可重试", async () => {
  57. let attempts = 0;
  58. const index = new PersistentCommandIndex({
  59. loadSession: async () => {
  60. attempts += 1;
  61. if (attempts === 1) throw new Error("sqlite busy");
  62. return { workspacePath: "/repo", facts: { transcript: [ack("recovered")] } };
  63. },
  64. });
  65. await expect(
  66. index.lookup("transcript", { sessionId: "session-1", commandId: "recovered" }),
  67. ).rejects.toThrow("sqlite busy");
  68. await expect(
  69. index.lookup("transcript", { sessionId: "session-1", commandId: "recovered" }),
  70. ).resolves.toEqual(ack("recovered"));
  71. });
  72. it("session unknown 不缓存,后续持久化出现后可见", async () => {
  73. let available = false;
  74. const loadSession = vi.fn(async () =>
  75. available ? { workspacePath: "/repo", facts: { transcript: [ack("appeared")] } } : null,
  76. );
  77. const index = new PersistentCommandIndex({ loadSession });
  78. const key = { sessionId: "session-later", commandId: "appeared" };
  79. await expect(index.lookup("transcript", key)).resolves.toBeNull();
  80. available = true;
  81. await expect(index.lookup("transcript", key)).resolves.toEqual(ack("appeared"));
  82. expect(loadSession).toHaveBeenCalledTimes(2);
  83. });
  84. });