journal-contract-isolation.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /**
  2. * 修复原因:journal-contract.ts 顶到 oxlint max-lines 上限(400 行),把存储边界隔离与按 runId 作用域的收尾用例拆到本文件;
  3. * 公开面仍从 journal-contract.ts 导出(`runJournalStoreContract` 按原顺序调用各主题的注册函数)。
  4. *
  5. * 注意:这里只登记 `it(...)`,不另开 describe——套件的分组与用例顺序必须与拆分前逐字相同。
  6. */
  7. import { expect, it } from "vitest";
  8. import type { JournalStorePort, NodeRecord } from "../engine/index.js";
  9. import { baseRun } from "./journal-contract-helpers.js";
  10. export function registerIsolationCases(factory: () => JournalStorePort): void {
  11. it("isolates stored records from later mutation of the caller's object", () => {
  12. const store = factory();
  13. store.createRun(baseRun("r1"));
  14. const node: NodeRecord = {
  15. runId: "r1",
  16. siteId: "ask#1",
  17. ordinal: 1,
  18. kind: "ask",
  19. inputHash: "abc",
  20. status: "completed",
  21. result: { nested: { value: 1 } },
  22. };
  23. store.putNode(node);
  24. // 改动调用方持有的对象不应影响已存记录(存储边界隔离)。
  25. (node.result as { nested: { value: number } }).nested.value = 999;
  26. const stored = store.getNode("r1", "ask#1", 1);
  27. expect(stored).toBeDefined();
  28. const result = stored!.result as { nested: { value: number } };
  29. expect(result.nested.value).toBe(1);
  30. });
  31. it("scopes records per runId", () => {
  32. const store = factory();
  33. store.createRun(baseRun("r1"));
  34. store.createRun(baseRun("r2"));
  35. store.putNode({
  36. runId: "r1",
  37. siteId: "ask#1",
  38. ordinal: 1,
  39. kind: "ask",
  40. inputHash: "h",
  41. status: "completed",
  42. });
  43. expect(store.listNodes("r1", { kinds: "all", withResult: true })).toHaveLength(1);
  44. expect(store.listNodes("r2", { kinds: "all", withResult: true })).toHaveLength(0);
  45. expect(store.getNode("r2", "ask#1", 1)).toBeUndefined();
  46. });
  47. }