-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify-docs-entry-language.mjs
More file actions
357 lines (332 loc) · 12.1 KB
/
verify-docs-entry-language.mjs
File metadata and controls
357 lines (332 loc) · 12.1 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
import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import path from 'node:path';
import vm from 'node:vm';
import { fileURLToPath } from 'node:url';
const scriptDir = path.dirname(fileURLToPath(import.meta.url));
const docsDir = path.resolve(scriptDir, '..');
const distDir = path.join(docsDir, 'dist');
async function readDistFile(relativePath) {
return readFile(path.join(distDir, relativePath), 'utf8');
}
function assertIncludes(haystack, needle, message) {
assert.ok(haystack.includes(needle), message + ` (missing: ${needle})`);
}
function createLocalStorage(initialValue) {
const store = new Map();
if (initialValue !== undefined && initialValue !== null) {
store.set('starlight-route', initialValue);
}
return {
getItem(key) {
return store.has(key) ? store.get(key) : null;
},
setItem(key, value) {
store.set(key, String(value));
},
dump() {
return Object.fromEntries(store.entries());
},
};
}
function createMockDocument(landingTargetPath = null) {
return {
querySelector(selector) {
if (
selector === 'meta[name="hagicode-docs-landing-target"]' &&
landingTargetPath
) {
return {
getAttribute(name) {
return name === 'content' ? landingTargetPath : null;
},
};
}
return null;
},
};
}
function createMockWindow(
href,
storedRouteValue,
navigatorConfig = {},
landingTargetPath = null,
) {
let currentUrl = new URL(href);
const redirects = [];
const localStorage = createLocalStorage(storedRouteValue);
const document = createMockDocument(landingTargetPath);
const location = {
get href() {
return currentUrl.toString();
},
set href(value) {
currentUrl = new URL(value, currentUrl);
redirects.push(currentUrl.toString());
},
get pathname() {
return currentUrl.pathname;
},
get search() {
return currentUrl.search;
},
get hash() {
return currentUrl.hash;
},
get origin() {
return currentUrl.origin;
},
replace(value) {
currentUrl = new URL(value, currentUrl);
redirects.push(currentUrl.toString());
},
};
return {
window: {
location,
localStorage,
document,
navigator: {
language: navigatorConfig.language ?? 'en-US',
languages: navigatorConfig.languages ?? [navigatorConfig.language ?? 'en-US'],
},
},
redirects,
localStorage,
getCurrentUrl() {
return currentUrl.toString();
},
};
}
function evaluateEntryScript(
scriptContent,
href,
storedRouteValue = null,
navigatorConfig = {},
landingTargetPath = null,
) {
const mock = createMockWindow(
href,
storedRouteValue,
navigatorConfig,
landingTargetPath,
);
const context = vm.createContext({
URL,
console,
document: mock.window.document,
window: mock.window,
});
vm.runInContext(scriptContent, context, { filename: 'lang-redirect.js' });
return {
api: mock.window.__HAGICODE_DOCS_ENTRY__,
redirects: mock.redirects,
localStorage: mock.localStorage.dump(),
finalUrl: mock.getCurrentUrl(),
};
}
function verifyScenario(scriptContent, scenario) {
const result = evaluateEntryScript(
scriptContent,
scenario.href,
scenario.storedRouteValue,
scenario.navigator,
scenario.landingTargetPath,
);
assert.equal(result.api.lastResolution.resolvedLocale, scenario.expectedLocale, `${scenario.name}: resolved locale`);
assert.equal(result.api.lastResolution.targetUrl, scenario.expectedTargetUrl, `${scenario.name}: target url`);
assert.equal(result.finalUrl, scenario.expectedFinalUrl, `${scenario.name}: final url`);
assert.equal(result.api.lastResolution.shouldRedirect, scenario.expectRedirect, `${scenario.name}: redirect flag`);
if (scenario.expectedStoredLang) {
const stored = JSON.parse(result.localStorage['starlight-route']);
assert.equal(stored.lang, scenario.expectedStoredLang, `${scenario.name}: stored lang`);
}
}
async function main() {
const [rootHtml, enHtml, redirectScript] = await Promise.all([
readDistFile('index.html'),
readDistFile(path.join('en', 'index.html')),
readDistFile('lang-redirect.js'),
]);
assertIncludes(rootHtml, 'name="hagicode-docs-default-entry" content="en"', 'root landing should advertise the English default entry');
assertIncludes(rootHtml, '<html lang="zh-CN"', 'root landing should keep Chinese metadata');
assertIncludes(rootHtml, 'name="hagicode-docs-landing-target" content="/product-overview/"', 'root landing should point directly to the product overview route');
assertIncludes(rootHtml, 'http-equiv="refresh"', 'root landing should expose a non-JS redirect fallback');
assertIncludes(rootHtml, '/lang-redirect.js', 'root landing should load the entry route resolver');
assertIncludes(enHtml, 'name="hagicode-docs-default-entry" content="en"', 'English landing should advertise the English default entry');
assertIncludes(enHtml, '<html lang="en"', 'English landing should expose English metadata');
assertIncludes(enHtml, 'name="hagicode-docs-landing-target" content="/product-overview/"', 'English landing should point directly to the product overview route');
assertIncludes(enHtml, 'http-equiv="refresh"', 'English landing should expose a non-JS redirect fallback');
assertIncludes(enHtml, '/lang-redirect.js', 'English landing should load the entry route resolver');
const [rootDocsHtml, rootBlogHtml, enDocsHtml, enBlogHtml] = await Promise.all([
readDistFile(path.join('product-overview', 'index.html')),
readDistFile(path.join('blog', 'index.html')),
readDistFile(path.join('en', 'product-overview', 'index.html')),
readDistFile(path.join('en', 'blog', 'index.html')),
]);
assertIncludes(rootDocsHtml, '/lang-redirect.js', 'root docs pages should load the shared route resolver');
assertIncludes(rootBlogHtml, '/lang-redirect.js', 'root blog pages should load the shared route resolver');
assertIncludes(enDocsHtml, '/lang-redirect.js', 'English docs pages should load the shared route resolver');
assertIncludes(enBlogHtml, '/lang-redirect.js', 'English blog pages should load the shared route resolver');
const scenarios = [
{
name: 'first-time English browser redirects to English product overview',
href: 'https://docs.hagicode.com/',
storedRouteValue: null,
navigator: {
language: 'en-US',
languages: ['en-US', 'en'],
},
landingTargetPath: '/product-overview/',
expectedLocale: 'en',
expectedTargetUrl: 'https://docs.hagicode.com/en/product-overview/',
expectedFinalUrl: 'https://docs.hagicode.com/en/product-overview/',
expectRedirect: true,
expectedStoredLang: 'en',
},
{
name: 'first-time Chinese browser redirects to Chinese product overview',
href: 'https://docs.hagicode.com/',
storedRouteValue: null,
navigator: {
language: 'zh-CN',
languages: ['zh-CN', 'zh'],
},
landingTargetPath: '/product-overview/',
expectedLocale: 'root',
expectedTargetUrl: 'https://docs.hagicode.com/product-overview/',
expectedFinalUrl: 'https://docs.hagicode.com/product-overview/',
expectRedirect: true,
expectedStoredLang: 'root',
},
{
name: 'explicit Chinese override on root',
href: 'https://docs.hagicode.com/?lang=zh-CN',
storedRouteValue: null,
navigator: {
language: 'en-US',
languages: ['en-US', 'en'],
},
landingTargetPath: '/product-overview/',
expectedLocale: 'root',
expectedTargetUrl: 'https://docs.hagicode.com/product-overview/',
expectedFinalUrl: 'https://docs.hagicode.com/product-overview/',
expectRedirect: true,
expectedStoredLang: 'root',
},
{
name: 'invalid language falls back to English product overview',
href: 'https://docs.hagicode.com/?lang=fr',
storedRouteValue: null,
navigator: {
language: 'zh-CN',
languages: ['zh-CN', 'zh'],
},
landingTargetPath: '/product-overview/',
expectedLocale: 'en',
expectedTargetUrl: 'https://docs.hagicode.com/en/product-overview/',
expectedFinalUrl: 'https://docs.hagicode.com/en/product-overview/',
expectRedirect: true,
expectedStoredLang: 'en',
},
{
name: 'stored Chinese preference keeps root redirect in Chinese',
href: 'https://docs.hagicode.com/',
storedRouteValue: JSON.stringify({ lang: 'root' }),
navigator: {
language: 'en-US',
languages: ['en-US', 'en'],
},
landingTargetPath: '/product-overview/',
expectedLocale: 'root',
expectedTargetUrl: 'https://docs.hagicode.com/product-overview/',
expectedFinalUrl: 'https://docs.hagicode.com/product-overview/',
expectRedirect: true,
expectedStoredLang: 'root',
},
{
name: 'root docs path defaults to English for first-time visitors',
href: 'https://docs.hagicode.com/product-overview/',
storedRouteValue: null,
navigator: {
language: 'zh-CN',
languages: ['zh-CN', 'zh'],
},
expectedLocale: 'en',
expectedTargetUrl: 'https://docs.hagicode.com/en/product-overview/',
expectedFinalUrl: 'https://docs.hagicode.com/en/product-overview/',
expectRedirect: true,
},
{
name: 'stored Chinese preference keeps root docs path in Chinese',
href: 'https://docs.hagicode.com/product-overview/',
storedRouteValue: JSON.stringify({ lang: 'root' }),
navigator: {
language: 'en-US',
languages: ['en-US', 'en'],
},
expectedLocale: 'root',
expectedTargetUrl: 'https://docs.hagicode.com/product-overview/',
expectedFinalUrl: 'https://docs.hagicode.com/product-overview/',
expectRedirect: false,
expectedStoredLang: 'root',
},
{
name: 'root blog path defaults to English for first-time visitors',
href: 'https://docs.hagicode.com/blog/',
storedRouteValue: null,
navigator: {
language: 'zh-CN',
languages: ['zh-CN', 'zh'],
},
expectedLocale: 'en',
expectedTargetUrl: 'https://docs.hagicode.com/en/blog/',
expectedFinalUrl: 'https://docs.hagicode.com/en/blog/',
expectRedirect: true,
},
{
name: 'invalid language on root blog falls back to English',
href: 'https://docs.hagicode.com/blog/?lang=invalid',
storedRouteValue: JSON.stringify({ lang: 'root' }),
navigator: {
language: 'zh-CN',
languages: ['zh-CN', 'zh'],
},
expectedLocale: 'en',
expectedTargetUrl: 'https://docs.hagicode.com/en/blog/',
expectedFinalUrl: 'https://docs.hagicode.com/en/blog/',
expectRedirect: true,
expectedStoredLang: 'en',
},
{
name: 'English landing redirects to English product overview even with stored Chinese preference',
href: 'https://docs.hagicode.com/en/',
storedRouteValue: JSON.stringify({ lang: 'root' }),
navigator: {
language: 'zh-CN',
languages: ['zh-CN', 'zh'],
},
landingTargetPath: '/product-overview/',
expectedLocale: 'en',
expectedTargetUrl: 'https://docs.hagicode.com/en/product-overview/',
expectedFinalUrl: 'https://docs.hagicode.com/en/product-overview/',
expectRedirect: true,
expectedStoredLang: 'root',
},
];
for (const scenario of scenarios) {
verifyScenario(redirectScript, scenario);
}
console.log('Docs entry language verification passed.');
console.log('- landing routes now redirect directly to product overview');
console.log('- first-time visitors still follow the browser language before falling back to English');
console.log('- root docs/blog paths default to English unless Chinese was explicitly chosen');
console.log('- explicit zh-CN keeps the Chinese redirect target');
console.log('- invalid lang values cleanly fall back to English');
console.log('- /en/ also redirects directly to the English product overview');
}
main().catch((error) => {
console.error('Docs entry language verification failed.');
console.error(error instanceof Error ? error.message : error);
process.exitCode = 1;
});