feat: add core contracts
This commit is contained in:
26
packages/core/src/hash.test.ts
Normal file
26
packages/core/src/hash.test.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { canonicalize, hash } from "./hash.js";
|
||||
|
||||
describe("content hashing", () => {
|
||||
it("canonicalizes object keys lexicographically while preserving array order", () => {
|
||||
expect(canonicalize({ z: 1, a: [{ b: true, a: null }] })).toBe(
|
||||
'{"a":[{"a":null,"b":true}],"z":1}',
|
||||
);
|
||||
});
|
||||
|
||||
it("hashes equivalent object key orders to the same sha256 hex", () => {
|
||||
const left = hash({ z: 1, a: 2 });
|
||||
const right = hash({ a: 2, z: 1 });
|
||||
|
||||
expect(left).toBe(right);
|
||||
expect(left).toMatch(/^[a-f0-9]{64}$/);
|
||||
});
|
||||
|
||||
it("rejects values that are not JSON-safe", () => {
|
||||
expect(() => canonicalize({ date: new Date("2026-05-09T00:00:00Z") })).toThrow(
|
||||
/non-plain object/,
|
||||
);
|
||||
expect(() => canonicalize({ missing: undefined })).toThrow(/undefined/);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user