@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 102 lines 3.93 KB
Raw
1 import { expect } from "bun:test";
2 import * as fs from "node:fs";
3 import * as path from "node:path";
4 import * as Drizzle from "@alchemy.run/drizzle";
5 import * as Fly from "@alchemy.run/fly";
6 import * as Test from "@alchemy.run/test/Bun";
7 import * as Alchemy from "alchemy";
8 import * as Effect from "effect/Effect";
9 import * as HttpClient from "effect/http/HttpClient";
10 import * as HttpClientRequest from "effect/http/HttpClientRequest";
11 import * as Layer from "effect/Layer";
12 import * as Schedule from "effect/Schedule";
13 import Stack from "../alchemy.run.ts";
14 import type { User } from "../src/schema.ts";
15 import { MIGRATE_TOKEN } from "../src/shared.ts";
16
17 /** A `User` row as it arrives over JSON — `createdAt` is an ISO string. */
18 type SerializedUser = Omit<User, "createdAt"> & { createdAt: string };
19
20 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
21 providers: Layer.mergeAll(Fly.providers(), Drizzle.providers()),
22 state: Alchemy.localState(),
23 profile: process.env.ALCHEMY_PROFILE,
24 });
25
26 /**
27 * Generated migration SQL, in journal order. `Drizzle.Schema` writes these
28 * during the deploy in `beforeAll`, so read lazily inside the test.
29 */
30 const readMigrationSql = (): string[] => {
31 const dir = path.resolve(import.meta.dirname, "..", "migrations");
32 return fs
33 .readdirSync(dir)
34 .filter((entry) => fs.existsSync(path.join(dir, entry, "migration.sql")))
35 .sort()
36 .map((entry) => fs.readFileSync(path.join(dir, entry, "migration.sql"), "utf8"));
37 };
38
39 /** Run a request, retrying non-200s through the fresh app's warmup. */
40 const executeOk = (request: HttpClientRequest.HttpClientRequest) =>
41 Effect.gen(function* () {
42 const client = yield* HttpClient.HttpClient;
43 return yield* client.execute(request).pipe(
44 Effect.flatMap((res) =>
45 res.status === 200 ? Effect.succeed(res) : Effect.fail(new Error(`HTTP ${res.status}`)),
46 ),
47 // Bounded (~90s): machine boot + route warmup. Never use unbounded
48 // exponential growth here — a genuinely broken route should surface
49 // well inside the test timeout.
50 Effect.retry({
51 schedule: Schedule.spaced("2 seconds"),
52 times: 45,
53 }),
54 );
55 });
56
57 const stack = beforeAll(deploy(Stack), { timeout: 300_000 });
58
59 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), {
60 timeout: 180_000,
61 });
62
63 test(
64 "Service connects to Managed Postgres through Drizzle",
65 Effect.gen(function* () {
66 const out = yield* stack;
67 expect(out.clusterId).toBeString();
68 expect(out.region).toBe("iad");
69 expect(out.apiUrl).toBe(`https://${out.appName}.fly.dev`);
70
71 // Apply the generated schema THROUGH the deployed Service. MPG is only
72 // reachable on the org's private network, so the in-network Service is
73 // the data plane for DDL too — the test never connects to the cluster
74 // directly. The `/migrate` route is idempotent, so warmup retries are
75 // safe.
76 const migrations = readMigrationSql();
77 expect(migrations.length).toBeGreaterThan(0);
78 for (const sqlText of migrations) {
79 const migrated = yield* executeOk(
80 HttpClientRequest.post(`${out.apiUrl}/migrate`).pipe(
81 HttpClientRequest.setHeader("x-migrate-token", MIGRATE_TOKEN),
82 HttpClientRequest.bodyText(sqlText),
83 ),
84 );
85 const { applied } = (yield* migrated.json) as { applied: number };
86 expect(applied).toBeGreaterThan(0);
87 }
88
89 const health = yield* executeOk(HttpClientRequest.get(`${out.apiUrl}/health`));
90 expect(health.status).toBe(200);
91 const body = (yield* health.json) as { ok: boolean; users: number };
92 expect(body.ok).toBe(true);
93 expect(body.users).toBeNumber();
94
95 const created = yield* HttpClient.execute(HttpClientRequest.post(`${out.apiUrl}/users`));
96 expect(created.status).toBe(200);
97 const createdBody = (yield* created.json) as { user: SerializedUser[] };
98 expect(createdBody.user).toHaveLength(1);
99 expect(createdBody.user[0]?.id).toBeNumber();
100 }),
101 { timeout: 240_000 },
102 );