Files
stack/packages/webui/tests/packs-workflows-browser.test.mjs

234 lines
19 KiB
JavaScript

// Starter packs SP6 (#1873): the owner's pack picker on an organization's page, and the organization's
// workflows, driven in Chromium against stand-in pack verbs and a stand-in workflow read. It checks what the
// page shows from what the bus returns: the preview's marks, flagged lines, credential lines and noHire mark;
// a step role that no longer resolves; an organization-supplied title or label rendered as text; and the save
// and delete requests the workflow pages send. SP17: the organization page's old workflow form is gone; the
// graph pages (#/orgs/<id>/workflows) are the one editor, so the workflow test drives those.
import test from 'node:test';
import assert from 'node:assert/strict';
import { startServer } from '../src/serve.mjs';
import { createDevOwner } from '../src/dev-owner.mjs';
import { browser } from './browser.mjs';
import { close } from './fixture.mjs';
const XSS = '<img src=x onerror=window.__sp6pwn=1>';
const waitFor = (b, expression, what) => b.evaluate(`(async()=>{for(let i=0;i<300;i++){if(${expression})return true;await new Promise(r=>setTimeout(r,30))}throw Error(${JSON.stringify(what)}+': '+document.body.innerText.slice(0,2000))})()`);
const REV = n => String(n).repeat(64);
const PACKS = [
{ id: 'software-team', origin: 'shipped', pack: { label: `Software team ${XSS}`, description: 'Roles, a board and a review workflow.', version: '1.0.0', scope: 'organization' } },
{ id: 'web-project', origin: 'installed', pack: { label: 'Web project', description: 'A project board.', version: '0.2.0', scope: 'project' } },
];
const PREVIEW = {
pack: { id: 'software-team', version: '1.0.0', label: `Software team ${XSS}`, origin: 'shipped', scope: 'organization' },
project: null, revision: REV('c'),
items: [
{ item: 'board', name: 'board', target: 'board', path: 'board', mark: 'add' },
{ item: 'definition', name: 'reviewer', target: 'reviewer', path: 'definitions.reviewer', mark: 'kept', diff: { primitives: ['tools', 'instructions'] } },
{ item: 'workflow', name: 'review', target: 'review', path: 'workflows.review', mark: 'same' },
{ item: 'workflow', name: 'ship', target: 'ship', path: 'workflows.ship', mark: 'removed' },
],
hires: [
{ name: 'lead-reviewer', definition: 'org:reviewer', mark: 'propose', pin: null, flagged: [{ field: 'authority.withinRole', value: 'decision.resolve.technical', reasons: ['above-chief', 'resolve-right'] }], credentialLines: [{ service: 'gitea', scope: 'repo:write' }, { service: 'matrix' }], noHire: true },
{ name: 'builder', definition: 'coder', mark: 'dropped', pin: null, flagged: null, credentialLines: [], noHire: false },
],
skills: { notEnabled: ['lint-fix'] }, chief: true, plan: {}, digest: REV('d'),
};
async function fixture(t, { send = true } = {}) {
const verbs = [];
let flows = {
revision: REV('a'),
document: {
workflows: {
intake: { title: `Intake ${XSS}`, description: `Sort ${XSS}`, steps: [{ title: `Sort ${XSS}`, state: 'todo', roles: ['triage', 'human'] }, { title: 'Hand off', state: 'in-progress', roles: ['ghost'] }] },
},
projects: { site: { workflows: { ship: { steps: [{ title: 'Build', state: 'in-progress', roles: ['coder'] }] } } } },
definitions: { triage: {} },
},
templates: ['coder', 'reviewer'],
};
const read = async (verb, args) => {
verbs.push([verb, args]);
if (verb === 'packs.console') return { packs: PACKS };
if (verb === 'pack.preview.console') return { ...PREVIEW, project: args.project ?? null };
if (verb === 'autonomy.get.console') return { revision: REV('e'), autonomy: {}, levels: {} };
throw Object.assign(new Error('not-configured'), { code: 'not-configured' });
};
const write = async (verb, args) => {
verbs.push([verb, args]);
if (verb === 'pack.apply.console') return { revision: REV('f'), added: ['board', 'definitions.reviewer'], pack: { id: args.pack, version: '1.0.0' }, hires: [{ name: 'lead-reviewer', state: 'refused', code: 'definition-not-hireable' }] };
if (verb === 'workflow.set.console') {
assert.equal(args.revision, flows.revision);
const doc = structuredClone(flows.document);
const scope = args.project ? doc.projects[args.project] : doc;
scope.workflows ??= {};
if (args.workflow === null) delete scope.workflows[args.name]; else scope.workflows[args.name] = args.workflow;
flows = { ...flows, revision: REV(flows.revision[0] === 'a' ? 'b' : '9'), document: doc };
return { revision: flows.revision, added: [] };
}
throw Object.assign(new Error('not-configured'), { code: 'not-configured' });
};
const server = await startServer({
port: 0, sso: createDevOwner(), send: async () => ({}), log: () => {},
organizations: {
list: async () => [{ id: 'acme', label: 'Acme', busHealthy: true }],
resolve: async () => ({
busHealthy: true, decisionWrite: async () => ({}), requestRead: read, requestWrite: read, packsWrite: send ? write : null,
workflowsRead: async () => structuredClone(flows), send: send ? async () => ({}) : null,
reads: { business: async () => ({ business: { id: 'acme', projects: [], instances: [] } }), settings: async () => ({}), queue: async () => ({ rows: [] }) },
bus: { instances: async () => ({ instances: {} }), agents: async () => [], 'thread.claims': async () => [], tasks: async () => [], inbox: async () => [], messages: async () => ({ messages: [] }) },
chat: { projects: async () => [], threads: async () => [] },
}),
},
});
t.after(() => close(server));
const b = await browser({ machine: false, args: ['--disable-extensions'] });
t.after(() => b.close());
await b.viewport(1440, 900);
await b.navigate(`http://127.0.0.1:${server.address().port}/#/orgs/acme/organization`);
return { b, verbs, flows: () => flows };
}
test('SP6 pack picker: list, preview with marks, flagged, credential and noHire lines, apply by digest; pack text stays text', { timeout: 60000 }, async t => {
const { b, verbs } = await fixture(t);
await waitFor(b, `document.querySelector('[data-pack="software-team"] [data-pack-preview-button]')`, 'pack list');
assert.equal(await b.evaluate(`document.querySelector('[data-pack="software-team"] strong').textContent`), `Software team ${XSS}`);
// The project pack offers the code projects the workflow read lists.
assert.deepEqual(await b.evaluate(`[...document.querySelectorAll('[data-pack="web-project"] [data-pack-project] option')].map(o=>o.value)`), ['site']);
await b.evaluate(`document.querySelector('[data-pack="software-team"] [data-pack-preview-button]').click();true`);
await waitFor(b, `document.querySelector('[data-pack-preview]')`, 'preview');
assert.deepEqual(await b.evaluate(`[...document.querySelectorAll('[data-pack-item]')].map(r=>[r.dataset.packItem,r.querySelector('[data-pack-mark]').dataset.packMark,r.querySelector('[data-pack-mark]').textContent])`), [
['board', 'add', 'will be added'],
['definitions.reviewer', 'kept', 'kept: the organization has its own version differs in tools, instructions'],
['workflows.review', 'same', 'already the same'],
['workflows.ship', 'removed', 'stays removed: you removed it after an earlier apply'],
]);
const lead = '[data-pack-hire="lead-reviewer"]';
assert.match(await b.evaluate(`document.querySelector('${lead} [data-pack-flagged]').textContent`), /authority\.withinRole decision\.resolve\.technical above the chief, a resolve right/);
// The bus sends each credential line as {service, scope} (hire-decisions credentialLines); scope is absent
// when the definition names a service without scopes.
assert.deepEqual(await b.evaluate(`[...document.querySelectorAll('${lead} [data-pack-credentials] li')].map(l=>l.textContent)`), ['gitea repo:write', 'matrix']);
assert.match(await b.evaluate(`document.querySelector('${lead} [data-pack-nohire]').textContent`), /no-hire list/);
assert.equal(await b.evaluate(`document.querySelector('[data-pack-hire="builder"] [data-pack-nohire]')`), null);
assert.ok(await b.evaluate(`document.querySelector('[data-pack-hire="builder"] [data-pack-noflag]')!==null`));
assert.match(await b.evaluate(`document.querySelector('[data-pack-hire="builder"]').textContent`), /Not proposed/);
assert.match(await b.evaluate(`document.querySelector('[data-pack-skills]').textContent`), /lint-fix/);
assert.equal(await b.evaluate(`document.querySelectorAll('[data-packs] img').length`), 0, 'a pack label never becomes markup');
assert.equal(await b.evaluate(`window.__sp6pwn===undefined`), true);
assert.equal(verbs.filter(([v]) => v.startsWith('pack.apply')).length, 0, 'preview writes nothing');
await b.evaluate(`document.querySelector('[data-pack-apply]').click();true`);
await waitFor(b, `/^Applied\\./.test(document.querySelector('[data-packs-status]').textContent)`, 'apply status');
assert.equal(await b.evaluate(`document.querySelector('[data-packs-status]').textContent`), 'Applied. Added board, definitions.reviewer. Not hired: lead-reviewer, its role is on the no-hire list.');
assert.deepEqual(verbs.filter(([v]) => v === 'pack.apply.console').map(([, a]) => a), [{ pack: 'software-team', digest: REV('d') }]);
assert.equal(await b.evaluate(`document.querySelector('[data-pack-preview]')`), null);
// A project pack previews against the chosen project.
await b.evaluate(`document.querySelector('[data-pack="web-project"] [data-pack-preview-button]').click();true`);
await waitFor(b, `/for project site/.test(document.querySelector('[data-pack-preview] h4')?.textContent??'')`, 'project preview');
assert.deepEqual(verbs.filter(([v]) => v === 'pack.preview.console').at(-1)[1], { pack: 'web-project', project: 'site' });
});
test('SP6 pack picker without send previews but offers no apply', { timeout: 60000 }, async t => {
const { b } = await fixture(t, { send: false });
await waitFor(b, `document.querySelector('[data-pack="software-team"] [data-pack-preview-button]')`, 'pack list');
await b.evaluate(`document.querySelector('[data-pack="software-team"] [data-pack-preview-button]').click();true`);
await waitFor(b, `document.querySelector('[data-pack-preview]')`, 'preview');
assert.equal(await b.evaluate(`document.querySelector('[data-pack-apply]')`), null);
assert.match(await b.evaluate(`document.querySelector('[data-pack-preview]').textContent`), /started without send/);
// Without send the workflow read is not writable: the graph page shows the step as text and no form.
await b.evaluate(`location.hash = '#/orgs/acme/workflows/intake?step=1'`);
await waitFor(b, `document.querySelector('[data-wfg-detail] [data-wfg-field="roles"]')`, 'read-only step');
assert.equal(await b.evaluate(`document.querySelector('[data-wfg-form],[data-wfg-save],[data-wfg-delete],[data-wf-edit],[data-wf-new]')`), null, 'no workflow change controls without send');
assert.ok(await b.evaluate(`!!document.querySelector('[data-wfg-readonly]')`));
});
test('SP6 workflows: list by scope, a step naming no current role, titles as text, edit, new and delete', { timeout: 90000 }, async t => {
const { b, verbs, flows } = await fixture(t);
const press = sel => b.evaluate(`(()=>{const e=document.querySelector(${JSON.stringify(sel)});if(!e)throw Error('no '+${JSON.stringify(sel)});if(e.disabled)throw Error('disabled '+${JSON.stringify(sel)});e.click();return true})()`);
const fill = (sel, value) => b.evaluate(`(()=>{const e=document.querySelector(${JSON.stringify(sel)});e.value=${JSON.stringify(value)};e.dispatchEvent(new Event('input',{bubbles:true}));return true})()`);
const text = sel => b.evaluate(`(document.querySelector(${JSON.stringify(sel)})?.textContent ?? '').replace(/\\s+/g,' ').trim()`);
const sets = () => verbs.filter(([v]) => v === 'workflow.set.console');
// The organization page: the Workflows card, no old form.
await waitFor(b, `document.querySelectorAll('[data-wfg-card] [data-wfg-card-item]').length === 2`, 'workflow card');
assert.equal(await b.evaluate(`document.querySelector('[data-workflows],[data-wf-edit],[data-wf-new]')`), null, 'the old organization-page editor is gone');
assert.equal(await b.evaluate(`document.querySelectorAll('[data-org-dashboard] img').length`), 0, 'a workflow title never becomes markup');
// The list: each scope, titles as text.
await b.evaluate(`location.hash = '#/orgs/acme/workflows'`);
await waitFor(b, `document.querySelector('[data-wfg-item="intake"]')`, 'workflow list');
assert.deepEqual(await b.evaluate(`[...document.querySelectorAll('[data-wfg-scope-list]')].map(n=>n.dataset.wfgScopeList)`), ['', 'site']);
assert.equal(await b.evaluate(`document.querySelector('[data-wfg-item="intake"] a[data-wfg-open]').textContent`), `Intake ${XSS}`);
assert.equal(await b.evaluate(`document.querySelectorAll('#bus-view img').length`), 0);
assert.equal(await b.evaluate(`window.__sp6pwn===undefined`), true);
// The graph: triage (an organization role), human and coder (a template) resolve; ghost names no current role.
await b.evaluate(`location.hash = '#/orgs/acme/workflows/intake'`);
await waitFor(b, `document.querySelectorAll('[data-wfg-graph] [data-wfg-step]').length === 2 && document.querySelector('[data-wfg-meta-form]')`, 'graph');
assert.deepEqual(await b.evaluate(`[...document.querySelectorAll('[data-wfg-graph] [data-wfg-step]')].map(s=>s.querySelectorAll('[data-wfg-missing]').length)`), [0, 1]);
assert.equal(await b.evaluate(`document.querySelectorAll('#bus-view img').length`), 0);
// The workflow title changes in the panel with no step selected; it is held until Save.
assert.equal(await b.evaluate(`document.querySelector('[data-wfg-meta-form] input[name=title]').value`), `Intake ${XSS}`);
await fill('[data-wfg-meta-form] input[name=title]', 'Intake');
// Step 2's form marks ghost; swapping it for reviewer clears the mark in the graph.
await b.evaluate(`document.querySelector('[data-wfg-step="2"]').dispatchEvent(new MouseEvent('click',{bubbles:true,cancelable:true}));true`);
await waitFor(b, `document.querySelector('[data-wfg-step-form="1"]')`, 'step 2 form');
assert.match(await text('[data-wfg-step-form="1"] [data-wfg-roles-pick]'), /ghost names no current role/);
await press('[data-wfg-step-form="1"] input[name="role"][value="ghost"]');
await press('[data-wfg-step-form="1"] input[name="role"][value="reviewer"]');
await waitFor(b, `document.querySelectorAll('[data-wfg-graph] [data-wfg-missing]').length === 0`, 'mark cleared in the graph');
assert.equal(sets().length, 0, 'nothing sent before Save');
await press('[data-wfg-step-form="1"] [data-wfg-save]');
await waitFor(b, `/Saved intake\\./.test(document.querySelector('[data-wfg-status]')?.textContent ?? '')`, 'save status');
assert.deepEqual(sets().at(-1)[1], { revision: REV('a'), name: 'intake', workflow: { title: 'Intake', description: `Sort ${XSS}`, steps: [{ title: `Sort ${XSS}`, state: 'todo', roles: ['triage', 'human'] }, { title: 'Hand off', state: 'in-progress', roles: ['reviewer'] }] } });
assert.equal(await b.evaluate(`document.querySelector('#bus-view h1').textContent`), 'Intake');
assert.equal(await b.evaluate(`document.querySelectorAll('[data-wfg-graph] [data-wfg-missing]').length`), 0, 'the saved workflow shows the fixed step');
// New, in the project scope: a bad name is refused on the page and sends nothing.
const before = sets().length;
await b.evaluate(`location.hash = '#/orgs/acme/workflows'`);
await waitFor(b, `document.querySelector('[data-wfg-new]')`, 'New workflow');
await press('[data-wfg-new]');
await waitFor(b, `document.querySelector('[data-wfg-newform] select[name=scope]')`, 'new form');
await b.evaluate(`{const f=document.querySelector('[data-wfg-newform]');f.elements.scope.value='site';f.elements.scope.dispatchEvent(new Event('change',{bubbles:true}))}true`);
await fill('[data-wfg-newform] input[name=title]', 'Plan');
await press('[data-wfg-newform] input[name="role"][value="human"]');
// The writer's name rule (PLAN_NAME): lowercase, no underscore, dot or leading digit. Each is refused here, not at the bus.
for (const bad of ['Bad Name', 'release_1', 'v2.ship', '2nd-review']) {
await fill('[data-wfg-newform] input[name=name]', bad);
await press('[data-wfg-create]');
await waitFor(b, `(document.querySelector('[data-wfg-newform] [data-wfg-why]')?.textContent ?? '') === 'A workflow name is 1 to 64 lowercase letters, digits or dashes, starting with a letter.'`, `${bad} refused`);
assert.equal(sets().length, before, `${bad} sends nothing`);
await fill('[data-wfg-newform] input[name=name]', '');
}
assert.equal(await b.evaluate(`document.querySelector('[data-wfg-newform] input[name=name]').placeholder`), 'lowercase letters, digits, dashes');
// A new workflow under a name the scope already holds would replace it: refused, nothing sent.
await fill('[data-wfg-newform] input[name=name]', 'ship');
await press('[data-wfg-create]');
await waitFor(b, `(document.querySelector('[data-wfg-newform] [data-wfg-why]')?.textContent ?? '') === 'A workflow named ship already exists here. Edit it instead.'`, 'existing name refused');
assert.equal(sets().length, before, 'an existing name sends nothing');
assert.equal(await b.evaluate(`document.querySelector('[data-wfg-newform] select[name=scope]').value`), 'site', 'the form keeps the chosen scope');
await fill('[data-wfg-newform] input[name=name]', 'plan');
await press('[data-wfg-create]');
await waitFor(b, `location.hash === '#/orgs/acme/workflows/plan?project=site&step=1' && document.querySelector('[data-wfg-save]') && !document.querySelector('[data-wfg-save]').disabled`, 'opens unsaved');
assert.equal(sets().length, before, 'Create writes nothing');
await press('[data-wfg-save]');
await waitFor(b, `/Saved plan\\./.test(document.querySelector('[data-wfg-status]')?.textContent ?? '')`, 'new save');
assert.deepEqual(sets().at(-1)[1], { revision: REV('b'), name: 'plan', project: 'site', workflow: { steps: [{ title: 'Plan', state: 'todo', roles: ['human'] }] } });
// Delete asks first; Keep sends nothing; Delete sends the project and the revision.
await b.evaluate(`location.hash = '#/orgs/acme/workflows/ship?project=site'`);
await waitFor(b, `document.querySelector('[data-wfg-delete]') && document.querySelector('#bus-view h1')?.textContent === 'ship'`, 'ship graph');
await press('[data-wfg-delete]');
await waitFor(b, `document.querySelector('[data-wfg-delete-no]')`, 'delete confirm');
await press('[data-wfg-delete-no]');
await waitFor(b, `document.querySelector('[data-wfg-delete]') && !document.querySelector('[data-wfg-confirm]')`, 'kept');
const n = sets().length;
await press('[data-wfg-delete]');
await waitFor(b, `document.querySelector('[data-wfg-delete-yes]')`, 'delete confirm again');
assert.equal(sets().length, n);
const rev = flows().revision;
await press('[data-wfg-delete-yes]');
await waitFor(b, `location.hash === '#/orgs/acme/workflows' && /Deleted ship\\./.test(document.querySelector('[data-wfg-list] [data-wfg-status]')?.textContent ?? '')`, 'delete status');
assert.deepEqual(sets().at(-1)[1], { revision: rev, name: 'ship', workflow: null, project: 'site' });
assert.equal(await b.evaluate(`document.querySelector('[data-wfg-scope-list="site"] [data-wfg-item="ship"]')`), null);
assert.equal(await b.evaluate(`document.documentElement.scrollWidth<=innerWidth`), true);
});