Keep a pack preview or receipt that overlaps a packs reload #2029

Merged
jason.woltje merged 1 commits from packs-flake into refactor 2026-11-21 03:36:03 +00:00
2 changed files with 121 additions and 13 deletions

No files matched your search

+17 -9
View File
@@ -62,21 +62,29 @@
}
// E2E6 P1-1, P2-2: a poll reloads the list but keeps the open preview, the project picked for each
// pack and the last receipt. A kept preview applies by its digest, so a stale one is refused by the bus.
// Each organization has one state object for the life of the page. A click keeps a reference to it
// across its own request, so a load that ends meanwhile updates the read fields in place and never
// swaps the object: the click's preview or receipt lands where the next draw reads it. A load that
// began before the last applied one carries older reads, so its answer is dropped.
async function load(org) {
const prev = states.get(org);
const s = { packs: null, applied: [], writable: false, error: null, projects: null, preview: prev?.preview ?? null, status: prev?.status ?? '', picks: prev?.picks ?? {} };
if (!states.has(org)) states.set(org, { packs: null, applied: [], writable: false, error: 'Starter packs could not be read.', projects: null, preview: null, status: '', picks: {}, began: 0, shown: 0 });
const s = states.get(org);
const began = ++s.began;
const read = { packs: null, applied: [], writable: false, error: null, projects: null };
try {
const value = await call(`${base(org)}/packs`);
s.packs = Array.isArray(value?.packs) ? value.packs.filter(p => p && typeof p === 'object' && p.pack && typeof p.pack === 'object') : [];
s.applied = Array.isArray(value?.applied) ? value.applied.filter(a => plainObj(a) && typeof a.id === 'string' && Array.isArray(a.hired) && Array.isArray(a.open)) : [];
s.writable = value?.writable === true;
} catch (error) { s.error = error.message; }
read.packs = Array.isArray(value?.packs) ? value.packs.filter(p => p && typeof p === 'object' && p.pack && typeof p.pack === 'object') : [];
read.applied = Array.isArray(value?.applied) ? value.applied.filter(a => plainObj(a) && typeof a.id === 'string' && Array.isArray(a.hired) && Array.isArray(a.open)) : [];
read.writable = value?.writable === true;
} catch (error) { read.error = error.message; }
// A project pack needs a code project. The workflow read lists them; when it fails, a project pack
// says so instead of guessing.
if (s.packs?.some(p => p.pack.scope === 'project')) {
try { const w = await call(`${base(org)}/workflows`); s.projects = w && typeof w.projects === 'object' ? Object.keys(w.projects).sort() : null; } catch { s.projects = null; }
if (read.packs?.some(p => p.pack.scope === 'project')) {
try { const w = await call(`${base(org)}/workflows`); read.projects = w && typeof w.projects === 'object' ? Object.keys(w.projects).sort() : null; } catch { read.projects = null; }
}
states.set(org, s);
if (began < s.shown) return;
s.shown = began;
Object.assign(s, read);
}
const markText = m => MARKS[m] ?? m;
function itemRow(item) {
@@ -35,7 +35,23 @@ const PREVIEW = {
skills: { notEnabled: ['lint-fix'] }, chief: true, plan: {}, digest: REV('d'),
};
async function fixture(t, { send = true } = {}) {
// hold(verb) makes the next read of that verb wait: its answer is computed when the read arrives, then
// held until release(). arrived resolves when the read has arrived.
function holds() {
const pending = new Map();
return {
hold(verb) {
const h = {};
h.arrived = new Promise(r => { h.reached = r; });
h.wait = new Promise(r => { h.release = r; });
pending.set(verb, [...(pending.get(verb) ?? []), h]);
return h;
},
take: verb => pending.get(verb)?.shift(),
};
}
async function fixture(t, { send = true, gates = null, packs = () => PACKS } = {}) {
const verbs = [];
let flows = {
revision: REV('a'),
@@ -48,13 +64,18 @@ async function fixture(t, { send = true } = {}) {
},
templates: ['coder', 'reviewer'],
};
const read = async (verb, args) => {
verbs.push([verb, args]);
if (verb === 'packs.console') return { packs: PACKS };
const answer = (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 read = async (verb, args) => {
verbs.push([verb, args]);
const value = answer(verb, args), held = gates?.take(verb);
if (held) { held.reached(); await held.wait; }
return value;
};
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' }] };
@@ -128,6 +149,85 @@ test('SP6 pack picker: list, preview with marks, flagged, credential and noHire
assert.deepEqual(verbs.filter(([v]) => v === 'pack.preview.console').at(-1)[1], { pack: 'web-project', project: 'site' });
});
// The page reads the packs again on a poll and after any write that changes the organization
// (mosaic-org-changed). A preview the owner asked for must survive a reload that overlaps it, in either
// order, and an older reload that answers last must not put its older list back.
const reload = b => b.evaluate(`document.dispatchEvent(new CustomEvent('mosaic-org-changed',{detail:{org:'acme'}}));true`);
const markDrawn = b => b.evaluate(`document.querySelector('[data-packs]').dataset.drawn='before';true`);
const redrawn = (b, what) => waitFor(b, `document.querySelector('[data-packs]') && !document.querySelector('[data-packs]').dataset.drawn`, what);
test('SP6 pack picker: a preview that overlaps a reload of the packs is kept, in either order', { timeout: 60000 }, async t => {
const gates = holds();
const { b, verbs } = await fixture(t, { gates });
await waitFor(b, `document.querySelector('[data-pack="software-team"] [data-pack-preview-button]')`, 'pack list');
// The reload's packs read starts first and answers after the preview has been drawn.
const list = gates.hold('packs.console');
await reload(b);
await list.arrived;
await b.evaluate(`document.querySelector('[data-pack="software-team"] [data-pack-preview-button]').click();true`);
await waitFor(b, `document.querySelector('[data-pack-preview="software-team"]')`, 'preview');
await markDrawn(b);
list.release();
await redrawn(b, 'page drawn again after the reload');
assert.ok(await b.evaluate(`!!document.querySelector('[data-pack-preview="software-team"]')`), 'the reload kept the preview');
// The preview's read starts first and answers after the reload has drawn the page.
const preview = gates.hold('pack.preview.console');
await b.evaluate(`document.querySelector('[data-pack="web-project"] [data-pack-preview-button]').click();true`);
await preview.arrived;
await markDrawn(b);
await reload(b);
await redrawn(b, 'page drawn again during the preview');
preview.release();
await waitFor(b, `/for project site/.test(document.querySelector('[data-pack-preview] h4')?.textContent??'')`, 'project preview after the reload');
assert.deepEqual(verbs.filter(([v]) => v === 'pack.preview.console').map(([, a]) => a), [{ pack: 'software-team' }, { pack: 'web-project', project: 'site' }]);
});
// Count each packs answer once the reading load has used it. With no project pack listed the load has
// no further read, so a task queued after the answer runs after the load's last step.
const countPacksAnswers = b => b.evaluate(`{const f=window.fetch;window.__packsDone=0;window.fetch=async(...a)=>{const r=await f(...a);if(String(a[0]).endsWith('/api/orgs/acme/packs')){const j=r.json.bind(r);r.json=()=>j().finally(()=>setTimeout(()=>{window.__packsDone++}))}return r}}true`);
test('SP6 pack picker: an older read of the packs that answers last does not replace a newer one', { timeout: 60000 }, async t => {
const gates = holds();
let packs = [PACKS[0]];
const { b } = await fixture(t, { gates, packs: () => packs });
await waitFor(b, `document.querySelector('[data-pack="software-team"] [data-pack-preview-button]')`, 'pack list');
await countPacksAnswers(b);
const older = gates.hold('packs.console');
await reload(b);
await older.arrived;
packs = [PACKS[0], { id: 'later', origin: 'installed', pack: { label: 'Later pack', description: 'Installed after the first read.', version: '0.1.0', scope: 'organization' } }];
await reload(b);
await waitFor(b, `document.querySelector('[data-pack="later"]') && window.__packsDone===1`, 'newer list drawn');
older.release();
await waitFor(b, `window.__packsDone===2`, 'older read answered');
assert.match(await b.evaluate(`window.MosaicPacks.section('acme')`), /data-pack="later"/, 'the older list was not put back');
await b.evaluate(`document.querySelector('[data-pack="software-team"] [data-pack-preview-button]').click();true`);
await waitFor(b, `document.querySelector('[data-pack-preview="software-team"]')`, 'preview');
assert.ok(await b.evaluate(`!!document.querySelector('[data-pack="later"]')`), 'the drawn list is the newer one');
});
test('SP6 pack picker: an apply receipt that lands while an earlier read of the packs is held is kept', { timeout: 60000 }, async t => {
const gates = holds();
const { b, verbs } = await fixture(t, { gates, packs: () => [PACKS[0]] });
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-apply]')`, 'preview');
await countPacksAnswers(b);
// A reload's packs read is held. The apply answers, reads the packs itself and asks the page to read
// everything again; both of those reads answer first. Then the held read answers last.
const earlier = gates.hold('packs.console');
await reload(b);
await earlier.arrived;
await b.evaluate(`document.querySelector('[data-pack-apply]').click();true`);
await waitFor(b, `/^Applied\./.test(document.querySelector('[data-packs-status]')?.textContent??'') && window.__packsDone===2`, 'apply status');
assert.equal(verbs.filter(([v]) => v === 'pack.apply.console').length, 1);
earlier.release();
await waitFor(b, `window.__packsDone===3`, 'held read answered');
const drawn = await b.evaluate(`window.MosaicPacks.section('acme')`);
assert.match(drawn, /data-packs-status>Applied\. Added board, definitions\.reviewer\./, 'the receipt is kept');
assert.doesNotMatch(drawn, /data-pack-preview=/, 'the applied preview does not come back');
});
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');