Files
pipelines/web/test/schedules-browser.mjs
T

126 lines
8.6 KiB
JavaScript

// Local static fixture only: real view + modal controller, no RuntimeSocket/server/providers.
// node web/test/schedules-browser.mjs /approved/temp/capture-directory
import assert from 'node:assert/strict';
import { createServer } from 'node:http';
import { readFile, mkdir, writeFile } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import { resolve, extname } from 'node:path';
import { createHash } from 'node:crypto';
import { launchChrome, eventually } from './browser.mjs';
const root = fileURLToPath(new URL('../', import.meta.url));
const out = process.argv[2];
if (!out) throw new Error('Pass an approved capture directory.');
await mkdir(out, { recursive: true });
const fixture = `<!doctype html><meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="stylesheet" href="/design/tokens/tokens.css"><link rel="stylesheet" href="/base.css"><link rel="stylesheet" href="/schedules.css">
<style>body{overflow:auto}.app-root{max-width:960px;margin:auto;padding:24px}#pane-main{min-width:0}</style>
<div class="app-root"><h1>Research</h1><main id="pane-main" tabindex="-1"></main></div><div id="modalLayer"></div>
<script type="module">
import { createSchedulesView } from '/src/views/schedules.js';
import { createModalController } from '/src/modal.js';
window.records=[]; window.calls={list:0,save:0,delete:0}; window.refuse=false; window.unsupported=false;
const pipeline={id:42,version:7,name:'Research'};
const rt={state:{workspace:'/srv/research'},modal:createModalController(),repo:{
async listSchedules(){calls.list++; if(window.holdRead)await new Promise(resolve=>window.releaseRead=resolve); if(window.unsupported)throw Error('Unknown message: list_schedules (old server)'); return structuredClone(records);},
async saveSchedule(record){calls.save++; if(window.refuse)throw Error('409 revision conflict');
if(record.id){const i=records.findIndex(x=>x.id===record.id);records[i]=structuredClone({...record,revision:record.revision+1});}
else records.push(structuredClone({...record,id:records.length+1,revision:1}));return record;},
async deleteSchedule(id,revision){calls.delete++; records=records.filter(x=>x.id!==id);}
},renderContent(){document.querySelector('#pane-main').replaceChildren(view.renderPipelineSchedules(pipeline));rt.modal.reconcile();}};
const view=createSchedulesView(rt); window.view=view;window.rt=rt;
window.seed=async()=>{records=[{id:1,revision:1,name:'AI news',pipeline_id:42,version:7,enabled:true,input:{prompt:'Research AI news',count:2},workspace:'/srv/research',rule:{every:'24h',timezone:'UTC'},next_due:'2026-10-22T09:00:00Z'},
{id:2,revision:1,name:'Battery news',pipeline_id:42,version:3,enabled:true,input:{prompt:'Research battery advances'},workspace:'/srv/batteries',rule:{times:['09:00'],days:['mon','fri'],timezone:'UTC'},next_due:'2026-10-23T09:00:00Z'}];await view.loadPipelineSchedules(42,{force:true});};
rt.renderContent();window.ready=true;
</script>`;
const server = createServer(async (req, res) => {
try {
if (req.url === '/') { res.setHeader('Content-Type', 'text/html'); res.end(fixture); return; }
const path = resolve(root, `.${new URL(req.url, 'http://fixture').pathname}`);
if (!path.startsWith(root)) { res.writeHead(403); res.end(); return; }
res.setHeader('Content-Type', ({ '.js': 'text/javascript', '.css': 'text/css' })[extname(path)] ?? 'text/plain');
res.end(await readFile(path));
} catch { res.writeHead(404); res.end(); }
});
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
let browser;
const manifest = [];
try {
browser = await launchChrome({ width: 1100, height: 900 });
const page = await browser.openPage(`http://127.0.0.1:${server.address().port}/`);
await page.waitForText('No schedules yet');
const capture = async name => {
const path = resolve(out, `${name}.png`); await page.screenshot(path);
manifest.push({ path, sha256: createHash('sha256').update(await readFile(path)).digest('hex') });
};
const noOverflow = async () => assert.equal(await page.evaluate('document.documentElement.scrollWidth <= innerWidth'), true);
await capture('schedule-empty-1100');
await page.evaluate('seed()'); await page.waitForText('Battery news');
assert.equal(await page.evaluate('document.querySelectorAll(".schedule-card").length'), 2);
await capture('schedule-two-1100');
await page.clickText('[data-schedule-id="2"] button', 'Edit');
await capture('schedule-clock-editor-1100'); await noOverflow();
assert.equal(await page.evaluate('document.querySelector("[name=mode]").value'), 'clock');
await page.clickText('.schedule-panel button', 'Cancel');
await page.clickText('[data-schedule-id="1"] button', 'Edit');
await page.fill('[name="prompt"]', 'AI draft remains here');
assert.equal(await page.evaluate('document.activeElement.name'), 'prompt');
await page.evaluate('rt.renderContent()');
assert.equal(await page.evaluate('document.querySelector("[name=prompt]").value'), 'AI draft remains here');
assert.equal(await page.evaluate('document.activeElement.name'), 'prompt');
await page.evaluate('window.refuse=true'); await page.clickText('.schedule-panel button', 'Save schedule');
await page.waitForText('409 revision conflict');
assert.equal(await page.evaluate('document.querySelector("[name=prompt]").value'), 'AI draft remains here');
await capture('schedule-conflict-1100');
await page.evaluate('window.refuse=false'); await page.clickText('.schedule-panel button', 'Save schedule');
await eventually(() => page.evaluate('!rt.modal.isOpen("schedule-editor")'));
assert.equal(await page.evaluate('records[1].input.prompt'), 'Research battery advances');
assert.equal(await page.evaluate('records[0].input.count'), 2);
await page.clickText('[data-schedule-id="1"] button', 'Pause');
await eventually(() => page.evaluate('!records[0].enabled'));
await capture('schedule-paused-1100');
await page.clickText('[data-schedule-id="1"] button', 'Delete');
assert.equal(await page.evaluate('calls.delete'), 0);
await capture('schedule-delete-confirm-1100');
await page.clickText('.schedule-confirm button', 'Keep schedule');
assert.equal(await page.evaluate('document.activeElement.textContent'), 'Delete');
await page.clickText('[data-schedule-id="1"] button', 'Delete');
await page.clickText('.schedule-confirm button', 'Delete schedule');
await eventually(() => page.evaluate('records.length===1 && !rt.modal.isOpen("schedule-delete")'));
assert.equal(await page.evaluate('records[0].id'), 2);
await page.evaluate('seed()');
await page.resize(390, 844);
await capture('schedule-two-390'); await noOverflow();
await page.click('.schedule-add');
await page.fill('[name="name"]', 'Third independent topic'); await page.fill('[name="prompt"]', 'Mobile draft');
await page.click('.schedule-advanced summary');
await page.fill('[name="otherInput"]', 'null');
await page.clickText('.schedule-panel button', 'Save schedule');
await page.waitForText('Other input must be a JSON object');
assert.equal(await page.evaluate('document.querySelector("[name=prompt]").value'), 'Mobile draft');
await capture('schedule-invalid-json-390');
await page.fill('[name="otherInput"]', '{"count":3}');
await page.clickText('.schedule-panel button', 'Save schedule');
await eventually(() => page.evaluate('records.length===3 && !rt.modal.isOpen("schedule-editor")'));
await page.clickText('[data-schedule-id="3"] button', 'Edit');
await capture('schedule-editor-390'); await noOverflow();
await page.click('.schedule-advanced summary');
await page.fill('[name="model"]', 'provider/model');
assert.equal(await page.evaluate('document.activeElement.name'), 'model');
await capture('schedule-advanced-390'); await noOverflow();
await page.clickText('.schedule-panel button', 'Cancel');
assert.equal(await page.evaluate('document.activeElement.textContent'), 'Edit');
await page.evaluate('holdRead=true;view.clearScheduleCache();rt.renderContent()');
await page.waitForText('Loading schedules'); await capture('schedule-loading-390');
await page.evaluate('holdRead=false;releaseRead()'); await page.waitForText('Third independent topic');
await page.evaluate('unsupported=true;view.clearScheduleCache();rt.renderContent()');
await page.waitForText('old server'); await capture('schedule-old-server-390');
await page.evaluate('unsupported=false'); await page.clickText('.schedule-section button', 'Retry');
await page.waitForText('Third independent topic');
await page.close();
await writeFile(resolve(out, 'manifest.json'), JSON.stringify(manifest, null, 2));
console.log(JSON.stringify({ checks: 'passed', captures: manifest }, null, 2));
} finally {
await browser?.close(); await new Promise(resolve => server.close(resolve));
}