@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 163 lines 5.64 KB
Raw
1 import { describe, expect } from "bun:test";
2 import * as GCP from "@alchemy.run/gcp";
3 import * as Test from "@alchemy.run/test/Bun";
4 import * as firestore from "@distilled.cloud/gcp/firestore_v1";
5 import * as secretmanager from "@distilled.cloud/gcp/secretmanager_v1";
6 import * as Alchemy from "alchemy";
7 import * as Effect from "effect/Effect";
8 import * as FetchHttpClient from "effect/http/FetchHttpClient";
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
15 const { test, beforeAll, afterAll, deploy, destroy } = Test.make({
16 providers: GCP.providers(),
17 state: Alchemy.localState(),
18 });
19
20 const { getWhenReady } = Test;
21
22 // Out-of-band calls to the Google APIs resolve the same stored credentials
23 // the deploy uses, so the test runs against the configured profile.
24 const GcpHttp = Layer.mergeAll(GCP.GcpAuth, GCP.fromAuthProvider(), FetchHttpClient.layer);
25
26 const API_KEY = "integ-test-key";
27
28 // Deploy, tests and destroy all sit behind the same Docker guard.
29 describe("gcp-cloud-run-api", () => {
30 const stack = beforeAll(
31 Effect.gen(function* () {
32 const outputs = yield* deploy(Stack);
33 // The secret is created empty on purpose — the value is an operator
34 // step. Add the version the service will read.
35 yield* secretmanager
36 .addVersionProjectsSecrets({
37 parent: outputs.secretName,
38 body: { payload: { data: btoa(API_KEY) } },
39 })
40 .pipe(Effect.orDie, Effect.provide(GcpHttp));
41 return outputs;
42 }),
43 { timeout: 900_000 },
44 );
45
46 afterAll.skipIf(!!process.env.NO_DESTROY)(destroy(Stack), {
47 timeout: 600_000,
48 });
49
50 const baseUrlOf = (url: string | undefined) => {
51 if (url === undefined) throw new Error("the service has no URL");
52 return url.replace(/\/+$/, "");
53 };
54
55 const createLink = (baseUrl: string, target: string, key = API_KEY) =>
56 HttpClient.execute(
57 HttpClientRequest.post(`${baseUrl}/links`).pipe(
58 HttpClientRequest.setHeader("x-api-key", key),
59 HttpClientRequest.bodyJsonUnsafe({ url: target }),
60 ),
61 );
62
63 test(
64 "serves the health route on a public Cloud Run URL",
65 Effect.gen(function* () {
66 const { url } = yield* stack;
67 expect(url).toMatch(/^https:\/\//);
68 const res = yield* getWhenReady(`${baseUrlOf(url)}/`);
69 expect(res.status).toBe(200);
70 }),
71 { timeout: 120_000 },
72 );
73
74 test(
75 "rejects a create without the Secret Manager key",
76 Effect.gen(function* () {
77 const { url } = yield* stack;
78 const baseUrl = baseUrlOf(url);
79 yield* getWhenReady(`${baseUrl}/`);
80
81 const missing = yield* HttpClient.execute(
82 HttpClientRequest.post(`${baseUrl}/links`).pipe(
83 HttpClientRequest.bodyJsonUnsafe({ url: "https://example.com" }),
84 ),
85 );
86 expect(missing.status).toBe(401);
87
88 const wrong = yield* createLink(baseUrl, "https://example.com", "nope");
89 expect(wrong.status).toBe(401);
90 }),
91 { timeout: 120_000 },
92 );
93
94 test(
95 "mints a code, redirects, counts the click, and deletes",
96 Effect.gen(function* () {
97 const { url, databaseName } = yield* stack;
98 const baseUrl = baseUrlOf(url);
99 yield* getWhenReady(`${baseUrl}/`);
100
101 const target = "https://alchemy.run/gcp";
102 // A fresh deploy's Firestore grant (a project binding under an IAM
103 // Condition) can take several minutes to propagate; until then the
104 // service answers 500 (Forbidden from Firestore).
105 const created = yield* createLink(baseUrl, target).pipe(
106 Effect.repeat({
107 schedule: Schedule.spaced("10 seconds"),
108 until: (response) => response.status !== 500,
109 times: 42,
110 }),
111 );
112 expect(created.status).toBe(201);
113 const { code, shortUrl } = (yield* created.json) as {
114 code: string;
115 shortUrl: string;
116 };
117 expect(code).toMatch(/^[0-9A-Za-z]{7}$/);
118 expect(shortUrl).toEqual(`${baseUrl}/l/${code}`);
119
120 // The row the service wrote is a real Firestore document.
121 const document = yield* firestore
122 .getProjectsDatabasesDocuments({
123 name: `${databaseName}/documents/links/${code}`,
124 })
125 .pipe(Effect.orDie, Effect.provide(GcpHttp));
126 expect(document.fields?.url?.stringValue).toEqual(target);
127
128 const before = yield* HttpClient.execute(HttpClientRequest.get(`${baseUrl}/links/${code}`));
129 expect(before.status).toBe(200);
130 expect((yield* before.json) as { clicks: number }).toMatchObject({
131 url: target,
132 clicks: 0,
133 });
134
135 // Inspect the redirect itself instead of following it to the target.
136 const redirect = yield* HttpClient.execute(
137 HttpClientRequest.get(`${baseUrl}/l/${code}`),
138 ).pipe(
139 Effect.provideService(FetchHttpClient.RequestInit, {
140 redirect: "manual",
141 }),
142 );
143 expect(redirect.status).toBe(302);
144 expect(redirect.headers.location).toEqual(target);
145
146 const after = yield* HttpClient.execute(HttpClientRequest.get(`${baseUrl}/links/${code}`));
147 expect((yield* after.json) as { clicks: number }).toMatchObject({
148 clicks: 1,
149 });
150
151 const deleted = yield* HttpClient.execute(
152 HttpClientRequest.delete(`${baseUrl}/links/${code}`).pipe(
153 HttpClientRequest.setHeader("x-api-key", API_KEY),
154 ),
155 );
156 expect(deleted.status).toBe(204);
157
158 const gone = yield* HttpClient.execute(HttpClientRequest.get(`${baseUrl}/links/${code}`));
159 expect(gone.status).toBe(404);
160 }),
161 { timeout: 180_000 },
162 );
163 });