@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 281 lines 9.01 KB
Raw
1 import * as Prisma from "@alchemy.run/prisma";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as Effect from "effect/Effect";
5 import * as FileSystem from "effect/FileSystem";
6 import * as HttpServerResponse from "effect/http/HttpServerResponse";
7 import * as Path from "effect/Path";
8 import * as Redacted from "effect/Redacted";
9 import * as Schedule from "effect/Schedule";
10
11 const { test } = Test.make({ providers: Prisma.providers(), dev: true });
12
13 test.provider(
14 "dev mode returns a local Compute without a token",
15 (stack) =>
16 Effect.gen(function* () {
17 yield* stack.destroy();
18
19 const app = yield* stack.deploy(
20 Effect.gen(function* () {
21 return yield* Prisma.Compute("App", {
22 project: "project-dev",
23 appName: "api",
24 port: 8787,
25 dev: { url: "http://localhost:8787" },
26 });
27 }),
28 );
29
30 expect(app.local).toBe(true);
31 expect(app.url).toBe("http://localhost:8787");
32 expect(app.appId).toContain("dev:");
33
34 yield* stack.destroy();
35 }),
36 { tags: ["provider:prisma", "provider:prisma:compute", "local"] },
37 );
38
39 test.provider(
40 "dev mode supports the same stack shape with Project",
41 (stack) =>
42 Effect.gen(function* () {
43 yield* stack.destroy();
44
45 const output = yield* stack.deploy(
46 Effect.gen(function* () {
47 const project = yield* Prisma.Project("Project", {
48 name: "local-project",
49 createDatabase: false,
50 });
51 const app = yield* Prisma.Compute("AppWithProject", {
52 project,
53 appName: "api",
54 port: 8787,
55 dev: { url: "http://localhost:8787" },
56 });
57 return { project, app };
58 }),
59 );
60
61 expect(output.project.projectId).toBe("dev:project:Project");
62 expect(output.app.projectId).toBe(output.project.projectId);
63 expect(output.app.local).toBe(true);
64 expect(output.app.url).toBe("http://localhost:8787");
65
66 yield* stack.destroy();
67 }),
68 { tags: ["provider:prisma", "provider:prisma:compute", "provider:prisma:project", "local"] },
69 );
70
71 test.provider(
72 "dev mode records effect-native Connection.bind env on Compute",
73 (stack) =>
74 Effect.gen(function* () {
75 yield* stack.destroy();
76
77 const output = yield* stack.deploy(
78 Effect.gen(function* () {
79 const project = yield* Prisma.Project("Project", { name: "local-project" });
80 const database = yield* Prisma.Database("Database", {
81 project,
82 name: "main",
83 dev: { name: "alchemy-compute-dev-bind-env" },
84 });
85 const connection = yield* Prisma.Connection("Connection", { database, name: "api" });
86 const keys = Prisma.connectEnvKeys(connection);
87 const app = yield* Prisma.Compute(
88 "App",
89 {
90 project,
91 appName: "api",
92 main: import.meta.filename,
93 dev: { url: "http://localhost:8787" },
94 },
95 Effect.gen(function* () {
96 const db = yield* Prisma.Connect(connection);
97
98 return {
99 fetch: Effect.gen(function* () {
100 const connectionId = yield* db.connectionId;
101 return HttpServerResponse.text(connectionId);
102 }),
103 };
104 }).pipe(Effect.provide(Prisma.ConnectBinding)),
105 );
106
107 return { app, keys };
108 }),
109 );
110
111 expect(output.app.local).toBe(true);
112 expect(output.app.environmentKeys).toEqual(
113 expect.arrayContaining([
114 output.keys.connectionId,
115 output.keys.databaseId,
116 output.keys.directConnectionString,
117 output.keys.pooledConnectionString,
118 output.keys.accelerateConnectionString,
119 output.keys.host,
120 output.keys.user,
121 output.keys.password,
122 ]),
123 );
124
125 yield* stack.destroy();
126 }),
127 {
128 tags: [
129 "provider:prisma",
130 "provider:prisma:compute",
131 "provider:prisma:connect",
132 "provider:prisma:connection",
133 "provider:prisma:database",
134 "provider:prisma:project",
135 "local",
136 ],
137 },
138 );
139
140 test.provider(
141 "dev mode resolves all owned Prisma resource shapes without a token",
142 (stack) =>
143 Effect.gen(function* () {
144 yield* stack.destroy();
145
146 const output = yield* stack.deploy(
147 Effect.gen(function* () {
148 const project = yield* Prisma.Project("Project", { name: "local-project" });
149 const database = yield* Prisma.Database("Database", {
150 project,
151 name: "main",
152 dev: { name: "alchemy-compute-dev-owned-shapes" },
153 });
154 const connection = yield* Prisma.Connection("Connection", { database, name: "api" });
155 const branch = yield* Prisma.Branch("Branch", {
156 project,
157 gitName: "main",
158 isDefault: true,
159 });
160 const appResource = yield* Prisma.App("App", {
161 project,
162 displayName: "api",
163 branchId: branch.branchId,
164 });
165 const deployment = yield* Prisma.Deployment("Deployment", {
166 app: appResource,
167 portMapping: { http: 3000 },
168 });
169 const env = yield* Prisma.EnvironmentVariable("Environment", {
170 project,
171 class: "production",
172 key: "TOKEN",
173 value: Redacted.make("secret"),
174 });
175 const repo = yield* Prisma.SourceRepository("SourceRepository", {
176 project,
177 providerRepositoryId: 123,
178 });
179
180 return { project, database, connection, branch, appResource, deployment, env, repo };
181 }),
182 );
183
184 expect(output.project.projectId).toBe("dev:project:Project");
185 expect(output.database.projectId).toBe(output.project.projectId);
186 expect(output.database.databaseId).toBe("dev:database:Database");
187 expect(output.connection.databaseId).toBe(output.database.databaseId);
188 expect(output.branch.projectId).toBe(output.project.projectId);
189 expect(output.branch.isDefault).toBe(true);
190 expect(output.appResource.projectId).toBe(output.project.projectId);
191 expect(output.appResource.branchId).toBe(output.branch.branchId);
192 expect(output.deployment.appId).toBe(output.appResource.appId);
193 expect(output.env.projectId).toBe(output.project.projectId);
194 expect(output.env.key).toBe("TOKEN");
195 expect(Redacted.value(output.env.value)).toBe("secret");
196 expect(output.repo.projectId).toBe(output.project.projectId);
197 expect(output.repo.repoId).toBe(123);
198
199 yield* stack.destroy();
200 }),
201 {
202 tags: [
203 "provider:prisma",
204 "provider:prisma:app",
205 "provider:prisma:branch",
206 "provider:prisma:compute",
207 "provider:prisma:connection",
208 "provider:prisma:database",
209 "provider:prisma:deployment",
210 "provider:prisma:environmentvariable",
211 "provider:prisma:project",
212 "provider:prisma:sourcerepository",
213 "local",
214 ],
215 exclusive: "prisma:dev",
216 },
217 );
218
219 test.provider(
220 "dev mode starts a local command and passes env",
221 (stack) =>
222 Effect.gen(function* () {
223 yield* stack.destroy();
224
225 const fs = yield* FileSystem.FileSystem;
226 const path = yield* Path.Path;
227 const root = yield* fs.makeTempDirectory({ prefix: "alchemy-prisma-dev-" });
228 const scriptPath = path.join(root, "dev-server.sh");
229 const outputPath = path.join(root, "dev-output.json");
230 const stoppedPath = path.join(root, "dev-stopped.txt");
231
232 yield* fs.writeFileString(
233 scriptPath,
234 [
235 "trap 'printf stopped > dev-stopped.txt; exit 0' TERM INT",
236 'printf \'{"port":"%s","greeting":"%s","nodeEnv":"%s"}\' "$PORT" "$GREETING" "$NODE_ENV" > dev-output.json',
237 "while true; do sleep 1; done",
238 "",
239 ].join("\n"),
240 );
241
242 const app = yield* stack.deploy(
243 Effect.gen(function* () {
244 return yield* Prisma.Compute("AppWithCommand", {
245 project: "project-dev",
246 appName: "api",
247 path: root,
248 port: 8788,
249 env: { GREETING: "hello-dev" },
250 dev: { command: "sh dev-server.sh", cwd: root, port: 8789 },
251 });
252 }),
253 );
254
255 expect(app.local).toBe(true);
256 expect(app.url).toBe("http://localhost:8789");
257
258 const output = yield* fs.readFileString(outputPath).pipe(
259 Effect.retry({
260 schedule: Schedule.max([Schedule.fixed("50 millis"), Schedule.recurs(40)]),
261 }),
262 );
263
264 expect(JSON.parse(output)).toEqual({
265 port: "8789",
266 greeting: "hello-dev",
267 nodeEnv: "development",
268 });
269
270 yield* stack.destroy();
271
272 const stopped = yield* fs.readFileString(stoppedPath).pipe(
273 Effect.retry({
274 schedule: Schedule.max([Schedule.fixed("50 millis"), Schedule.recurs(40)]),
275 }),
276 );
277
278 expect(stopped).toBe("stopped");
279 }),
280 { tags: ["provider:prisma", "provider:prisma:compute", "local"], timeout: 10_000 },
281 );