The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it } from "@alchemy.run/test"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as FileSystem from "effect/FileSystem"; |
| 4 | import * as Path from "effect/Path"; |
| 5 | import { STATE_STORE_VERSION } from "@/State/HttpStateApi.ts"; |
| 6 | import { makeLocalState } from "@/State/LocalState.ts"; |
| 7 | import type { ResourceState } from "@/State/ResourceState.ts"; |
| 8 | import { initialCwd } from "@/Util/Node.ts"; |
| 9 | import { PlatformServices } from "@/Util/PlatformServices"; |
| 10 | |
| 11 | const resource = ( |
| 12 | fqn: string, |
| 13 | attr: Record<string, unknown>, |
| 14 | overrides?: Partial<ResourceState>, |
| 15 | ): ResourceState => |
| 16 | ({ |
| 17 | resourceType: "test:resource", |
| 18 | namespace: undefined, |
| 19 | fqn, |
| 20 | logicalId: fqn, |
| 21 | instanceId: `instance-${fqn}`, |
| 22 | providerVersion: 1, |
| 23 | status: "created", |
| 24 | downstream: [], |
| 25 | bindings: [], |
| 26 | props: {}, |
| 27 | attr, |
| 28 | ...overrides, |
| 29 | }) as ResourceState; |
| 30 | |
| 31 | /** |
| 32 | * Absolute path into the store's on-disk layout (`.alchemy/state/...`). |
| 33 | * |
| 34 | * Anchored to `initialCwd` — the same anchor `makeLocalState` uses — instead |
| 35 | * of a live `process.cwd()` read: another suite's in-process build (e.g. a |
| 36 | * Waku source build) can transiently chdir the whole test process, and a |
| 37 | * live read racing that window points at an unrelated (missing) tree. |
| 38 | */ |
| 39 | const statePath = (...segments: string[]) => |
| 40 | Effect.gen(function* () { |
| 41 | const path = yield* Path.Path; |
| 42 | return path.join(initialCwd, ".alchemy", "state", ...segments); |
| 43 | }); |
| 44 | |
| 45 | describe("makeLocalState", { tags: ["unit", "local"] }, () => { |
| 46 | it.effect("reads of absent state recover to empty results", () => |
| 47 | Effect.gen(function* () { |
| 48 | const state = yield* makeLocalState(); |
| 49 | const stack = "local-state-test-absent"; |
| 50 | |
| 51 | expect(yield* state.get({ stack, stage: "test", fqn: "nope" })).toBeUndefined(); |
| 52 | expect(yield* state.list({ stack, stage: "test" })).toEqual([]); |
| 53 | expect(yield* state.listStages(stack)).toEqual([]); |
| 54 | expect(yield* state.getOutput({ stack, stage: "test" })).toBeUndefined(); |
| 55 | expect(yield* state.getReplacedResources({ stack, stage: "test" })).toEqual([]); |
| 56 | }).pipe(Effect.provide(PlatformServices)), |
| 57 | ); |
| 58 | |
| 59 | it.effect("set/get roundtrip with list, listStacks and listStages", () => |
| 60 | Effect.gen(function* () { |
| 61 | const state = yield* makeLocalState(); |
| 62 | const stack = "local-state-test-crud"; |
| 63 | const stage = "test"; |
| 64 | |
| 65 | const plain = resource("resource-a", { value: "a" }); |
| 66 | // fqns contain `/` separators; the store must encode them into flat |
| 67 | // file names and decode them back on list. |
| 68 | const nested = resource("scope/nested/resource-b", { value: "b" }); |
| 69 | yield* state.set({ stack, stage, fqn: plain.fqn, value: plain }); |
| 70 | yield* state.set({ stack, stage, fqn: nested.fqn, value: nested }); |
| 71 | |
| 72 | expect(yield* state.get({ stack, stage, fqn: plain.fqn })).toEqual(plain); |
| 73 | expect(yield* state.get({ stack, stage, fqn: nested.fqn })).toEqual(nested); |
| 74 | expect((yield* state.list({ stack, stage })).toSorted()).toEqual([ |
| 75 | "resource-a", |
| 76 | "scope/nested/resource-b", |
| 77 | ]); |
| 78 | expect(yield* state.listStacks()).toContain(stack); |
| 79 | expect(yield* state.listStages(stack)).toEqual([stage]); |
| 80 | |
| 81 | yield* state.deleteStack({ stack }); |
| 82 | }).pipe(Effect.provide(PlatformServices)), |
| 83 | ); |
| 84 | |
| 85 | it.effect("delete removes one resource without touching its siblings", () => |
| 86 | Effect.gen(function* () { |
| 87 | const state = yield* makeLocalState(); |
| 88 | const stack = "local-state-test-delete"; |
| 89 | const stage = "test"; |
| 90 | const a = resource("resource-a", { value: "a" }); |
| 91 | const b = resource("resource-b", { value: "b" }); |
| 92 | yield* state.set({ stack, stage, fqn: a.fqn, value: a }); |
| 93 | yield* state.set({ stack, stage, fqn: b.fqn, value: b }); |
| 94 | |
| 95 | yield* state.delete({ stack, stage, fqn: a.fqn }); |
| 96 | // deleting an already-deleted resource is not an error |
| 97 | yield* state.delete({ stack, stage, fqn: a.fqn }); |
| 98 | |
| 99 | expect(yield* state.get({ stack, stage, fqn: a.fqn })).toBeUndefined(); |
| 100 | expect(yield* state.get({ stack, stage, fqn: b.fqn })).toEqual(b); |
| 101 | |
| 102 | // `delete` only removes files, so the directory cache stays valid |
| 103 | // and a re-set must persist. |
| 104 | yield* state.set({ stack, stage, fqn: a.fqn, value: a }); |
| 105 | expect(yield* state.get({ stack, stage, fqn: a.fqn })).toEqual(a); |
| 106 | |
| 107 | yield* state.deleteStack({ stack }); |
| 108 | }).pipe(Effect.provide(PlatformServices)), |
| 109 | ); |
| 110 | |
| 111 | it.effect("list excludes bookkeeping, in-flight and non-json files", () => |
| 112 | Effect.gen(function* () { |
| 113 | const fs = yield* FileSystem.FileSystem; |
| 114 | const state = yield* makeLocalState(); |
| 115 | const stack = "local-state-test-list-filter"; |
| 116 | const stage = "test"; |
| 117 | const a = resource("resource-a", { value: "a" }); |
| 118 | yield* state.set({ stack, stage, fqn: a.fqn, value: a }); |
| 119 | // writes `__stack_output__.json`, which must not surface as a resource |
| 120 | yield* state.setOutput({ stack, stage, value: { out: true } }); |
| 121 | // a lingering writeAtomic temp file must not surface either |
| 122 | const tmp = yield* statePath(stack, stage, "resource-a.json.123.tmp"); |
| 123 | yield* fs.writeFileString(tmp, "{}"); |
| 124 | |
| 125 | expect(yield* state.list({ stack, stage })).toEqual(["resource-a"]); |
| 126 | |
| 127 | yield* state.deleteStack({ stack }); |
| 128 | }).pipe(Effect.provide(PlatformServices)), |
| 129 | ); |
| 130 | |
| 131 | it.effect("get treats a zero-length state file as absent", () => |
| 132 | Effect.gen(function* () { |
| 133 | const fs = yield* FileSystem.FileSystem; |
| 134 | const state = yield* makeLocalState(); |
| 135 | const stack = "local-state-test-empty-file"; |
| 136 | const stage = "test"; |
| 137 | const dir = yield* statePath(stack, stage); |
| 138 | yield* fs.makeDirectory(dir, { recursive: true }); |
| 139 | const path = yield* Path.Path; |
| 140 | yield* fs.writeFileString(path.join(dir, "resource-a.json"), ""); |
| 141 | |
| 142 | expect(yield* state.get({ stack, stage, fqn: "resource-a" })).toBeUndefined(); |
| 143 | |
| 144 | yield* state.deleteStack({ stack }); |
| 145 | }).pipe(Effect.provide(PlatformServices)), |
| 146 | ); |
| 147 | |
| 148 | it.effect("getReplacedResources returns only replaced resources", () => |
| 149 | Effect.gen(function* () { |
| 150 | const state = yield* makeLocalState(); |
| 151 | const stack = "local-state-test-replaced"; |
| 152 | const stage = "test"; |
| 153 | const created = resource("resource-a", { value: "a" }); |
| 154 | const replaced = resource("resource-b", { value: "b" }, { |
| 155 | status: "replaced", |
| 156 | old: resource("resource-b", { value: "old" }), |
| 157 | deleteFirst: false, |
| 158 | } as Partial<ResourceState>); |
| 159 | yield* state.set({ stack, stage, fqn: created.fqn, value: created }); |
| 160 | yield* state.set({ stack, stage, fqn: replaced.fqn, value: replaced }); |
| 161 | |
| 162 | expect(yield* state.getReplacedResources({ stack, stage })).toEqual([replaced]); |
| 163 | |
| 164 | yield* state.deleteStack({ stack }); |
| 165 | }).pipe(Effect.provide(PlatformServices)), |
| 166 | ); |
| 167 | |
| 168 | it.effect("setOutput/getOutput roundtrip", () => |
| 169 | Effect.gen(function* () { |
| 170 | const state = yield* makeLocalState(); |
| 171 | const stack = "local-state-test-output"; |
| 172 | const stage = "test"; |
| 173 | const value = { url: "https://example.com", nested: { n: 1 } }; |
| 174 | yield* state.setOutput({ stack, stage, value }); |
| 175 | |
| 176 | expect(yield* state.getOutput({ stack, stage })).toEqual(value); |
| 177 | |
| 178 | yield* state.deleteStack({ stack }); |
| 179 | }).pipe(Effect.provide(PlatformServices)), |
| 180 | ); |
| 181 | |
| 182 | it.effect("set persists again after deleteStack removes the stage dir", () => |
| 183 | Effect.gen(function* () { |
| 184 | const state = yield* makeLocalState(); |
| 185 | const key = { |
| 186 | stack: "local-state-created-cache-test", |
| 187 | stage: "test", |
| 188 | fqn: "resource-a", |
| 189 | }; |
| 190 | |
| 191 | yield* state.set({ ...key, value: resource(key.fqn, { round: 1 }) }); |
| 192 | expect(yield* state.get(key)).toMatchObject({ attr: { round: 1 } }); |
| 193 | |
| 194 | yield* state.deleteStack({ stack: key.stack }); |
| 195 | expect(yield* state.get(key)).toBeUndefined(); |
| 196 | |
| 197 | // Before the `created`-cache invalidation, this write skipped |
| 198 | // makeDirectory, failed with NotFound, and silently no-op'd. |
| 199 | yield* state.set({ ...key, value: resource(key.fqn, { round: 2 }) }); |
| 200 | expect(yield* state.get(key)).toMatchObject({ attr: { round: 2 } }); |
| 201 | |
| 202 | yield* state.deleteStack({ stack: key.stack }); |
| 203 | }).pipe(Effect.provide(PlatformServices)), |
| 204 | ); |
| 205 | |
| 206 | it.effect("set persists again after a stage-scoped deleteStack", () => |
| 207 | Effect.gen(function* () { |
| 208 | const state = yield* makeLocalState(); |
| 209 | const key = { |
| 210 | stack: "local-state-created-cache-test-stage", |
| 211 | stage: "test", |
| 212 | fqn: "resource-a", |
| 213 | }; |
| 214 | |
| 215 | yield* state.set({ ...key, value: resource(key.fqn, { round: 1 }) }); |
| 216 | yield* state.deleteStack({ stack: key.stack, stage: key.stage }); |
| 217 | expect(yield* state.get(key)).toBeUndefined(); |
| 218 | |
| 219 | yield* state.set({ ...key, value: resource(key.fqn, { round: 2 }) }); |
| 220 | expect(yield* state.get(key)).toMatchObject({ attr: { round: 2 } }); |
| 221 | |
| 222 | yield* state.deleteStack({ stack: key.stack }); |
| 223 | }).pipe(Effect.provide(PlatformServices)), |
| 224 | ); |
| 225 | |
| 226 | it.effect("whole-stack deleteStack invalidates every stage's cache", () => |
| 227 | Effect.gen(function* () { |
| 228 | const state = yield* makeLocalState(); |
| 229 | const stack = "local-state-created-cache-test-multi"; |
| 230 | const fqn = "resource-a"; |
| 231 | |
| 232 | // ensure both stage dirs are cached before the wholesale delete — |
| 233 | // this exercises the prefix (not exact) match in the cache pruning |
| 234 | yield* state.set({ |
| 235 | stack, |
| 236 | stage: "a", |
| 237 | fqn, |
| 238 | value: resource(fqn, { round: 1 }), |
| 239 | }); |
| 240 | yield* state.set({ |
| 241 | stack, |
| 242 | stage: "b", |
| 243 | fqn, |
| 244 | value: resource(fqn, { round: 1 }), |
| 245 | }); |
| 246 | yield* state.deleteStack({ stack }); |
| 247 | |
| 248 | yield* state.set({ |
| 249 | stack, |
| 250 | stage: "a", |
| 251 | fqn, |
| 252 | value: resource(fqn, { round: 2 }), |
| 253 | }); |
| 254 | yield* state.set({ |
| 255 | stack, |
| 256 | stage: "b", |
| 257 | fqn, |
| 258 | value: resource(fqn, { round: 2 }), |
| 259 | }); |
| 260 | expect(yield* state.get({ stack, stage: "a", fqn })).toMatchObject({ |
| 261 | attr: { round: 2 }, |
| 262 | }); |
| 263 | expect(yield* state.get({ stack, stage: "b", fqn })).toMatchObject({ |
| 264 | attr: { round: 2 }, |
| 265 | }); |
| 266 | |
| 267 | yield* state.deleteStack({ stack }); |
| 268 | }).pipe(Effect.provide(PlatformServices)), |
| 269 | ); |
| 270 | |
| 271 | it.effect("stage-scoped deleteStack leaves the sibling stage intact", () => |
| 272 | Effect.gen(function* () { |
| 273 | const state = yield* makeLocalState(); |
| 274 | const stack = "local-state-created-cache-test-sibling"; |
| 275 | const fqn = "resource-a"; |
| 276 | |
| 277 | yield* state.set({ |
| 278 | stack, |
| 279 | stage: "a", |
| 280 | fqn, |
| 281 | value: resource(fqn, { round: 1 }), |
| 282 | }); |
| 283 | yield* state.set({ |
| 284 | stack, |
| 285 | stage: "b", |
| 286 | fqn, |
| 287 | value: resource(fqn, { round: 1 }), |
| 288 | }); |
| 289 | yield* state.deleteStack({ stack, stage: "a" }); |
| 290 | |
| 291 | // sibling stage keeps its files and its still-valid cache entry |
| 292 | expect(yield* state.get({ stack, stage: "b", fqn })).toMatchObject({ |
| 293 | attr: { round: 1 }, |
| 294 | }); |
| 295 | yield* state.set({ |
| 296 | stack, |
| 297 | stage: "b", |
| 298 | fqn, |
| 299 | value: resource(fqn, { round: 2 }), |
| 300 | }); |
| 301 | expect(yield* state.get({ stack, stage: "b", fqn })).toMatchObject({ |
| 302 | attr: { round: 2 }, |
| 303 | }); |
| 304 | |
| 305 | // deleted stage is gone and re-settable |
| 306 | expect(yield* state.get({ stack, stage: "a", fqn })).toBeUndefined(); |
| 307 | yield* state.set({ |
| 308 | stack, |
| 309 | stage: "a", |
| 310 | fqn, |
| 311 | value: resource(fqn, { round: 2 }), |
| 312 | }); |
| 313 | expect(yield* state.get({ stack, stage: "a", fqn })).toMatchObject({ |
| 314 | attr: { round: 2 }, |
| 315 | }); |
| 316 | |
| 317 | yield* state.deleteStack({ stack }); |
| 318 | }).pipe(Effect.provide(PlatformServices)), |
| 319 | ); |
| 320 | |
| 321 | it.effect("setOutput persists again after deleteStack", () => |
| 322 | Effect.gen(function* () { |
| 323 | const state = yield* makeLocalState(); |
| 324 | const stack = "local-state-created-cache-test-output"; |
| 325 | const stage = "test"; |
| 326 | |
| 327 | // setOutput goes through the same `ensure`/`created` cache as `set` |
| 328 | yield* state.setOutput({ stack, stage, value: { round: 1 } }); |
| 329 | yield* state.deleteStack({ stack }); |
| 330 | expect(yield* state.getOutput({ stack, stage })).toBeUndefined(); |
| 331 | |
| 332 | yield* state.setOutput({ stack, stage, value: { round: 2 } }); |
| 333 | expect(yield* state.getOutput({ stack, stage })).toEqual({ round: 2 }); |
| 334 | |
| 335 | yield* state.deleteStack({ stack }); |
| 336 | }).pipe(Effect.provide(PlatformServices)), |
| 337 | ); |
| 338 | |
| 339 | it.effect("getVersion returns the state store version", () => |
| 340 | Effect.gen(function* () { |
| 341 | const state = yield* makeLocalState(); |
| 342 | expect(yield* state.getVersion()).toBe(STATE_STORE_VERSION); |
| 343 | }).pipe(Effect.provide(PlatformServices)), |
| 344 | ); |
| 345 | |
| 346 | it.effect("deleteStack removes the stack from listings", () => |
| 347 | Effect.gen(function* () { |
| 348 | const state = yield* makeLocalState(); |
| 349 | const stack = "local-state-test-delete-listing"; |
| 350 | yield* state.set({ |
| 351 | stack, |
| 352 | stage: "test", |
| 353 | fqn: "resource-a", |
| 354 | value: resource("resource-a", {}), |
| 355 | }); |
| 356 | |
| 357 | yield* state.deleteStack({ stack }); |
| 358 | |
| 359 | expect(yield* state.listStacks()).not.toContain(stack); |
| 360 | expect(yield* state.listStages(stack)).toEqual([]); |
| 361 | }).pipe(Effect.provide(PlatformServices)), |
| 362 | ); |
| 363 | |
| 364 | it.effect("concurrent writes to the same resource never corrupt it", () => |
| 365 | Effect.gen(function* () { |
| 366 | const state = yield* makeLocalState(); |
| 367 | const stack = "local-state-test-concurrent"; |
| 368 | const stage = "test"; |
| 369 | const fqn = "resource-a"; |
| 370 | |
| 371 | const rounds = Array.from({ length: 20 }, (_, i) => i + 1); |
| 372 | yield* Effect.all( |
| 373 | rounds.map((round) => state.set({ stack, stage, fqn, value: resource(fqn, { round }) })), |
| 374 | { concurrency: "unbounded" }, |
| 375 | ); |
| 376 | |
| 377 | // whichever write won, the file must parse as one complete state — |
| 378 | // never a truncated interleaving (the writeAtomic guarantee) |
| 379 | const final = (yield* state.get({ stack, stage, fqn })) as ResourceState; |
| 380 | expect(rounds).toContain((final.attr as { round: number }).round); |
| 381 | |
| 382 | yield* state.deleteStack({ stack }); |
| 383 | }).pipe(Effect.provide(PlatformServices)), |
| 384 | ); |
| 385 | |
| 386 | describe("weird names", () => { |
| 387 | it.effect("unicode, spaces and punctuation in every name position", () => |
| 388 | Effect.gen(function* () { |
| 389 | const state = yield* makeLocalState(); |
| 390 | const stack = "local wéird 🚀..stack"; |
| 391 | const stage = "stage v1.2.3 ünïcode"; |
| 392 | const fqn = "my resource (α) #1 100%"; |
| 393 | const value = resource(fqn, { value: "weird" }); |
| 394 | |
| 395 | yield* state.set({ stack, stage, fqn, value }); |
| 396 | expect(yield* state.get({ stack, stage, fqn })).toEqual(value); |
| 397 | expect(yield* state.list({ stack, stage })).toEqual([fqn]); |
| 398 | expect(yield* state.listStages(stack)).toEqual([stage]); |
| 399 | |
| 400 | yield* state.delete({ stack, stage, fqn }); |
| 401 | expect(yield* state.get({ stack, stage, fqn })).toBeUndefined(); |
| 402 | |
| 403 | yield* state.deleteStack({ stack }); |
| 404 | }).pipe(Effect.provide(PlatformServices)), |
| 405 | ); |
| 406 | |
| 407 | it.effect("empty-string fqn roundtrips", () => |
| 408 | Effect.gen(function* () { |
| 409 | const state = yield* makeLocalState(); |
| 410 | const stack = "local-state-test-empty-fqn"; |
| 411 | const stage = "test"; |
| 412 | const value = resource("", { empty: true }); |
| 413 | |
| 414 | yield* state.set({ stack, stage, fqn: "", value }); |
| 415 | expect(yield* state.get({ stack, stage, fqn: "" })).toEqual(value); |
| 416 | expect(yield* state.list({ stack, stage })).toEqual([""]); |
| 417 | |
| 418 | yield* state.deleteStack({ stack }); |
| 419 | }).pipe(Effect.provide(PlatformServices)), |
| 420 | ); |
| 421 | |
| 422 | it.effect("literal __ in a logical id: get roundtrips, list decodes", () => |
| 423 | Effect.gen(function* () { |
| 424 | const state = yield* makeLocalState(); |
| 425 | const stack = "local-state-test-underscores"; |
| 426 | const stage = "test"; |
| 427 | const value = resource("foo__bar", { value: "u" }); |
| 428 | |
| 429 | yield* state.set({ stack, stage, fqn: "foo__bar", value }); |
| 430 | expect(yield* state.get({ stack, stage, fqn: "foo__bar" })).toEqual(value); |
| 431 | // Known encoding collision: `/` is stored as `__`, so decodeFqn |
| 432 | // cannot distinguish a literal `__` in a logical id from a |
| 433 | // namespace separator. `list` reports this resource as "foo/bar". |
| 434 | expect(yield* state.list({ stack, stage })).toEqual(["foo/bar"]); |
| 435 | |
| 436 | yield* state.deleteStack({ stack }); |
| 437 | }).pipe(Effect.provide(PlatformServices)), |
| 438 | ); |
| 439 | |
| 440 | it.effect("a resource named __stack_output__ never surfaces in list", () => |
| 441 | Effect.gen(function* () { |
| 442 | const state = yield* makeLocalState(); |
| 443 | const stack = "local-state-test-output-clash"; |
| 444 | const stage = "test"; |
| 445 | const value = resource("__stack_output__", { value: "clash" }); |
| 446 | |
| 447 | yield* state.set({ stack, stage, fqn: "__stack_output__", value }); |
| 448 | expect(yield* state.list({ stack, stage })).toEqual([]); |
| 449 | |
| 450 | yield* state.deleteStack({ stack }); |
| 451 | }).pipe(Effect.provide(PlatformServices)), |
| 452 | ); |
| 453 | |
| 454 | it.effect("overlong names fail with a typed StateStoreError", () => |
| 455 | Effect.gen(function* () { |
| 456 | const state = yield* makeLocalState(); |
| 457 | const stack = "local-state-test-overlong"; |
| 458 | const stage = "test"; |
| 459 | // > 255 bytes exceeds the filename limit; only NotFound recovers, |
| 460 | // so the platform error must surface as a typed StateStoreError |
| 461 | // (never a silent no-op). |
| 462 | const fqn = "x".repeat(300); |
| 463 | |
| 464 | const setError = yield* state |
| 465 | .set({ stack, stage, fqn, value: resource(fqn, {}) }) |
| 466 | .pipe(Effect.flip); |
| 467 | expect(setError._tag).toBe("StateStoreError"); |
| 468 | |
| 469 | const getError = yield* state.get({ stack, stage, fqn }).pipe(Effect.flip); |
| 470 | expect(getError._tag).toBe("StateStoreError"); |
| 471 | |
| 472 | yield* state.deleteStack({ stack }); |
| 473 | }).pipe(Effect.provide(PlatformServices)), |
| 474 | ); |
| 475 | }); |
| 476 | }); |