feat: add core contracts

This commit is contained in:
chungyeong
2026-05-09 22:45:44 +09:00
parent 42f0fb193d
commit 44103839af
11 changed files with 377 additions and 0 deletions

View 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/);
});
});