Files
reverse-engineering-anything/src/domain/immutableJsonSteps.test.ts
T

103 lines
4.0 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { completeSteps } from "../../tests/support/completeSteps.js";
import {
freezeOwnedJsonSnapshotSteps,
immutableJsonSnapshotMember,
isImmutableJsonSnapshot,
} from "./immutableJson.js";
import { proxySnapshotFixture } from "./immutableJson.fixture.js";
import { jsonValueValidationIssue, type JsonValue } from "./jsonValue.js";
describe("cooperative owned snapshot sealing", () => {
it("locks reachable edges before yielding and authenticates only completion", () => {
const deferred: { [key: string]: JsonValue } = { value: 1 };
const value = {
deferred,
many: Array.from({ length: 10_000 }, () => ({ value: 1 })),
};
const steps = freezeOwnedJsonSnapshotSteps(value);
expect(steps.next().done).toBe(false);
expect(Object.isFrozen(value)).toBe(true);
expect(isImmutableJsonSnapshot(value)).toBe(false);
expect(immutableJsonSnapshotMember(value, "deferred")).toBeUndefined();
const introduced = { value: 2 };
deferred.child = introduced;
expect(completeSteps(steps)).toBe(value);
expect(Object.isFrozen(introduced)).toBe(true);
expect(isImmutableJsonSnapshot(value)).toBe(true);
expect(jsonValueValidationIssue(value)).toBeUndefined();
});
it("never authenticates abandoned or invalid provisional roots", () => {
const deferred: { [key: string]: JsonValue } = { value: 1 };
const value = {
deferred,
many: Array.from({ length: 10_000 }, () => ({ value: 1 })),
};
const steps = freezeOwnedJsonSnapshotSteps(value);
expect(steps.next().done).toBe(false);
deferred.value = Number.NaN;
expect(() => completeSteps(steps)).toThrow(
"Immutable JSON snapshots require JSON values",
);
expect(isImmutableJsonSnapshot(value)).toBe(false);
const cancelled = {
values: Array.from({ length: 10_000 }, () => ({ value: 1 })),
};
const interrupted = freezeOwnedJsonSnapshotSteps(cancelled);
expect(interrupted.next().done).toBe(false);
interrupted.return(cancelled);
expect(isImmutableJsonSnapshot(cancelled)).toBe(false);
expect(immutableJsonSnapshotMember(cancelled, "values")).toBeUndefined();
});
it("preserves aliases and rejects accessors, sparse values and cycles", () => {
const shared = { value: 1 };
const value = { a: shared, b: shared };
completeSteps(freezeOwnedJsonSnapshotSteps(value));
expect(Object.isFrozen(shared)).toBe(true);
const sparse: JsonValue[] = [];
sparse.length = 2;
const cycle: { [key: string]: JsonValue } = {};
cycle.self = cycle;
let accessed = false;
const accessor = {
get value() {
accessed = true;
return 1;
},
};
for (const [malformed, refusal] of [
[sparse, "Immutable JSON snapshots cannot contain sparse arrays"],
[cycle, "Immutable JSON snapshots cannot contain circular references"],
[accessor, "Immutable JSON snapshots cannot contain accessors"],
] as const) {
expect(() =>
completeSteps(freezeOwnedJsonSnapshotSteps(malformed)),
).toThrow(refusal);
expect(isImmutableJsonSnapshot(malformed)).toBe(false);
}
expect(accessed).toBe(false);
});
it("does not cache validation of a caller's shallow freeze", () => {
const value = Object.freeze({ child: { value: 1 } });
expect(jsonValueValidationIssue(value)).toBeUndefined();
value.child.value = Number.NaN;
expect(jsonValueValidationIssue(value)).toBe("JSON numbers must be finite");
});
it("never authenticates roots containing proxies or invokes their traps", () => {
const { proxy, reflected } = proxySnapshotFixture();
const root = { child: proxy };
for (const input of [proxy, root]) {
expect(() => completeSteps(freezeOwnedJsonSnapshotSteps(input))).toThrow(
"proxies",
);
expect(isImmutableJsonSnapshot(input)).toBe(false);
expect(immutableJsonSnapshotMember(input, "child")).toBeUndefined();
}
expect(reflected()).toBe(false);
});
});