-
Notifications
You must be signed in to change notification settings - Fork 523
Expand file tree
/
Copy pathfunctions.unit.test.ts
More file actions
156 lines (142 loc) · 5.34 KB
/
Copy pathfunctions.unit.test.ts
File metadata and controls
156 lines (142 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { describe, expect, it } from "@effect/vitest";
import { BunServices } from "@effect/platform-bun";
import { mkdtempSync } from "node:fs";
import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { Effect } from "effect";
import { resolveConfig } from "./createStack.ts";
import {
configureFunctionsRuntime,
functionsRuntimeConfigPath,
resolveFunctionsRuntimeConfig,
} from "./functions.ts";
function makeTempProject(): string {
return mkdtempSync(join(tmpdir(), "supabase-stack-functions-"));
}
async function writeProject(cwd: string) {
await mkdir(join(cwd, "supabase", "functions", "hello-world"), { recursive: true });
await mkdir(join(cwd, "supabase", "functions", "disabled-function"), { recursive: true });
await writeFile(
join(cwd, "supabase", "functions", "hello-world", "index.ts"),
"Deno.serve(() => Response.json({ ok: true }));\n",
);
await writeFile(
join(cwd, "supabase", "functions", "disabled-function", "index.ts"),
"Deno.serve(() => Response.json({ disabled: true }));\n",
);
await writeFile(
join(cwd, "supabase", ".env"),
"CONFIG_ONLY=from-project-env\nSHARED=from-project-env\n",
);
await writeFile(
join(cwd, "supabase", "functions", ".env"),
"FILE_ONLY=from-functions-env\nSHARED=from-functions-env\n",
);
await writeFile(
join(cwd, "supabase", "config.json"),
JSON.stringify({
functions: {
"hello-world": {
verify_jwt: true,
env: {
CONFIG_ONLY: "env(CONFIG_ONLY)",
SHARED: "env(SHARED)",
},
},
"disabled-function": {
enabled: false,
},
},
}),
);
}
describe("stack Functions runtime config", () => {
it.live("auto-detects enabled functions from projectDir", () => {
const cwd = makeTempProject();
return Effect.gen(function* () {
yield* Effect.promise(() => writeProject(cwd));
const stackConfig = yield* Effect.promise(() => resolveConfig({ projectDir: cwd }));
const config = yield* resolveFunctionsRuntimeConfig(stackConfig, {
hostname: "127.0.0.1",
});
expect(config).toBeDefined();
expect(Object.keys(config!.functions)).toEqual(["hello-world"]);
expect(config!.functions["hello-world"]).toEqual({
verifyJWT: true,
entrypointPath: join(cwd, "supabase", "functions", "hello-world", "index.ts"),
importMapPath: "",
staticFiles: [],
});
expect(config!.env).toMatchObject({
FILE_ONLY: "from-functions-env",
CONFIG_ONLY: "from-project-env",
SHARED: "from-project-env",
});
// JWKS is injected so the edge runtime can verify asymmetric JWTs.
expect(JSON.parse(config!.jwks)).toEqual({
keys: [expect.objectContaining({ kty: "oct", k: expect.any(String) })],
});
}).pipe(
Effect.provide(BunServices.layer),
Effect.ensuring(Effect.promise(() => rm(cwd, { recursive: true, force: true }))),
);
});
it.live("supports explicit env files and disabling JWT verification", () => {
const cwd = makeTempProject();
return Effect.gen(function* () {
yield* Effect.promise(() => writeProject(cwd));
yield* Effect.promise(() => writeFile(join(cwd, "custom.env"), "FILE_ONLY=custom\n"));
const stackConfig = yield* Effect.promise(() =>
resolveConfig({
projectDir: cwd,
functions: {
envFile: "custom.env",
noVerifyJwt: true,
},
}),
);
const config = yield* resolveFunctionsRuntimeConfig(stackConfig, {
hostname: "127.0.0.1",
});
expect(config!.env.FILE_ONLY).toBe("custom");
expect(config!.functions["hello-world"]?.verifyJWT).toBe(false);
}).pipe(
Effect.provide(BunServices.layer),
Effect.ensuring(Effect.promise(() => rm(cwd, { recursive: true, force: true }))),
);
});
it.live("keeps placeholder mode when Functions are disabled", () => {
const cwd = makeTempProject();
return Effect.gen(function* () {
yield* Effect.promise(() => writeProject(cwd));
const stackConfig = yield* Effect.promise(() =>
resolveConfig({ projectDir: cwd, functions: false }),
);
const config = yield* resolveFunctionsRuntimeConfig(stackConfig, {
hostname: "127.0.0.1",
});
expect(config).toBeUndefined();
}).pipe(
Effect.provide(BunServices.layer),
Effect.ensuring(Effect.promise(() => rm(cwd, { recursive: true, force: true }))),
);
});
it.live("writes generated runtime config into the stack runtime directory", () => {
const cwd = makeTempProject();
return Effect.gen(function* () {
yield* Effect.promise(() => writeProject(cwd));
const stackConfig = yield* Effect.promise(() => resolveConfig({ projectDir: cwd }));
yield* configureFunctionsRuntime(stackConfig, { hostname: "127.0.0.1" });
const written = JSON.parse(
yield* Effect.promise(() =>
readFile(functionsRuntimeConfigPath(stackConfig.runtimeRoot), "utf8"),
),
) as { functions: Record<string, unknown> };
expect(Object.keys(written.functions)).toEqual(["hello-world"]);
}).pipe(
Effect.provide(BunServices.layer),
Effect.ensuring(Effect.promise(() => rm(cwd, { recursive: true, force: true }))),
);
});
});