-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathschema.test.ts
More file actions
280 lines (235 loc) · 8.85 KB
/
schema.test.ts
File metadata and controls
280 lines (235 loc) · 8.85 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import { test, expect, describe, beforeEach, afterEach, spyOn, mock } from "bun:test";
import { mkdtemp, rm } from "node:fs/promises";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { _setConfigDir, setProfile } from "../../lib/config.ts";
import { credentialStoreStubs, gitStubs, stubFetch, captureLog } from "../../test/lib/stubs.ts";
mock.module("../../lib/credential-store.ts", () => credentialStoreStubs);
mock.module("../../lib/git.ts", () => gitStubs);
describe("config schema", () => {
const originalEnv = { ...process.env };
const originalFetch = globalThis.fetch;
let tempDir: string;
let logSpy: ReturnType<typeof spyOn>;
let errorSpy: ReturnType<typeof spyOn>;
let exitSpy: ReturnType<typeof spyOn>;
let captured: ReturnType<typeof captureLog>;
const mockSchema = {
type: "object",
properties: {
session: {
type: "object",
properties: { lifetime: { type: "integer" } },
},
},
};
beforeEach(async () => {
tempDir = await mkdtemp(join(tmpdir(), "clerk-config-schema-test-"));
_setConfigDir(tempDir);
process.env.CLERK_PLATFORM_API_KEY = "test_key";
process.env.CLERK_PLATFORM_API_URL = "https://test-api.clerk.com";
logSpy = spyOn(console, "log").mockImplementation(() => {});
errorSpy = spyOn(console, "error").mockImplementation(() => {});
exitSpy = spyOn(process, "exit").mockImplementation(() => {
throw new Error("process.exit");
});
captured = captureLog();
stubFetch(async () => new Response(JSON.stringify(mockSchema), { status: 200 }));
});
afterEach(async () => {
captured.teardown();
_setConfigDir(undefined);
process.env = { ...originalEnv };
globalThis.fetch = originalFetch;
logSpy.mockRestore();
errorSpy.mockRestore();
exitSpy.mockRestore();
await rm(tempDir, { recursive: true, force: true });
});
async function runConfigSchema(
options: { app?: string; instance?: string; output?: string; keys?: string[] } = {},
) {
const { configSchema } = await import("./schema.ts");
return captured.run(() => configSchema(options));
}
test("errors when no profile is linked", async () => {
await expect(runConfigSchema()).rejects.toThrow("No Clerk project linked");
});
test("errors when CLERK_PLATFORM_API_KEY is missing", async () => {
await setProfile(process.cwd(), {
workspaceId: "org_1",
appId: "app_1",
instances: { development: "ins_dev" },
});
delete process.env.CLERK_PLATFORM_API_KEY;
await expect(runConfigSchema()).rejects.toThrow("Not authenticated");
});
test("prints schema JSON to stdout by default", async () => {
await setProfile(process.cwd(), {
workspaceId: "org_1",
appId: "app_1",
instances: { development: "ins_dev" },
});
await runConfigSchema();
expect(captured.out).toBe(JSON.stringify(mockSchema, null, 2));
});
test("supports --app without a linked profile", async () => {
const mockApp = {
application_id: "app_1",
instances: [{ instance_id: "ins_dev", environment_type: "development" }],
};
stubFetch(async (input) => {
const url = input.toString();
if (url.includes("/instances/") && url.includes("/config")) {
return new Response(JSON.stringify(mockSchema), { status: 200 });
}
if (url.includes("/v1/platform/applications/app_1")) {
return new Response(JSON.stringify(mockApp), { status: 200 });
}
throw new Error(`Unexpected fetch: ${url}`);
});
await runConfigSchema({ app: "app_1" });
expect(captured.out).toBe(JSON.stringify(mockSchema, null, 2));
});
test("writes schema to file with --output", async () => {
await setProfile(process.cwd(), {
workspaceId: "org_1",
appId: "app_1",
instances: { development: "ins_dev" },
});
const outFile = join(tempDir, "schema.json");
await runConfigSchema({ output: outFile });
const written = await Bun.file(outFile).json();
expect(written).toEqual(mockSchema);
expect(captured.err).toContain("Schema written to");
});
test("shows which environment is being pulled", async () => {
await setProfile(process.cwd(), {
workspaceId: "org_1",
appId: "app_1",
instances: { development: "ins_dev" },
});
await runConfigSchema();
expect(captured.err).toContain("Pulling config schema from app_1 (development)...");
});
test("shows production label when --instance prod", async () => {
await setProfile(process.cwd(), {
workspaceId: "org_1",
appId: "app_1",
instances: { development: "ins_dev", production: "ins_prod" },
});
await runConfigSchema({ instance: "prod" });
expect(captured.err).toContain("Pulling config schema from app_1 (production)...");
});
test("uses development instance by default", async () => {
let requestedUrl = "";
stubFetch(async (input) => {
requestedUrl = input.toString();
return new Response(JSON.stringify(mockSchema), { status: 200 });
});
await setProfile(process.cwd(), {
workspaceId: "org_1",
appId: "app_1",
instances: { development: "ins_dev", production: "ins_prod" },
});
await runConfigSchema();
expect(requestedUrl).toContain("/instances/ins_dev/");
});
test("--instance prod targets production instance", async () => {
let requestedUrl = "";
stubFetch(async (input) => {
requestedUrl = input.toString();
return new Response(JSON.stringify(mockSchema), { status: 200 });
});
await setProfile(process.cwd(), {
workspaceId: "org_1",
appId: "app_1",
instances: { development: "ins_dev", production: "ins_prod" },
});
await runConfigSchema({ instance: "prod" });
expect(requestedUrl).toContain("/instances/ins_prod/");
});
test("--instance with literal ID passes through", async () => {
let requestedUrl = "";
stubFetch(async (input) => {
requestedUrl = input.toString();
return new Response(JSON.stringify(mockSchema), { status: 200 });
});
await setProfile(process.cwd(), {
workspaceId: "org_1",
appId: "app_1",
instances: { development: "ins_dev" },
});
await runConfigSchema({ instance: "ins_custom_123" });
expect(requestedUrl).toContain("/instances/ins_custom_123/");
});
test("passes --keys to API as query params", async () => {
let requestedUrl = "";
stubFetch(async (input) => {
requestedUrl = input.toString();
return new Response(JSON.stringify(mockSchema), { status: 200 });
});
await setProfile(process.cwd(), {
workspaceId: "org_1",
appId: "app_1",
instances: { development: "ins_dev" },
});
await runConfigSchema({ keys: ["session", "auth_email"] });
expect(requestedUrl).toContain("keys=session");
expect(requestedUrl).toContain("keys=auth_email");
expect(requestedUrl).toContain("/config/schema");
});
test("splits comma-separated --keys into individual params", async () => {
let requestedUrl = "";
stubFetch(async (input) => {
requestedUrl = input.toString();
return new Response(JSON.stringify(mockSchema), { status: 200 });
});
await setProfile(process.cwd(), {
workspaceId: "org_1",
appId: "app_1",
instances: { development: "ins_dev" },
});
await runConfigSchema({ keys: ["session,auth_email"] });
expect(requestedUrl).toContain("/config/schema");
expect(requestedUrl).toContain("keys=session");
expect(requestedUrl).toContain("keys=auth_email");
});
test("trims whitespace from comma-separated --keys", async () => {
let requestedUrl = "";
stubFetch(async (input) => {
requestedUrl = input.toString();
return new Response(JSON.stringify(mockSchema), { status: 200 });
});
await setProfile(process.cwd(), {
workspaceId: "org_1",
appId: "app_1",
instances: { development: "ins_dev" },
});
await runConfigSchema({ keys: ["session, auth_email"] });
expect(requestedUrl).toContain("/config/schema");
expect(requestedUrl).toContain("keys=session");
expect(requestedUrl).toContain("keys=auth_email");
expect(requestedUrl).not.toContain("keys=+");
expect(requestedUrl).not.toContain("keys=%20");
});
test("errors when production instance not configured", async () => {
await setProfile(process.cwd(), {
workspaceId: "org_1",
appId: "app_1",
instances: { development: "ins_dev" },
});
await expect(runConfigSchema({ instance: "prod" })).rejects.toThrow(
"No production instance configured",
);
});
test("handles API errors gracefully", async () => {
stubFetch(async () => new Response("Unauthorized", { status: 401 }));
await setProfile(process.cwd(), {
workspaceId: "org_1",
appId: "app_1",
instances: { development: "ins_dev" },
});
await expect(runConfigSchema()).rejects.toThrow("Unauthorized");
});
});