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

1000 lines
81 KiB
JavaScript

// Single pane SP9 (#1873): an organization's workflows, read only. #/orgs/<id>/workflows lists them,
// #/orgs/<id>/workflows/<name> draws one as an inline-SVG graph (one box per step, an arrowed line per
// hand-off), and the organization page carries a small card. The data is what GET /api/orgs/<id>/workflows
// returns from the organization file: steps in order, each with a title, a state and its roles. Nothing
// else is drawn. A stopped organization is not read and says why.
// SP10 (#1873): the graph page edits the workflow. The change is held in the page until Save, which sends one
// POST with the revision the page read; a refusal is shown in words and the change kept; the poll never draws
// over a pending change. The fixture's write stub keeps the organization file and its revision, as the bus does.
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 { mkdtempSync } from 'node:fs';
import { join } from 'node:path';
import { createOrgAdmin } from '../src/org-admin.mjs';
import { actionTrail } from '../src/action-trail.mjs';
import { browser } from './browser.mjs';
import { close } from './fixture.mjs';
const XSS = '<img src=x onerror=window.__sp9pwn=1>';
const at = minutes => new Date(Date.now() - minutes * 60000).toISOString();
const waitFor = b => (expression, what = expression) => 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,1500))})()`);
// The coding starter pack's workflow as pack.apply writes it (the pack's step ids are not kept).
const CHANGE = {
description: 'One change from task to merge.',
steps: [
{ title: 'Write the task', roles: ['pm'], state: 'todo' },
{ title: 'Implement the change', roles: ['coder'], state: 'in-progress' },
{ title: 'Review the change', roles: ['reviewer'], state: 'in-review' },
{ title: 'Approve the merge', roles: ['human', 'merge-gate'], state: 'done' },
],
};
const DOCS = {
acme: {
definitions: { pm: {}, coder: {}, reviewer: {} },
workflows: { change: CHANGE, intake: { title: `Intake ${XSS}`, steps: [{ title: `Sort ${XSS}`, state: 'todo', roles: ['human'] }] } },
projects: { site: { workflows: { change: { title: 'Site release', steps: [{ title: 'Build', state: 'in-progress', roles: ['coder'] }, { title: 'Ship', state: 'done', roles: ['human'] }] } } }, notes: { kind: 'library', workflows: { hidden: { steps: [{ title: 'x', state: 'todo', roles: ['human'] }] } } } },
},
bare: { definitions: {}, projects: {} },
};
const LIST = [
{ id: 'acme', label: 'Acme Studio', theme: 'cobalt', busHealthy: true },
{ id: 'bare', label: 'Bare Works', theme: 'grove', busHealthy: true },
{ id: 'gamma', label: 'Gamma Home', theme: 'rosewood', busHealthy: false, busStopped: true },
];
// The organization files as the bus holds them: each organization's document and revision, the writes the
// page sent, and a refusal to give the next write. A write with another revision is a revision-conflict.
const hex = n => n.toString(16).padStart(64, '0');
function store(writable) {
const st = { writable, docs: structuredClone(DOCS), revs: { acme: 'a'.repeat(64), bare: 'b'.repeat(64), gamma: 'c'.repeat(64) }, writes: [], refuse: null, n: 0, held: {}, down: {} };
// SP17 item 4: st.down[id] is 'restarting' (the bus does not answer: the list says unavailable, the file
// still reads, a write finds no bus host) or 'stopped' (the owner stopped it: the page does not read it).
st.view = org => (st.down[org.id] ? { ...org, busHealthy: false, ...(st.down[org.id] === 'stopped' ? { busStopped: true } : {}) } : org);
// SP17: hold('read') holds the next workflow read, which answers with the file as it was when the read
// began; hold('write') holds the next write before it reaches the file. Each returns the release.
st.hold = which => { let open; st.held[which] = new Promise(r => { open = r; }); return open; };
const fail = code => Object.assign(new Error(code), { code });
st.write = (id, verb, args) => {
st.writes.push({ id, verb, args: structuredClone(args) });
if (verb !== 'workflow.set.console') throw fail('not-configured');
if (st.down[id]) throw fail('no-bus-host');
if (st.refuse) { const code = st.refuse; st.refuse = null; throw fail(code); }
if (args.revision !== st.revs[id]) throw fail('revision-conflict');
const doc = st.docs[id], scope = args.project ? doc.projects[args.project] : doc;
scope.workflows ??= {};
if (args.workflow === null) delete scope.workflows[args.name]; else scope.workflows[args.name] = structuredClone(args.workflow);
st.revs[id] = hex(++st.n);
return { revision: st.revs[id], added: [] };
};
// Another writer changes the file outside the page.
st.change = (id, fn) => { fn(st.docs[id]); st.revs[id] = hex(1000 + ++st.n); };
return st;
}
function organizations(reads, st) {
const member = org0 => { const org = st.view(org0); return {
id: org.id, doc: { appearance: { theme: org.theme }, roles: {}, projects: {} },
status: async () => (org.busHealthy ? { healthy: true, pid: 4242, boot: null } : { healthy: false, boot: { ok: false, at: at(90), lastGood: null, reason: 'stopped by the owner' } }),
options: () => ({ bus: { inbox: async () => [], agents: async () => [], 'thread.claims': async () => [], tasks: async () => [] }, chat: { threads: async () => [], projects: async () => [] } }),
}; };
return {
list: async () => LIST.map(st.view),
members: async () => LIST.map(member),
resolve: async id => {
const org = LIST.find(o => o.id === id); if (!org) return null;
const workflowsRead = async () => {
reads.push(id);
if (st.failRead) throw Object.assign(new Error('org-unavailable'), { code: 'org-unavailable' });
const answer = { revision: st.revs[id], document: structuredClone(st.docs[id] ?? {}), templates: ['writer'] };
const h = st.held.read; if (h) { st.held.read = null; await h; }
return answer;
};
if (!org.busHealthy) return { busHealthy: false, reads: null, bus: {}, workflowsRead, chat: { projects: async () => [], threads: async () => [] } };
return {
busHealthy: true, workflowsRead, packsWrite: st.writable ? async (verb, args) => { const h = st.held.write; if (h) { st.held.write = null; await h; } return st.write(id, verb, args); } : null, decisionWrite: async () => ({}), send: async () => ({}),
requestRead: async verb => { if (verb === 'packs.console') return { packs: [] }; if (verb === 'autonomy.get.console') return { revision: 'e'.repeat(64), autonomy: {}, levels: {} }; throw Object.assign(new Error('not-configured'), { code: 'not-configured' }); },
reads: { business: async () => ({ business: { id, 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 () => [] },
};
},
};
}
async function setup(t, { path, viewport = [1440, 900], mode = null, writable = true, admin = null } = {}) {
const reads = [], st = store(writable);
const extra = admin ? { orgAdmin: admin(st), trail: actionTrail(join(mkdtempSync(join(process.env.TMPDIR, 'wfg-')), 'console')) } : {};
const server = await startServer({ port: 0, sso: createDevOwner(), send: async () => ({}), log: () => {}, organizations: organizations(reads, st), ...extra });
t.after(() => close(server));
const b = await browser({ machine: false, args: ['--disable-extensions'] });
t.after(() => b.close());
await b.viewport(...viewport);
// The Console's own mode wins over the OS setting (Harbor in the dark by default), so a theme test sets it per organization.
if (mode) await b.call('Page.addScriptToEvaluateOnNewDocument', { source: `try { for (const o of ['acme','bare','gamma']) localStorage.setItem('mosaic-console-appearance:' + o, JSON.stringify({ theme: 'harbor', mode: ${JSON.stringify(mode)} })); } catch {}` });
await b.navigate(`http://127.0.0.1:${server.address().port}/${path}`);
return { b, reads, st, wait: waitFor(b) };
}
const boxes = `[...document.querySelectorAll('[data-wfg-graph] [data-wfg-step]')]`;
test('the list names each workflow by scope with its steps in order, links to its graph, and keeps titles as text', { timeout: 60000 }, async t => {
const { b, wait } = await setup(t, { path: '#/orgs/acme/workflows' });
await wait(`document.querySelector('[data-wfg-list] [data-wfg-item="change"]')`, 'workflow list');
assert.equal(await b.evaluate(`document.querySelector('#bus-view h1').textContent`), 'Workflows');
// Organization first, then each code project; a library project has no workflows.
assert.deepEqual(await b.evaluate(`[...document.querySelectorAll('[data-wfg-scope-list]')].map(n=>[n.dataset.wfgScopeList,n.querySelector('h2').textContent])`), [['', 'Organization'], ['site', 'Project site']]);
assert.deepEqual(await b.evaluate(`[...document.querySelectorAll('[data-wfg-scope-list=""] [data-wfg-item]')].map(n=>n.dataset.wfgItem)`), ['change', 'intake']);
const change = `[data-wfg-scope-list=""] [data-wfg-item="change"]`;
assert.equal(await b.evaluate(`document.querySelector('${change} a[data-wfg-open]').getAttribute('href')`), '#/orgs/acme/workflows/change');
assert.equal(await b.evaluate(`document.querySelector('[data-wfg-scope-list="site"] [data-wfg-item="change"] a[data-wfg-open]').getAttribute('href')`), '#/orgs/acme/workflows/change?project=site');
assert.deepEqual(await b.evaluate(`[...document.querySelectorAll('${change} [data-wfg-chain] li')].map(l=>l.textContent)`), ['Write the task', 'Implement the change', 'Review the change', 'Approve the merge']);
assert.match(await b.evaluate(`document.querySelector('${change}').textContent`), /4 steps/);
assert.match(await b.evaluate(`document.querySelector('${change}').textContent`), /One change from task to merge\./);
// The title is shown as written.
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.__sp9pwn===undefined`), true);
assert.equal(await b.evaluate(`document.querySelector('#bus-view [data-ws-unread]')`), null, 'a running bus has no unread note');
// The trail names the organization and the page.
await wait(`document.querySelector('#breadcrumbs [data-crumb-org]')?.textContent === 'Acme Studio'`, 'org crumb');
// A click opens the graph.
await b.evaluate(`document.querySelector('${change} a[data-wfg-open]').click();true`);
await wait(`location.hash === '#/orgs/acme/workflows/change' && document.querySelectorAll('[data-wfg-graph] [data-wfg-step]').length === 4`, 'graph after click');
});
test('the graph draws one box per step in order with its roles and one arrowed edge per hand-off', { timeout: 60000 }, async t => {
const { b, wait } = await setup(t, { path: '#/orgs/acme/workflows/change' });
await wait(`${boxes}.length === 4`, 'four boxes');
assert.equal(await b.evaluate(`document.querySelector('#bus-view h1').textContent`), 'change');
assert.equal(await b.evaluate(`document.querySelector('[data-wfg-graph]').tagName.toLowerCase()`), 'svg');
assert.deepEqual(await b.evaluate(`${boxes}.map(g=>g.dataset.wfgStep)`), ['1', '2', '3', '4']);
assert.deepEqual(await b.evaluate(`${boxes}.map(g=>g.querySelector('[data-wfg-title]').textContent)`), ['Write the task', 'Implement the change', 'Review the change', 'Approve the merge']);
// One role per line.
assert.deepEqual(await b.evaluate(`${boxes}.map(g=>[...g.querySelectorAll('[data-wfg-roles] tspan')].map(t=>t.textContent))`), [['pm'], ['coder'], ['reviewer'], ['human', 'merge-gate']]);
// A role that resolves to no definition or template (and is not human) is marked on its box.
assert.deepEqual(await b.evaluate(`${boxes}.map(g=>g.querySelectorAll('[data-wfg-missing]').length)`), [0, 0, 0, 1]);
// sp9 follow-up: the missing role's whole name shows at 1440, on its own line under "No current role:".
assert.deepEqual(await b.evaluate(`[...document.querySelectorAll('[data-wfg-step="4"] [data-wfg-missing] tspan')].map(t=>t.textContent)`), ['No current role:', 'merge-gate']);
// At 1440 the steps run left to right.
const xs = await b.evaluate(`${boxes}.map(g=>g.querySelector('rect').getBoundingClientRect().left)`);
assert.deepEqual([...xs].sort((p, q) => p - q), xs);
assert.equal(new Set(xs).size, 4);
// One edge per hand-off, from each box to the next, with an arrowhead.
assert.deepEqual(await b.evaluate(`[...document.querySelectorAll('[data-wfg-graph] [data-wfg-edge]')].map(e=>e.dataset.wfgEdge)`), ['1-2', '2-3', '3-4']);
const touches = await b.evaluate(`[...document.querySelectorAll('[data-wfg-graph] [data-wfg-edge]')].map(e=>{
const [f,t]=e.dataset.wfgEdge.split('-'), box=n=>document.querySelector('[data-wfg-graph] [data-wfg-step="'+n+'"] rect').getBBox();
const p=e.getPointAtLength(0), q=e.getPointAtLength(e.getTotalLength()), a=box(f), z=box(t), near=(pt,r)=>pt.x>=r.x-14&&pt.x<=r.x+r.width+14&&pt.y>=r.y-14&&pt.y<=r.y+r.height+14;
return near(p,a)&&near(q,z)&&!!e.getAttribute('marker-end');})`);
assert.deepEqual(touches, [true, true, true]);
// The graph scrolls inside its own box, never the page.
assert.equal(await b.evaluate(`document.documentElement.scrollWidth<=innerWidth`), true);
// Nothing running is drawn: no read links a task to a step.
assert.equal(await b.evaluate(`document.querySelector('[data-wfg-running]')`), null);
});
test('selecting a step shows what the data holds for it and survives a repaint', { timeout: 60000 }, async t => {
// A reader's page: the roles and state are lines of text. A writer's page shows them as the step's form (below).
const { b, wait, reads } = await setup(t, { path: '#/orgs/acme/workflows/change', writable: false });
await wait(`${boxes}.length === 4`, 'four boxes');
assert.match(await b.evaluate(`document.querySelector('[data-wfg-detail]').textContent`), /Select a step/);
await b.evaluate(`document.querySelector('[data-wfg-step="2"]').dispatchEvent(new MouseEvent('click',{bubbles:true,cancelable:true}));true`);
await wait(`/[?&]step=2/.test(location.hash) && document.querySelector('[data-wfg-detail] [data-wfg-detail-title]')?.textContent === 'Implement the change'`, 'detail for step 2');
assert.equal(await b.evaluate(`location.hash`), '#/orgs/acme/workflows/change?step=2');
const field = name => b.evaluate(`document.querySelector('[data-wfg-detail] [data-wfg-field="${name}"]')?.textContent.replace(/\\s+/g,' ').trim() ?? null`);
assert.equal(await field('position'), 'Step 2 of 4');
assert.equal(await field('roles'), 'coder');
assert.equal(await field('state'), 'In progress');
assert.equal(await field('after'), 'Step 1, Write the task');
assert.equal(await field('next'), 'Step 3, Review the change');
// The workflow file holds no gate or inputs for a step; the panel says so once instead of inventing them.
assert.match(await b.evaluate(`document.querySelector('[data-wfg-detail]').textContent`), /records no gate or inputs/);
assert.equal(await b.evaluate(`document.querySelector('[data-wfg-step="2"]').getAttribute('aria-current')`), 'step');
assert.equal(await b.evaluate(`document.querySelectorAll('[data-wfg-graph] [aria-current]').length`), 1);
// The last step hands on to nothing; an unknown role is named in the panel too.
await b.evaluate(`document.querySelector('[data-wfg-step="4"]').dispatchEvent(new MouseEvent('click',{bubbles:true,cancelable:true}));true`);
await wait(`document.querySelector('[data-wfg-detail] [data-wfg-detail-title]')?.textContent === 'Approve the merge'`, 'detail for step 4');
assert.equal(await field('next'), 'Nothing: this is the last step');
assert.match(await field('roles'), /human \(a person\)/);
assert.match(await field('roles'), /merge-gate names no current role/);
// Keyboard: a step box takes focus and opens on Enter, and keeps focus through the repaint.
await b.evaluate(`document.querySelector('[data-wfg-step="1"]').focus();true`);
assert.equal(await b.evaluate(`document.activeElement.dataset.focusKey`), 'wfg-step:1');
await b.key('Enter');
await wait(`document.querySelector('[data-wfg-detail] [data-wfg-detail-title]')?.textContent === 'Write the task' && document.activeElement?.dataset.focusKey === 'wfg-step:1'`, 'Enter opens step 1 and keeps focus');
assert.equal(await b.evaluate(`location.hash`), '#/orgs/acme/workflows/change?step=1');
await b.evaluate(`document.querySelector('[data-wfg-step="4"]').focus();true`);
await b.key('Enter');
await wait(`document.querySelector('[data-wfg-detail] [data-wfg-detail-title]')?.textContent === 'Approve the merge'`, 'Enter opens step 4');
// A repaint (the poll) keeps the selection.
const before = reads.length;
await b.evaluate(`document.dispatchEvent(new CustomEvent('mosaic-workspace-render'));true`);
await wait(`document.querySelector('[data-wfg-detail] [data-wfg-detail-title]')?.textContent === 'Approve the merge'`, 'kept after repaint');
assert.ok(reads.length >= before);
// A project's workflow of the same name is its own graph.
await b.evaluate(`location.hash = '#/orgs/acme/workflows/change?project=site'`);
await wait(`${boxes}.length === 2`, 'project graph');
assert.match(await b.evaluate(`document.querySelector('#bus-view').textContent`), /Project site/);
// A name the scope does not hold says so.
await b.evaluate(`location.hash = '#/orgs/acme/workflows/nope'`);
await wait(`/No workflow named nope/.test(document.querySelector('#bus-view').textContent)`, 'unknown workflow');
});
test('a step with more roles than fit shows three lines, the last counting the rest, all inside its box', { timeout: 60000 }, async t => {
// The business-management pack's first step names four long roles.
const roles = ['chief-executive-officer', 'chief-financial-officer', 'chief-information-officer', 'chief-information-security-officer'];
DOCS.acme.workflows.brief = { steps: [{ title: 'Draft the brief', state: 'todo', roles }, { title: 'WWWWWWWWWWWWWWWWWWWWWWWW WWWWWWWWWWWWWWWWWWWWWWWW WWWWWWWWWWWWWWWWWWWW', state: 'done', roles: ['human'] }] };
t.after(() => { delete DOCS.acme.workflows.brief; });
for (const viewport of [[1440, 900], [390, 844]]) {
await t.test(String(viewport[0]), async t2 => {
const { b, wait } = await setup(t2, { path: '#/orgs/acme/workflows/brief', viewport });
await wait(`${boxes}.length === 2`, 'two boxes');
const one = `document.querySelector('[data-wfg-step="1"]')`;
// A role longer than the box is shortened with an ellipsis (the full name is in the box's title and the step panel).
const lines = await b.evaluate(`[...${one}.querySelectorAll('[data-wfg-roles] tspan')].map(t=>t.textContent)`);
assert.equal(lines.length, 3);
assert.equal(lines[2], '+2 more');
lines.slice(0, 2).forEach((line, i) => assert.ok(line === roles[i] || (line.endsWith('…') && roles[i].startsWith(line.slice(0, -1))), line));
if (viewport[0] === 390) assert.deepEqual(lines.slice(0, 2), roles.slice(0, 2), 'the 390 column is wide enough for the whole name');
assert.equal(await b.evaluate(`${one}.querySelector('[data-wfg-roles] [data-wfg-more]').textContent`), '+2 more');
// The box names every role for a pointer or a screen reader.
const named = await b.evaluate(`${one}.querySelector('title').textContent + ' ' + (${one}.getAttribute('aria-label') ?? '')`);
for (const r of roles) assert.ok(named.includes(r), r);
// Every text line ends inside its own box: its drawn extent, not what the clip path lets show, ends left
// of the clip edge (8px inside the box) and above the box bottom.
const out = await b.evaluate(`${boxes}.flatMap(g=>{const r=g.querySelector('rect'),right=r.x.baseVal.value+r.width.baseVal.value-8,bottom=r.y.baseVal.value+r.height.baseVal.value;
return [...g.querySelectorAll('text')].flatMap(t=>{const spans=[...t.querySelectorAll('tspan')];return (spans.length?spans:[t]).filter(e=>e.getNumberOfChars()>0).filter(e=>{let n=e.getNumberOfChars();while(n>1&&/\\s/.test(e.textContent[n-1]))n--;const z=e.getExtentOfChar(n-1),a=e.getExtentOfChar(0);return z.x+z.width>right+0.5||z.y+z.height>bottom+0.5||a.x<r.x.baseVal.value}).map(e=>e.textContent)})})`);
assert.deepEqual(out, []);
assert.equal(await b.evaluate(`document.documentElement.scrollWidth<=innerWidth`), true);
});
}
});
test('an organization with no workflows says so on its page and its card, and links to apply a pack', { timeout: 60000 }, async t => {
const { b, wait } = await setup(t, { path: '#/orgs/bare/workflows' });
await wait(`document.querySelector('[data-wfg-list] [data-wfg-none]')`, 'none note');
assert.match(await b.evaluate(`document.querySelector('[data-wfg-none]').textContent`), /No workflows in Bare Works yet/);
assert.equal(await b.evaluate(`document.querySelector('[data-wfg-list] [data-wfg-apply]').getAttribute('href')`), '#/orgs/bare/organization');
assert.equal(await b.evaluate(`document.querySelector('[data-wfg-list] [data-wfg-item]')`), null);
// The organization page's card says the same and links the list.
await b.evaluate(`location.hash = '#/orgs/bare/organization'`);
await wait(`document.querySelector('[data-org-dashboard] [data-wfg-card] [data-wfg-none]')`, 'card none');
assert.equal(await b.evaluate(`document.querySelector('[data-wfg-card] [data-wfg-all]').getAttribute('href')`), '#/orgs/bare/workflows');
// Apply a pack takes the person to the pack picker on the same page.
await wait(`document.querySelector('[data-packs]')`, 'packs section');
await b.evaluate(`document.querySelector('[data-wfg-card] [data-wfg-apply]').click();true`);
await wait(`(()=>{const r=document.querySelector('[data-packs]').getBoundingClientRect();return r.top>=0&&r.top<innerHeight})()`, 'packs in view');
assert.equal(await b.evaluate(`location.hash`), '#/orgs/bare/organization');
});
test('the organization page card lists each workflow as a link to its graph', { timeout: 60000 }, async t => {
const { b, wait, reads } = await setup(t, { path: '#/orgs/acme/organization' });
await wait(`document.querySelectorAll('[data-org-dashboard] [data-wfg-card] [data-wfg-card-item]').length === 3`, 'card items');
assert.deepEqual(await b.evaluate(`[...document.querySelectorAll('[data-wfg-card] [data-wfg-card-item]')].map(a=>[a.getAttribute('href'),a.textContent.replace(/\\s+/g,' ').trim()])`), [
['#/orgs/acme/workflows/change', 'change 4 steps'],
['#/orgs/acme/workflows/intake', `Intake ${XSS} 1 step`],
['#/orgs/acme/workflows/change?project=site', 'Site release 2 steps, project site'],
]);
assert.equal(await b.evaluate(`document.querySelector('[data-wfg-card] [data-wfg-all]').getAttribute('href')`), '#/orgs/acme/workflows');
assert.equal(await b.evaluate(`document.querySelectorAll('[data-org-dashboard] img').length`), 0);
assert.equal(await b.evaluate(`window.__sp9pwn===undefined`), true);
// The card reuses the page's one workflow read; it sends no second one.
assert.equal(reads.filter(id => id === 'acme').length, 1);
// SP17: under each workflow, each step links to its own form.
assert.deepEqual(await b.evaluate(`[...document.querySelectorAll('[data-wfg-card] .wfg-cardlist > li:first-child [data-wfg-card-step]')].map(a=>[a.getAttribute('href'),a.textContent,a.getAttribute('aria-label')])`), [
['#/orgs/acme/workflows/change?step=1', 'Write the task', 'Change step 1, Write the task'],
['#/orgs/acme/workflows/change?step=2', 'Implement the change', 'Change step 2, Implement the change'],
['#/orgs/acme/workflows/change?step=3', 'Review the change', 'Change step 3, Review the change'],
['#/orgs/acme/workflows/change?step=4', 'Approve the merge', 'Change step 4, Approve the merge'],
]);
assert.deepEqual(await b.evaluate(`[...document.querySelectorAll('[data-wfg-card] [data-wfg-card-step]')].map(a=>a.getAttribute('href')).filter(h=>/project=site/.test(h))`), ['#/orgs/acme/workflows/change?project=site&step=1', '#/orgs/acme/workflows/change?project=site&step=2']);
assert.equal(await b.evaluate(`document.documentElement.scrollWidth<=innerWidth`), true);
await b.evaluate(`document.querySelector('[data-wfg-card] [data-wfg-card-item]').click();true`);
await wait(`location.hash === '#/orgs/acme/workflows/change' && document.querySelectorAll('[data-wfg-graph] [data-wfg-step]').length === 4`, 'graph from card');
});
test('a stopped organization is not read: the page and card say the bus is stopped with the reason, never "no workflows"', { timeout: 60000 }, async t => {
const { b, wait, reads } = await setup(t, { path: '#/orgs/gamma/workflows' });
await wait(`document.querySelector('[data-wfg-list] [data-ws-unread]')`, 'unread note');
const note = await b.evaluate(`document.querySelector('[data-wfg-list] [data-ws-unread]').textContent`);
assert.match(note, /^Not read: Gamma Home/);
assert.match(note, /stopped by the owner/);
assert.doesNotMatch(await b.evaluate(`document.querySelector('#bus-view').textContent`), /No workflows/);
assert.equal(await b.evaluate(`document.querySelector('[data-wfg-none],[data-wfg-item]')`), null);
// The graph address says the same.
await b.evaluate(`location.hash = '#/orgs/gamma/workflows/change'`);
await wait(`document.querySelector('[data-wfg-page] [data-ws-unread]')`, 'graph unread note');
assert.equal(await b.evaluate(`document.querySelector('[data-wfg-graph]')`), null);
assert.doesNotMatch(await b.evaluate(`document.querySelector('#bus-view').textContent`), /No workflow/);
// The organization page card.
await b.evaluate(`location.hash = '#/orgs/gamma/organization'`);
await wait(`document.querySelector('[data-org-dashboard] [data-wfg-card][data-bus-stopped]')`, 'stopped card');
assert.match(await b.evaluate(`document.querySelector('[data-wfg-card]').textContent`), /Not read: the bus is stopped\./);
assert.equal(await b.evaluate(`document.querySelector('[data-wfg-card] [data-wfg-none]')`), null);
assert.equal(reads.filter(id => id === 'gamma').length, 0, 'a stopped organization is not read');
});
test('at 390 the graph runs top to bottom inside its box and the page does not scroll sideways', { timeout: 60000 }, async t => {
for (const mode of ['light', 'dark']) {
const dark = mode === 'dark';
await t.test(mode, async t2 => {
const { b, wait } = await setup(t2, { path: '#/orgs/acme/workflows/change?step=3', viewport: [390, 844], mode });
await wait(`${boxes}.length === 4 && document.querySelector('[data-wfg-detail-title]')`, 'graph at 390');
assert.equal(await b.evaluate(`document.documentElement.dataset.mode`), mode, 'the page shows the mode under test');
const ys = await b.evaluate(`${boxes}.map(g=>g.querySelector('rect').getBoundingClientRect().top)`);
assert.deepEqual([...ys].sort((p, q) => p - q), ys);
assert.equal(new Set(ys).size, 4);
assert.equal(await b.evaluate(`document.documentElement.scrollWidth<=innerWidth`), true, 'no sideways page scroll');
// Every box fits the width of its own box.
assert.equal(await b.evaluate(`(()=>{const w=document.querySelector('[data-wfg-graph]').closest('[data-wfg-frame]').getBoundingClientRect();return ${boxes}.every(g=>{const r=g.querySelector('rect').getBoundingClientRect();return r.left>=w.left-1&&r.right<=w.right+1})})()`), true);
// The box text and the edges read in the theme's colours, not the browser default black on dark.
const fill = await b.evaluate(`getComputedStyle(document.querySelector('[data-wfg-step="1"] [data-wfg-title]')).fill`);
const bg = await b.evaluate(`getComputedStyle(document.body).backgroundColor`);
assert.notEqual(fill, bg);
assert.notEqual(fill, dark ? 'rgb(0, 0, 0)' : 'rgb(255, 255, 255)');
// An SVG rect with no theme fill is drawn black.
assert.notEqual(await b.evaluate(`getComputedStyle(document.querySelector('[data-wfg-step="1"] rect')).fill`), 'rgb(0, 0, 0)');
// The list and the card fit too.
await b.evaluate(`location.hash = '#/orgs/acme/workflows'`);
await wait(`document.querySelector('[data-wfg-list] [data-wfg-item]')`, 'list at 390');
assert.equal(await b.evaluate(`document.documentElement.scrollWidth<=innerWidth`), true, 'list: no sideways page scroll');
});
}
});
// ---------- SP10: editing a workflow in its graph ----------
const REV_A = 'a'.repeat(64);
const titles = `${boxes}.map(g=>g.querySelector('[data-wfg-title]').textContent)`;
const press = sel => `(()=>{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 pick = step => `document.querySelector('[data-wfg-step="${step}"]').dispatchEvent(new MouseEvent('click',{bubbles:true,cancelable:true}))`;
// Sets a form field as typing would: the value, then the input event the page listens for.
const fill = (sel, value) => `(()=>{const e=document.querySelector(${JSON.stringify(sel)});e.value=${JSON.stringify(value)};e.dispatchEvent(new Event('input',{bubbles:true}));return true})()`;
const role = (form, name) => `${form} input[name="role"][value="${name}"]`;
const text = sel => `(document.querySelector(${JSON.stringify(sel)})?.textContent ?? '').replace(/\\s+/g,' ').trim()`;
const form = '[data-wfg-step-form]';
const saveOn = `(()=>{const e=document.querySelector('[data-wfg-save]');return !!e&&!e.disabled})()`;
test('a selected step is its edit form: a change draws in the graph, and Save sends one POST with the revision the page read', { timeout: 60000 }, async t => {
const { b, wait, st } = await setup(t, { path: '#/orgs/acme/workflows/change?step=4' });
await wait(`document.querySelector('[data-wfg-detail-title]')?.textContent === 'Approve the merge' && document.querySelector('${form}')`, 'step 4 with its form');
// Nothing pending: no Unsaved, no Discard, no Reset, and Save is there but off.
assert.equal(await b.evaluate(`document.querySelector('[data-wfg-unsaved],[data-wfg-discard]')`), null);
assert.equal(await b.evaluate(`document.querySelector('${form} [data-wfg-save]').disabled`), true);
assert.equal(await b.evaluate(`document.querySelector('${form} [data-wfg-cancel]').hidden`), true);
assert.equal(await b.evaluate(`document.querySelector('${form} input[name="title"]').value`), 'Approve the merge');
assert.equal(await b.evaluate(`document.querySelector('${form} select[name="state"]').value`), 'done');
// The picker offers human, the organization's roles and the templates; the step's unknown role is first, checked and marked.
assert.deepEqual(await b.evaluate(`[...document.querySelectorAll('${form} input[name="role"]')].map(i=>[i.value,i.checked])`), [['merge-gate', true], ['human', true], ['coder', false], ['pm', false], ['reviewer', false], ['writer', false]]);
assert.ok(await b.evaluate(`!!document.querySelector('${form} [data-wfg-pick-missing]')`));
await b.evaluate(fill(`${form} input[name="title"]`, 'Approve and merge'));
await b.evaluate(press(role(form, 'merge-gate')));
await b.evaluate(press(role(form, 'reviewer')));
// The graph draws the step as typed; nothing is written yet.
await wait(`${titles}[3] === 'Approve and merge' && document.querySelector('[data-wfg-unsaved]') && ${saveOn}`, 'pending change drawn');
assert.deepEqual(await b.evaluate(`[...document.querySelectorAll('[data-wfg-step="4"] [data-wfg-roles] tspan')].map(t=>t.textContent)`), ['human', 'reviewer']);
assert.equal(await b.evaluate(`document.querySelector('[data-wfg-step="4"] [data-wfg-missing]')`), null);
assert.equal(await b.evaluate(text('[data-wfg-unsaved]')), 'Unsaved');
assert.equal(await b.evaluate(`document.querySelector('${form} [data-wfg-cancel]').hidden`), false, 'Reset step shows once the step differs');
// The panel's Save is the only one; the bar does not repeat it.
assert.equal(await b.evaluate(`document.querySelectorAll('[data-wfg-save]').length`), 1);
assert.equal(st.writes.length, 0, 'typing writes nothing');
await b.evaluate(press('[data-wfg-save]'));
await wait(`/Saved change\\./.test(${text('[data-wfg-status]')}) && !document.querySelector('[data-wfg-unsaved]')`, 'saved');
assert.ok(await b.evaluate(`!!document.querySelector('${form} [data-wfg-status]')`), 'the status sits with the Save that was pressed');
assert.equal(st.writes.length, 1);
assert.deepEqual(st.writes[0], { id: 'acme', verb: 'workflow.set.console', args: { revision: REV_A, name: 'change', workflow: {
description: 'One change from task to merge.',
steps: [
{ title: 'Write the task', state: 'todo', roles: ['pm'] },
{ title: 'Implement the change', state: 'in-progress', roles: ['coder'] },
{ title: 'Review the change', state: 'in-review', roles: ['reviewer'] },
{ title: 'Approve and merge', state: 'done', roles: ['human', 'reviewer'] },
] } } });
await wait(`document.querySelector('[data-wfg-detail-title]')?.textContent === 'Approve and merge'`, 'detail after save');
assert.deepEqual(st.docs.acme.workflows.change.steps[3], { title: 'Approve and merge', state: 'done', roles: ['human', 'reviewer'] });
// Reset step puts a typed step back as the page holds it.
await b.evaluate(fill(`${form} input[name="title"]`, 'Oops'));
await wait(`${titles}[3] === 'Oops'`, 'typed');
await b.evaluate(press(`${form} [data-wfg-cancel]`));
await wait(`document.querySelector('${form} input[name="title"]')?.value === 'Approve and merge' && ${titles}[3] === 'Approve and merge' && !document.querySelector('[data-wfg-unsaved]')`, 'reset');
assert.equal(await b.evaluate(`window.MosaicWorkflows.hasDraft()`), false);
});
test('the workflow title and description change in the panel with no step selected, and save with the workflow', { timeout: 60000 }, async t => {
const { b, wait, st } = await setup(t, { path: '#/orgs/acme/workflows/change?project=site' });
await wait(`document.querySelector('[data-wfg-meta-form]')`, 'workflow form');
assert.equal(await b.evaluate(`document.querySelector('[data-wfg-meta-form] input[name="title"]').value`), 'Site release');
assert.equal(await b.evaluate(`document.querySelector('[data-wfg-meta-form] [data-wfg-save]').disabled`), true);
await b.evaluate(fill('[data-wfg-meta-form] input[name="title"]', 'Site launch'));
await b.evaluate(fill('[data-wfg-meta-form] textarea[name="description"]', 'From build to live.'));
await wait(`document.querySelector('[data-wfg-unsaved]') && ${saveOn}`, 'pending');
await b.evaluate(press('[data-wfg-meta-form] [data-wfg-save]'));
await wait(`/Saved change\\./.test(${text('[data-wfg-status]')})`, 'saved');
assert.deepEqual(st.writes[0].args, { revision: REV_A, name: 'change', project: 'site', workflow: { title: 'Site launch', description: 'From build to live.', steps: [
{ title: 'Build', state: 'in-progress', roles: ['coder'] }, { title: 'Ship', state: 'done', roles: ['human'] }] } });
await wait(`document.querySelector('#bus-view h1')?.textContent === 'Site launch'`, 'heading after save');
});
test('add, move, remove and discard change only the page until Save', { timeout: 60000 }, async t => {
const { b, wait, st } = await setup(t, { path: '#/orgs/acme/workflows/change?step=2' });
await wait(`document.querySelector('[data-wfg-detail-title]')?.textContent === 'Implement the change'`, 'step 2');
// Add after the selected step.
await b.evaluate(press('[data-wfg-add]'));
await wait(`document.querySelector('[data-wfg-form-title]')?.textContent === 'New step 3'`, 'add form');
await b.evaluate(fill('[data-wfg-add-form] input[name="title"]', 'Test the change'));
await b.evaluate(`(()=>{const e=document.querySelector('[data-wfg-add-form] select[name="state"]');e.value='in-review';e.dispatchEvent(new Event('input',{bubbles:true}));return true})()`);
// Add step with no role is refused in words, and the form stays.
await b.evaluate(press('[data-wfg-add-step]'));
await wait(`${text('[data-wfg-why]')} === 'Step 3 needs at least one role.'`, 'no role refused');
await b.evaluate(press(role('[data-wfg-add-form]', 'coder')));
await b.evaluate(press('[data-wfg-add-step]'));
await wait(`${boxes}.length === 5 && ${titles}[2] === 'Test the change'`, 'step added');
assert.equal(await b.evaluate(`location.hash`), '#/orgs/acme/workflows/change?step=3');
assert.equal(await b.evaluate(`document.querySelector('${form} select[name="state"]').value`), 'in-review');
assert.ok(await b.evaluate(`!!document.querySelector('[data-wfg-unsaved]')`));
// Move earlier and later.
await b.evaluate(press('[data-wfg-up]'));
await wait(`${titles}[1] === 'Test the change' && /step=2/.test(location.hash)`, 'moved earlier');
assert.equal(await b.evaluate(`document.activeElement.dataset.focusKey`), 'wfg-up');
await b.evaluate(press('[data-wfg-down]'));
await wait(`${titles}[2] === 'Test the change' && /step=3/.test(location.hash)`, 'moved later');
// Selecting another step goes through a full render (a read); the pending change stays.
await b.evaluate(pick(1));
await wait(`document.querySelector('[data-wfg-detail-title]')?.textContent === 'Write the task' && ${boxes}.length === 5`, 'step 1, change kept');
assert.equal(await b.evaluate(`document.querySelector('[data-wfg-up]').disabled`), true, 'the first step cannot move earlier');
await b.evaluate(press('[data-wfg-remove]'));
await wait(`${boxes}.length === 4`, 'step removed');
assert.deepEqual(await b.evaluate(titles), ['Implement the change', 'Test the change', 'Review the change', 'Approve the merge']);
// With no step selected, Add puts a step at the end.
await b.evaluate(`location.hash = '#/orgs/acme/workflows/change'`);
await wait(`${text('[data-wfg-add]')} === 'Add a step at the end'`, 'add at end');
await b.evaluate(press('[data-wfg-add]'));
await wait(`document.querySelector('[data-wfg-form-title]')?.textContent === 'New step 5'`, 'add-at-end form');
await b.evaluate(fill('[data-wfg-add-form] input[name="title"]', 'Announce it'));
await b.evaluate(press(role('[data-wfg-add-form]', 'pm')));
await b.evaluate(press('[data-wfg-add-step]'));
await wait(`${boxes}.length === 5 && ${titles}[4] === 'Announce it' && /step=5/.test(location.hash)`, 'added at end');
// Discard drops all of it; nothing was written.
await b.evaluate(press('[data-wfg-discard]'));
await wait(`${boxes}.length === 4 && !document.querySelector('[data-wfg-unsaved]')`, 'discarded');
assert.deepEqual(await b.evaluate(titles), ['Write the task', 'Implement the change', 'Review the change', 'Approve the merge']);
assert.equal(st.writes.length, 0);
});
test('a refused save says why next to Save and keeps the pending change', { timeout: 60000 }, async t => {
const { b, wait, st } = await setup(t, { path: '#/orgs/acme/workflows/change?step=1' });
await wait(`document.querySelector('${form}')`, 'step 1');
await b.evaluate(fill(`${form} input[name="title"]`, 'Write the brief'));
await wait(`${titles}[0] === 'Write the brief' && ${saveOn}`, 'pending');
// Step 4 still names merge-gate: the page refuses before sending.
await b.evaluate(press('[data-wfg-save]'));
await wait(`${text('[data-wfg-status]')} === 'Step 4: merge-gate names no current role. Use an organization role, a role template or human.'`, 'unknown role refused');
assert.equal(st.writes.length, 0);
assert.ok(await b.evaluate(`${titles}[0] === 'Write the brief' && !!document.querySelector('[data-wfg-unsaved]')`));
// Fix step 4, then the bus is down.
await b.evaluate(pick(4));
await wait(`document.querySelector('[data-wfg-detail-title]')?.textContent === 'Approve the merge' && document.querySelector('${form}')`, 'step 4');
assert.equal(await b.evaluate(`${titles}[0]`), 'Write the brief', 'the step 1 edit went into the workflow');
await b.evaluate(press(role(form, 'merge-gate')));
await wait(`!document.querySelector('[data-wfg-step="4"] [data-wfg-missing]')`, 'step 4 fixed');
st.refuse = 'no-bus-host';
await b.evaluate(press('[data-wfg-save]'));
await wait(`/bus is not running/.test(${text('[data-wfg-status]')})`, 'bus down refused');
assert.match(await b.evaluate(text('[data-wfg-status]')), /Your change is kept/);
assert.equal(st.writes.length, 1);
assert.equal(st.writes[0].args.revision, REV_A);
assert.deepEqual(st.writes[0].args.workflow.steps[3].roles, ['human']);
assert.ok(await b.evaluate(`${titles}[0] === 'Write the brief' && !!document.querySelector('[data-wfg-unsaved]') && ${saveOn}`));
assert.equal(st.docs.acme.workflows.change.steps[0].title, 'Write the task', 'nothing written');
// The status sits in the panel form with its Save.
assert.ok(await b.evaluate(`!!document.querySelector('${form} [data-wfg-status]') && !!document.querySelector('${form} [data-wfg-save]')`));
// Another writer changes this workflow: the save is refused as stale, the change kept.
st.change('acme', doc => { doc.workflows.change.description = 'Changed elsewhere.'; });
await b.evaluate(press('[data-wfg-save]'));
await wait(`/changed since this page read it/.test(${text('[data-wfg-status]')})`, 'stale refused');
assert.match(await b.evaluate(text('[data-wfg-status]')), /Your change is kept\. Discard to load the current version\./);
assert.ok(await b.evaluate(`${titles}[0] === 'Write the brief' && !!document.querySelector('[data-wfg-unsaved]')`));
assert.equal(st.writes.length, 2);
});
test('the poll does not draw over a pending change, takes a new revision when only other workflows changed, and says when this one changed', { timeout: 60000 }, async t => {
const { b, wait, st, reads } = await setup(t, { path: '#/orgs/acme/workflows/change?project=site&step=2' });
// The site project's workflow names only current roles, so a save goes to the bus.
await wait(`document.querySelector('${form}')`, 'step 2');
const repaint = async () => { const n = reads.length; await b.evaluate(`document.dispatchEvent(new CustomEvent('mosaic-workspace-render'));true`); await wait(`true`); for (let i = 0; i < 100 && reads.length === n; i++) await new Promise(r => setTimeout(r, 30)); await new Promise(r => setTimeout(r, 150)); };
// A form being typed in survives a repaint as typed, the same node.
await b.evaluate(fill(`${form} input[name="title"]`, 'Ship the release'));
await b.evaluate(`window.__form = document.querySelector('${form}'); true`);
await repaint();
assert.equal(await b.evaluate(`document.querySelector('${form} input[name="title"]')?.value`), 'Ship the release');
assert.equal(await b.evaluate(`document.querySelector('${form}') === window.__form`), true);
assert.equal(await b.evaluate(`${titles}[1]`), 'Ship the release', 'the poll keeps the pending change');
assert.ok(await b.evaluate(`!!document.querySelector('[data-wfg-unsaved]')`));
// Another workflow changes in the file: this one is as read, so the page takes the new revision and no warning shows.
st.change('acme', doc => { doc.workflows.intake.title = 'Intake, renamed'; });
const fresh = st.revs.acme;
await repaint();
assert.equal(await b.evaluate(`document.querySelector('[data-wfg-stale]')`), null);
assert.equal(await b.evaluate(`document.querySelector('${form} input[name="title"]')?.value`), 'Ship the release');
await b.evaluate(press('[data-wfg-save]'));
await wait(`/Saved change\\./.test(${text('[data-wfg-status]')})`, 'saved on the new revision');
assert.equal(st.writes.at(-1).args.revision, fresh);
assert.equal(st.docs.acme.workflows.intake.title, 'Intake, renamed', 'the other workflow is untouched');
// Now this workflow changes underneath a pending change: the page says so and offers Discard.
await b.evaluate(fill(`${form} input[name="title"]`, 'Build it'));
await wait(`${titles}[1] === 'Build it'`, 'pending again');
// A change elsewhere to the step being typed in: the page says so instead of taking the file silently.
st.change('acme', doc => { const w = doc.projects.site.workflows.change; w.description = 'Changed elsewhere.'; w.steps[1].state = 'in-review'; });
await repaint();
await wait(`!!document.querySelector('[data-wfg-stale]')`, 'stale note');
assert.match(await b.evaluate(text('[data-wfg-stale]')), /changed this workflow since the page read it.*Discard to load the current version/);
assert.equal(await b.evaluate(`document.querySelector('${form} input[name="title"]')?.value`), 'Build it');
assert.ok(await b.evaluate(`!!document.querySelector('[data-wfg-discard]')`));
await b.evaluate(press('[data-wfg-discard]'));
await wait(`${titles}[1] === 'Ship the release' && !document.querySelector('[data-wfg-stale],[data-wfg-unsaved]')`, 'discarded to current');
assert.match(await b.evaluate(`document.querySelector('#bus-view .sub').textContent`), /Changed elsewhere\./);
});
// SP17: changes step `step`'s title on the open graph page, so the change is pending and Save is on.
async function retitle(b, wait, step, title) {
await wait(`document.querySelector('${form}')?.dataset.wfgStepForm === '${step - 1}'`, 'step form');
await b.evaluate(fill(`${form} input[name="title"]`, title));
await wait(`${titles}[${step - 1}] === ${JSON.stringify(title)} && ${saveOn}`, 'pending change');
}
test('SP17: a Save in flight holds the poll, and the page shows the saved workflow from the answer', { timeout: 60000 }, async t => {
const { b, wait, st, reads } = await setup(t, { path: '#/orgs/acme/workflows/change?project=site&step=2' });
await wait(`${boxes}.length === 2 && document.querySelector('[data-wfg-detail]')`, 'site graph');
await retitle(b, wait, 2, 'Ship the release');
// The write is held, so the Save is in flight.
const openWrite = st.hold('write');
await b.evaluate(press('[data-wfg-save]'));
await wait(`document.querySelector('[data-wfg-save]')?.disabled === true`, 'save in flight');
// A poll begins while the Save is in flight: its read takes the file as it was before the write.
const openRead = st.hold('read'), n = reads.length;
await b.evaluate(`document.dispatchEvent(new CustomEvent('mosaic-workspace-render'));true`);
for (let i = 0; i < 200 && reads.length === n; i++) await new Promise(r => setTimeout(r, 20));
assert.ok(reads.length > n, 'the poll read began');
openWrite();
await wait(`/Saved change\\./.test(${text('[data-wfg-status]')})`, 'saved');
// The poll's read answers only now, with the file as it was before the Save.
openRead();
await new Promise(r => setTimeout(r, 800));
assert.deepEqual(await b.evaluate(titles), ['Build', 'Ship the release'], 'the step shows the saved title, not the read from before the Save');
assert.equal(await b.evaluate(`document.querySelector('[data-wfg-stale],[data-wfg-unsaved]')`), null);
assert.equal(st.writes.length, 1, 'one write');
// The page took the revision the write answered: the next Save is not refused as changed elsewhere.
const saved = st.revs.acme;
await retitle(b, wait, 2, 'Ship it');
await b.evaluate(press('[data-wfg-save]'));
await wait(`/Saved change\\./.test(${text('[data-wfg-status]')}) && !document.querySelector('[data-wfg-unsaved]')`, 'second save');
assert.equal(st.writes.length, 2);
assert.equal(st.writes[1].args.revision, saved);
assert.equal(st.docs.acme.projects.site.workflows.change.steps[1].title, 'Ship it');
});
test('a reader who cannot write sees no edit controls and one line saying who can', { timeout: 60000 }, async t => {
const { b, wait } = await setup(t, { path: '#/orgs/acme/workflows/change?step=2', writable: false });
await wait(`document.querySelector('[data-wfg-detail-title]')?.textContent === 'Implement the change'`, 'step 2');
assert.equal(await b.evaluate(`document.querySelector('[data-wfg-bar],[data-wfg-actions],[data-wfg-add],[data-wfg-save],[data-wfg-delete],[data-wfg-form],#bus-view input,#bus-view select,#bus-view textarea')`), null);
assert.equal(await b.evaluate(text('[data-wfg-field="roles"]')), 'coder', 'a reader sees the roles as text');
assert.deepEqual(await b.evaluate(`[...document.querySelectorAll('#bus-view [data-wfg-readonly]')].map(n=>n.textContent)`), ['Only the owner, on a Console started with send, can change workflows.']);
assert.equal(await b.evaluate(`document.querySelectorAll('#bus-view button').length`), 0);
await b.evaluate(`location.hash = '#/orgs/acme/workflows'`);
await wait(`document.querySelector('[data-wfg-list] [data-wfg-item]')`, 'list');
assert.equal(await b.evaluate(`document.querySelector('[data-wfg-new]')`), null);
assert.ok(await b.evaluate(`!!document.querySelector('[data-wfg-list] [data-wfg-readonly]')`));
});
test('a new workflow starts on the list page, opens in its graph unsaved, saves, and deletes after a confirm', { timeout: 60000 }, async t => {
const { b, wait, st } = await setup(t, { path: '#/orgs/acme/workflows' });
await wait(`document.querySelector('[data-wfg-new]')`, 'New workflow');
await b.evaluate(press('[data-wfg-new]'));
await wait(`document.querySelector('[data-wfg-newform]')`, 'new form');
assert.equal(await b.evaluate(`document.activeElement.dataset.focusKey`), 'wfg-n-name');
const create = async () => b.evaluate(press('[data-wfg-create]'));
await b.evaluate(fill('[data-wfg-newform] input[name="name"]', 'Release Plan'));
await create();
await wait(`${text('[data-wfg-newform] [data-wfg-why]')} === 'A workflow name is 1 to 64 lowercase letters, digits or dashes, starting with a letter.'`, 'name refused');
await b.evaluate(fill('[data-wfg-newform] input[name="name"]', 'change'));
await create();
await wait(`${text('[data-wfg-newform] [data-wfg-why]')} === 'A workflow named change already exists here. Edit it instead.'`, 'taken refused');
await b.evaluate(fill('[data-wfg-newform] input[name="name"]', 'release'));
await b.evaluate(fill('[data-wfg-newform] input[name="title"]', 'Cut the release'));
await create();
await wait(`${text('[data-wfg-newform] [data-wfg-why]')} === 'Step 1 needs at least one role.'`, 'role refused');
await b.evaluate(press(role('[data-wfg-newform]', 'human')));
await create();
await wait(`location.hash === '#/orgs/acme/workflows/release?step=1' && ${boxes}.length === 1`, 'opens in the graph');
assert.equal(await b.evaluate(text('[data-wfg-unsaved]')), 'Unsaved, not in the file yet');
assert.equal(await b.evaluate(`document.querySelector('[data-wfg-delete]')`), null, 'nothing to delete yet');
assert.equal(st.writes.length, 0);
// The list shows it, marked.
await b.evaluate(`location.hash = '#/orgs/acme/workflows'`);
await wait(`document.querySelector('[data-wfg-item="release"] [data-wfg-unsaved]')`, 'listed unsaved');
await b.evaluate(press('[data-wfg-item="release"] a[data-wfg-open]'));
await wait(`${boxes}.length === 1 && document.querySelector('[data-wfg-save]')`, 'back in the graph');
await b.evaluate(press('[data-wfg-save]'));
await wait(`/Saved release\\./.test(${text('[data-wfg-status]')}) && document.querySelector('[data-wfg-delete]')`, 'saved');
assert.deepEqual(st.writes[0].args, { revision: REV_A, name: 'release', workflow: { steps: [{ title: 'Cut the release', state: 'todo', roles: ['human'] }] } });
// Delete asks first; Keep keeps it.
await b.evaluate(press('[data-wfg-delete]'));
await wait(`document.querySelector('[data-wfg-confirm]')`, 'confirm');
assert.match(await b.evaluate(text('[data-wfg-confirm]')), /Delete release from the organization file\?/);
assert.equal(await b.evaluate(`document.activeElement.dataset.focusKey`), 'wfg-delete-no');
await b.evaluate(press('[data-wfg-delete-no]'));
await wait(`!document.querySelector('[data-wfg-confirm]') && document.activeElement?.dataset.focusKey === 'wfg-delete'`, 'kept');
assert.equal(st.writes.length, 1);
const saved = st.revs.acme;
await b.evaluate(press('[data-wfg-delete]'));
await wait(`document.querySelector('[data-wfg-delete-yes]')`, 'confirm again');
await b.evaluate(press('[data-wfg-delete-yes]'));
await wait(`location.hash === '#/orgs/acme/workflows' && /Deleted release\\./.test(${text('[data-wfg-list] [data-wfg-status]')}) && !document.querySelector('[data-wfg-item="release"]')`, 'deleted');
assert.deepEqual(st.writes[1].args, { revision: saved, name: 'release', workflow: null });
assert.equal(Object.hasOwn(st.docs.acme.workflows, 'release'), false);
});
test('the keyboard alone opens a step, edits it and saves', { timeout: 60000 }, async t => {
const { b, wait, st } = await setup(t, { path: '#/orgs/acme/workflows/change?project=site' });
await wait(`${boxes}.length === 2`, 'graph');
await b.evaluate(`document.querySelector('[data-wfg-step="1"]').focus();true`);
await b.key('Enter');
await wait(`document.querySelector('[data-wfg-detail-title]')?.textContent === 'Build' && document.activeElement?.dataset.focusKey === 'wfg-step:1'`, 'Enter opens step 1');
const tabTo = async (sel, what) => {
for (let i = 0; i < 30; i++) { if (await b.evaluate(`document.activeElement?.matches(${JSON.stringify(sel)}) ?? false`)) return; await b.key('Tab'); }
throw new Error(`Tab never reached ${what}`);
};
await tabTo(`${form} input[name="title"]`, 'the step title');
// Tab selects the field's text; the right arrow puts the caret after it (the harness sends no End key).
await b.key('ArrowRight');
await b.call('Input.insertText', { text: ' now' });
await tabTo(`${form} input[name="role"][value="reviewer"]`, 'the reviewer box');
await b.key(' ', 'Space');
await tabTo(`${form} [data-wfg-save]`, 'Save');
await wait(`${titles}[0] === 'Build now'`, 'typed into the graph');
await b.key('Enter');
await wait(`/Saved change\\./.test(${text('[data-wfg-status]')})`, 'saved');
assert.deepEqual(st.writes[0].args, { revision: REV_A, name: 'change', project: 'site', workflow: { title: 'Site release', steps: [
{ title: 'Build now', state: 'in-progress', roles: ['coder', 'reviewer'] }, { title: 'Ship', state: 'done', roles: ['human'] }] } });
// Enter in the title field is Save too, and Escape resets a typed step.
await wait(`document.querySelector('${form} input[name="title"]')?.value === 'Build now'`, 'form after save');
await b.evaluate(`(()=>{const e=document.querySelector('${form} input[name="title"]');e.focus();e.setSelectionRange(e.value.length,e.value.length);return true})()`);
await b.call('Input.insertText', { text: '!' });
await wait(`${titles}[0] === 'Build now!'`, 'typed');
await b.key('Escape');
await wait(`document.querySelector('${form} input[name="title"]')?.value === 'Build now' && !document.querySelector('[data-wfg-unsaved]')`, 'Escape reset the step');
await b.evaluate(`(()=>{const e=document.querySelector('${form} input[name="title"]');e.focus();e.setSelectionRange(e.value.length,e.value.length);return true})()`);
await b.call('Input.insertText', { text: ', fast' });
await b.key('Enter');
await wait(`${titles}[0] === 'Build now, fast' && !document.querySelector('[data-wfg-unsaved]')`, 'saved by Enter');
assert.equal(st.writes.length, 2);
assert.equal(st.writes[1].args.workflow.steps[0].title, 'Build now, fast');
});
test('switching organization with a pending workflow change asks first, through the existing draft guard', { timeout: 60000 }, async t => {
const { b, wait, st } = await setup(t, { path: '#/orgs/acme/workflows/change?project=site&step=1' });
await wait(`document.querySelector('${form}') && document.querySelector('#breadcrumbs [data-crumb-org]')`, 'step 1');
await b.evaluate(press('[data-wfg-remove]'));
await wait(`${boxes}.length === 1 && document.querySelector('[data-wfg-unsaved]')`, 'pending removal');
assert.equal(await b.evaluate(`window.MosaicWorkflows.hasDraft()`), true);
await b.evaluate(`location.hash = '#/orgs/bare/workflows'`);
await wait(`document.querySelector('dialog[aria-label="Unsaved draft"][open]')`, 'guard dialog');
await b.evaluate(press('dialog [data-stay]'));
await wait(`!document.querySelector('dialog[open]') && /#\\/orgs\\/acme\\/workflows\\/change/.test(location.hash) && ${boxes}.length === 1`, 'stayed with the change');
await b.evaluate(`location.hash = '#/orgs/bare/workflows'`);
await wait(`document.querySelector('dialog[aria-label="Unsaved draft"][open]')`, 'guard dialog again');
await b.evaluate(press('dialog [data-discard]'));
await wait(`location.hash === '#/orgs/bare/workflows' && document.querySelector('[data-wfg-list][data-wfg-org="bare"]')`, 'switched');
assert.equal(await b.evaluate(`window.MosaicWorkflows.hasDraft()`), false);
assert.equal(st.writes.length, 0);
});
test('a typed step alone counts as a pending change for the switch guard', { timeout: 60000 }, async t => {
const { b, wait, st } = await setup(t, { path: '#/orgs/acme/workflows/change?project=site&step=1' });
await wait(`document.querySelector('${form}') && document.querySelector('#breadcrumbs [data-crumb-org]')`, 'step 1');
await b.evaluate(fill(`${form} input[name="title"]`, 'Build it'));
await wait(`window.MosaicWorkflows.hasDraft()`, 'typed is a draft');
await b.evaluate(`location.hash = '#/orgs/bare/workflows'`);
await wait(`document.querySelector('dialog[aria-label="Unsaved draft"][open]')`, 'guard dialog');
await b.evaluate(press('dialog [data-stay]'));
await wait(`!document.querySelector('dialog[open]') && document.querySelector('${form} input[name="title"]')?.value === 'Build it'`, 'stayed with the typed title');
assert.equal(st.writes.length, 0);
});
test('a step edit goes into the workflow when another step is opened or a step moves, and Save sends it; one the save check refuses stays in its form', { timeout: 60000 }, async t => {
const { b, wait, st } = await setup(t, { path: '#/orgs/acme/workflows/change?project=site&step=2' });
await wait(`document.querySelector('[data-wfg-up]') && document.querySelector('${form}')`, 'step 2');
// A typed title, then Move earlier: the step moves with its new title.
await b.evaluate(fill(`${form} input[name="title"]`, 'Ship the release'));
await b.evaluate(press('[data-wfg-up]'));
await wait(`${titles}[0] === 'Ship the release' && /step=1/.test(location.hash) && ${saveOn}`, 'moved with the edit');
assert.equal(await b.evaluate(`document.querySelector('${form} input[name="title"]').value`), 'Ship the release');
// An edit the save check refuses (no role left) stays: opening another step does not happen, and the form says why.
await b.evaluate(press(role(form, 'human')));
await b.evaluate(pick(2));
await wait(`${text('[data-wfg-why]')} === 'Step 1 needs at least one role.'`, 'refused in the form');
assert.match(await b.evaluate(`location.hash`), /step=1/);
assert.equal(await b.evaluate(`document.activeElement?.dataset.focusKey`), 'wfg-f-title');
// Save says the same and sends nothing.
await b.evaluate(press('[data-wfg-save]'));
await wait(`${text('[data-wfg-status]')} === 'Not saved: Step 1 needs at least one role.'`, 'save refused');
assert.equal(st.writes.length, 0);
assert.equal(await b.evaluate(`document.querySelector('${form} input[name="title"]')?.value`), 'Ship the release', 'the typed title stays');
await b.evaluate(press(role(form, 'coder')));
await b.evaluate(press('[data-wfg-save]'));
await wait(`/Saved change\\./.test(${text('[data-wfg-status]')})`, 'saved');
assert.equal(st.writes.length, 1);
assert.deepEqual(st.writes[0].args.workflow.steps, [{ title: 'Ship the release', state: 'done', roles: ['coder'] }, { title: 'Build', state: 'in-progress', roles: ['coder'] }]);
});
test('Delete on a view another writer has changed is refused, not deleted over', { timeout: 60000 }, async t => {
const { b, wait, st, reads } = await setup(t, { path: '#/orgs/acme/workflows/change?project=site&step=1' });
await wait(`document.querySelector('[data-wfg-remove]')`, 'step 1');
await b.evaluate(press('[data-wfg-remove]'));
await wait(`${boxes}.length === 1 && document.querySelector('[data-wfg-unsaved]')`, 'pending removal');
// A change elsewhere to the step being typed in: the page says so instead of taking the file silently.
st.change('acme', doc => { const w = doc.projects.site.workflows.change; w.description = 'Changed elsewhere.'; w.steps[1].state = 'in-review'; });
const n = reads.length;
await b.evaluate(`document.dispatchEvent(new CustomEvent('mosaic-workspace-render'));true`);
await wait(`!!document.querySelector('[data-wfg-stale]')`, 'stale note');
assert.ok(reads.length > n);
await b.evaluate(press('[data-wfg-delete]'));
await wait(`document.querySelector('[data-wfg-delete-yes]')`, 'confirm');
await b.evaluate(press('[data-wfg-delete-yes]'));
await wait(`/^Not deleted/.test(${text('[data-wfg-status]')})`, 'delete refused');
assert.equal(await b.evaluate(text('[data-wfg-status]')), 'Not deleted: the organization file changed this workflow since the page read it. Discard to load the current version.');
assert.equal(st.writes.length, 1);
assert.equal(st.writes[0].args.revision, REV_A);
assert.equal(st.writes[0].args.workflow, null);
assert.equal(st.docs.acme.projects.site.workflows.change.description, 'Changed elsewhere.', 'the newer workflow is still in the file');
assert.ok(await b.evaluate(`${boxes}.length === 1 && !!document.querySelector('[data-wfg-unsaved]')`), 'the pending change stays');
// With no pending change there is no Discard, so the refusal does not point at one.
await b.evaluate(press('[data-wfg-discard]'));
await wait(`${boxes}.length === 2 && !document.querySelector('[data-wfg-unsaved]')`, 'discarded');
await b.evaluate(press('[data-wfg-delete]'));
await wait(`document.querySelector('[data-wfg-delete-yes]')`, 'confirm again');
st.change('acme', doc => { doc.projects.site.workflows.change.description = 'Changed again.'; });
await b.evaluate(press('[data-wfg-delete-yes]'));
await wait(`/^Not deleted/.test(${text('[data-wfg-status]')})`, 'second delete refused');
assert.equal(await b.evaluate(text('[data-wfg-status]')), 'Not deleted: the organization file changed this workflow since the page read it. The page shows the current version at its next read.');
assert.equal(await b.evaluate(`document.querySelector('[data-wfg-discard]')`), null);
assert.equal(st.docs.acme.projects.site.workflows.change.description, 'Changed again.');
});
test('a failed read says a pending change is kept, and it comes back when the read works', { timeout: 60000 }, async t => {
const { b, wait, st } = await setup(t, { path: '#/orgs/acme/workflows/change?project=site&step=1' });
await wait(`document.querySelector('[data-wfg-remove]')`, 'step 1');
await b.evaluate(press('[data-wfg-remove]'));
await wait(`${boxes}.length === 1 && document.querySelector('[data-wfg-unsaved]')`, 'pending removal');
st.failRead = true;
await b.evaluate(`document.dispatchEvent(new CustomEvent('mosaic-workspace-render'));true`);
await wait(`!!document.querySelector('[data-wfg-error]') && !!document.querySelector('[data-wfg-held]')`, 'error with kept note');
// SP17: the page keeps drawing the last read with the change on it, and says the read failed.
assert.equal(await b.evaluate(text('[data-wfg-error]')), 'The last workflows read failed: The organization file could not be read. The page shows the read before it.');
assert.equal(await b.evaluate(text('[data-wfg-held]')), 'Unsaved change to change is kept on this page.');
assert.equal(await b.evaluate(`${boxes}.length`), 1, 'the pending removal is still drawn');
assert.equal(await b.evaluate(`window.MosaicWorkflows.hasDraft()`), true);
st.failRead = false;
await b.evaluate(`document.dispatchEvent(new CustomEvent('mosaic-workspace-render'));true`);
await wait(`${boxes}.length === 1 && !!document.querySelector('[data-wfg-unsaved]') && !document.querySelector('[data-wfg-held],[data-wfg-error]')`, 'back');
assert.equal(st.writes.length, 0);
});
// SP17 item 1: changing one field of one step of a saved workflow and saving it, from another organization's
// page: the organization in the sidebar, the step on its Workflows card, Save. Typing is not a click. Each
// click is a real pointer press at the element's centre, counted by the page. At 390 the sidebar sits behind
// the menu button, which is one more click; from the organization's own page it is two clicks at any width.
test('SP17: one field of one step changes and saves in three clicks', { timeout: 90000 }, async t => {
const cases = [
{ name: '1440 from another organization', viewport: [1440, 900], from: 'bare', want: [/Acme Studio/, 'Ship', 'Save'] },
{ name: '390 from another organization', viewport: [390, 844], from: 'bare', want: ['Open menu', /Acme Studio/, 'Ship', 'Save'] },
{ name: '390 from the organization page', viewport: [390, 844], from: 'acme', want: ['Ship', 'Save'] },
];
for (const c of cases) {
await t.test(c.name, async t2 => {
const { b, wait, st } = await setup(t2, { path: `#/orgs/${c.from}/organization`, viewport: c.viewport });
await wait(`document.querySelector('[data-org-dashboard] [data-wfg-card]')`, 'organization page');
await b.evaluate(`window.__clicks = []; addEventListener('click', e => { if (e.isTrusted) { const a = e.target.closest('a,button'); __clicks.push(a?.getAttribute('aria-label') === 'Open menu' ? 'Open menu' : a?.textContent.trim() ?? e.target.tagName); } }, true); true`);
const click = async (sel, what) => {
await wait(`(()=>{const e=document.querySelector(${JSON.stringify(sel)});return !!e && !e.disabled && e.getClientRects().length>0})()`, what);
const { x, y } = await b.evaluate(`(()=>{const e=document.querySelector(${JSON.stringify(sel)});e.scrollIntoView({block:'center'});const r=e.getBoundingClientRect();return {x:r.left+r.width/2,y:r.top+r.height/2}})()`);
// What is under the pointer is the element meant, not something drawn over it.
assert.ok(await b.evaluate(`(()=>{const e=document.querySelector(${JSON.stringify(sel)}),h=document.elementFromPoint(${x},${y});return !!h&&(e===h||e.contains(h))})()`), `${what} is under the pointer`);
for (const type of ['mousePressed', 'mouseReleased']) await b.call('Input.dispatchMouseEvent', { type, x, y, button: 'left', clickCount: 1 });
};
if (c.from !== 'acme') {
if (c.viewport[0] === 390) {
await click('#nav-open', 'the menu button');
await wait(`document.body.classList.contains('nav-open')`, 'menu open');
await new Promise(r => setTimeout(r, 400));
}
await click('[data-workspace-org="acme"] a', 'Acme in the sidebar');
await wait(`location.hash === '#/orgs/acme/organization' && document.querySelector('[data-org-dashboard] [data-wfg-card] [data-wfg-card-step]') && !document.body.classList.contains('nav-open')`, 'acme page with its card');
await new Promise(r => setTimeout(r, 400));
}
await click('[data-wfg-card] [data-wfg-card-step][href="#/orgs/acme/workflows/change?project=site&step=2"]', 'step 2 on the card');
// The step's title field has focus, the caret at the end, ready to type.
await wait(`document.activeElement?.matches('[data-wfg-step-form] input[name="title"]') && document.activeElement.value === 'Ship'`, 'focus in the title');
assert.equal(await b.evaluate(`document.activeElement.selectionStart`), 4);
// The title and Save are both on screen without a scroll (SP17 shots: at 390 Save was below the fold).
const inView = sel => `(()=>{const r=document.querySelector(${JSON.stringify(sel)}).getBoundingClientRect();return r.top>=0&&r.bottom<=innerHeight})()`;
assert.equal(await b.evaluate(inView('[data-wfg-step-form] input[name="title"]')), true, 'title in view');
assert.equal(await b.evaluate(inView('[data-wfg-step-form] [data-wfg-save]')), true, 'Save in view');
assert.match(await b.evaluate(`document.getElementById('announce')?.textContent ?? ''`), /Step 2, Ship\. Change it and press Save\./);
await b.call('Input.insertText', { text: ' the release' });
await click('[data-wfg-step-form] [data-wfg-save]', 'Save');
await wait(`/Saved change\\./.test(${text('[data-wfg-status]')}) && !document.querySelector('[data-wfg-unsaved]')`, 'saved');
// Focus stays in the step's title, and the line saying it saved is on screen.
await wait(`document.activeElement?.dataset.focusKey === 'wfg-f-title'`, 'focus kept in the form');
assert.equal(await b.evaluate(inView('[data-wfg-step-form] [data-wfg-status]')), true, 'Saved line in view');
const clicks = await b.evaluate(`__clicks`);
assert.equal(clicks.length, c.want.length, `${c.want.length} clicks: ${JSON.stringify(clicks)}`);
c.want.forEach((w, i) => (w instanceof RegExp ? assert.match(clicks[i], w) : assert.equal(clicks[i], w)));
assert.equal(st.writes.length, 1);
assert.deepEqual(st.writes[0].args, { revision: REV_A, name: 'change', project: 'site', workflow: { title: 'Site release', steps: [
{ title: 'Build', state: 'in-progress', roles: ['coder'] }, { title: 'Ship the release', state: 'done', roles: ['human'] }] } });
});
}
});
// SP17 item 4: an edit open when the organization's bus restarts is not lost without a word. The list is
// read again (as the bus banner and Restart bus do) and the page drawn again (as the poll does).
const relist = `(async()=>{await window.MosaicOrg.refreshList();document.dispatchEvent(new CustomEvent('mosaic-workspace-render'));return true})()`;
const heldLinks = `[...document.querySelectorAll('[data-wfg-card] [data-wfg-held] [data-wfg-held-link]')].map(a=>[a.getAttribute('href'),a.textContent])`;
test('SP17: a step edit open while the bus restarts, then stops, is kept and named, and saves when the bus is back', { timeout: 90000 }, async t => {
const { b, wait, st } = await setup(t, { path: '#/orgs/acme/workflows/change?project=site&step=2' });
await retitle(b, wait, 2, 'Ship it');
// Restarting: the bus does not answer, the file still reads. The form stays as typed; Save says why it did not save.
st.down.acme = 'restarting';
await b.evaluate(relist);
await b.evaluate(`new Promise(r=>setTimeout(()=>r(true),300))`);
assert.equal(await b.evaluate(`document.querySelector('${form} input[name="title"]')?.value`), 'Ship it');
await b.evaluate(press('[data-wfg-save]'));
await wait(`/the organization bus is not running/.test(document.querySelector('[data-wfg-status]')?.textContent ?? '')`, 'no bus host');
assert.match(await b.evaluate(`document.body.innerText`), /Your change is kept/);
assert.equal(await b.evaluate(`document.querySelector('${form} input[name="title"]')?.value`), 'Ship it');
// Stopped: the page does not read the organization. It says the change is kept, on this page and on the
// organization's card, which links back to it.
st.down.acme = 'stopped';
await b.evaluate(relist);
await wait(`!document.querySelector('${form}') && !!document.querySelector('[data-wfg-held]')`, 'kept note while stopped');
assert.equal(await b.evaluate(text('[data-wfg-held]')), 'Unsaved change to change is kept on this page. It comes back when the workflows can be read.');
assert.equal(await b.evaluate(`window.MosaicWorkflows.hasDraft()`), true);
await b.evaluate(`location.hash = '#/orgs/acme/organization';true`);
await wait(`!!document.querySelector('[data-wfg-card][data-bus-stopped] [data-wfg-held]')`, 'card kept note while stopped');
assert.deepEqual(await b.evaluate(heldLinks), [['#/orgs/acme/workflows/change?project=site', 'change (project site)']]);
// Back: the card still names it; its link opens the graph with the step as typed, and Save writes it.
delete st.down.acme;
await b.evaluate(relist);
await wait(`!document.querySelector('[data-wfg-card][data-bus-stopped]') && !!document.querySelector('[data-wfg-card] [data-wfg-held-link]')`, 'card back with kept note');
await b.evaluate(`document.querySelector('[data-wfg-card] [data-wfg-held-link]').click();true`);
await wait(`location.hash === '#/orgs/acme/workflows/change?project=site' && ${titles}[1] === 'Ship it' && ${saveOn}`, 'kept change drawn');
const before = st.writes.length;
await b.evaluate(press('[data-wfg-save]'));
await wait(`!document.querySelector('[data-wfg-unsaved]') && !${saveOn}`, 'saved');
assert.equal(st.writes.length, before + 1);
assert.equal(st.writes.at(-1).args.revision, REV_A);
assert.equal(st.writes.at(-1).args.workflow.steps[1].title, 'Ship it');
assert.equal(st.docs.acme.projects.site.workflows.change.steps[1].title, 'Ship it');
assert.equal(await b.evaluate(`window.MosaicWorkflows.hasDraft()`), false);
await b.evaluate(`location.hash = '#/orgs/acme/organization';true`);
await wait(`!!document.querySelector('[data-wfg-card] [data-wfg-card-item]')`, 'card');
assert.equal(await b.evaluate(`!!document.querySelector('[data-wfg-card] [data-wfg-held]')`), false, 'nothing is held once saved');
});
test('SP17: Restart bus on the organization page with a step edit open keeps it, names it on the card, and it saves after', { timeout: 90000 }, async t => {
const restarts = [];
const admin = st => createOrgAdmin({
service: { showOrg: async (ctx, id) => ({ id, revision: 1, doc: {} }) }, registry: { listOrgs: async () => [] }, context: async () => ({ configDir: '/cfg', dataRoot: '/data', instance: 'test' }),
bus: {
busStatus: async (ctx, id) => ({ org: id, healthy: !st.down[id], pid: st.down[id] ? null : 4242 }),
// The restart takes the bus down, and it comes back before the answer, as a real restart does.
busRestart: async (ctx, id) => { restarts.push(id); st.down[id] = 'restarting'; await new Promise(r => setTimeout(r, 200)); delete st.down[id]; return { org: id, pid: 4343, socket: '/data/s', root: `/data/org-runtime/${id}` }; },
busStart: async () => { throw Error('not reached: the bus runs'); },
},
});
const { b, wait, st } = await setup(t, { path: '#/orgs/acme/workflows/change?project=site&step=2', admin });
await retitle(b, wait, 2, 'Ship it');
await b.evaluate(`location.hash = '#/orgs/acme/organization';true`);
await wait(`!!document.querySelector('[data-org-dashboard] [data-bus-restart]') && !!document.querySelector('[data-wfg-card] [data-wfg-held-link]')`, 'restart button and kept note');
assert.deepEqual(await b.evaluate(heldLinks), [['#/orgs/acme/workflows/change?project=site', 'change (project site)']]);
await b.evaluate(press('[data-org-dashboard] [data-bus-restart]'));
await wait(`/It answers/.test(document.querySelector('[data-org-dashboard] [data-bus-restart-receipt]')?.textContent || '')`, 'restart receipt');
assert.deepEqual(restarts, ['acme']);
await b.evaluate(relist);
await wait(`!!document.querySelector('[data-wfg-card] [data-wfg-held-link]')`, 'kept note after the restart');
assert.deepEqual(await b.evaluate(heldLinks), [['#/orgs/acme/workflows/change?project=site', 'change (project site)']]);
assert.equal(await b.evaluate(`window.MosaicWorkflows.hasDraft()`), true);
await b.evaluate(`document.querySelector('[data-wfg-card] [data-wfg-held-link]').click();true`);
await wait(`location.hash === '#/orgs/acme/workflows/change?project=site' && ${titles}[1] === 'Ship it' && ${saveOn}`, 'kept change drawn');
await b.evaluate(press('[data-wfg-save]'));
await wait(`!document.querySelector('[data-wfg-unsaved]') && !${saveOn}`, 'saved');
assert.equal(st.writes.length, 1);
assert.equal(st.writes[0].args.revision, REV_A);
assert.equal(st.docs.acme.projects.site.workflows.change.steps[1].title, 'Ship it');
});
// SP17 review P2-1: an open edit of one step is not dropped when another step is opened from the card or the
// address and typed in. Both edits go in the one Save.
test('SP17: an edit of step 1 is kept when step 2 is opened from the card or the address and changed, and Save sends both', { timeout: 90000 }, async t => {
const { b, wait, st } = await setup(t, { path: '#/orgs/acme/workflows/change?project=site&step=1' });
await retitle(b, wait, 1, 'Build it');
// From the card: the card says the change is kept; its step link opens step 2.
await b.evaluate(`location.hash = '#/orgs/acme/organization';true`);
await wait(`!!document.querySelector('[data-wfg-card] [data-wfg-held-link]')`, 'card kept note');
await b.evaluate(`document.querySelector('[data-wfg-card] a[data-wfg-card-step][href="#/orgs/acme/workflows/change?project=site&step=2"]').click();true`);
await wait(`document.querySelector('${form}')?.dataset.wfgStepForm === '1'`, 'step 2 form');
assert.deepEqual(await b.evaluate(titles), ['Build it', 'Ship']);
await retitle(b, wait, 2, 'Ship it');
assert.deepEqual(await b.evaluate(titles), ['Build it', 'Ship it'], 'typing in step 2 keeps step 1 as changed');
await b.evaluate(press('[data-wfg-save]'));
await wait(`/Saved change\\./.test(${text('[data-wfg-status]')})`, 'saved');
assert.deepEqual(st.writes.at(-1).args.workflow.steps.map(s => s.title), ['Build it', 'Ship it']);
// From the address: step 1 changed, then the address names step 2.
await b.evaluate(`location.hash = '#/orgs/acme/workflows/change?project=site&step=1';true`);
await retitle(b, wait, 1, 'Build again');
await b.evaluate(`location.hash = '#/orgs/acme/workflows/change?project=site&step=2';true`);
await wait(`document.querySelector('${form}')?.dataset.wfgStepForm === '1'`, 'step 2 form from the address');
await retitle(b, wait, 2, 'Ship again');
assert.deepEqual(await b.evaluate(titles), ['Build again', 'Ship again']);
// Reset step on step 2 puts back step 2 only.
await b.evaluate(press('[data-wfg-cancel]'));
await wait(`${titles}[1] === 'Ship it'`, 'step 2 reset');
assert.deepEqual(await b.evaluate(titles), ['Build again', 'Ship it'], 'Reset step on step 2 leaves step 1 changed');
await b.evaluate(press('[data-wfg-save]'));
await wait(`/Saved change\\./.test(${text('[data-wfg-status]')})`, 'second save');
assert.deepEqual(st.writes.at(-1).args.workflow.steps.map(s => s.title), ['Build again', 'Ship it']);
});
// SP17 review P2-2: nothing typed while a Save is in flight is dropped: the form's fields take no typing until
// the write answers.
test('SP17: while a Save is in flight the form takes no typing, and takes it again after', { timeout: 60000 }, async t => {
const { b, wait, st } = await setup(t, { path: '#/orgs/acme/workflows/change?project=site&step=2' });
await retitle(b, wait, 2, 'Ship it');
const fields = `[...document.querySelectorAll('${form} input[name="title"], ${form} select, ${form} [data-wfg-roles-pick]')].map(e=>e.disabled)`;
const openWrite = st.hold('write');
await b.evaluate(press('[data-wfg-save]'));
await wait(`document.querySelector('[data-wfg-save]')?.disabled === true`, 'save in flight');
assert.deepEqual(await b.evaluate(fields), [true, true, true], 'title, state and roles are off while saving');
// A poll redraw during the write keeps them off.
await b.evaluate(`document.dispatchEvent(new CustomEvent('mosaic-workspace-render'));true`);
await b.evaluate(`new Promise(r=>setTimeout(()=>r(true),300))`);
assert.deepEqual(await b.evaluate(fields), [true, true, true], 'still off after a redraw');
openWrite();
await wait(`/Saved change\\./.test(${text('[data-wfg-status]')})`, 'saved');
assert.deepEqual(await b.evaluate(fields), [false, false, false], 'on again once saved');
assert.equal(st.writes.at(-1).args.workflow.steps[1].title, 'Ship it');
// A refused Save gives the fields back too.
await retitle(b, wait, 2, 'Ship again');
st.down.acme = 'restarting';
await b.evaluate(press('[data-wfg-save]'));
await wait(`/the organization bus is not running/.test(document.querySelector('[data-wfg-status]')?.textContent ?? '')`, 'refused');
assert.deepEqual(await b.evaluate(fields), [false, false, false], 'on again after a refusal');
assert.equal(await b.evaluate(`document.querySelector('${form} input[name="title"]').value`), 'Ship again');
});
// SP17 review 2 P2-1: a new step typed in the add form is not dropped by the bar's Save: Save says it is not
// added yet and keeps the form; once added, Save sends it.
test('SP17: the bar Save with a new step typed in the add form says so and keeps it, and saves it once added', { timeout: 60000 }, async t => {
const { b, wait, st } = await setup(t, { path: '#/orgs/acme/workflows/change?project=site&step=1' });
await retitle(b, wait, 1, 'Build it');
await b.evaluate(press('[data-wfg-add]'));
await wait(`!!document.querySelector('[data-wfg-add-form]')`, 'add form');
await b.evaluate(fill('[data-wfg-add-form] input[name="title"]', 'Check'));
await b.evaluate(`document.querySelector('[data-wfg-add-form] input[name="role"][value="human"]').click();true`);
await b.evaluate(press('[data-wfg-bar] [data-wfg-save]'));
await wait(`/new step 2 is not added yet/.test(${text('[data-wfg-bar] [data-wfg-status]')})`, 'refused in words');
assert.equal(st.writes.length, 0, 'nothing written');
assert.equal(await b.evaluate(`document.querySelector('[data-wfg-add-form] input[name="title"]')?.value`), 'Check', 'the typed step is kept');
assert.equal(await b.evaluate(`document.activeElement === document.querySelector('[data-wfg-add-form] input[name="title"]')`), true, 'focus in the new step');
assert.equal(await b.evaluate(`window.MosaicWorkflows.hasDraft()`), true);
await b.evaluate(press('[data-wfg-add-step]'));
await wait(`${titles}.length === 3`, 'step added');
await b.evaluate(press('[data-wfg-save]'));
await wait(`/Saved change\\./.test(${text('[data-wfg-status]')})`, 'saved');
assert.deepEqual(st.writes.at(-1).args.workflow.steps.map(s => [s.title, s.roles]), [['Build it', ['coder']], ['Check', ['human']], ['Ship', ['human']]]);
});