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, 06:00 UTC
| 1 | import { expect } from "bun:test"; |
| 2 | import * as Cloudflare from "@alchemy.run/cloudflare"; |
| 3 | import * as Drizzle from "@alchemy.run/drizzle"; |
| 4 | import * as Test from "@alchemy.run/test/Bun"; |
| 5 | import * as Turso from "@alchemy.run/turso"; |
| 6 | import * as Alchemy from "alchemy"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as HttpClient from "effect/http/HttpClient"; |
| 9 | import * as HttpClientRequest from "effect/http/HttpClientRequest"; |
| 10 | import * as Layer from "effect/Layer"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | import Stack from "../alchemy.run.ts"; |
| 13 | import type { Note } from "../src/schema.ts"; |
| 14 | |
| 15 | const { test, beforeAll, afterAll, deploy, destroy } = Test.make({ |
| 16 | providers: Layer.mergeAll(Cloudflare.providers(), Turso.providers(), Drizzle.providers()), |
| 17 | state: Alchemy.localState(), |
| 18 | }); |
| 19 | |
| 20 | const stack = beforeAll(deploy(Stack)); |
| 21 | |
| 22 | afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack)); |
| 23 | |
| 24 | const { getWhenReady } = Test; |
| 25 | |
| 26 | // A fresh workers.dev route can still 404 on some edges after the first |
| 27 | // successful GET. A 404 means the Worker never ran, so retrying is safe even |
| 28 | // for non-idempotent requests. |
| 29 | const send = (request: HttpClientRequest.HttpClientRequest) => |
| 30 | HttpClient.execute(request).pipe( |
| 31 | Effect.repeat({ |
| 32 | schedule: Schedule.spaced("1 second"), |
| 33 | while: (response) => response.status === 404, |
| 34 | times: 10, |
| 35 | }), |
| 36 | ); |
| 37 | |
| 38 | test( |
| 39 | "each tenant gets its own database forked from the migrated template", |
| 40 | Effect.gen(function* () { |
| 41 | const { url, template } = yield* stack; |
| 42 | const baseUrl = url.replace(/\/+$/, ""); |
| 43 | // Tenant names are org-wide database names, so derive them from the template. |
| 44 | const acme = `${template}-acme`.slice(0, 50); |
| 45 | const globex = `${template}-globex`.slice(0, 50); |
| 46 | |
| 47 | const ready = yield* getWhenReady(`${baseUrl}/tenants`); |
| 48 | expect(ready.status).toBe(200); |
| 49 | |
| 50 | for (const tenant of [acme, globex]) { |
| 51 | const created = yield* send(HttpClientRequest.post(`${baseUrl}/tenants/${tenant}`)); |
| 52 | expect(created.status).toBe(200); |
| 53 | } |
| 54 | |
| 55 | const write = yield* send( |
| 56 | HttpClientRequest.post(`${baseUrl}/tenants/${acme}/notes`).pipe( |
| 57 | HttpClientRequest.bodyJsonUnsafe({ body: "hello from acme" }), |
| 58 | ), |
| 59 | ); |
| 60 | expect(write.status).toBe(200); |
| 61 | |
| 62 | const acmeNotes = (yield* (yield* send( |
| 63 | HttpClientRequest.get(`${baseUrl}/tenants/${acme}/notes`), |
| 64 | )).json) as unknown as { notes: Note[] }; |
| 65 | expect(acmeNotes.notes.map((n) => n.body)).toEqual(["hello from acme"]); |
| 66 | |
| 67 | // globex's database is isolated from acme's. |
| 68 | const globexNotes = (yield* (yield* send( |
| 69 | HttpClientRequest.get(`${baseUrl}/tenants/${globex}/notes`), |
| 70 | )).json) as unknown as { notes: Note[] }; |
| 71 | expect(globexNotes.notes).toEqual([]); |
| 72 | |
| 73 | const listed = (yield* (yield* send(HttpClientRequest.get(`${baseUrl}/tenants`))) |
| 74 | .json) as unknown as { |
| 75 | tenants: string[]; |
| 76 | }; |
| 77 | expect(listed.tenants).toEqual(expect.arrayContaining([acme, globex])); |
| 78 | expect(listed.tenants).not.toContain(template); |
| 79 | |
| 80 | for (const tenant of [acme, globex]) { |
| 81 | const deleted = yield* send(HttpClientRequest.delete(`${baseUrl}/tenants/${tenant}`)); |
| 82 | expect(deleted.status).toBe(200); |
| 83 | } |
| 84 | }), |
| 85 | { timeout: 180_000 }, |
| 86 | ); |