journal-store.test.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { describe, expect, it } from "vitest";
  2. import { InMemoryJournalStore, canonicalJson, fnv1a, inputHash } from "../../src/engine/index.js";
  3. import { runJournalStoreContract } from "../../src/testing/journal-contract.js";
  4. // 内存实现跑通共享契约;SQLite 实现在 @zcode/adapters 经 `@zcode/dynamic-workflow/testing` 跑同一份。
  5. runJournalStoreContract(() => new InMemoryJournalStore());
  6. describe("canonicalJson", () => {
  7. it("sorts object keys so semantically equal values serialize identically", () => {
  8. expect(canonicalJson({ b: 1, a: 2 })).toBe(canonicalJson({ a: 2, b: 1 }));
  9. expect(canonicalJson({ a: 2, b: 1 })).toBe('{"a":2,"b":1}');
  10. });
  11. it("is stable across nesting and arrays", () => {
  12. expect(canonicalJson({ x: [{ z: 1, y: 2 }] })).toBe('{"x":[{"y":2,"z":1}]}');
  13. });
  14. it("skips undefined members like JSON.stringify", () => {
  15. expect(canonicalJson({ a: undefined, b: 1 })).toBe('{"b":1}');
  16. });
  17. });
  18. describe("fnv1a / inputHash", () => {
  19. it("is deterministic and 8-hex-wide", () => {
  20. const h = fnv1a("hello");
  21. expect(h).toMatch(/^[0-9a-f]{8}$/);
  22. expect(fnv1a("hello")).toBe(h);
  23. });
  24. it("distinguishes different inputs", () => {
  25. expect(fnv1a("hello")).not.toBe(fnv1a("world"));
  26. });
  27. it("hashes canonical JSON so key order does not change the hash", () => {
  28. expect(inputHash({ a: 1, b: 2 })).toBe(inputHash({ b: 2, a: 1 }));
  29. expect(inputHash("do it")).not.toBe(inputHash("do that"));
  30. });
  31. });