1import * as Cloudflare from "@/Cloudflare";
2import * as Prisma from "@/Prisma";
3import * as Test from "@/Test/Alchemy";
4import { describe, expect } from "alchemy-test";
5import * as Effect from "effect/Effect";
6import * as Layer from "effect/Layer";
7import { MinimumLogLevel } from "effect/References";
8import PrismaHostStack, { state } from "./fixtures/prismahost/stack.ts";
9import { expectDatabaseReachable } from "./fixtures/sqlreach/expect.ts";
10
11const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
12 providers: Layer.merge(Cloudflare.providers(), Prisma.providers()),
13 state,
14 dev: true,
15});
16
17const logLevel = Effect.provideService(
18 MinimumLogLevel,
19 process.env.DEBUG ? "Debug" : "Info",
20);
21
22const HOOK_TIMEOUT = 300_000;
23const TEST_TIMEOUT = 240_000;
24
25/**
26 * Regression for alchemy-run/alchemy#1334 on Linux: an arbitrary image given
27 * a local Prisma `DATABASE_URL` (`postgres://…@127.0.0.1:…`) must be able to
28 * reach `@prisma/dev` from inside the container. host-gateway rewrites the
29 * hostname, but the server still has to accept connections on the Docker
30 * bridge — a 127.0.0.1 listener times out with
31 * `dial error: timeout` to `172.17.0.1`.
32 */
33describe(
34 "local container reaches Prisma Postgres",
35 {
36 tags: [
37 "provider:cloudflare",
38 "provider:cloudflare:container",
39 "provider:cloudflare:worker",
40 "provider:prisma",
41 "provider:prisma:connection",
42 "provider:prisma:project",
43 ],
44 },
45 () => {
46 const stack = beforeAll(deploy(PrismaHostStack), { timeout: HOOK_TIMEOUT });
47 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(PrismaHostStack), {
48 timeout: HOOK_TIMEOUT,
49 });
50
51 test(
52 "container DATABASE_URL is rewritten once and reaches the host Prisma",
53 Effect.gen(function* () {
54 const { url } = yield* stack;
55 yield* expectDatabaseReachable(url, (hostname) => {
56 expect(hostname).not.toBe("localhost");
57 expect(hostname).not.toBe("127.0.0.1");
58 expect(hostname).toContain("localhost");
59 });
60 }).pipe(logLevel),
61 { tags: ["local"], timeout: TEST_TIMEOUT },
62 );
63 },
64);