The best way to control Claude Code, Codex and more from any computer or phone

main
ts 140 lines 6.07 KB
Raw
1 import { describe, expect, test } from "@alchemy.run/test";
2 import * as Duration from "effect/Duration";
3 import { deepEqual } from "@/Diff";
4 import {
5 BYTES_MARKER,
6 DATE_MARKER,
7 decodeDuration,
8 DURATION_MARKER,
9 encodeState,
10 reviveState,
11 reviveStateRecursive,
12 } from "@/State/StateEncoding";
13
14 // Dates in persisted props must ROUND-TRIP: a provider's `diff`/`delete`/
15 // `read` receive `olds` from the state store on a later run, and a
16 // Date-typed prop must come back as a real Date — not `{}` (the old
17 // structural-walk bug, which churned a phantom update on every plan) and
18 // not a bare ISO string (which lies about the declared prop type).
19 describe("StateEncoding Duration round-trip", { tags: ["unit", "local"] }, () => {
20 // The revived value is untyped JSON, but a Duration in = a Duration out is
21 // exactly what these tests assert; the cast lets `Duration.equals` (which
22 // no longer accepts `unknown`) verify it.
23 const roundTrip = (value: Duration.Duration) =>
24 JSON.parse(JSON.stringify(encodeState(value)), reviveState) as Duration.Duration;
25
26 test("finite, infinity, and negative infinity survive encode → JSON → revive", () => {
27 expect(Duration.equals(roundTrip(Duration.seconds(15)), Duration.seconds(15))).toBe(true);
28 expect(Duration.equals(roundTrip(Duration.infinity), Duration.infinity)).toBe(true);
29 expect(Duration.equals(roundTrip(Duration.negativeInfinity), Duration.negativeInfinity)).toBe(
30 true,
31 );
32 });
33
34 test("decodeDuration rebuilds Duration.toJSON without clamping negatives", () => {
35 expect(
36 Duration.equals(
37 decodeDuration(Duration.negativeInfinity.toJSON())!,
38 Duration.negativeInfinity,
39 ),
40 ).toBe(true);
41 expect(encodeState(Duration.millis(15000))).toEqual({
42 [DURATION_MARKER]: Duration.millis(15000).toJSON(),
43 });
44 });
45 });
46
47 describe("StateEncoding Date round-trip", { tags: ["unit", "local"] }, () => {
48 const value = {
49 expires: new Date("2027-01-01T00:00:00.000Z"),
50 nested: { dates: [new Date("2028-06-15T12:30:00.000Z")] },
51 text: "unrelated",
52 };
53
54 test("encode → JSON → reviveState rebuilds Date instances (local store path)", () => {
55 const revived = JSON.parse(JSON.stringify(encodeState(value)), reviveState) as typeof value;
56 expect(revived.expires).toBeInstanceOf(Date);
57 expect(revived.expires.toISOString()).toBe("2027-01-01T00:00:00.000Z");
58 expect(revived.nested.dates[0]).toBeInstanceOf(Date);
59 expect(revived.nested.dates[0]!.toISOString()).toBe("2028-06-15T12:30:00.000Z");
60 expect(revived.text).toBe("unrelated");
61 });
62
63 test("encode → transport JSON → reviveStateRecursive rebuilds Date instances (HTTP store path)", () => {
64 // The HTTP state store sends `encodeState(...)` as a JSON payload and
65 // revives the pre-parsed value on the way back out.
66 const transported = JSON.parse(JSON.stringify(encodeState(value)));
67 const revived = reviveStateRecursive(transported) as typeof value;
68 expect(revived.expires).toBeInstanceOf(Date);
69 expect(revived.expires.toISOString()).toBe("2027-01-01T00:00:00.000Z");
70 expect(revived.nested.dates[0]).toBeInstanceOf(Date);
71 });
72
73 test("the persisted envelope is the marker, not a structural walk", () => {
74 expect(encodeState(new Date("2027-01-01T00:00:00.000Z"))).toEqual({
75 [DATE_MARKER]: "2027-01-01T00:00:00.000Z",
76 });
77 });
78
79 test("legacy rows with bare ISO strings stay strings and never churn", () => {
80 // Rows written before DATE_MARKER persisted plain ISO strings. The
81 // reviver must not promote arbitrary strings to Dates — and the diff
82 // must still treat them as equal to the program's live Date, so legacy
83 // rows re-deploy without a phantom update.
84 const legacy = JSON.parse(
85 JSON.stringify({ expires: "2027-01-01T00:00:00.000Z" }),
86 reviveState,
87 ) as { expires: unknown };
88 expect(typeof legacy.expires).toBe("string");
89 expect(
90 deepEqual(
91 { expires: new Date("2027-01-01T00:00:00.000Z") },
92 { expires: "2027-01-01T00:00:00.000Z" },
93 ),
94 ).toBe(true);
95 });
96
97 test("a user object that happens to carry the marker key is not corrupted into a Date", () => {
98 // Both revivers only rewrite EXACT single-key envelopes, so a user prop
99 // shaped `{ __date__: ..., other: ... }` survives untouched through the
100 // HTTP path and the local JSON.parse path alike.
101 const suspicious = { [DATE_MARKER]: "2027-01-01T00:00:00.000Z", n: 1 };
102 const viaHttp = reviveStateRecursive(
103 JSON.parse(JSON.stringify(suspicious)),
104 ) as typeof suspicious;
105 expect(viaHttp.n).toBe(1);
106 expect(typeof viaHttp[DATE_MARKER]).toBe("string");
107 const viaLocal = JSON.parse(JSON.stringify(suspicious), reviveState) as typeof suspicious;
108 expect(viaLocal.n).toBe(1);
109 expect(typeof viaLocal[DATE_MARKER]).toBe("string");
110 });
111 });
112
113 // Byte-valued props (a WAF byte-match string, a zip) must round-trip too:
114 // drift repair and recreate run the provider with the persisted props, so a
115 // dropped or mangled byte array cannot be rebuilt.
116 describe("StateEncoding bytes round-trip", { tags: ["unit", "local"] }, () => {
117 const value = {
118 search: new TextEncoder().encode("/blocked"),
119 nested: [{ zip: new Uint8Array([0, 255]) }],
120 };
121
122 test("encode → JSON → reviveState rebuilds Uint8Array instances (local store path)", () => {
123 const revived = JSON.parse(JSON.stringify(encodeState(value)), reviveState) as typeof value;
124 expect(revived.search).toBeInstanceOf(Uint8Array);
125 expect(new TextDecoder().decode(revived.search)).toBe("/blocked");
126 expect([...revived.nested[0]!.zip]).toEqual([0, 255]);
127 });
128
129 test("encode → transport JSON → reviveStateRecursive rebuilds Uint8Array instances (HTTP store path)", () => {
130 const revived = reviveStateRecursive(
131 JSON.parse(JSON.stringify(encodeState(value))),
132 ) as typeof value;
133 expect(revived.search).toBeInstanceOf(Uint8Array);
134 expect([...revived.nested[0]!.zip]).toEqual([0, 255]);
135 });
136
137 test("the persisted envelope is the base64 marker", () => {
138 expect(encodeState(new Uint8Array([1, 2, 3]))).toEqual({ [BYTES_MARKER]: "AQID" });
139 });
140 });