Repository navigation
Expand file tree
/
Copy pathverify.mjs
More file actions
413 lines (380 loc) · 12.1 KB
/
Copy pathverify.mjs
File metadata and controls
413 lines (380 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
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
/**
* Headless verification for SCHRIFT — Swiss International design system.
*
* CLI-only. Serves the project with a tiny static server, drives headless
* Chromium (Playwright) through it at desktop (1280) and mobile (390) widths,
* and asserts the design system is actually wired up: tokens applied, Inter
* loaded, no console errors, FAQ accordion + stat count-up + mobile drawer work,
* patterns present, and core accessibility holds.
*
* Usage:
* PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH=/path/to/chrome \
* PW_DIR=/abs/path/to/scripts/record-demos \
* node verify.mjs
*
* PW_DIR points at any folder whose node_modules has `playwright` installed.
*/
import http from "node:http";
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import { createRequire } from "node:module";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const ROOT = __dirname;
const PORT = Number(process.env.PORT || 5311);
// ---- resolve playwright from a sibling install if not local --------------
const require = createRequire(import.meta.url);
let chromium;
function pickChromium(mod) {
return mod.chromium || (mod.default && mod.default.chromium);
}
try {
chromium = pickChromium(require("playwright"));
} catch {
// not resolvable from this folder
}
if (!chromium) {
const pwDir = process.env.PW_DIR;
if (!pwDir)
throw new Error(
"playwright not found; set PW_DIR to a folder with playwright installed",
);
// require() against the sibling install handles CJS cleanly
const sideRequire = createRequire(
pathToFileURL(path.join(pwDir, "package.json")).href,
);
chromium = pickChromium(sideRequire("playwright"));
}
if (!chromium) throw new Error("could not load chromium from playwright");
// ---- tiny static server ---------------------------------------------------
const MIME = {
".html": "text/html; charset=utf-8",
".css": "text/css; charset=utf-8",
".js": "text/javascript; charset=utf-8",
".svg": "image/svg+xml",
".woff2": "font/woff2",
".json": "application/json",
};
const server = http.createServer((req, res) => {
let p = decodeURIComponent(req.url.split("?")[0]);
if (p === "/") p = "/index.html";
const file = path.join(ROOT, p);
if (
!file.startsWith(ROOT) ||
!fs.existsSync(file) ||
fs.statSync(file).isDirectory()
) {
res.writeHead(404);
return res.end("not found");
}
res.writeHead(200, {
"Content-Type": MIME[path.extname(file)] || "application/octet-stream",
});
fs.createReadStream(file).pipe(res);
});
// ---- assertion helpers ----------------------------------------------------
let pass = 0;
const failures = [];
function check(name, cond, detail = "") {
if (cond) {
pass++;
console.log(` PASS ${name}`);
} else {
failures.push(name + (detail ? ` — ${detail}` : ""));
console.log(` FAIL ${name}${detail ? ` — ${detail}` : ""}`);
}
}
const launchOpts = { headless: true };
if (process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH) {
launchOpts.executablePath = process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH;
launchOpts.args = [
"--no-sandbox",
"--disable-setuid-sandbox",
"--disable-dev-shm-usage",
];
}
await new Promise((r) => server.listen(PORT, r));
const BASE = `http://localhost:${PORT}/`;
console.log(`\nServing ${ROOT}\n at ${BASE}\n`);
const browser = await chromium.launch(launchOpts);
try {
// ====================================================== DESKTOP (1280) ===
console.log("DESKTOP · 1280×800");
const ctx = await browser.newContext({
viewport: { width: 1280, height: 800 },
});
const page = await ctx.newPage();
const consoleErrors = [];
const isNoise = (t) => /favicon/i.test(t); // browser-initiated favicon probe, not a page bug
page.on(
"console",
(m) =>
m.type() === "error" &&
!isNoise(m.text()) &&
consoleErrors.push(m.text()),
);
page.on("pageerror", (e) => consoleErrors.push("pageerror: " + e.message));
const resp = await page.goto(BASE, {
waitUntil: "networkidle",
timeout: 30000,
});
check(
"page responds 200",
resp && resp.status() === 200,
resp && String(resp.status()),
);
// Title + landmarks
check("document title is SCHRIFT", (await page.title()).includes("SCHRIFT"));
check(
"has <header>, <main>, <footer>",
(await page.locator("header, main, footer").count()) >= 3,
);
// Tokens applied from :root
const tokens = await page.evaluate(() => {
const s = getComputedStyle(document.documentElement);
return {
accent: s.getPropertyValue("--accent").trim(),
fg: s.getPropertyValue("--foreground").trim(),
radius: s.getPropertyValue("--radius").trim(),
};
});
check(
"--accent token is #ff3000",
tokens.accent.toLowerCase() === "#ff3000",
tokens.accent,
);
check(
"--foreground token is #000000",
tokens.fg.toLowerCase() === "#000000",
tokens.fg,
);
check("--radius token is 0px", tokens.radius === "0px", tokens.radius);
// Headline actually huge (mathematical scale) + uppercase
const h1 = page.locator("#hero-title");
const h1size = await h1.evaluate((el) =>
parseFloat(getComputedStyle(el).fontSize),
);
check(
"hero headline ≥ 80px at desktop",
h1size >= 80,
`${Math.round(h1size)}px`,
);
const h1transform = await h1.evaluate(
(el) => getComputedStyle(el).textTransform,
);
check("hero headline is uppercase", h1transform === "uppercase", h1transform);
// Strict rectangular: no rounded corners on buttons
const btnRadius = await page
.locator(".btn")
.first()
.evaluate((el) => getComputedStyle(el).borderRadius);
check("buttons have 0 border-radius", btnRadius === "0px", btnRadius);
// Inter loaded (FontFace API)
const interLoaded = await page.evaluate(async () => {
await document.fonts.ready;
return (
document.fonts.check('900 48px "Inter"') &&
document.fonts.check('400 16px "Inter"')
);
});
check("Inter font (400 + 900) loaded", interLoaded);
// Patterns present
check(
"grid pattern element present",
(await page.locator(".swiss-grid-pattern").count()) >= 1,
);
check(
"dot pattern element present",
(await page.locator(".swiss-dots").count()) >= 1,
);
check(
"diagonal pattern element present",
(await page.locator(".swiss-diagonal").count()) >= 1,
);
const noiseBg = await page.evaluate(() => {
const b = getComputedStyle(document.body, "::before").backgroundImage;
return b && b.includes("noise.svg");
});
check("global noise texture applied to body", noiseBg);
// Section count / numbered structure
check(
"≥ 6 numbered eyebrows",
(await page.locator(".eyebrow__num").count()) >= 6,
);
// Stat count-up runs (scroll into view, then read the number)
await page.locator("#system").scrollIntoViewIfNeeded();
await page.waitForTimeout(1600);
const statText = await page
.locator(".stat__num[data-count='21']")
.textContent();
check("stat counts up to 21:1", statText.trim() === "21:1", statText);
// FAQ accordion toggles open
const faqBtn = page.locator(".faq__q").first();
await faqBtn.scrollIntoViewIfNeeded();
check(
"FAQ closed initially (aria-expanded=false)",
(await faqBtn.getAttribute("aria-expanded")) === "false",
);
await faqBtn.click();
await page.waitForTimeout(300);
check(
"FAQ opens (aria-expanded=true)",
(await faqBtn.getAttribute("aria-expanded")) === "true",
);
const faqOpen = await page
.locator(".faq__item")
.first()
.evaluate((el) => el.classList.contains("is-open"));
check("FAQ item gets .is-open (red inversion)", faqOpen);
const answerH = await page
.locator(".faq__a")
.first()
.evaluate((el) => el.getBoundingClientRect().height);
check(
"FAQ answer panel expands (height > 0)",
answerH > 0,
`${Math.round(answerH)}px`,
);
// Toggle group selection
const choices = page.locator(".demo-choices .choice");
await choices.nth(1).click();
check(
"toggle group sets aria-pressed on click",
(await choices.nth(1).getAttribute("aria-pressed")) === "true",
);
check(
"toggle group clears previous selection",
(await choices.nth(0).getAttribute("aria-pressed")) === "false",
);
// CTA form validation + success
await page.locator("#cta").scrollIntoViewIfNeeded();
await page.locator("#cta-email").fill("not-an-email");
await page.locator("#cta-form button[type=submit]").click();
check(
"CTA rejects invalid email",
(await page.locator("#cta-note").textContent()).includes("valid"),
);
await page.locator("#cta-email").fill("studio@schrift.ch");
await page.locator("#cta-form button[type=submit]").click();
await page.waitForTimeout(200);
const okState = await page.locator("#cta-note").getAttribute("data-state");
check(
"CTA accepts valid email (success state)",
okState === "ok",
String(okState),
);
// Accessibility: skip link, focus-visible ring color, alt/aria on icons
check(
"has skip link to content",
(await page.locator("a.sr-only[href='#system']").count()) === 1,
);
const decorativeHidden = await page
.locator(".hero__panel[aria-hidden='true']")
.count();
check("decorative composition is aria-hidden", decorativeHidden === 1);
const headings = await page.evaluate(() =>
Array.from(document.querySelectorAll("h1,h2,h3")).map((h) =>
Number(h.tagName[1]),
),
);
check("exactly one <h1>", headings.filter((n) => n === 1).length === 1);
check(
"no console errors",
consoleErrors.length === 0,
consoleErrors.slice(0, 3).join(" | "),
);
await ctx.close();
// ======================================================= MOBILE (390) ====
console.log("\nMOBILE · 390×844");
const mctx = await browser.newContext({
viewport: { width: 390, height: 844 },
isMobile: true,
hasTouch: true,
});
const mpage = await mctx.newPage();
const mErrors = [];
const isNoiseM = (t) => /favicon/i.test(t);
mpage.on(
"console",
(m) =>
m.type() === "error" && !isNoiseM(m.text()) && mErrors.push(m.text()),
);
mpage.on("pageerror", (e) => mErrors.push("pageerror: " + e.message));
await mpage.goto(BASE, { waitUntil: "networkidle", timeout: 30000 });
// Desktop nav hidden, burger visible
const navDisplay = await mpage
.locator(".nav")
.evaluate((el) => getComputedStyle(el).display);
check("desktop nav hidden on mobile", navDisplay === "none", navDisplay);
const burger = mpage.locator("#burger");
check("burger visible on mobile", await burger.isVisible());
// Borders never thin out: a section-defining border is still 4px
const heroBorder = await mpage
.locator(".hero")
.evaluate((el) => getComputedStyle(el).borderBottomWidth);
check("section borders stay 4px on mobile", heroBorder === "4px", heroBorder);
// Drawer opens
await burger.click();
await mpage.waitForTimeout(300);
check(
"body gets .menu-open after burger tap",
await mpage.evaluate(() => document.body.classList.contains("menu-open")),
);
const drawerVisible = await mpage
.locator("#drawer")
.evaluate((el) => getComputedStyle(el).visibility);
check(
"mobile drawer becomes visible",
drawerVisible === "visible",
drawerVisible,
);
check(
"burger aria-expanded=true when open",
(await burger.getAttribute("aria-expanded")) === "true",
);
// Touch target size on drawer links (≥ 44px)
const linkH = await mpage
.locator("#drawer a")
.first()
.evaluate((el) => el.getBoundingClientRect().height);
check(
"drawer link touch target ≥ 44px",
linkH >= 44,
`${Math.round(linkH)}px`,
);
// No horizontal overflow
const overflow = await mpage.evaluate(
() =>
document.documentElement.scrollWidth -
document.documentElement.clientWidth,
);
check("no horizontal overflow on mobile", overflow <= 1, `${overflow}px`);
// Headline scales down but stays bold/uppercase
const mh1 = await mpage
.locator("#hero-title")
.evaluate((el) => parseFloat(getComputedStyle(el).fontSize));
check(
"hero headline still ≥ 48px on mobile",
mh1 >= 48,
`${Math.round(mh1)}px`,
);
check(
"no console errors on mobile",
mErrors.length === 0,
mErrors.slice(0, 3).join(" | "),
);
await mctx.close();
} finally {
await browser.close();
server.close();
}
// ---- summary --------------------------------------------------------------
console.log(`\n${"=".repeat(48)}`);
if (failures.length === 0) {
console.log(`ALL CHECKS PASSED · ${pass} assertions`);
process.exit(0);
} else {
console.log(`${pass} passed, ${failures.length} FAILED:`);
failures.forEach((f) => console.log(" · " + f));
process.exit(1);
}