oficina / meet Public

Clone
git clone https://amadan.net/oficina/meet

Plain git — no account needed to clone.

Download

Download this file

1// Fiesta's rhythm audit over Calls' own pages, in a real Chromium
2// (internal/callstest/rhythm_browser_test.go, TestEveryPageKeepsTheRhythm).
3// Go starts the app, signs a member in and names the states; this opens
4// each one at a desktop width and a phone's, checks it is the page it was
5// asked for, and evaluates the audit Go hands it (rhythm.Expression) over
6// <main> (or the state's own root). It prints one JSON line per state and width; Go decides.
7//
8// The configuration comes on stdin, never argv: it carries the member's
9// session cookie, and a command line is readable by every process on
10// the box.
11//
12// {"base": "http://127.0.0.1:1234", "cookie": {"name": "…", "value": "…"},
13// "audits": {"main": "(function fiestaRhythm…)(\"main\", {…})"},
14// "states": [{"name": "the meetings", "path": "/", "marker": "…", "root": "main",
15// "as": "member", "noScript": false, "act": "", "then": "", "after": ""}]}
16//
17// A state's page is checked three ways before it is audited, so a pass
18// can never mean "the audit read some other page": the browser is on
19// the state's path (after its act, on the path "after" names), the
20// page's own marker is there, and <main> is a stack (rst-stack, laid out
21// by tokens.css as a flex column), which is the CSS under test. With
22// noScript the context runs no script at all, and the page says so: its
23// (scripting) media feature is none and its <noscript> is parsed as
24// markup.
25//
26// A state with noRefresh has its <meta http-equiv="refresh"> taken out
27// of the response (the app hand-off page's, which without script reloads
28// it every three seconds), so the page holds still to be measured.
29//
30// shots, when set, is a directory each state's screenshot is written to.
31import { chromium } from 'playwright';
32import { mkdirSync } from 'node:fs';
33import { join } from 'node:path';
34
35const chunks = [];
36for await (const chunk of process.stdin) chunks.push(chunk);
37const config = JSON.parse(Buffer.concat(chunks).toString('utf8'));
38
39// The holding screen's camera and microphone: Chromium's fakes, as the
40// call gate's, so the device panel is drawn as a person sees it.
41const browser = await chromium.launch({
42 headless: true,
43 args: ['--use-fake-device-for-media-stream', '--use-fake-ui-for-media-stream'],
44});
45
46// blocks is how many blocks the audit had to measure under the state's
47// root: its in-flow children with a box, through display: contents.
48// Fewer than two is a page with no gap to measure, which would pass
49// vacuously.
50const blocks = (root) => `((root) => {
51 const flow = (el) => [...el.children].flatMap((c) => {
52 const s = getComputedStyle(c);
53 if (s.display === 'contents') return flow(c);
54 if (s.display === 'none' || s.position === 'absolute' || s.position === 'fixed') return [];
55 const r = c.getBoundingClientRect();
56 return r.height > 0 && r.width > 0 ? [c] : [];
57 });
58 const el = document.querySelector(root);
59 return el ? flow(el).length : 0;
60})(${JSON.stringify(root)})`;
61
62const stack = `(() => {
63 const main = document.querySelector('main');
64 if (!main) return false;
65 const s = getComputedStyle(main);
66 return main.hasAttribute('rst-stack') && s.display === 'flex' && s.flexDirection === 'column';
67})()`;
68
69try {
70 for (const width of [1280, 390]) {
71 for (const state of config.states) {
72 const context = await browser.newContext({
73 viewport: { width, height: 900 },
74 colorScheme: 'light',
75 javaScriptEnabled: !state.noScript,
76 });
77 if (state.noMedia) {
78 await context.addInitScript(() => { Object.defineProperty(Navigator.prototype, 'mediaDevices', { get: () => undefined }); });
79 }
80 if (state.as === 'member') {
81 await context.addCookies([{ name: config.cookie.name, value: config.cookie.value, url: config.base }]);
82 }
83 const page = await context.newPage();
84 if (state.noRefresh) {
85 await page.route(config.base + state.path, async (route) => {
86 const response = await route.fetch();
87 const body = (await response.text()).replace(/<meta http-equiv="refresh"[^>]*>/g, '');
88 await route.fulfill({ response, body });
89 });
90 }
91 const out = { name: state.name, width };
92 const started = Date.now();
93 try {
94 const response = await page.goto(config.base + state.path, { waitUntil: 'load' });
95 out.status = response ? response.status() : 0;
96 if (state.act && state.after) {
97 // A step that sends a form: wait for the page it lands on.
98 await Promise.all([
99 page.waitForURL((u) => u.pathname === state.after, { waitUntil: 'load', timeout: 15000 }),
100 page.evaluate(state.act),
101 ]);
102 } else if (state.act) {
103 if (state.then) await page.waitForSelector(state.then, { state: 'attached', timeout: 15000 });
104 await page.evaluate(state.act);
105 }
106 await page.waitForSelector(state.marker, { state: 'attached', timeout: 15000 });
107 // Web fonts and the scripts' first layout pass settle before the
108 // gaps are read.
109 // (With no script there is no frame callback to wait on, and
110 // nothing of the page's own that moves anything.)
111 if (!state.noScript) {
112 await page.evaluate(() => document.fonts.ready.then(() => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r)))));
113 }
114 out.path = new URL(page.url()).pathname;
115 out.marker = (await page.$(state.marker)) !== null;
116 // The document's own answer: (scripting: none) matches only where
117 // the browser runs no script for it (an evaluate from here still
118 // runs, so a marker set from here would prove nothing).
119 out.scripted = await page.evaluate("matchMedia('(scripting: enabled)').matches && !document.querySelector('noscript > *')");
120 out.stack = await page.evaluate(stack);
121 out.blocks = await page.evaluate(blocks(state.root));
122 out.found = await page.evaluate(config.audits[state.root]);
123 if (config.shots) {
124 mkdirSync(config.shots, { recursive: true });
125 const file = `${state.name.replace(/[^a-z0-9]+/gi, '-').toLowerCase()}-${width}.png`;
126 await page.screenshot({ path: join(config.shots, file), fullPage: true });
127 }
128 } catch (err) {
129 out.error = String(err && err.message || err).slice(0, 500);
130 }
131 out.ms = Date.now() - started;
132 process.stdout.write(JSON.stringify(out) + '\n');
133 await context.close();
134 }
135 }
136} finally {
137 await browser.close();
138}
139