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

main
ts 172 lines 5.8 KB
Raw
1 import { describe, expect, test } from "@alchemy.run/test";
2 import * as Context from "effect/Context";
3 import * as Duration from "effect/Duration";
4 import * as Effect from "effect/Effect";
5 import * as Layer from "effect/Layer";
6 import * as Redacted from "effect/Redacted";
7 import {
8 isPlainData,
9 isPlainObject,
10 mapPlainData,
11 stripNullFields,
12 stripUndefinedFields,
13 unwrapRedacted,
14 } from "@/Util/data";
15
16 describe("data utilities", { tags: ["unit", "local"] }, () => {
17 test("isPlainObject accepts object literals", () => {
18 expect(isPlainObject({})).toBe(true);
19 });
20
21 test("isPlainObject rejects arrays and object instances", () => {
22 expect(isPlainObject([])).toBe(false);
23 expect(isPlainObject(Object.create(null))).toBe(false);
24 expect(isPlainObject(new Date())).toBe(false);
25 expect(isPlainObject(Redacted.make("secret"))).toBe(false);
26 });
27
28 test("stripNullFields removes nulls recursively from arrays and records", () => {
29 expect(
30 stripNullFields({
31 a: null,
32 b: undefined,
33 c: [{ d: null, e: 1 }],
34 }),
35 ).toEqual({
36 b: undefined,
37 c: [{ e: 1 }],
38 });
39 });
40
41 test("stripUndefinedFields removes undefined recursively from arrays and records", () => {
42 expect(
43 stripUndefinedFields({
44 a: null,
45 b: undefined,
46 c: [{ d: undefined, e: 1 }],
47 }),
48 ).toEqual({
49 a: null,
50 c: [{ e: 1 }],
51 });
52 });
53
54 test("unwrapRedacted unwraps only arrays and plain records recursively", () => {
55 const date = new Date("2026-05-20T00:00:00.000Z");
56
57 expect(
58 unwrapRedacted({
59 value: Redacted.make("secret"),
60 nested: [Redacted.make("nested")],
61 wrapped: Redacted.make({ token: Redacted.make("s3cr3t") }),
62 date,
63 }),
64 ).toEqual({
65 value: "secret",
66 nested: ["nested"],
67 wrapped: { token: "s3cr3t" },
68 date,
69 });
70 });
71 });
72
73 // The engine's traversal rule (#1082): plain data is walked; every class
74 // instance is a leaf. isPlainData is the single gate, mapPlainData the
75 // single cycle-guarded rebuild — pin their exact semantics here.
76 describe("isPlainData", { tags: ["unit", "local"] }, () => {
77 test("accepts arrays, object literals, and null-prototype objects", () => {
78 expect(isPlainData([])).toBe(true);
79 expect(isPlainData([1, 2, 3])).toBe(true);
80 expect(isPlainData({})).toBe(true);
81 expect(isPlainData({ a: 1 })).toBe(true);
82 expect(isPlainData(Object.create(null))).toBe(true);
83 });
84
85 test("rejects primitives and functions", () => {
86 expect(isPlainData(undefined)).toBe(false);
87 expect(isPlainData(null)).toBe(false);
88 expect(isPlainData(0)).toBe(false);
89 expect(isPlainData("s")).toBe(false);
90 expect(isPlainData(true)).toBe(false);
91 expect(isPlainData(Symbol("s"))).toBe(false);
92 expect(isPlainData(10n)).toBe(false);
93 expect(isPlainData(() => {})).toBe(false);
94 });
95
96 test("rejects every class instance — built-in, effect, and user-defined", () => {
97 class Custom {}
98 expect(isPlainData(new Custom())).toBe(false);
99 expect(isPlainData(new Date())).toBe(false);
100 expect(isPlainData(new Map())).toBe(false);
101 expect(isPlainData(new Set())).toBe(false);
102 expect(isPlainData(new URL("https://example.com"))).toBe(false);
103 expect(isPlainData(/regex/)).toBe(false);
104 expect(isPlainData(Buffer.from("x"))).toBe(false);
105 expect(isPlainData(Effect.succeed(1))).toBe(false);
106 expect(isPlainData(Layer.empty)).toBe(false);
107 expect(isPlainData(Context.empty())).toBe(false);
108 expect(isPlainData(Redacted.make("s"))).toBe(false);
109 expect(isPlainData(Duration.seconds(1))).toBe(false);
110 });
111 });
112
113 describe("mapPlainData", { tags: ["unit", "local"] }, () => {
114 const identity = (ancestors: WeakSet<object>) => {
115 const walk = (child: unknown): unknown =>
116 isPlainData(child) ? mapPlainData(child, ancestors, walk) : child;
117 return walk;
118 };
119
120 test("rebuilds arrays as arrays and objects as objects", () => {
121 const ancestors = new WeakSet<object>();
122 const rebuilt: any = mapPlainData(
123 { a: [1, { b: 2 }], c: { d: [3] } },
124 ancestors,
125 identity(ancestors),
126 );
127 expect(rebuilt).toEqual({ a: [1, { b: 2 }], c: { d: [3] } });
128 expect(Array.isArray(rebuilt.a)).toBe(true);
129 expect(Array.isArray(rebuilt.c.d)).toBe(true);
130 });
131
132 test("cuts a self-referencing object to undefined", () => {
133 const cyclic: any = { name: "x" };
134 cyclic.self = cyclic;
135 const ancestors = new WeakSet<object>();
136 const rebuilt: any = mapPlainData(cyclic, ancestors, identity(ancestors));
137 expect(rebuilt.name).toBe("x");
138 expect(rebuilt.self).toBeUndefined();
139 });
140
141 test("cuts a mutual object<->array cycle to undefined", () => {
142 const obj: any = { tag: "obj" };
143 const arr: any[] = [obj];
144 obj.arr = arr;
145 const ancestors = new WeakSet<object>();
146 const rebuilt: any = mapPlainData({ root: obj }, ancestors, identity(ancestors));
147 expect(rebuilt.root.tag).toBe("obj");
148 expect(rebuilt.root.arr[0]).toBeUndefined();
149 });
150
151 test("preserves diamonds — the same object referenced twice is rebuilt twice", () => {
152 const shared = { v: 1 };
153 const ancestors = new WeakSet<object>();
154 const rebuilt: any = mapPlainData(
155 { left: shared, right: shared, list: [shared] },
156 ancestors,
157 identity(ancestors),
158 );
159 expect(rebuilt.left).toEqual({ v: 1 });
160 expect(rebuilt.right).toEqual({ v: 1 });
161 expect(rebuilt.list[0]).toEqual({ v: 1 });
162 });
163
164 test("leaves the ancestors set empty afterwards (guard is path-scoped)", () => {
165 const value = { a: { b: [1] } };
166 const ancestors = new WeakSet<object>();
167 mapPlainData(value, ancestors, identity(ancestors));
168 // Re-walking the same value must not see stale ancestors.
169 const again: any = mapPlainData(value, ancestors, identity(ancestors));
170 expect(again).toEqual({ a: { b: [1] } });
171 });
172 });