-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpull.test.ts
More file actions
315 lines (265 loc) · 9.92 KB
/
pull.test.ts
File metadata and controls
315 lines (265 loc) · 9.92 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
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 { captureLog, credentialStoreStubs, gitStubs, stubFetch } from "../../test/lib/stubs.ts";
mock.module("../../lib/credential-store.ts", () => credentialStoreStubs);
mock.module("../../lib/git.ts", () => gitStubs);
mock.module("../../lib/spinner.ts", () => ({
withSpinner: async (msg: string, fn: () => Promise<unknown>) => {
const { log } = await import("../../lib/log.ts");
log.info(msg);
return fn();
},
}));
describe("config pull", () => {
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 mockConfig = {
session: { lifetime: 604800 },
sign_up: { mode: "public" },
};
beforeEach(async () => {
tempDir = await mkdtemp(join(tmpdir(), "clerk-config-pull-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(mockConfig), { 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 });
});
// Dynamically import to get fresh module state
async function runConfigPull(
options: { app?: string; instance?: string; output?: string; keys?: string[] } = {},
) {
const { configPull } = await import("./pull.ts");
return captured.run(() => configPull(options));
}
test("errors when no profile is linked", async () => {
await expect(runConfigPull()).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(runConfigPull()).rejects.toThrow("Not authenticated");
});
test("prints config JSON to stdout by default", async () => {
await setProfile(process.cwd(), {
workspaceId: "org_1",
appId: "app_1",
instances: { development: "ins_dev" },
});
await runConfigPull();
expect(captured.out).toContain(JSON.stringify(mockConfig, 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(mockConfig), { 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 runConfigPull({ app: "app_1" });
expect(captured.out).toContain(JSON.stringify(mockConfig, null, 2));
});
test("writes config to file with --output", async () => {
await setProfile(process.cwd(), {
workspaceId: "org_1",
appId: "app_1",
instances: { development: "ins_dev" },
});
const outFile = join(tempDir, "output.json");
await runConfigPull({ output: outFile });
const written = await Bun.file(outFile).json();
expect(written).toEqual(mockConfig);
expect(captured.err).toContain("Config 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 runConfigPull();
expect(captured.err).toContain("Pulling config from app_1 (development)");
});
test("shows app name when stored in profile", async () => {
await setProfile(process.cwd(), {
workspaceId: "org_1",
appId: "app_1",
appName: "My SaaS App",
instances: { development: "ins_dev" },
});
await runConfigPull();
expect(captured.err).toContain("Pulling config from My SaaS App (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 runConfigPull({ instance: "prod" });
expect(captured.err).toContain("Pulling config from app_1 (production)");
});
test("uses development instance by default", async () => {
let requestedUrl = "";
stubFetch(async (input) => {
requestedUrl = input.toString();
return new Response(JSON.stringify(mockConfig), { status: 200 });
});
await setProfile(process.cwd(), {
workspaceId: "org_1",
appId: "app_1",
instances: { development: "ins_dev", production: "ins_prod" },
});
await runConfigPull();
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(mockConfig), { status: 200 });
});
await setProfile(process.cwd(), {
workspaceId: "org_1",
appId: "app_1",
instances: { development: "ins_dev", production: "ins_prod" },
});
await runConfigPull({ 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(mockConfig), { status: 200 });
});
await setProfile(process.cwd(), {
workspaceId: "org_1",
appId: "app_1",
instances: { development: "ins_dev" },
});
await runConfigPull({ instance: "ins_custom_123" });
expect(requestedUrl).toContain("/instances/ins_custom_123/");
});
test("errors when production instance not configured", async () => {
await setProfile(process.cwd(), {
workspaceId: "org_1",
appId: "app_1",
instances: { development: "ins_dev" },
});
await expect(runConfigPull({ instance: "prod" })).rejects.toThrow(
"No production instance configured",
);
});
test("--keys passes keys as query params to the API", async () => {
let requestedUrl = "";
stubFetch(async (input) => {
requestedUrl = input.toString();
return new Response(JSON.stringify({ session: { lifetime: 604800 } }), { status: 200 });
});
await setProfile(process.cwd(), {
workspaceId: "org_1",
appId: "app_1",
instances: { development: "ins_dev" },
});
await runConfigPull({ keys: ["session"] });
expect(requestedUrl).toContain("keys=session");
expect(captured.out).toContain(JSON.stringify({ session: { lifetime: 604800 } }, null, 2));
});
test("--keys passes multiple keys as repeated query params", async () => {
let requestedUrl = "";
stubFetch(async (input) => {
requestedUrl = input.toString();
return new Response(
JSON.stringify({ session: { lifetime: 604800 }, sign_up: { mode: "public" } }),
{
status: 200,
},
);
});
await setProfile(process.cwd(), {
workspaceId: "org_1",
appId: "app_1",
instances: { development: "ins_dev" },
});
await runConfigPull({ keys: ["session", "auth_email"] });
expect(requestedUrl).toContain("keys=session");
expect(requestedUrl).toContain("keys=auth_email");
});
test("splits comma-separated --keys into individual params", async () => {
let requestedUrl = "";
stubFetch(async (input) => {
requestedUrl = input.toString();
return new Response(JSON.stringify({ session: { lifetime: 604800 } }), { status: 200 });
});
await setProfile(process.cwd(), {
workspaceId: "org_1",
appId: "app_1",
instances: { development: "ins_dev" },
});
await runConfigPull({ keys: ["session,auth_email"] });
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({ session: { lifetime: 604800 } }), { status: 200 });
});
await setProfile(process.cwd(), {
workspaceId: "org_1",
appId: "app_1",
instances: { development: "ins_dev" },
});
await runConfigPull({ keys: ["session, auth_email"] });
expect(requestedUrl).toContain("keys=session");
expect(requestedUrl).toContain("keys=auth_email");
expect(requestedUrl).not.toContain("keys=+");
expect(requestedUrl).not.toContain("keys=%20");
});
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(runConfigPull()).rejects.toThrow("Unauthorized");
});
});