feat(app): turn documents in the artifact gallery

This commit is contained in:
2026-10-22 03:24:40 +01:00
parent 4f684ba467
commit 3fa4657be7
18 changed files with 1365 additions and 168 deletions
+58
View File
@@ -121,6 +121,64 @@ List<JsonMap> _steps(String origin, String state) {
},
];
}
if (state == 'documents') {
return <JsonMap>[
<String, dynamic>{
'stage': 'reviewer',
'sequence': 1,
'status': 'completed',
'control': <String, dynamic>{'visit': 1},
'artifacts': [
_file(
'report.csv',
url: '$origin/fixture/report.csv',
contentType: 'text/csv',
sizeBytes: 23,
),
_file(
'input.md',
url: '$origin/fixture/input.md',
contentType: 'text/markdown',
sizeBytes: 120,
),
_file(
'output.md',
url: '$origin/fixture/output.md',
contentType: 'text/markdown',
sizeBytes: 240,
),
// Deliberately sniffed as text/plain: the session export must still
// stay external-only.
_file(
'session.html',
url: '$origin/fixture/session.html',
contentType: 'text/plain',
sizeBytes: 512,
),
],
},
<String, dynamic>{
'stage': 'reviewer',
'sequence': 2,
'status': 'completed',
'control': <String, dynamic>{'visit': 2},
'artifacts': [
_file(
'input.md',
url: '$origin/fixture/input-2.md',
contentType: 'text/markdown',
sizeBytes: 130,
),
_file(
'memory.md',
url: '$origin/fixture/memory.md',
contentType: 'text/markdown',
sizeBytes: 256,
),
],
},
];
}
if (state == 'files') {
return <JsonMap>[
<String, dynamic>{
+163 -1
View File
@@ -156,7 +156,7 @@ const waitFor = async (fn, what) => {
throw new Error(`Timed out waiting for ${what}: ${stderr.slice(-1500)}`);
};
const evidence = { origin, captures: [], checks: [] };
const evidence = { origin, captures: [], checks: [], contrast: [], pager: null };
let ws;
try {
const endpoint = await waitFor(
@@ -366,6 +366,58 @@ try {
return { max, width: img.width, height: img.height };
})()`);
};
// WCAG contrast of the explanatory (subtitle) line of a two-line row against
// its own background. Only the row's lower half is sampled, so the darker
// title glyphs cannot mask a dim absence line. The text luminance is the
// text-coloured extreme (p2 in light mode, p98 in dark); the background is
// the opposite extreme.
const subtitleContrast = async (prefix, dark) => {
const rect = await rectOfPrefix(prefix);
if (!rect) throw new Error(`No row starting ${JSON.stringify(prefix)}`);
const clip = {
x: rect.x,
y: rect.y + rect.height * 0.45,
width: rect.width,
height: rect.height * 0.55,
scale: 1,
};
const shot = await send('Page.captureScreenshot', { format: 'png', clip }, session);
return evaluate(`(async () => {
const img = new Image();
img.src = 'data:image/png;base64,${shot.result.data}';
await img.decode();
const canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
const data = ctx.getImageData(0, 0, img.width, img.height).data;
const linear = (value) => {
const c = value / 255;
return c <= 0.04045 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4);
};
const lums = [];
for (let i = 0; i < data.length; i += 4) {
lums.push(0.2126 * linear(data[i]) + 0.7152 * linear(data[i + 1]) + 0.0722 * linear(data[i + 2]));
}
lums.sort((a, b) => a - b);
const at = (fraction) => lums[Math.min(lums.length - 1, Math.floor(lums.length * fraction))];
const text = ${dark} ? at(0.98) : at(0.02);
const background = ${dark} ? at(0.02) : at(0.98);
const high = Math.max(text, background);
const low = Math.min(text, background);
return {
ratio: (high + 0.05) / (low + 0.05),
textLuminance: text,
backgroundLuminance: background,
p01: at(0.01),
p99: at(0.99),
pixels: lums.length,
width: img.width,
height: img.height,
};
})()`);
};
const openGallery = async (query) => {
await navigate(`${origin}/?${query}`);
if (!(await hasLabel('Artifacts'))) {
@@ -660,6 +712,116 @@ try {
await openScaled('cupertino', 'a1w-cupertino-2x-320');
evidence.checks.push({ check: 'Cupertino 2x/320 keeps the recorded rows, facts and actions', value: true });
// Turn documents: recorded input/memory/output/session.html are their own
// rows per visit, absent ones carry the exact wording, markdown opens in the
// renderer and the session export stays external-only even when sniffed as
// text/plain.
for (const platform of ['material', 'cupertino']) {
const prefix = `a4-${platform}`;
await navigate(galleryView('&state=documents', platform), 'Artifacts');
const documentsBody = await bodyText();
for (const fact of [
'Turn documents',
'input.md',
'output.md',
'session.html',
'memory.md',
'text/markdown · 120\u00a0B',
'text/plain · 512\u00a0B',
'No memory recorded for this visit.',
'No output recorded.',
'This visit has no harness session.',
]) {
if (!documentsBody.includes(fact)) {
throw new Error(`${platform} documents view is missing ${fact}: ${JSON.stringify(documentsBody)}`);
}
}
if (documentsBody.includes('No input recorded.')) {
throw new Error(`${platform} fabricated a missing input document: ${JSON.stringify(documentsBody)}`);
}
evidence.checks.push({ check: `${platform} presents recorded turn documents and exact absence wording`, value: true });
await capture(`${prefix}-documents`);
await clickExact('session.html');
const sessionBody = await visibleText();
if (!sessionBody.includes('text/plain · 512\u00a0B') || !(await hasLabel('Open'))) {
throw new Error(`${platform} session export lost its facts/Open: ${JSON.stringify(sessionBody)}`);
}
if (sessionBody.includes('Previous artifact') || sessionBody.includes('1 of ')) {
throw new Error(`${platform} opened session.html in the in-app renderer`);
}
evidence.checks.push({ check: `${platform} keeps session.html external-only`, value: true });
await capture(`${prefix}-session`);
await clickExact('Close');
await delay(300);
await navigate(galleryView('&state=documents', platform), 'Artifacts');
await clickExact('input.md');
await delay(700);
const inputBody = await visibleText();
if (!inputBody.includes('recorded artifact /fixture/input.md') ||
!inputBody.includes('1 of 4 · text/markdown')) {
throw new Error(`${platform} markdown document did not render in the viewer: ${JSON.stringify(inputBody)}`);
}
evidence.checks.push({ check: `${platform} opens a recorded markdown document in the renderer`, value: true });
await capture(`${prefix}-document-viewer`);
await clickExact('Back');
await delay(400);
}
// Absence rows are informational text, not disabled controls: the Material
// subtitle keeps readable contrast in both themes.
for (const theme of ['light', 'dark']) {
// Rasterise at 3x so a 14px subtitle stroke reaches its nominal colour;
// at 1x, antialiasing dilutes every text colour.
await send('Emulation.setDeviceMetricsOverride', {
width: 390, height: 844, deviceScaleFactor: 3, mobile: true,
}, session);
await navigate(galleryView(`&state=documents&theme=${theme}`), 'Artifacts');
const sample = await subtitleContrast('memory.md', theme === 'dark');
evidence.contrast.push({
sample: `material ${theme} absence subtitle (memory.md)`,
...sample,
});
if (sample.ratio < 4.5) {
throw new Error(`Material ${theme} absence subtitle contrast is ${sample.ratio.toFixed(2)}:1`);
}
evidence.checks.push({
check: `material ${theme} absence subtitle meets 4.5:1`,
value: true,
ratio: Number(sample.ratio.toFixed(2)),
});
await capture(`a4-material-${theme}-absence-row`);
}
// The Material pager must keep both labelled controls whole at 320 px with 2x
// text, not clip the second button at the viewport edge.
await send('Emulation.setDeviceMetricsOverride', {
width: 320, height: 720, deviceScaleFactor: 1, mobile: true,
}, session);
await navigate(galleryView('&state=documents&scale=2'), 'Artifacts');
await clickExact('input.md');
await delay(700);
if (!(await visibleText()).includes('1 of 4 · text/markdown')) {
throw new Error(`2x document viewer did not open: ${JSON.stringify(await visibleText())}`);
}
const pagerControls = {};
for (const label of ['Previous artifact', 'Next artifact']) {
const rect = await rectOfPrefix(label);
if (!rect) throw new Error(`2x pager lost ${label}: ${JSON.stringify(await labels())}`);
if (rect.x < -0.5 || rect.x + rect.width > 320.5) {
throw new Error(`${label} leaves the 320px viewport: ${JSON.stringify(rect)}`);
}
pagerControls[label] = rect;
}
evidence.pager = { viewport: 320, textScale: 2, controls: pagerControls };
evidence.checks.push({
check: 'Material paging controls stay inside a 320px viewport at 2x text',
value: true,
controls: pagerControls,
});
await capture('a4-material-320-2x-document-viewer', 320, 720, 1);
// File actions: a real same-origin artifact is fetched, then handed to the
// host as a named file. Blob/anchor and navigator.share are intercepted above
// so the evidence is the exact bytes, MIME type and download name, not a