180 lines
11 KiB
JavaScript
180 lines
11 KiB
JavaScript
// Scratch-only extension of ui-smoke: real DOM/views, deterministic protocol-shaped
|
|
// evidence; no backend connection, production reads, workflow creation or devices.
|
|
import { createServer } from "node:http";
|
|
import { readFile, mkdir, writeFile } from "node:fs/promises";
|
|
import { resolve, extname } from "node:path";
|
|
import assert from "node:assert/strict";
|
|
import { launchChrome } from "./browser.mjs";
|
|
|
|
const root = resolve(import.meta.dirname, "..");
|
|
const out = resolve(process.argv[2] ?? "captures-miniatures");
|
|
await mkdir(out, { recursive: true });
|
|
const server = createServer(async (req, res) => {
|
|
try {
|
|
const path = resolve(root, `.${["/", "/fixture"].includes(req.url) ? "/index.html" : req.url.split("?")[0]}`);
|
|
if (!path.startsWith(root + "/")) throw new Error("outside root");
|
|
let data = await readFile(path);
|
|
if (req.url === '/fixture') data = data.toString().replace('<script type="module" src="./src/app.js"></script>',
|
|
'<div class="app-root"><main class="workspace"><div class="pane-main"><div id="content" class="content"></div></div></main></div>');
|
|
res.setHeader("Content-Type", ({ ".js": "text/javascript", ".css": "text/css", ".html": "text/html", ".svg": "image/svg+xml" })[extname(path)] ?? "application/octet-stream");
|
|
res.end(data);
|
|
} catch { res.writeHead(404); res.end(); }
|
|
});
|
|
await new Promise((r) => server.listen(0, "127.0.0.1", r));
|
|
const browser = await launchChrome();
|
|
const results = [];
|
|
try {
|
|
const page = await browser.openPage(`http://127.0.0.1:${server.address().port}/`);
|
|
await page.waitForSelector("#content");
|
|
// Real shell controls, before replacing content with the list fixtures.
|
|
await page.resize(1100, 1000);
|
|
await page.click('#railCollapse');
|
|
const rail = await page.evaluate(`(() => {
|
|
const button=document.querySelector('#railCollapse');
|
|
const brand=document.querySelector('.rail-brand-icon').getBoundingClientRect();
|
|
const first=document.querySelector('.rail-item').getBoundingClientRect();
|
|
const b=button.getBoundingClientRect();
|
|
return {collapsed:document.querySelector('#appRail').classList.contains('collapsed'),
|
|
expanded:button.getAttribute('aria-expanded'),position:getComputedStyle(button).position,
|
|
bottom:b.bottom,brandTop:brand.top,firstTop:first.top,width:b.width,height:b.height};
|
|
})()`);
|
|
assert.equal(rail.collapsed,true);
|
|
assert.equal(rail.expanded,'false');
|
|
assert.equal(rail.position,'static');
|
|
assert.ok(rail.bottom<=rail.brandTop && rail.bottom<=rail.firstTop);
|
|
assert.equal(rail.width,40);
|
|
assert.equal(rail.height,40);
|
|
await page.screenshot(resolve(out,'rail-collapsed-1100.png'));
|
|
await page.click('#railCollapse');
|
|
assert.equal(await page.evaluate(`document.querySelector('#railCollapse').getAttribute('aria-expanded')`),'true');
|
|
await page.screenshot(resolve(out,'rail-expanded-1100.png'));
|
|
results.push({view:'rail',...rail});
|
|
for (const width of [390,420]) {
|
|
await page.resize(width,1000);
|
|
assert.equal(await page.evaluate(`getComputedStyle(document.querySelector('#appRail')).display`),'none');
|
|
}
|
|
await page.goto(`http://127.0.0.1:${server.address().port}/fixture`);
|
|
await page.evaluate(`(async () => {
|
|
const { createRunsView } = await import('/src/views/runs.js');
|
|
const { createPipelinesView } = await import('/src/views/pipelines.js');
|
|
const { defaultRunFilters } = await import('/src/runs.js');
|
|
const { EMPTY_PIPELINE_FILTERS } = await import('/src/pipelines.js');
|
|
const graph = { stages: [{ key:'plan', edges:{success:'build',error:'reject'} },
|
|
{key:'build',edges:{success:'finish'}},{key:'reject',edges:{error:'#failure'}},
|
|
{key:'finish',edges:{success:'#success'}}] };
|
|
const statuses = ['succeeded','failed','running','interrupted','waiting','queued','failed'];
|
|
const visits = (status) => status === 'queued' ? [] : [
|
|
{sequence:1,stage:'plan',status:'completed',outcome:'success',target:'build'},
|
|
{sequence:2,stage:'build',status:'completed',outcome:'success',target:'finish'},
|
|
{sequence:3,stage:'finish',status:status==='succeeded'?'completed':status,
|
|
outcome:status==='succeeded'?'success':'',target:status==='succeeded'?'#success':''}];
|
|
const state = { view:'runs', url:'scratch', loading:false, selectedRunId:1,
|
|
openMenu:null, runFilters:{...defaultRunFilters(),pipelineId:7}, runSort:'server_order',
|
|
pipelineFilters:{...EMPTY_PIPELINE_FILTERS},pipelineSort:'server_order', pipelinesLoaded:true,
|
|
runsLoaded:true, pipelines:[{id:7,name:'Pinned workflow',version:3,stages:4,shape:graph}],
|
|
runs:statuses.map((status,i)=>({id:i+1,pipeline_id:7,version:2,name:'Pinned workflow',
|
|
title:'A wrapped run title that preserves the complete task context across narrow screens '+status,
|
|
status,executing:['running','queued'].includes(status),current_stage:status==='queued'?'plan':'finish',
|
|
completed_steps:status==='queued'||i===6?0:status==='succeeded'?3:2,
|
|
created_at:new Date().toISOString(),
|
|
...(i===6?{current_stage:'',executing:false}:{graph_evidence:{complete:true,visits:visits(status)}})})) };
|
|
let runs, pipelines;
|
|
const renderContent=()=>document.querySelector('#content').replaceChildren(
|
|
state.view==='runs'?runs.renderRunList():pipelines.renderPipelineList());
|
|
const rt={state,repo:{getPipeline:async(id,version)=>{window.pinnedReads=(window.pinnedReads||0)+1;return {id,version,graph};}},
|
|
renderContent,renderAttentionBell:()=>document.createElement('span'),selectRun:()=>{},pushRoute:()=>{}};
|
|
runs=createRunsView(rt);pipelines=createPipelinesView(rt);
|
|
window.fixtureState=state;window.paintFixture=renderContent;renderContent();
|
|
})()`);
|
|
await page.waitForSelector(".run-mini");
|
|
for (const width of [390, 420, 1100]) {
|
|
await page.resize(width, 1000);
|
|
await page.evaluate(`new Promise(resolve=>setTimeout(resolve,150))`);
|
|
for (const view of ["runs", "pipelines"]) {
|
|
await page.evaluate(`fixtureState.view=${JSON.stringify(view)};paintFixture()`);
|
|
const geometry = await page.evaluate(`(() => {
|
|
const rows=[...document.querySelectorAll('#content .row')];
|
|
return { width:innerWidth, overflow:document.documentElement.scrollWidth>innerWidth,
|
|
rows:rows.map(row=>{const mini=row.querySelector('.pipeline-mini');const r=row.getBoundingClientRect();
|
|
const m=mini.getBoundingClientRect();const svg=mini.querySelector('svg');const node=svg.querySelector('rect');
|
|
const status=row.querySelector('.status-icon')?.dataset.status;
|
|
const swatch=document.createElement('span');swatch.style.color=status==='waiting'?'var(--warn)':status==='running'?'var(--accent)':'var(--status-'+status+')';mini.append(swatch);
|
|
const expectedColor=getComputedStyle(swatch).color;swatch.remove();
|
|
const nodes=[...svg.querySelectorAll('rect')].map(n=>({stage:n.dataset.stage,
|
|
color:getComputedStyle(n).color,fill:getComputedStyle(n).fill,untaken:n.classList.contains('mini-untaken')}));
|
|
const edges=[...svg.querySelectorAll('polyline')].map(n=>({route:n.dataset.route,
|
|
color:getComputedStyle(n).color,untaken:n.classList.contains('mini-untaken')}));
|
|
return {id:Number(row.dataset.runId),nodes,edges,evidence:mini.dataset.evidence,inset:r.right-m.right,align:svg.getAttribute('preserveAspectRatio'),status,expectedColor,
|
|
color:getComputedStyle(mini).color,nodeColor:getComputedStyle(node).color,
|
|
right:m.right,rowRight:r.right, rowWidth:r.width,parentWidth:row.parentElement.clientWidth,
|
|
selected:row.classList.contains('selected')};}),
|
|
reads:window.pinnedReads,filterSeparate:!document.querySelector('.run-toolbar .run-applied-filters'),
|
|
titleWrap:[...document.querySelectorAll('.run-row-title')].every(el=>getComputedStyle(el).whiteSpace!=='nowrap')};
|
|
})()`);
|
|
assert.equal(geometry.overflow, false);
|
|
assert.ok(geometry.rows.length > 0);
|
|
assert.ok(geometry.rows.every((r)=>r.inset>=6 && r.inset<40 && r.align==='xMaxYMid meet'));
|
|
assert.equal(geometry.reads, 1);
|
|
if (view === 'runs') {
|
|
assert.equal(geometry.titleWrap, true);
|
|
assert.equal(geometry.filterSeparate, true);
|
|
const node=(row,stage)=>row.nodes.find(n=>n.stage===stage);
|
|
const byId=(id)=>geometry.rows.find(row=>row.id===id);
|
|
const failed=byId(2);
|
|
assert.equal(node(failed,'plan').color,node(failed,'build').color);
|
|
assert.notEqual(node(failed,'build').color,node(failed,'finish').color);
|
|
assert.equal(node(failed,'finish').color,failed.expectedColor);
|
|
assert.equal(node(failed,'reject').fill,'none');
|
|
assert.equal(node(failed,'reject').untaken,true);
|
|
assert.equal(failed.edges.filter(e=>!e.untaken).length,2);
|
|
assert.ok(failed.edges.filter(e=>e.route.includes(':error:')).every(e=>e.untaken));
|
|
assert.equal(byId(7).evidence,'unknown');
|
|
assert.ok(byId(7).nodes.every(n=>n.untaken && n.fill==='none'));
|
|
for (const row of geometry.rows.filter(row=>row.id!==7)) {
|
|
assert.equal(row.evidence,'visits');
|
|
if (['running','waiting','queued'].includes(row.status)) {
|
|
assert.equal(node(row,row.status==='queued'?'plan':'finish').color,row.expectedColor);
|
|
}
|
|
}
|
|
assert.notEqual(node(byId(3),'finish').color,node(byId(5),'finish').color);
|
|
const selected=geometry.rows.find(row=>row.selected);
|
|
assert.equal(selected.rowWidth,selected.parentWidth);
|
|
}
|
|
results.push({view,...geometry});
|
|
await page.screenshot(resolve(out, `${view}-${width}.png`));
|
|
if (view === 'runs') {
|
|
await page.evaluate(`document.querySelector('.run-row[data-run-id="2"]').scrollIntoView({block:'center'})`);
|
|
await page.screenshot(resolve(out, `runs-evidence-${width}.png`));
|
|
}
|
|
}
|
|
for (const status of ['waiting','failed']) {
|
|
await page.evaluate(`(async () => {
|
|
const {foldRunEvent}=await import('/src/runEvents.js');
|
|
window.beforeLiveRuns??=fixtureState.runs;
|
|
fixtureState.view='runs';
|
|
fixtureState.runs=foldRunEvent(fixtureState.runs,{run_id:3,status:${JSON.stringify(status)},
|
|
current_stage:'finish',step:{sequence:3,stage:'finish',status:${JSON.stringify(status)}}}).runs;
|
|
paintFixture();
|
|
document.querySelector('.run-row[data-run-id="3"]').scrollIntoView({block:'center'});
|
|
})()`);
|
|
const live = await page.evaluate(`(() => {
|
|
const mini=document.querySelector('.run-row[data-run-id="3"] .run-mini');
|
|
const nodes=[...mini.querySelectorAll('rect')];
|
|
return {colors:nodes.map(n=>getComputedStyle(n).color),
|
|
taken:mini.querySelectorAll('polyline:not(.mini-untaken)').length,
|
|
unknown:mini.querySelectorAll('rect.mini-untaken').length};
|
|
})()`);
|
|
assert.equal(live.colors[0],live.colors[1]);
|
|
assert.equal(live.taken,2);
|
|
assert.equal(live.unknown,1);
|
|
assert.notEqual(live.colors[0],live.colors[3]);
|
|
await page.screenshot(resolve(out, `runs-live-${status}-${width}.png`));
|
|
results.push({view:'live-'+status,width,...live});
|
|
}
|
|
await page.evaluate(`fixtureState.runs=beforeLiveRuns;beforeLiveRuns=null;paintFixture()`);
|
|
}
|
|
await writeFile(resolve(out, 'geometry.json'), JSON.stringify(results,null,2));
|
|
await page.close();
|
|
} finally { await browser.close(); await new Promise((r)=>server.close(r)); }
|
|
console.log(`Scratch captures and geometry: ${out}`);
|