-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtemplates.test.ts
More file actions
556 lines (482 loc) · 17.4 KB
/
Copy pathtemplates.test.ts
File metadata and controls
556 lines (482 loc) · 17.4 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
import fs from "fs";
import os from "os";
import path from "path";
import AdmZip from "adm-zip";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
// templates.ts imports VERSION from ../index.js, and index.ts runs main() at
// import time. Mock it so importing templates.ts never dispatches the CLI.
vi.mock("../index.js", () => ({ VERSION: "0.0.0-test" }));
import { SEAMLESS_TEMPLATES_REF, SEAMLESS_TEMPLATES_REPO } from "./images.js";
import {
applyTemplateEnv,
assertCliSupports,
matchesTemplateFlag,
openTemplateSource,
templateFlags,
type RegistryEntry,
type ScaffoldContext,
type TemplateManifest,
} from "./templates.js";
function mkTmpDir(prefix: string): string {
return fs.mkdtempSync(path.join(os.tmpdir(), prefix));
}
function rmDir(dir: string) {
fs.rmSync(dir, { recursive: true, force: true });
}
// Routes fetch calls by a substring of the request URL, so a test can answer the
// registry.json request differently from the archive .zip request.
function mockFetchByUrl(
routes: Record<
string,
() => {
ok: boolean;
status?: number;
text?: () => Promise<string> | string;
arrayBuffer?: () => Promise<Buffer> | Buffer;
}
>,
) {
const fn = vi.fn(async (url: string) => {
const key = Object.keys(routes).find((k) => url.includes(k));
if (!key) throw new Error(`no mocked route for ${url}`);
return routes[key]();
});
vi.stubGlobal("fetch", fn);
return fn;
}
// Builds a zip buffer whose entries mimic a GitHub codeload archive: everything
// nested under one top-level directory.
function buildZip(entries: Record<string, string>): Buffer {
const zip = new AdmZip();
for (const [name, content] of Object.entries(entries)) {
zip.addFile(name, Buffer.from(content, "utf-8"));
}
return zip.toBuffer();
}
afterEach(() => {
vi.unstubAllGlobals();
delete process.env.SEAMLESS_TEMPLATES_DIR;
delete process.env.SEAMLESS_TEMPLATES_REF;
});
describe("openTemplateSource (local)", () => {
let dir: string;
beforeEach(() => {
dir = mkTmpDir("seamless-templates-local-");
process.env.SEAMLESS_TEMPLATES_DIR = dir;
});
afterEach(() => {
rmDir(dir);
});
it("throws when registry.json is missing", async () => {
await expect(openTemplateSource()).rejects.toThrow(
/no registry\.json was found/,
);
});
it("throws when the registry has no templates array", async () => {
fs.writeFileSync(path.join(dir, "registry.json"), JSON.stringify({ foo: 1 }));
await expect(openTemplateSource()).rejects.toThrow(/malformed/);
});
it("throws when templates is not an array", async () => {
fs.writeFileSync(
path.join(dir, "registry.json"),
JSON.stringify({ schemaVersion: 1, templates: "nope" }),
);
await expect(openTemplateSource()).rejects.toThrow(/malformed/);
});
it("parses a valid registry and exposes it", async () => {
const registry = {
schemaVersion: 1,
templates: [
{
id: "web-a",
kind: "web",
framework: "react",
label: "React",
status: "stable",
path: "templates/web-a",
},
],
};
fs.writeFileSync(path.join(dir, "registry.json"), JSON.stringify(registry));
const source = await openTemplateSource();
expect(source.registry).toEqual(registry);
});
it("readManifest reads template.json from the local checkout", async () => {
const registry = {
schemaVersion: 1,
templates: [
{
id: "web-a",
kind: "web",
framework: "react",
label: "React",
status: "stable",
path: "templates/web-a",
},
],
};
fs.writeFileSync(path.join(dir, "registry.json"), JSON.stringify(registry));
const manifest: TemplateManifest = { id: "web-a", targetDir: "web" };
fs.mkdirSync(path.join(dir, "templates", "web-a"), { recursive: true });
fs.writeFileSync(
path.join(dir, "templates", "web-a", "template.json"),
JSON.stringify(manifest),
);
const source = await openTemplateSource();
const read = await source.readManifest(registry.templates[0] as RegistryEntry);
expect(read).toEqual(manifest);
});
it("copyInto copies files recursively while skipping ignored names", async () => {
const registry = {
schemaVersion: 1,
templates: [
{
id: "web-a",
kind: "web",
framework: "react",
label: "React",
status: "stable",
path: "templates/web-a",
},
],
};
fs.writeFileSync(path.join(dir, "registry.json"), JSON.stringify(registry));
const templateDir = path.join(dir, "templates", "web-a");
fs.mkdirSync(path.join(templateDir, "src", "nested"), { recursive: true });
fs.mkdirSync(path.join(templateDir, "node_modules", "pkg"), { recursive: true });
fs.mkdirSync(path.join(templateDir, ".git"), { recursive: true });
fs.writeFileSync(path.join(templateDir, "template.json"), "{}");
fs.writeFileSync(path.join(templateDir, "src", "index.ts"), "export {};");
fs.writeFileSync(path.join(templateDir, "src", "nested", "deep.ts"), "deep");
fs.writeFileSync(path.join(templateDir, "node_modules", "pkg", "index.js"), "ignored");
fs.writeFileSync(path.join(templateDir, ".git", "HEAD"), "ignored");
fs.writeFileSync(path.join(templateDir, ".DS_Store"), "ignored");
const source = await openTemplateSource();
const destDir = mkTmpDir("seamless-templates-dest-");
try {
await source.copyInto(registry.templates[0] as RegistryEntry, destDir);
expect(fs.readFileSync(path.join(destDir, "template.json"), "utf-8")).toBe("{}");
expect(fs.readFileSync(path.join(destDir, "src", "index.ts"), "utf-8")).toBe(
"export {};",
);
expect(
fs.readFileSync(path.join(destDir, "src", "nested", "deep.ts"), "utf-8"),
).toBe("deep");
expect(fs.existsSync(path.join(destDir, "node_modules"))).toBe(false);
expect(fs.existsSync(path.join(destDir, ".git"))).toBe(false);
expect(fs.existsSync(path.join(destDir, ".DS_Store"))).toBe(false);
} finally {
rmDir(destDir);
}
});
});
describe("openTemplateSource (remote)", () => {
const registryPayload = {
schemaVersion: 1,
templates: [
{
id: "web-a",
kind: "web",
framework: "react",
label: "React",
status: "stable",
path: "templates/web-a",
},
],
};
it("uses the SEAMLESS_TEMPLATES_REF override in the registry URL", async () => {
process.env.SEAMLESS_TEMPLATES_REF = "custom-ref";
const fetchMock = mockFetchByUrl({
"registry.json": () => ({
ok: true,
text: () => JSON.stringify(registryPayload),
}),
});
await openTemplateSource();
const url = fetchMock.mock.calls[0][0] as string;
expect(url).toContain(`/${SEAMLESS_TEMPLATES_REPO}/custom-ref/registry.json`);
});
it("falls back to the default ref when SEAMLESS_TEMPLATES_REF is unset", async () => {
const fetchMock = mockFetchByUrl({
"registry.json": () => ({
ok: true,
text: () => JSON.stringify(registryPayload),
}),
});
await openTemplateSource();
const url = fetchMock.mock.calls[0][0] as string;
expect(url).toContain(`/${SEAMLESS_TEMPLATES_REPO}/${SEAMLESS_TEMPLATES_REF}/registry.json`);
});
it("throws with the status code when the registry fetch fails", async () => {
mockFetchByUrl({
"registry.json": () => ({ ok: false, status: 404 }),
});
await expect(openTemplateSource()).rejects.toThrow(
/Failed to fetch the template registry \(404\)/,
);
});
it("names the registry URL when the connection fails", async () => {
mockFetchByUrl({
"registry.json": () => {
throw new TypeError("fetch failed");
},
});
await expect(openTemplateSource()).rejects.toThrow(
`Could not reach https://raw.githubusercontent.com/${SEAMLESS_TEMPLATES_REPO}/${SEAMLESS_TEMPLATES_REF}/registry.json to read the template registry. Check your network connection.`,
);
});
it("throws when the fetched registry is malformed", async () => {
mockFetchByUrl({
"registry.json": () => ({ ok: true, text: () => JSON.stringify({}) }),
});
await expect(openTemplateSource()).rejects.toThrow(/malformed/);
});
it("throws with the status code when the archive download fails", async () => {
mockFetchByUrl({
"registry.json": () => ({
ok: true,
text: () => JSON.stringify(registryPayload),
}),
".zip": () => ({ ok: false, status: 503 }),
});
const source = await openTemplateSource();
await expect(
source.readManifest(registryPayload.templates[0] as RegistryEntry),
).rejects.toThrow(/Failed to download templates \(503\)/);
});
it("names the archive URL when the connection fails", async () => {
mockFetchByUrl({
"registry.json": () => ({
ok: true,
text: () => JSON.stringify(registryPayload),
}),
".zip": () => {
throw new TypeError("fetch failed");
},
});
const source = await openTemplateSource();
await expect(
source.readManifest(registryPayload.templates[0] as RegistryEntry),
).rejects.toThrow(
`Could not reach https://github.com/${SEAMLESS_TEMPLATES_REPO}/archive/${SEAMLESS_TEMPLATES_REF}.zip to download the project templates. Check your network connection.`,
);
});
it("throws when the downloaded archive has no entries", async () => {
mockFetchByUrl({
"registry.json": () => ({
ok: true,
text: () => JSON.stringify(registryPayload),
}),
".zip": () => ({ ok: true, arrayBuffer: () => new AdmZip().toBuffer() }),
});
const source = await openTemplateSource();
await expect(
source.readManifest(registryPayload.templates[0] as RegistryEntry),
).rejects.toThrow(/archive was empty/);
});
it("throws when the entry has no template.json in the archive", async () => {
const root = "seamless-templates-abc123";
const zipBuffer = buildZip({
[`${root}/templates/other/template.json`]: "{}",
});
mockFetchByUrl({
"registry.json": () => ({
ok: true,
text: () => JSON.stringify(registryPayload),
}),
".zip": () => ({ ok: true, arrayBuffer: () => zipBuffer }),
});
const source = await openTemplateSource();
await expect(
source.readManifest(registryPayload.templates[0] as RegistryEntry),
).rejects.toThrow(/missing template\.json in the registry/);
});
it("reads the manifest and copies filtered files, downloading the archive only once", async () => {
const root = "seamless-templates-abc123";
const manifest: TemplateManifest = { id: "web-a", targetDir: "web" };
const zipBuffer = buildZip({
[`${root}/templates/web-a/template.json`]: JSON.stringify(manifest),
[`${root}/templates/web-a/src/App.tsx`]: "app content",
[`${root}/templates/web-a/node_modules/pkg/index.js`]: "ignored",
[`${root}/templates/web-a/.env`]: "ignored",
[`${root}/templates/other/template.json`]: "{}",
});
const fetchMock = mockFetchByUrl({
"registry.json": () => ({
ok: true,
text: () => JSON.stringify(registryPayload),
}),
".zip": () => ({ ok: true, arrayBuffer: () => zipBuffer }),
});
const source = await openTemplateSource();
const entry = registryPayload.templates[0] as RegistryEntry;
const readManifest = await source.readManifest(entry);
expect(readManifest).toEqual(manifest);
const destDir = mkTmpDir("seamless-templates-dest-");
try {
await source.copyInto(entry, destDir);
expect(fs.readFileSync(path.join(destDir, "template.json"), "utf-8")).toBe(
JSON.stringify(manifest),
);
expect(fs.readFileSync(path.join(destDir, "src", "App.tsx"), "utf-8")).toBe(
"app content",
);
expect(fs.existsSync(path.join(destDir, "node_modules"))).toBe(false);
expect(fs.existsSync(path.join(destDir, ".env"))).toBe(false);
} finally {
rmDir(destDir);
}
// Registry fetch + one archive fetch, shared across readManifest and copyInto.
expect(fetchMock).toHaveBeenCalledTimes(2);
});
});
describe("assertCliSupports", () => {
it("does nothing when requires is absent", () => {
expect(() => assertCliSupports({ id: "x", targetDir: "." }, "X")).not.toThrow();
});
it("does nothing when cliMin is satisfied by the running CLI version", () => {
expect(() =>
assertCliSupports({ id: "x", targetDir: ".", requires: { cliMin: "0.0.0" } }, "X"),
).not.toThrow();
});
it("throws a clear error when cliMin exceeds the running CLI version", () => {
expect(() =>
assertCliSupports({ id: "x", targetDir: ".", requires: { cliMin: "0.0.1" } }, "X"),
).toThrow(/Template "X" requires seamless-cli >= 0\.0\.1, but this is 0\.0\.0-test/);
});
});
describe("applyTemplateEnv", () => {
let destDir: string;
const ctx: ScaffoldContext = {
authServerUrl: "http://auth.local",
apiUrl: "http://api.local",
apiToken: "tok-123",
jwksKid: "kid-1",
};
beforeEach(() => {
destDir = mkTmpDir("seamless-apply-env-");
});
afterEach(() => {
rmDir(destDir);
});
function readEnv(): string {
return fs.readFileSync(path.join(destDir, ".env"), "utf-8");
}
it("seeds values from env.fromExample when the file exists", () => {
fs.writeFileSync(path.join(destDir, ".env.example"), "FOO=bar\n");
applyTemplateEnv(destDir, { id: "x", targetDir: ".", env: { fromExample: ".env.example" } }, ctx);
expect(readEnv()).toContain("FOO=bar");
});
it("ignores a fromExample file that does not exist", () => {
applyTemplateEnv(destDir, { id: "x", targetDir: ".", env: { fromExample: ".env.example" } }, ctx);
expect(readEnv()).toBe("\n");
});
it("reads an existing .env when there is no fromExample", () => {
fs.writeFileSync(path.join(destDir, ".env"), "EXIST=1\n");
applyTemplateEnv(destDir, { id: "x", targetDir: "." }, ctx);
expect(readEnv()).toContain("EXIST=1");
});
it("starts empty when there is neither fromExample nor an existing .env", () => {
applyTemplateEnv(destDir, { id: "x", targetDir: "." }, ctx);
expect(readEnv()).toBe("\n");
});
it("resolves known context placeholders and leaves plain values untouched", () => {
applyTemplateEnv(
destDir,
{
id: "x",
targetDir: ".",
env: {
set: {
AUTH: "{{authServerUrl}}",
API: "{{apiUrl}}",
TOKEN: "{{apiToken}}",
KID: "{{jwksKid}}",
PLAIN: "literal-value",
},
},
},
ctx,
);
const written = readEnv();
expect(written).toContain("AUTH=http://auth.local");
expect(written).toContain("API=http://api.local");
expect(written).toContain("TOKEN=tok-123");
expect(written).toContain("KID=kid-1");
expect(written).toContain("PLAIN=literal-value");
});
it("resolves the serveAdminConsole placeholder from the scaffold context", () => {
applyTemplateEnv(
destDir,
{
id: "x",
targetDir: ".",
env: { set: { SERVE_ADMIN_CONSOLE: "{{serveAdminConsole}}" } },
},
{ ...ctx, serveAdminConsole: "true" },
);
expect(readEnv()).toContain("SERVE_ADMIN_CONSOLE=true");
});
it("resolves secret:N placeholders to N bytes of hex", () => {
applyTemplateEnv(
destDir,
{ id: "x", targetDir: ".", env: { set: { SECRET: "{{secret:16}}" } } },
ctx,
);
const match = /SECRET=([0-9a-f]+)/.exec(readEnv());
expect(match?.[1]).toHaveLength(32);
});
it("throws when a known placeholder has no value in the scaffold context", () => {
const partialCtx: ScaffoldContext = { authServerUrl: "a", apiUrl: "b" };
expect(() =>
applyTemplateEnv(
destDir,
{ id: "x", targetDir: ".", env: { set: { TOKEN: "{{apiToken}}" } } },
partialCtx,
),
).toThrow(/Template placeholder \{\{apiToken\}\} has no value in this configuration/);
});
it("throws for a placeholder token that is not recognized", () => {
expect(() =>
applyTemplateEnv(
destDir,
{ id: "x", targetDir: ".", env: { set: { X: "{{bogus}}" } } },
ctx,
),
).toThrow(/Unknown template placeholder \{\{bogus\}\}/);
});
});
describe("template flags", () => {
const withAlias: RegistryEntry = {
id: "react-vite",
kind: "web",
framework: "react",
label: "React (Vite)",
alias: "basic",
status: "stable",
path: "templates/web/react-vite",
};
const noAlias: RegistryEntry = {
id: "express",
kind: "api",
framework: "express",
label: "Express",
status: "stable",
path: "templates/api/express",
};
it("offers the alias before the id when a template declares one", () => {
expect(templateFlags(withAlias)).toEqual(["--basic", "--react-vite"]);
});
it("offers the id alone when a template declares no alias", () => {
expect(templateFlags(noAlias)).toEqual(["--express"]);
});
it("matches a template by either its alias or its id", () => {
expect(matchesTemplateFlag(withAlias, "basic")).toBe(true);
expect(matchesTemplateFlag(withAlias, "react-vite")).toBe(true);
expect(matchesTemplateFlag(withAlias, "vite")).toBe(false);
});
it("matches an alias-less template by its id", () => {
expect(matchesTemplateFlag(noAlias, "express")).toBe(true);
expect(matchesTemplateFlag(noAlias, "fastify")).toBe(false);
});
});