@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 143 lines 5.49 KB
Raw
1 import { expect } from "bun:test";
2 import * as Neon from "@alchemy.run/neon";
3 import * as SQL from "@alchemy.run/sql/Postgres";
4 import * as Test from "@alchemy.run/test/Bun";
5 import * as SDK from "@distilled.cloud/neon";
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 Redacted from "effect/Redacted";
11 import * as Schedule from "effect/Schedule";
12 import Api from "../src/Api.ts";
13 import Events from "../src/Events.ts";
14 import { resources } from "../src/resources.ts";
15
16 const { test } = Test.make({
17 providers: Neon.providers(),
18 state: Alchemy.localState(),
19 profile: process.env.ALCHEMY_PROFILE ?? "testing",
20 stage: "test-neon-compute-tutorial",
21 });
22
23 test.provider(
24 "a real private upload dispatches the Effect event and persists status",
25 (stack) =>
26 Effect.gen(function* () {
27 yield* stack.destroy();
28 const { project, branch, uploads, publicAssets, api, events } = yield* stack.deploy(
29 Effect.gen(function* () {
30 const backend = yield* resources;
31 const api = yield* Api;
32 const events = yield* Events;
33 return { ...backend, api, events };
34 }),
35 );
36 const scope = {
37 project_id: project.projectId,
38 branch_id: branch.branchId,
39 };
40 const observed = yield* SDK.listProjectBranchBuckets(scope);
41 expect(
42 observed.buckets.find((bucket) => bucket.name === uploads.bucketName)?.access_level,
43 ).toBe("private");
44 expect(
45 observed.buckets.find((bucket) => bucket.name === publicAssets.bucketName)?.access_level,
46 ).toBe("public_read");
47 const triggers = yield* SDK.listProjectBranchTriggers(scope);
48 expect(
49 triggers.triggers.some(
50 (trigger) =>
51 trigger.type === "schedule" &&
52 trigger.function_slug === events.slug &&
53 trigger.function_path === "/__alchemy/neon/cron/Nightly" &&
54 trigger.enabled,
55 ),
56 ).toBe(true);
57 expect(
58 triggers.triggers.some(
59 (trigger) =>
60 trigger.type === "storage_object_created" &&
61 trigger.function_slug === api.slug &&
62 trigger.function_path === "/__alchemy/neon/bucket/ProcessUploads" &&
63 trigger.enabled,
64 ),
65 ).toBe(true);
66 const http = yield* HttpClient.HttpClient;
67 const apiUrl = api.url.replace(/\/$/, "");
68 const health = yield* http.get(`${apiUrl}/health`).pipe(
69 Effect.flatMap((response) =>
70 response.status === 200
71 ? Effect.succeed(response)
72 : Effect.fail(new Error(`Health HTTP ${response.status}`)),
73 ),
74 Effect.retry({ schedule: Schedule.spaced("2 seconds"), times: 8 }),
75 );
76 expect(health.status).toBe(200);
77 for (const path of ["/api/uploads", "/api/settings", "/api/me"]) {
78 expect((yield* http.get(`${apiUrl}${path}`)).status).toBe(401);
79 }
80 expect(
81 (yield* http.get(`${apiUrl}/api/uploads`, {
82 headers: { authorization: "Bearer invalid.jwt.signature" },
83 })).status,
84 ).toBe(401);
85 expect((yield* http.post(`${apiUrl}/__alchemy/neon/bucket/ProcessUploads`)).status).toBe(403);
86
87 const sql = yield* SQL.Postgres({
88 url: Redacted.make(branch.connectionUri),
89 });
90 const files = yield* Neon.bucketStorageClient(uploads);
91 const id = "10000000-0000-4000-8000-000000000001";
92 const key = `incoming/tutorial-test/${id}`;
93 const body = "real upload event";
94 yield* sql`INSERT INTO uploads (id, owner_id, object_key, filename, content_type, expected_bytes)
95 VALUES (${id}, 'tutorial-test', ${key}, 'event.txt', 'text/plain', ${body.length})`;
96 const signed = yield* files.presign(key, "PUT", {
97 contentType: "text/plain",
98 expiresIn: 120,
99 });
100 const put = yield* http.execute(
101 HttpClientRequest.put(signed).pipe(HttpClientRequest.bodyText(body, "text/plain")),
102 );
103 expect(put.status).toBe(200);
104 const [processed] = yield* sql<{
105 status: string;
106 actual_bytes: string | number;
107 }>`SELECT status, actual_bytes FROM uploads WHERE id = ${id}`.pipe(
108 Effect.repeat({
109 schedule: Schedule.spaced("3 seconds"),
110 times: 8,
111 until: (rows) => rows[0]?.status === "ready",
112 }),
113 );
114 expect(processed?.status).toBe("ready");
115 expect(Number(processed?.actual_bytes)).toBe(body.length);
116 expect(
117 (yield* sql`SELECT invocation_id FROM upload_events WHERE object_key = ${key}`).length,
118 ).toBeGreaterThan(0);
119 const download = yield* files.presign(key, "GET", { expiresIn: 60 });
120 const response = yield* http.get(download);
121 expect(yield* response.text).toBe(body);
122 const anonymousUrl = yield* Effect.sync(() => {
123 const url = new URL(download);
124 url.search = "";
125 return url.href;
126 });
127 expect((yield* http.get(anonymousUrl)).status).not.toBe(200);
128 yield* stack.destroy();
129 const gone = yield* SDK.getProject({
130 project_id: project.projectId,
131 }).pipe(
132 Effect.map(() => false),
133 Effect.catchTag("NotFound", () => Effect.succeed(true)),
134 Effect.repeat({
135 schedule: Schedule.spaced("1 second"),
136 times: 8,
137 until: (value) => value,
138 }),
139 );
140 expect(gone).toBe(true);
141 }).pipe(Effect.ensuring(stack.destroy().pipe(Effect.orDie))),
142 { timeout: 120_000 },
143 );