@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 236 lines 8.68 KB
Raw
1 import { Function, type FunctionProps } from "@alchemy.run/neon/Function";
2 import { Project } from "@alchemy.run/neon/Project";
3 import { providers } from "@alchemy.run/neon/Providers";
4 import { expect } from "@alchemy.run/test";
5 import * as Test from "@alchemy.run/test/Alchemy";
6 import * as NeonApi from "@distilled.cloud/neon";
7 import * as Provider from "alchemy/Provider";
8 import * as Effect from "effect/Effect";
9 import * as HttpClient from "effect/http/HttpClient";
10 import {
11 functionRolloutSamples,
12 functionRolloutTimeout,
13 functionTextSamples,
14 } from "./FunctionRollout.ts";
15
16 const { test } = Test.make({ providers: providers() });
17
18 test.provider(
19 "incomplete never-created Function rows have no recoverable physical identity",
20 (stack) =>
21 Effect.gen(function* () {
22 yield* stack.destroy();
23 const provider = yield* Provider.providerForMode(
24 yield* Provider.Provider<Function>("Neon.Function"),
25 "live",
26 );
27 const cases: FunctionProps[] = [
28 { branch: { projectId: "", branchId: "" }, main: "never-created.ts" },
29 {
30 branch: { projectId: "project", branchId: "" },
31 main: "never-created.ts",
32 },
33 {
34 branch: { projectId: "", branchId: "branch" },
35 main: "never-created.ts",
36 },
37 { project: { projectId: "" }, main: "never-created.ts" },
38 ];
39 for (const olds of cases) {
40 expect(
41 yield* provider.read!({
42 id: "Incomplete",
43 fqn: "Incomplete",
44 instanceId: "never-created",
45 olds,
46 output: undefined,
47 }),
48 ).toBeUndefined();
49 }
50 yield* stack.destroy();
51 }),
52 {
53 tags: ["provider:neon", "provider:neon:function", "live"],
54 timeout: 120_000,
55 },
56 );
57
58 // Live lifecycles below poll deployment rollout for minutes; skip under --fast.
59 test.provider.skipIf(!!process.env.FAST)(
60 "native Function create, no-op, name update, environment removal and deletion",
61 (stack) =>
62 Effect.gen(function* () {
63 yield* stack.destroy();
64 const main = new URL("./fixtures/function-native.ts", import.meta.url).href;
65 const deploy = (env: Record<string, string>, name?: string) =>
66 stack.deploy(
67 Effect.gen(function* () {
68 const project = yield* Project("FunctionProject", {
69 region: "aws-us-east-2",
70 });
71 const api = yield* Function("Api", { project, main, env, name });
72 return { project, api };
73 }),
74 );
75 const first = yield* deploy({
76 FUNCTION_TEST_VALUE: "one",
77 FUNCTION_TEST_REMOVED: "remove-me",
78 });
79 expect(first.api.slug).toMatch(/^[a-z0-9]{1,20}$/);
80 expect(first.api.currentDeploymentId).toBe(first.api.activeDeploymentId);
81 const client = yield* HttpClient.HttpClient;
82 const response = yield* client.get(`${first.api.url}env`);
83 expect(yield* response.json).toMatchObject({
84 value: "one",
85 removed: "remove-me",
86 hasDatabase: true,
87 hasAccountKey: false,
88 });
89 const observed = yield* NeonApi.getProjectBranchFunction({
90 project_id: first.api.projectId,
91 branch_id: first.api.branchId,
92 slug: first.api.slug,
93 });
94 expect(observed.function.active_deployment?.id).toBe(first.api.activeDeploymentId);
95 const noop = yield* deploy({
96 FUNCTION_TEST_VALUE: "one",
97 FUNCTION_TEST_REMOVED: "remove-me",
98 });
99 expect(noop.api.activeDeploymentId).toBe(first.api.activeDeploymentId);
100 const renamed = yield* deploy(
101 { FUNCTION_TEST_VALUE: "one", FUNCTION_TEST_REMOVED: "remove-me" },
102 "Friendly name",
103 );
104 expect(renamed.api.name).toBe("Friendly name");
105 expect(renamed.api.activeDeploymentId).toBe(first.api.activeDeploymentId);
106 const updated = yield* deploy({ FUNCTION_TEST_VALUE: "two" });
107 expect(updated.api.functionId).toBe(first.api.functionId);
108 expect(updated.api.name).toBe(first.api.slug);
109 expect(updated.api.activeDeploymentId).not.toBe(first.api.activeDeploymentId);
110 expect(updated.api.url).toBe(first.api.url);
111 const changedEnv = yield* functionRolloutSamples(
112 client.get(`${updated.api.url}env`).pipe(
113 Effect.flatMap((response) => {
114 expect(response.status).toBe(200);
115 return response.json;
116 }),
117 ),
118 (body) =>
119 typeof body === "object" &&
120 body !== null &&
121 "value" in body &&
122 body.value === "two" &&
123 !("removed" in body),
124 );
125 yield* Effect.logInfo(
126 JSON.stringify({
127 functionEnvironmentUpdate: {
128 projectId: updated.api.projectId,
129 branchId: updated.api.branchId,
130 slug: updated.api.slug,
131 previousDeployment: first.api.activeDeploymentId,
132 activeDeployment: updated.api.activeDeploymentId,
133 observed: changedEnv,
134 },
135 }),
136 );
137 for (const body of changedEnv) {
138 expect(body).toMatchObject({ value: "two" });
139 expect(body).not.toHaveProperty("removed");
140 }
141 const state = yield* NeonApi.getProjectBranchFunction({
142 project_id: updated.api.projectId,
143 branch_id: updated.api.branchId,
144 slug: updated.api.slug,
145 });
146 expect(state.function.active_deployment?.environment).not.toContain("FUNCTION_TEST_REMOVED");
147 yield* stack.deploy(Project("FunctionProject", { region: "aws-us-east-2" }));
148 const absent = yield* NeonApi.getProjectBranchFunction({
149 project_id: updated.api.projectId,
150 branch_id: updated.api.branchId,
151 slug: updated.api.slug,
152 }).pipe(
153 Effect.as(false),
154 Effect.catchTag("NotFound", () => Effect.succeed(true)),
155 );
156 expect(absent).toBe(true);
157 yield* stack.destroy();
158 }),
159 {
160 tags: ["provider:neon", "provider:neon:function", "provider:neon:project", "live"],
161 timeout: functionRolloutTimeout,
162 },
163 );
164
165 test.provider.skipIf(!!process.env.FAST)(
166 "code update after a config-only deployment serves the requested artifact",
167 (stack) =>
168 Effect.gen(function* () {
169 yield* stack.destroy();
170 const deploy = (main: string, value: string) =>
171 stack.deploy(
172 Effect.gen(function* () {
173 const project = yield* Project("CodeProject", {
174 region: "aws-us-east-2",
175 });
176 return yield* Function("Api", {
177 project,
178 main,
179 env: { FUNCTION_TEST_VALUE: value },
180 });
181 }),
182 );
183 const main = new URL("./fixtures/function-native.ts", import.meta.url).href;
184 const first = yield* deploy(main, "one");
185 const client = yield* HttpClient.HttpClient;
186 expect(yield* (yield* client.get(first.url)).text).toBe("native-v1");
187 const configured = yield* deploy(main, "two");
188 const code = yield* deploy(
189 new URL("./fixtures/function-bare.ts", import.meta.url).href,
190 "two",
191 );
192 expect(code.codeHash).not.toBe(configured.codeHash);
193 expect(code.activeDeploymentId).not.toBe(configured.activeDeploymentId);
194 const observed = yield* NeonApi.getProjectBranchFunction({
195 project_id: code.projectId,
196 branch_id: code.branchId,
197 slug: code.slug,
198 });
199 expect(observed.function.active_deployment?.id).toBe(code.activeDeploymentId);
200 yield* Effect.logInfo(
201 JSON.stringify({
202 functionCodeUpdate: {
203 projectId: code.projectId,
204 branchId: code.branchId,
205 slug: code.slug,
206 previousDeployment: configured.activeDeploymentId,
207 activeDeployment: code.activeDeploymentId,
208 status: observed.function.active_deployment?.status,
209 previousCodeHash: configured.codeHash,
210 codeHash: code.codeHash,
211 },
212 }),
213 );
214 expect(code.functionId).toBe(first.functionId);
215 expect(code.url).toBe(first.url);
216 const samples = yield* functionTextSamples(code.url, (body) => body === "bare-v2");
217 yield* Effect.logInfo(JSON.stringify({ functionStableUrl: samples }));
218 const noop = yield* deploy(
219 new URL("./fixtures/function-bare.ts", import.meta.url).href,
220 "two",
221 );
222 expect(noop.activeDeploymentId).toBe(code.activeDeploymentId);
223 yield* stack.destroy();
224 expect(
225 yield* NeonApi.getProject({ project_id: code.projectId }).pipe(
226 Effect.as(false),
227 Effect.catchTag("NotFound", () => Effect.succeed(true)),
228 ),
229 ).toBe(true);
230 for (const text of samples) expect(text).toBe("bare-v2");
231 }),
232 {
233 tags: ["provider:neon", "provider:neon:function", "provider:neon:project", "live"],
234 timeout: functionRolloutTimeout,
235 },
236 );