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

main
ts 89 lines 3.84 KB
Raw
1 import { describe, expect } from "@alchemy.run/test";
2 import * as Test from "@alchemy.run/test/Alchemy";
3 import * as Core from "@alchemy.run/test/Core";
4 import * as Effect from "effect/Effect";
5 import { State } from "@/State";
6 import { TestLayers, TestResource } from "./test.resources.ts";
7
8 const { test } = Test.make({ providers: TestLayers() });
9
10 // `test.provider` scratch state must be DURABLE (disk-backed) when the
11 // adapter can namespace it by test file. A run killed while a provider
12 // delete is still in flight (the runner abandons teardown 10s after a test
13 // timeout) leaves its `deleting` rows behind; the next run's leading
14 // `stack.destroy()` must see those rows and resume the delete. With the old
15 // in-memory scratch store the rows died with the process, the next destroy
16 // planned "no changes", and the cloud resource was orphaned invisibly.
17 //
18 // Cross-process durability is exercised here as cross-INSTANCE durability:
19 // each `Core.scratchStack` call builds a fresh store handle, so a second
20 // instance only sees the first instance's rows if they were persisted.
21 describe("test.provider scratch state durability", { tags: ["unit", "local"] }, () => {
22 const options = { providers: TestLayers() };
23 const NAME = "durability-probe";
24 const FILE = "test/scratch-state.test.ts";
25
26 const listRows = (scratch: Core.ScratchStack, stackName: string) =>
27 Effect.gen(function* () {
28 const state = yield* yield* State;
29 return yield* state.list({ stack: stackName, stage: scratch.stage });
30 }).pipe(Effect.provide(scratch.state));
31
32 test(
33 "rows persist across scratch instances and a later destroy drains them",
34 Effect.gen(function* () {
35 // First "run": deploy a resource, then walk away without destroying —
36 // the shape of a run whose teardown was abandoned.
37 const run1 = Core.scratchStack(options, NAME, FILE);
38 yield* run1.deploy(
39 Effect.gen(function* () {
40 yield* TestResource("A", { string: "v1" });
41 }),
42 );
43
44 // Second "run": a fresh scratch instance for the same (file, test)
45 // must see the persisted row...
46 const run2 = Core.scratchStack(options, NAME, FILE);
47 expect(run2.name).toEqual(run1.name);
48 expect(yield* listRows(run2, run2.name)).toEqual(["A"]);
49
50 // ...and its destroy must reclaim it.
51 yield* run2.destroy();
52 const run3 = Core.scratchStack(options, NAME, FILE);
53 expect(yield* listRows(run3, run3.name)).toEqual([]);
54 }),
55 );
56
57 test(
58 "scratch stack names are namespaced by file",
59 Effect.gen(function* () {
60 // Test names repeat across files ("create and delete bucket with
61 // default props" exists in several suites); the durable store must
62 // never collide across files or one test's destroy would delete
63 // another's live cloud resources.
64 const a = Core.scratchStack(options, "same name", "test/A/One.test.ts");
65 const b = Core.scratchStack(options, "same name", "test/B/One.test.ts");
66 expect(a.name).not.toEqual(b.name);
67
68 // Without a file (bun/vitest adapters), the store stays in-memory and
69 // the name stays the bare test name — the pre-existing behavior.
70 const bare = Core.scratchStack(options, "same name");
71 expect(bare.name).toEqual("same-name");
72 }),
73 );
74
75 test(
76 "scratch stacks default to ALCHEMY_TEST_STAGE, then test_${USER}",
77 Effect.sync(() => {
78 // `--stage` sets ALCHEMY_TEST_STAGE for the whole run.
79 const expected =
80 process.env.ALCHEMY_TEST_STAGE ||
81 `test_${process.env.USER || process.env.USERNAME || "unknown"}`;
82 expect(Core.defaultStage()).toBe(expected);
83 const scratch = Core.scratchStack(options, NAME, FILE);
84 expect(scratch.stage).toBe(expected);
85 const overridden = Core.scratchStack({ ...options, stage: "custom" }, NAME, FILE);
86 expect(overridden.stage).toBe("custom");
87 }),
88 );
89 });