The best way to control Claude Code, Codex and more from any computer or phone
| 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 | deepEqual, |
| 9 | hasUnresolvedInputs, |
| 10 | havePropsChanged, |
| 11 | stripEffects, |
| 12 | stripUnresolved, |
| 13 | } from "@/Diff"; |
| 14 | import * as Output from "@/Output"; |
| 15 | |
| 16 | describe("Diff", { tags: ["unit", "local"] }, () => { |
| 17 | describe("havePropsChanged with Redacted values", () => { |
| 18 | // Config values yielded in a Worker's init phase (e.g. |
| 19 | // `yield* Config.String("MY_VARIABLE")`) land in `props.env` |
| 20 | // as `Redacted<string>`. Before unwrapping, every Redacted serialized |
| 21 | // to the constant mask `"<redacted>"`, so a changed secret was |
| 22 | // invisible to the diff and the Worker never redeployed. |
| 23 | test("detects a changed yielded config value in env", () => { |
| 24 | const olds = { |
| 25 | env: { |
| 26 | MY_VARIABLE: Redacted.make("my-variable-abc1234"), |
| 27 | }, |
| 28 | }; |
| 29 | const news = { |
| 30 | env: { |
| 31 | MY_VARIABLE: Redacted.make("my-variable-CHANGED"), |
| 32 | }, |
| 33 | }; |
| 34 | expect(havePropsChanged(olds, news)).toBe(true); |
| 35 | }); |
| 36 | |
| 37 | test("does not flag an unchanged yielded config value", () => { |
| 38 | const olds = { |
| 39 | env: { |
| 40 | MY_VARIABLE: Redacted.make("my-variable-abc1234"), |
| 41 | }, |
| 42 | }; |
| 43 | const news = { |
| 44 | env: { |
| 45 | MY_VARIABLE: Redacted.make("my-variable-abc1234"), |
| 46 | }, |
| 47 | }; |
| 48 | expect(havePropsChanged(olds, news)).toBe(false); |
| 49 | }); |
| 50 | |
| 51 | test("detects a changed top-level Redacted value", () => { |
| 52 | expect(havePropsChanged({ secret: Redacted.make("a") }, { secret: Redacted.make("b") })).toBe( |
| 53 | true, |
| 54 | ); |
| 55 | }); |
| 56 | |
| 57 | test("detects a Redacted value changing to a different inner type", () => { |
| 58 | expect( |
| 59 | havePropsChanged({ secret: Redacted.make("123") }, { secret: Redacted.make(123) }), |
| 60 | ).toBe(true); |
| 61 | }); |
| 62 | |
| 63 | test("detects a changed Redacted value nested in arrays", () => { |
| 64 | expect( |
| 65 | havePropsChanged( |
| 66 | { secrets: [Redacted.make("a"), Redacted.make("b")] }, |
| 67 | { secrets: [Redacted.make("a"), Redacted.make("c")] }, |
| 68 | ), |
| 69 | ).toBe(true); |
| 70 | }); |
| 71 | |
| 72 | test("does not flag unchanged plain env values", () => { |
| 73 | expect( |
| 74 | havePropsChanged({ env: { MY_VARIABLE: "value" } }, { env: { MY_VARIABLE: "value" } }), |
| 75 | ).toBe(false); |
| 76 | }); |
| 77 | |
| 78 | test("detects changed env when a Redacted value sits alongside plain values", () => { |
| 79 | const olds = { |
| 80 | env: { |
| 81 | MY_VARIABLE: "value", |
| 82 | MY_SECRET: Redacted.make("secret-1"), |
| 83 | }, |
| 84 | }; |
| 85 | const news = { |
| 86 | env: { |
| 87 | MY_VARIABLE: "value", |
| 88 | MY_SECRET: Redacted.make("secret-2"), |
| 89 | }, |
| 90 | }; |
| 91 | expect(havePropsChanged(olds, news)).toBe(true); |
| 92 | }); |
| 93 | }); |
| 94 | |
| 95 | describe("deepEqual with Redacted values", () => { |
| 96 | test("distinguishes Redacted values with different inner values", () => { |
| 97 | expect(deepEqual(Redacted.make("a"), Redacted.make("b"))).toBe(false); |
| 98 | }); |
| 99 | |
| 100 | test("equates Redacted values with the same inner value", () => { |
| 101 | expect(deepEqual(Redacted.make("a"), Redacted.make("a"))).toBe(true); |
| 102 | }); |
| 103 | |
| 104 | test("distinguishes Redacted values nested in objects", () => { |
| 105 | expect(deepEqual({ secret: Redacted.make("a") }, { secret: Redacted.make("b") })).toBe(false); |
| 106 | }); |
| 107 | }); |
| 108 | |
| 109 | // effect ≥4.0.0-beta.103's Context is self-referential; every deep walker |
| 110 | // must treat Effect/Layer/Context values as leaves instead of recursing |
| 111 | // into (or JSON-encoding) their internals (#1082). |
| 112 | describe("opaque effect values in props (#1082)", () => { |
| 113 | const opaqueProps = () => ({ |
| 114 | name: "worker", |
| 115 | exports: { |
| 116 | Store: { |
| 117 | kind: "durableObject", |
| 118 | constructor: Effect.void, |
| 119 | services: Context.empty(), |
| 120 | }, |
| 121 | }, |
| 122 | layer: Layer.empty, |
| 123 | }); |
| 124 | |
| 125 | test("hasUnresolvedInputs treats Layer/Context as resolved leaves", () => { |
| 126 | expect(hasUnresolvedInputs({ context: Context.empty(), layer: Layer.empty })).toBe(false); |
| 127 | }); |
| 128 | |
| 129 | test("stripUnresolved drops Effect/Layer/Context", () => { |
| 130 | const stripped: any = stripUnresolved(opaqueProps()); |
| 131 | expect(stripped.name).toBe("worker"); |
| 132 | expect(stripped.exports.Store.kind).toBe("durableObject"); |
| 133 | expect(stripped.exports.Store.constructor).toBeUndefined(); |
| 134 | expect(stripped.exports.Store.services).toBeUndefined(); |
| 135 | expect(stripped.layer).toBeUndefined(); |
| 136 | // Round-trips through JSON — the commit boundary's contract. |
| 137 | expect(() => JSON.stringify(stripped)).not.toThrow(); |
| 138 | }); |
| 139 | |
| 140 | test("stripEffects drops Effect/Layer/Context", () => { |
| 141 | const stripped: any = stripEffects(opaqueProps()); |
| 142 | expect(stripped.exports.Store.constructor).toBeUndefined(); |
| 143 | expect(stripped.exports.Store.services).toBeUndefined(); |
| 144 | expect(stripped.layer).toBeUndefined(); |
| 145 | }); |
| 146 | |
| 147 | test("havePropsChanged terminates and compares only plain data", () => { |
| 148 | expect(havePropsChanged(opaqueProps(), opaqueProps())).toBe(false); |
| 149 | expect( |
| 150 | havePropsChanged(opaqueProps(), { |
| 151 | ...opaqueProps(), |
| 152 | name: "renamed", |
| 153 | }), |
| 154 | ).toBe(true); |
| 155 | }); |
| 156 | |
| 157 | test("deepEqual does not recurse into Context internals", () => { |
| 158 | expect(deepEqual({ ctx: Context.empty() }, { ctx: Context.empty() })).toBe(true); |
| 159 | }); |
| 160 | |
| 161 | test("havePropsChanged terminates on cyclic plain objects", () => { |
| 162 | const make = () => { |
| 163 | const cyclic: any = { name: "cycle" }; |
| 164 | cyclic.self = cyclic; |
| 165 | return { config: cyclic }; |
| 166 | }; |
| 167 | // The full comparison — stripUnresolved cuts the cycle, so the |
| 168 | // JSON.stringify comparison sees identical truncated shapes. |
| 169 | expect(havePropsChanged(make(), make())).toBe(false); |
| 170 | expect(havePropsChanged(make(), { ...make(), extra: "x" } as any)).toBe(true); |
| 171 | expect(hasUnresolvedInputs(make())).toBe(false); |
| 172 | }); |
| 173 | }); |
| 174 | |
| 175 | describe("hasUnresolvedInputs across nesting shapes", () => { |
| 176 | test("finds an Output expr deep in arrays-in-objects-in-arrays", () => { |
| 177 | const expr = Output.literal("x"); |
| 178 | expect(hasUnresolvedInputs({ layers: [{ config: { hosts: [expr] } }] })).toBe(true); |
| 179 | expect(hasUnresolvedInputs({ matrix: [[[expr]]] })).toBe(true); |
| 180 | }); |
| 181 | |
| 182 | test("finds an Effect deep in nested containers", () => { |
| 183 | expect(hasUnresolvedInputs({ a: [{ b: { c: [Effect.void] } }] })).toBe(true); |
| 184 | }); |
| 185 | |
| 186 | test("a shared diamond subtree containing an expr is found from either parent", () => { |
| 187 | const shared = { dep: Output.literal("x") }; |
| 188 | expect(hasUnresolvedInputs({ left: shared, right: shared })).toBe(true); |
| 189 | expect(hasUnresolvedInputs({ left: { v: 1 }, right: shared })).toBe(true); |
| 190 | }); |
| 191 | |
| 192 | test("fully-plain deep structures are resolved", () => { |
| 193 | expect(hasUnresolvedInputs({ a: [{ b: [[{ c: 1 }]], d: new Date(0) }] })).toBe(false); |
| 194 | }); |
| 195 | }); |
| 196 | |
| 197 | describe("stripUnresolved semantics", () => { |
| 198 | test("preserves byte arrays by identity", () => { |
| 199 | const bytes = new Uint8Array([1, 2, 3]); |
| 200 | const stripped: any = stripUnresolved({ nested: [{ bytes }] }); |
| 201 | expect(stripped.nested[0].bytes).toBe(bytes); |
| 202 | }); |
| 203 | |
| 204 | test("preserves Date/Redacted/Duration by identity", () => { |
| 205 | const date = new Date(0); |
| 206 | const secret = Redacted.make("s"); |
| 207 | const dur = Duration.seconds(5); |
| 208 | const stripped: any = stripUnresolved({ date, secret, dur }); |
| 209 | expect(stripped.date).toBe(date); |
| 210 | expect(stripped.secret).toBe(secret); |
| 211 | expect(stripped.dur).toBe(dur); |
| 212 | }); |
| 213 | |
| 214 | test("drops Output exprs and Effects at any nesting depth", () => { |
| 215 | const stripped: any = stripUnresolved({ |
| 216 | deep: [{ inner: { expr: Output.literal("x") } }], |
| 217 | arr: [[Effect.void]], |
| 218 | keep: [{ v: 1 }], |
| 219 | }); |
| 220 | expect(stripped.deep[0].inner.expr).toBeUndefined(); |
| 221 | expect(stripped.arr[0][0]).toBeUndefined(); |
| 222 | expect(stripped.keep).toEqual([{ v: 1 }]); |
| 223 | }); |
| 224 | |
| 225 | test("cuts cycles but preserves diamonds", () => { |
| 226 | const shared = { v: 1 }; |
| 227 | const cyclic: any = { name: "c" }; |
| 228 | cyclic.self = cyclic; |
| 229 | const stripped: any = stripUnresolved({ |
| 230 | left: shared, |
| 231 | right: shared, |
| 232 | cyc: cyclic, |
| 233 | }); |
| 234 | expect(stripped.left).toEqual({ v: 1 }); |
| 235 | expect(stripped.right).toEqual({ v: 1 }); |
| 236 | expect(stripped.cyc.name).toBe("c"); |
| 237 | expect(stripped.cyc.self).toBeUndefined(); |
| 238 | expect(() => JSON.stringify(stripped)).not.toThrow(); |
| 239 | }); |
| 240 | }); |
| 241 | |
| 242 | describe("stripEffects semantics", () => { |
| 243 | test("keeps Output exprs intact but drops Effects, deeply", () => { |
| 244 | const expr = Output.literal("x"); |
| 245 | const stripped: any = stripEffects({ |
| 246 | deep: [{ expr, eff: Effect.void }], |
| 247 | }); |
| 248 | expect(stripped.deep[0].expr).toBe(expr); |
| 249 | expect(stripped.deep[0].eff).toBeUndefined(); |
| 250 | }); |
| 251 | |
| 252 | test("preserves Date/Redacted/Duration and cuts cycles", () => { |
| 253 | const date = new Date(0); |
| 254 | const cyclic: any = { date }; |
| 255 | cyclic.self = cyclic; |
| 256 | const stripped: any = stripEffects({ cyc: cyclic }); |
| 257 | expect(stripped.cyc.date).toBe(date); |
| 258 | expect(stripped.cyc.self).toBeUndefined(); |
| 259 | }); |
| 260 | }); |
| 261 | |
| 262 | describe("deepEqual / havePropsChanged across nesting shapes", () => { |
| 263 | test("deepEqual is key-order insensitive at every depth", () => { |
| 264 | expect( |
| 265 | deepEqual( |
| 266 | { a: [{ x: 1, y: [{ p: 1, q: 2 }] }], b: 2 }, |
| 267 | { b: 2, a: [{ y: [{ q: 2, p: 1 }], x: 1 }] }, |
| 268 | ), |
| 269 | ).toBe(true); |
| 270 | }); |
| 271 | |
| 272 | test("deepEqual compares Dates by value", () => { |
| 273 | expect(deepEqual({ d: new Date("2027-01-01") }, { d: new Date("2027-01-01") })).toBe(true); |
| 274 | expect(deepEqual({ d: new Date("2027-01-01") }, { d: new Date("2027-01-02") })).toBe(false); |
| 275 | }); |
| 276 | |
| 277 | test("deepEqual compares byte arrays by content", () => { |
| 278 | expect(deepEqual({ b: new Uint8Array([1, 2]) }, { b: new Uint8Array([1, 2]) })).toBe(true); |
| 279 | expect(deepEqual({ b: new Uint8Array([1, 2]) }, { b: new Uint8Array([1, 3]) })).toBe(false); |
| 280 | }); |
| 281 | |
| 282 | test("deepEqual compares Durations by value", () => { |
| 283 | expect(deepEqual({ d: Duration.seconds(5) }, { d: Duration.seconds(5) })).toBe(true); |
| 284 | expect(deepEqual({ d: Duration.seconds(5) }, { d: Duration.seconds(6) })).toBe(false); |
| 285 | }); |
| 286 | |
| 287 | test("deepEqual terminates on cyclic values on either side", () => { |
| 288 | const make = () => { |
| 289 | const c: any = { v: 1 }; |
| 290 | c.self = c; |
| 291 | return c; |
| 292 | }; |
| 293 | expect(deepEqual({ c: make() }, { c: make() })).toBe(true); |
| 294 | }); |
| 295 | |
| 296 | test("havePropsChanged detects a change deep in arrays-in-objects-in-arrays", () => { |
| 297 | const shape = (url: string) => ({ |
| 298 | layers: [{ config: { hosts: [{ url }, { url: "static" }] } }], |
| 299 | }); |
| 300 | expect(havePropsChanged(shape("a"), shape("a"))).toBe(false); |
| 301 | expect(havePropsChanged(shape("a"), shape("b"))).toBe(true); |
| 302 | }); |
| 303 | |
| 304 | test("havePropsChanged detects array length and order changes", () => { |
| 305 | expect(havePropsChanged({ arr: [[1, 2]] }, { arr: [[2, 1]] } as any)).toBe(true); |
| 306 | expect(havePropsChanged({ arr: [[1]] }, { arr: [[1], []] } as any)).toBe(true); |
| 307 | }); |
| 308 | |
| 309 | test("havePropsChanged detects a changed Date", () => { |
| 310 | expect( |
| 311 | havePropsChanged({ expires: new Date("2027-01-01") }, { expires: new Date("2027-01-01") }), |
| 312 | ).toBe(false); |
| 313 | expect( |
| 314 | havePropsChanged({ expires: new Date("2027-01-01") }, { expires: new Date("2028-01-01") }), |
| 315 | ).toBe(true); |
| 316 | }); |
| 317 | |
| 318 | test("class instances never churn a diff — different instances compare equal", () => { |
| 319 | class SdkConfig { |
| 320 | constructor(readonly v: number) {} |
| 321 | } |
| 322 | // Runtime-only wiring: stripped at the commit boundary, so two deploys |
| 323 | // constructing fresh instances must not report a phantom change. |
| 324 | expect( |
| 325 | havePropsChanged({ config: new SdkConfig(1) } as any, { config: new SdkConfig(2) } as any), |
| 326 | ).toBe(false); |
| 327 | }); |
| 328 | }); |
| 329 | }); |