okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221rc1 merge tui-update (3165c2e8a)a1cc285fa

main
4.7 KiB124 linesraw
1#!/usr/bin/env node
2/**
3 * Reading-measure check.
4 *
5 *   node scripts/measure.mjs
6 *
7 * Long-form reading wants 45–75 characters per line, and ~66 is the classic
8 * target. This asserts the transcript stays inside that band at every content
9 * font size the settings offer, in both normal and focus mode — the property
10 * most likely to silently regress when a width or a padding changes.
11 *
12 * Note: `--st-measure` is expressed in `ch`, but `ch` is the width of the "0"
13 * glyph, which is narrower than average running text in GT Walsheim. Measured
14 * characters therefore run ~1.2× the ch value, which is why the assertion is on
15 * the rendered result rather than on the token.
16 *
17 * Requires the dev server. Exits non-zero if any size falls outside 45–75.
18 */
19import { chromium } from 'playwright-core';
20import { existsSync } from 'node:fs';
21import { join } from 'node:path';
22import { homedir } from 'node:os';
23import { requireChromium } from './browser.mjs';
24
25const BASE = process.env.STRATA_URL ?? 'http://localhost:5199';
26const MIN = 45;
27const MAX = 75;
28
29const executablePath = requireChromium();
30
31if (!executablePath) {
32  console.error('No cached Chromium found.');
33  process.exit(2);
34}
35
36const browser = await chromium.launch({ executablePath, args: ['--no-sandbox'] });
37const page = await browser.newPage({ viewport: { width: 1512, height: 1110 } });
38const errors = [];
39page.on('pageerror', (e) => errors.push(e.message));
40
41/** Characters per line of assistant prose, measured from the rendered element. */
42const measure = async () => {
43  await page.locator('.st-msg--assistant .st-bubble p').first().scrollIntoViewIfNeeded();
44  return page.evaluate(() => {
45    const para = document.querySelector('.st-msg--assistant .st-bubble p');
46    if (!para) return null;
47    const fs = getComputedStyle(para).fontSize;
48    const span = document.createElement('span');
49    span.style.cssText = `position:absolute;visibility:hidden;white-space:nowrap;font:400 ${fs} var(--st-font)`;
50    span.textContent = 'abcdefghijklmnopqrstuvwxyz'.repeat(8);
51    document.body.appendChild(span);
52    const avg = span.getBoundingClientRect().width / 208;
53    span.remove();
54    return { chars: Math.round(para.getBoundingClientRect().width / avg), fontSize: fs };
55  });
56};
57
58async function setSize(px) {
59  await page.goto(`${BASE}/#/settings`, { waitUntil: 'domcontentloaded' });
60  await page.waitForSelector('.st-slider', { timeout: 8000 });
61  // React tracks the previous value of a controlled input, so assigning .value
62  // directly is ignored. Go through the native setter, then dispatch.
63  await page.evaluate((value) => {
64    const el = document.querySelector('.st-slider');
65    const setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value').set;
66    setter.call(el, String(value));
67    el.dispatchEvent(new Event('input', { bubbles: true }));
68  }, px);
69  await page.waitForTimeout(250);
70  // Confirm the setting actually took, so this check can never pass vacuously.
71  const applied = await page.evaluate(() =>
72    getComputedStyle(document.documentElement).getPropertyValue('--st-content-fs').trim(),
73  );
74  if (applied !== `${px}px`) {
75    throw new Error(`text size did not apply: asked for ${px}px, --st-content-fs is "${applied}"`);
76  }
77}
78
79async function prepareConversation() {
80  await page.goto(`${BASE}/#/chat`, { waitUntil: 'domcontentloaded' });
81  await page.waitForTimeout(1300);
82  await page.click('button.nav-new');
83  await page.waitForTimeout(300);
84  await page.fill('textarea', 'read the readme');
85  await page.keyboard.press('Enter');
86  await page.waitForTimeout(6500);
87}
88
89const rows = [];
90await prepareConversation();
91
92for (const size of [10, 16, 22]) {
93  await setSize(size);
94  await page.goto(`${BASE}/#/chat`, { waitUntil: 'domcontentloaded' });
95  await page.waitForTimeout(1000);
96  rows.push([`${size}px · normal`, await measure()]);
97
98  await page.keyboard.press('Meta+Shift+F');
99  await page.waitForTimeout(700);
100  rows.push([`${size}px · focus`, await measure()]);
101  await page.keyboard.press('Escape');
102  await page.waitForTimeout(400);
103}
104
105await browser.close();
106
107let failures = 0;
108console.log('reading measure (target 45–75 characters per line)\n');
109for (const [label, m] of rows) {
110  if (!m) {
111    console.log(`  ${label.padEnd(16)} no prose found`);
112    failures += 1;
113    continue;
114  }
115  const ok = m.chars >= MIN && m.chars <= MAX;
116  if (!ok) failures += 1;
117  console.log(`  ${label.padEnd(16)} ${String(m.chars).padStart(3)} chars/line at ${m.fontSize}  ${ok ? 'PASS' : 'FAIL'}`);
118}
119if (errors.length) {
120  failures += errors.length;
121  console.log(`\n  page errors: ${errors.join('; ')}`);
122}
123console.log(failures ? `\nreading measure: ${failures} failure(s)` : '\nreading measure: every size inside the comfortable band');
124process.exit(failures ? 1 : 0);