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 Effect from "effect/Effect"; |
| 3 | import * as Exit from "effect/Exit"; |
| 4 | import { makeBucketState, type BucketObjects } from "@/State/BucketState.ts"; |
| 5 | |
| 6 | /** A bucket held in memory, with versioned conditional writes. */ |
| 7 | const memoryObjects = (): BucketObjects => { |
| 8 | const objects = new Map<string, { text: string; version: string }>(); |
| 9 | let next = 0; |
| 10 | return { |
| 11 | listKeys: (prefix) => |
| 12 | Effect.sync(() => [...objects.keys()].filter((key) => key.startsWith(prefix))), |
| 13 | listChildren: () => Effect.succeed([]), |
| 14 | read: (key) => Effect.sync(() => objects.get(key)), |
| 15 | write: (key, text, condition) => |
| 16 | Effect.sync(() => { |
| 17 | const existing = objects.get(key); |
| 18 | if (condition && "ifAbsent" in condition && existing) return undefined; |
| 19 | if (condition && "ifVersion" in condition && existing?.version !== condition.ifVersion) { |
| 20 | return undefined; |
| 21 | } |
| 22 | const version = String(++next); |
| 23 | objects.set(key, { text, version }); |
| 24 | return version; |
| 25 | }), |
| 26 | delete: (key, ifVersion) => |
| 27 | Effect.sync(() => { |
| 28 | if (ifVersion === undefined || objects.get(key)?.version === ifVersion) objects.delete(key); |
| 29 | }), |
| 30 | deleteMany: (keys) => Effect.sync(() => keys.forEach((key) => objects.delete(key))), |
| 31 | }; |
| 32 | }; |
| 33 | |
| 34 | describe("BucketState lease", { tags: ["unit", "local"] }, () => { |
| 35 | test("names an earlier open build in the same process as the holder", async () => { |
| 36 | const objects = Effect.succeed(memoryObjects()); |
| 37 | const open = makeBucketState({ id: "memory", label: "memory", objects }); |
| 38 | const request = { stack: "app", stage: "prod" }; |
| 39 | |
| 40 | const exit = await Effect.runPromise( |
| 41 | Effect.gen(function* () { |
| 42 | const first = yield* open; |
| 43 | const second = yield* open; |
| 44 | yield* first.list(request); |
| 45 | return yield* Effect.exit(second.list(request)); |
| 46 | }).pipe(Effect.scoped), |
| 47 | ); |
| 48 | |
| 49 | expect(Exit.isFailure(exit)).toBe(true); |
| 50 | expect(String(Exit.isFailure(exit) ? exit.cause : "")).toContain( |
| 51 | "another deploy holds the memory state lock 'app/prod': this process holds it", |
| 52 | ); |
| 53 | }); |
| 54 | }); |