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

main
ts 124 lines 4.63 KB
Raw
1 import { makePostgresState } from "@alchemy.run/sql/PostgresState";
2 import { describe, expect, it } from "@alchemy.run/test";
3 import { StateStoreError, type StateService } from "alchemy/State/State";
4 import * as Effect from "effect/Effect";
5 import * as Redacted from "effect/Redacted";
6
7 /**
8 * Live counterpart of `PostgresState.test.ts`. The hermetic suite answers the
9 * store's fixed SQL from an in-memory fake, so the statements Postgres itself
10 * must accept — `hashtextextended` key derivation, `pg_try_advisory_lock`
11 * session semantics, the `pg_locks` holder verification, the transactional
12 * schema migration — only run for real here.
13 *
14 * Guarded like the other live suites: set
15 * `ALCHEMY_RUN_LIVE_POSTGRES_STATE_TESTS=true` and point
16 * `POSTGRES_STATE_TEST_URL` at a scratch Postgres database.
17 */
18 const wantsLive = process.env.ALCHEMY_RUN_LIVE_POSTGRES_STATE_TESTS === "true";
19 const liveUrl = process.env.POSTGRES_STATE_TEST_URL?.trim();
20 const runLive = wantsLive && !!liveUrl;
21
22 if (wantsLive && !liveUrl) {
23 it.effect(
24 "requires a database url for the live Postgres state suite",
25 () =>
26 Effect.fail(
27 new Error(
28 [
29 "Live Postgres state suite requested but no database is configured.",
30 "Set POSTGRES_STATE_TEST_URL to a scratch Postgres connection URL,",
31 "then rerun with ALCHEMY_RUN_LIVE_POSTGRES_STATE_TESTS=true.",
32 ].join(" "),
33 ),
34 ),
35 { tags: ["local"] },
36 );
37 }
38
39 /** Each store gets its own pool, as separate deploy processes would. */
40 const withLiveStore = <A, E>(
41 use: (store: StateService) => Effect.Effect<A, E>,
42 ): Effect.Effect<A, E | StateStoreError> =>
43 Effect.gen(function* () {
44 const scope = yield* Effect.scope;
45 const store = yield* makePostgresState({ url: Redacted.make(liveUrl!) }, scope);
46 return yield* use(store);
47 }).pipe(Effect.scoped);
48
49 const stack = "alchemy-postgres-state-live";
50 const request = { stack, stage: "test", fqn: `${stack}/test/db` };
51
52 const sampleState = {
53 kind: "resource",
54 status: "created",
55 logicalId: "db",
56 output: { password: Redacted.make("s3cret") },
57 } as never;
58
59 describe.skipIf(!runLive)("Postgres state store against real Postgres", { tags: ["local"] }, () => {
60 it.effect("round-trips resource state through a real database", () =>
61 withLiveStore((store) =>
62 Effect.gen(function* () {
63 // Leftovers from an interrupted earlier run must not fail this one.
64 yield* store.deleteStack({ stack });
65
66 expect(yield* store.get(request)).toBeUndefined();
67
68 yield* store.set({ ...request, value: sampleState });
69 const revived = (yield* store.get(request)) as {
70 output: { password: Redacted.Redacted<string> };
71 };
72 expect(Redacted.isRedacted(revived.output.password)).toBe(true);
73 expect(Redacted.value(revived.output.password)).toBe("s3cret");
74
75 yield* store.delete(request);
76 expect(yield* store.get(request)).toBeUndefined();
77
78 yield* store.deleteStack({ stack });
79 }),
80 ),
81 );
82
83 it.effect("refuses a second deploy while the stage advisory lock is held", () =>
84 withLiveStore((holder) =>
85 Effect.gen(function* () {
86 // The first operation acquires the session advisory lock for the
87 // (stack, stage) and keeps it for the life of the holder's scope.
88 yield* holder.set({ ...request, value: sampleState });
89
90 const error = yield* withLiveStore((contender) => contender.get(request)).pipe(Effect.flip);
91 expect(error).toBeInstanceOf(StateStoreError);
92 expect(error.message).toContain("holds the Postgres state lock");
93
94 yield* holder.deleteStack({ stack });
95 }),
96 ),
97 );
98
99 it.effect("releases the lock when the store's scope closes", () =>
100 Effect.gen(function* () {
101 yield* withLiveStore((store) => store.set({ ...request, value: sampleState }));
102 // The previous store's scope has closed, so a fresh deploy proceeds.
103 yield* withLiveStore((store) =>
104 Effect.gen(function* () {
105 expect(yield* store.get(request)).toBeDefined();
106 yield* store.deleteStack({ stack });
107 }),
108 );
109 }),
110 );
111
112 it.effect("migrates the schema concurrently without colliding", () =>
113 // Two fresh pools race the first state operation, so both run the
114 // transactional `create table if not exists` migration at once — the
115 // concurrency that fails on real Postgres without the advisory lock.
116 Effect.all(
117 [
118 withLiveStore((store) => store.deleteStack({ stack: `${stack}-migrate-a` })),
119 withLiveStore((store) => store.deleteStack({ stack: `${stack}-migrate-b` })),
120 ],
121 { concurrency: 2 },
122 ),
123 );
124 });