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/**
* Metadata route generation unit tests.
*
* Tests sitemap XML generation, robots.txt generation, manifest JSON
* generation, and metadata file scanning. These are direct counterparts
* to Next.js's metadata-dynamic-routes tests, verifying that vinext
* produces correct output for file-based metadata routes.
*/
import { describe, it, expect, beforeEach, afterEach } from "vitest";
import fs from "node:fs";
import path from "node:path";
import os from "node:os";
import {
sitemapToXml,
robotsToText,
manifestToJson,
scanMetadataFiles,
METADATA_FILE_MAP,
type SitemapEntry,
type RobotsConfig,
type ManifestConfig,
} from "../packages/vinext/src/server/metadata-routes.js";
// โโโ sitemapToXml โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
describe("sitemapToXml", () => {
it("generates valid XML for basic entries", () => {
const entries: SitemapEntry[] = [
{ url: "https://example.com" },
{ url: "https://example.com/about" },
];
const xml = sitemapToXml(entries);
expect(xml).toContain('<?xml version="1.0" encoding="UTF-8"?>');
expect(xml).toContain('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">');
expect(xml).toContain("<loc>https://example.com</loc>");
expect(xml).toContain("<loc>https://example.com/about</loc>");
expect(xml).toContain("</urlset>");
});
it("generates lastModified from Date", () => {
const date = new Date("2024-01-15T00:00:00Z");
const xml = sitemapToXml([{ url: "https://example.com", lastModified: date }]);
expect(xml).toContain("<lastmod>2024-01-15T00:00:00.000Z</lastmod>");
});
it("generates lastModified from string", () => {
const xml = sitemapToXml([{ url: "https://example.com", lastModified: "2024-01-15" }]);
expect(xml).toContain("<lastmod>2024-01-15</lastmod>");
});
it("generates changeFrequency", () => {
const xml = sitemapToXml([{ url: "https://example.com", changeFrequency: "weekly" }]);
expect(xml).toContain("<changefreq>weekly</changefreq>");
});
it("generates priority", () => {
const xml = sitemapToXml([{ url: "https://example.com", priority: 0.8 }]);
expect(xml).toContain("<priority>0.8</priority>");
});
it("generates image entries", () => {
const xml = sitemapToXml([{
url: "https://example.com",
images: ["https://example.com/photo1.jpg", "https://example.com/photo2.jpg"],
}]);
expect(xml).toContain("<image:image>");
expect(xml).toContain("<image:loc>https://example.com/photo1.jpg</image:loc>");
expect(xml).toContain("<image:loc>https://example.com/photo2.jpg</image:loc>");
expect(xml).toContain("</image:image>");
});
it("generates all fields together", () => {
const xml = sitemapToXml([{
url: "https://example.com/blog",
lastModified: "2024-06-01",
changeFrequency: "daily",
priority: 0.9,
images: ["https://example.com/hero.jpg"],
}]);
expect(xml).toContain("<loc>https://example.com/blog</loc>");
expect(xml).toContain("<lastmod>2024-06-01</lastmod>");
expect(xml).toContain("<changefreq>daily</changefreq>");
expect(xml).toContain("<priority>0.9</priority>");
expect(xml).toContain("<image:loc>https://example.com/hero.jpg</image:loc>");
});
it("generates valid XML for empty entries array", () => {
const xml = sitemapToXml([]);
expect(xml).toContain('<?xml version="1.0"');
expect(xml).toContain("<urlset");
expect(xml).toContain("</urlset>");
expect(xml).not.toContain("<url>");
});
it("escapes XML entities in URLs", () => {
const xml = sitemapToXml([{ url: "https://example.com?a=1&b=2" }]);
expect(xml).toContain("&");
expect(xml).not.toMatch(/<loc>[^<]*[^&]&[^a]/); // No unescaped & in loc
});
it("escapes angle brackets in text content", () => {
const xml = sitemapToXml([{ url: "https://example.com/<script>" }]);
expect(xml).toContain("<script>");
expect(xml).not.toContain("<script>");
});
});
// โโโ robotsToText โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
describe("robotsToText", () => {
it("generates basic robots.txt", () => {
const config: RobotsConfig = {
rules: { userAgent: "*", allow: "/", disallow: "/private" },
};
const txt = robotsToText(config);
expect(txt).toContain("User-Agent: *");
expect(txt).toContain("Allow: /");
expect(txt).toContain("Disallow: /private");
});
it("handles multiple rules", () => {
const config: RobotsConfig = {
rules: [
{ userAgent: "Googlebot", allow: "/" },
{ userAgent: "Bingbot", disallow: "/secret" },
],
};
const txt = robotsToText(config);
expect(txt).toContain("User-Agent: Googlebot");
expect(txt).toContain("Allow: /");
expect(txt).toContain("User-Agent: Bingbot");
expect(txt).toContain("Disallow: /secret");
});
it("handles multiple user agents per rule", () => {
const config: RobotsConfig = {
rules: { userAgent: ["Googlebot", "Bingbot"], disallow: "/admin" },
};
const txt = robotsToText(config);
expect(txt).toContain("User-Agent: Googlebot");
expect(txt).toContain("User-Agent: Bingbot");
expect(txt).toContain("Disallow: /admin");
});
it("handles array allow/disallow", () => {
const config: RobotsConfig = {
rules: {
allow: ["/public", "/docs"],
disallow: ["/admin", "/api"],
},
};
const txt = robotsToText(config);
expect(txt).toContain("Allow: /public");
expect(txt).toContain("Allow: /docs");
expect(txt).toContain("Disallow: /admin");
expect(txt).toContain("Disallow: /api");
});
it("includes crawl delay", () => {
const config: RobotsConfig = {
rules: { crawlDelay: 10 },
};
const txt = robotsToText(config);
expect(txt).toContain("Crawl-delay: 10");
});
it("includes sitemap directive", () => {
const config: RobotsConfig = {
rules: { allow: "/" },
sitemap: "https://example.com/sitemap.xml",
};
const txt = robotsToText(config);
expect(txt).toContain("Sitemap: https://example.com/sitemap.xml");
});
it("includes multiple sitemaps", () => {
const config: RobotsConfig = {
rules: { allow: "/" },
sitemap: ["https://example.com/sitemap1.xml", "https://example.com/sitemap2.xml"],
};
const txt = robotsToText(config);
expect(txt).toContain("Sitemap: https://example.com/sitemap1.xml");
expect(txt).toContain("Sitemap: https://example.com/sitemap2.xml");
});
it("includes host directive", () => {
const config: RobotsConfig = {
rules: { allow: "/" },
host: "example.com",
};
const txt = robotsToText(config);
expect(txt).toContain("Host: example.com");
});
it("defaults user agent to *", () => {
const config: RobotsConfig = {
rules: { allow: "/" },
};
const txt = robotsToText(config);
expect(txt).toContain("User-Agent: *");
});
it("ends with newline", () => {
const config: RobotsConfig = { rules: { allow: "/" } };
const txt = robotsToText(config);
expect(txt.endsWith("\n")).toBe(true);
});
});
// โโโ manifestToJson โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
describe("manifestToJson", () => {
it("generates valid JSON", () => {
const config: ManifestConfig = {
name: "My App",
short_name: "App",
start_url: "/",
display: "standalone",
background_color: "#ffffff",
theme_color: "#000000",
};
const json = manifestToJson(config);
const parsed = JSON.parse(json);
expect(parsed.name).toBe("My App");
expect(parsed.short_name).toBe("App");
expect(parsed.start_url).toBe("/");
expect(parsed.display).toBe("standalone");
});
it("generates pretty-printed JSON", () => {
const config: ManifestConfig = { name: "Test" };
const json = manifestToJson(config);
// Pretty-printed with 2-space indent
expect(json).toContain(" ");
expect(json).toContain("{\n");
});
it("handles icons array", () => {
const config: ManifestConfig = {
icons: [
{ src: "/icon-192.png", sizes: "192x192", type: "image/png" },
{ src: "/icon-512.png", sizes: "512x512", type: "image/png" },
],
};
const parsed = JSON.parse(manifestToJson(config));
expect(parsed.icons).toHaveLength(2);
expect(parsed.icons[0].src).toBe("/icon-192.png");
expect(parsed.icons[1].sizes).toBe("512x512");
});
});
// โโโ scanMetadataFiles โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
describe("scanMetadataFiles", () => {
let tmpDir: string;
beforeEach(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "vinext-test-metadata-"));
});
afterEach(() => {
fs.rmSync(tmpDir, { recursive: true, force: true });
});
function createFile(relativePath: string, content = ""): void {
const fullPath = path.join(tmpDir, relativePath);
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
fs.writeFileSync(fullPath, content);
}
it("returns empty array for non-existent directory", () => {
const routes = scanMetadataFiles("/non/existent/path");
expect(routes).toEqual([]);
});
it("discovers sitemap.xml at root", () => {
createFile("sitemap.xml");
const routes = scanMetadataFiles(tmpDir);
const sitemap = routes.find(r => r.type === "sitemap");
expect(sitemap).toBeDefined();
expect(sitemap!.servedUrl).toBe("/sitemap.xml");
expect(sitemap!.isDynamic).toBe(false);
expect(sitemap!.contentType).toBe("application/xml");
});
it("discovers dynamic sitemap.ts at root", () => {
createFile("sitemap.ts");
const routes = scanMetadataFiles(tmpDir);
const sitemap = routes.find(r => r.type === "sitemap");
expect(sitemap).toBeDefined();
expect(sitemap!.isDynamic).toBe(true);
expect(sitemap!.servedUrl).toBe("/sitemap.xml");
});
it("discovers robots.txt at root", () => {
createFile("robots.txt");
const routes = scanMetadataFiles(tmpDir);
const robots = routes.find(r => r.type === "robots");
expect(robots).toBeDefined();
expect(robots!.servedUrl).toBe("/robots.txt");
expect(robots!.contentType).toBe("text/plain");
});
it("discovers manifest.webmanifest at root", () => {
createFile("manifest.webmanifest");
const routes = scanMetadataFiles(tmpDir);
const manifest = routes.find(r => r.type === "manifest");
expect(manifest).toBeDefined();
expect(manifest!.servedUrl).toBe("/manifest.webmanifest");
});
it("discovers favicon.ico at root", () => {
createFile("favicon.ico");
const routes = scanMetadataFiles(tmpDir);
const favicon = routes.find(r => r.type === "favicon");
expect(favicon).toBeDefined();
expect(favicon!.servedUrl).toBe("/favicon.ico");
expect(favicon!.contentType).toBe("image/x-icon");
});
it("discovers dynamic icon.tsx at root", () => {
createFile("icon.tsx");
const routes = scanMetadataFiles(tmpDir);
const icon = routes.find(r => r.type === "icon");
expect(icon).toBeDefined();
expect(icon!.isDynamic).toBe(true);
expect(icon!.servedUrl).toBe("/icon");
});
it("discovers static icon.png at root", () => {
createFile("icon.png");
const routes = scanMetadataFiles(tmpDir);
const icon = routes.find(r => r.type === "icon");
expect(icon).toBeDefined();
expect(icon!.isDynamic).toBe(false);
expect(icon!.contentType).toBe("image/png");
});
it("discovers opengraph-image.tsx", () => {
createFile("opengraph-image.tsx");
const routes = scanMetadataFiles(tmpDir);
const og = routes.find(r => r.type === "opengraph-image");
expect(og).toBeDefined();
expect(og!.servedUrl).toBe("/opengraph-image");
});
it("discovers twitter-image.jpg", () => {
createFile("twitter-image.jpg");
const routes = scanMetadataFiles(tmpDir);
const twitter = routes.find(r => r.type === "twitter-image");
expect(twitter).toBeDefined();
expect(twitter!.contentType).toBe("image/jpeg");
});
it("discovers apple-icon.png", () => {
createFile("apple-icon.png");
const routes = scanMetadataFiles(tmpDir);
const apple = routes.find(r => r.type === "apple-icon");
expect(apple).toBeDefined();
expect(apple!.servedUrl).toBe("/apple-icon");
});
it("nestable types discovered in subdirectories", () => {
createFile("blog/sitemap.xml");
createFile("blog/icon.png");
const routes = scanMetadataFiles(tmpDir);
const blogSitemap = routes.find(r => r.type === "sitemap" && r.servedUrl.includes("blog"));
expect(blogSitemap).toBeDefined();
expect(blogSitemap!.servedUrl).toBe("/blog/sitemap.xml");
const blogIcon = routes.find(r => r.type === "icon" && r.servedUrl.includes("blog"));
expect(blogIcon).toBeDefined();
expect(blogIcon!.servedUrl).toBe("/blog/icon");
});
it("non-nestable types only at root", () => {
createFile("blog/robots.txt");
createFile("blog/manifest.json");
createFile("blog/favicon.ico");
const routes = scanMetadataFiles(tmpDir);
// robots, manifest, favicon should NOT be found in subdirectories
expect(routes.find(r => r.type === "robots")).toBeUndefined();
expect(routes.find(r => r.type === "manifest")).toBeUndefined();
expect(routes.find(r => r.type === "favicon")).toBeUndefined();
});
it("route groups are transparent in URLs", () => {
createFile("(marketing)/icon.png");
const routes = scanMetadataFiles(tmpDir);
const icon = routes.find(r => r.type === "icon");
expect(icon).toBeDefined();
// (marketing) should NOT appear in URL
expect(icon!.servedUrl).toBe("/icon");
expect(icon!.servedUrl).not.toContain("marketing");
});
it("dynamic takes priority over static at same URL", () => {
createFile("sitemap.xml");
createFile("sitemap.ts");
const routes = scanMetadataFiles(tmpDir);
const sitemaps = routes.filter(r => r.type === "sitemap");
// Only one should remain (dynamic wins)
expect(sitemaps).toHaveLength(1);
expect(sitemaps[0].isDynamic).toBe(true);
});
it("discovers multiple metadata files", () => {
createFile("sitemap.xml");
createFile("robots.txt");
createFile("favicon.ico");
createFile("icon.tsx");
createFile("opengraph-image.tsx");
const routes = scanMetadataFiles(tmpDir);
expect(routes.length).toBeGreaterThanOrEqual(5);
expect(routes.find(r => r.type === "sitemap")).toBeDefined();
expect(routes.find(r => r.type === "robots")).toBeDefined();
expect(routes.find(r => r.type === "favicon")).toBeDefined();
expect(routes.find(r => r.type === "icon")).toBeDefined();
expect(routes.find(r => r.type === "opengraph-image")).toBeDefined();
});
});
// โโโ METADATA_FILE_MAP structure โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
describe("METADATA_FILE_MAP", () => {
it("has all 8 metadata types", () => {
expect(Object.keys(METADATA_FILE_MAP)).toEqual(
expect.arrayContaining([
"sitemap", "robots", "manifest", "favicon",
"icon", "opengraph-image", "twitter-image", "apple-icon",
]),
);
});
it("favicon is not dynamic-capable", () => {
expect(METADATA_FILE_MAP.favicon.canBeDynamic).toBe(false);
expect(METADATA_FILE_MAP.favicon.dynamicExtensions).toEqual([]);
});
it("robots is not nestable", () => {
expect(METADATA_FILE_MAP.robots.nestable).toBe(false);
});
it("icon is nestable and dynamic-capable", () => {
expect(METADATA_FILE_MAP.icon.nestable).toBe(true);
expect(METADATA_FILE_MAP.icon.canBeDynamic).toBe(true);
expect(METADATA_FILE_MAP.icon.dynamicExtensions).toContain(".tsx");
});
});