Skip to content
Source
Sign inSign up
Open History to see the last commit

Function.local.test.ts

93 lines3.2 KB

1import { Function } from "@alchemy.run/neon/Function";
2import { Project } from "@alchemy.run/neon/Project";
3import { providers } from "@alchemy.run/neon/Providers";
4import { expect } from "@alchemy.run/test";
5import * as Test from "@alchemy.run/test/Alchemy";
6import * as Api from "@distilled.cloud/neon";
7import * as Alchemy from "alchemy";
8import * as Effect from "effect/Effect";
9import * as HttpClient from "effect/http/HttpClient";
10​
11const { test } = Test.make({ providers: providers(), dev: true });
12test.provider(
13 "local Function runs behind the RPC sidecar and restarts on env changes",
14 (stack) =>
15 Effect.gen(function* () {
16 yield* stack.destroy();
17 const deploy = (value: string) =>
18 stack.deploy(
19 Function("LocalApi", {
20 branch: { projectId: "local-project", branchId: "local-branch" },
21 main: new URL("./fixtures/function-native.ts", import.meta.url).href,
22 env: { FUNCTION_TEST_VALUE: value },
23 }),
24 );
25 const first = yield* deploy("one");
26 expect(first.functionId).toMatch(/^dev:/);
27 const client = yield* HttpClient.HttpClient;
28 expect(yield* (yield* client.get(`${first.url}/env`)).json).toMatchObject({
29 value: "one",
30 hasAccountKey: false,
31 });
32 const second = yield* deploy("two");
33 expect(yield* (yield* client.get(`${second.url}/env`)).json).toMatchObject({
34 value: "two",
35 hasAccountKey: false,
36 });
37 yield* stack.destroy();
38 expect(
39 yield* client.get(second.url).pipe(
40 Effect.timeout("5 seconds"),
41 Effect.as(false),
42 Effect.catchTag("HttpClientError", () => Effect.succeed(true)),
43 ),
44 ).toBe(true);
45 }),
46 {
47 tags: ["provider:neon", "provider:neon:function", "local"],
48 timeout: 120_000,
49 },
50);
51​
52test.provider(
53 "remote Function opt-out uses the live provider and stamped cleanup",
54 (stack) =>
55 Effect.gen(function* () {
56 yield* stack.destroy();
57 const first = yield* stack.deploy(
58 Effect.gen(function* () {
59 const project = yield* Project("RemoteFunctionProject", {
60 region: "aws-us-east-2",
61 });
62 return yield* Function("RemoteApi", {
63 project,
64 main: new URL("./fixtures/function-bare.ts", import.meta.url).href,
65 }).pipe(Alchemy.remote());
66 }),
67 );
68 expect(first.functionId.startsWith("dev:")).toBe(false);
69 const client = yield* HttpClient.HttpClient;
70 expect(yield* (yield* client.get(first.url)).text).toBe("bare-v2");
71 const observed = yield* Api.getProjectBranchFunction({
72 project_id: first.projectId,
73 branch_id: first.branchId,
74 slug: first.slug,
75 });
76 expect(observed.function.id).toBe(first.functionId);
77 yield* stack.destroy();
78 expect(
79 yield* Api.getProjectBranchFunction({
80 project_id: first.projectId,
81 branch_id: first.branchId,
82 slug: first.slug,
83 }).pipe(
84 Effect.as(false),
85 Effect.catchTag("NotFound", () => Effect.succeed(true)),
86 ),
87 ).toBe(true);
88 }),
89 {
90 tags: ["provider:neon", "provider:neon:function", "provider:neon:project", "live"],
91 timeout: 120_000,
92 },
93);