diff --git a/changelog.d/415.md b/changelog.d/415.md new file mode 100644 index 000000000..cb2455677 --- /dev/null +++ b/changelog.d/415.md @@ -0,0 +1,5 @@ +--- +section: fixes +--- + +- **The renderer check “No status text at the endpoints” no longer fails an item for its own name** (#415). An endpoint `POST /Library/Movies/Added`, or a member named `Removed`, failed `quiet-endpoints` at Before and After because its label held a status word. The check now reads status from the parts that state it (`data-change-status`, and the kit's badges, status lines and tabs), where any status word still fails, and elsewhere in an item's text fails only a status word that the item's own ID, label or data doesn't hold. diff --git a/packages/renderers/harness/browser/status-parts.spec.ts b/packages/renderers/harness/browser/status-parts.spec.ts new file mode 100644 index 000000000..c33322d6c --- /dev/null +++ b/packages/renderers/harness/browser/status-parts.spec.ts @@ -0,0 +1,35 @@ +import {expect, test} from '@playwright/test' +import {openStage, type EntitySample} from '@githubnext/projections-renderer-testing/playwright' +import {harnessInspector} from '@githubnext/projections-renderer-harness/playwright' + +/** + * The probe records the text of an entity's status parts (`data-change-status`, the kit's badges, status lines and + * tabs) apart from its name, so `quiet-endpoints` reads a status word in a badge as status and one in a name as a name. + */ +test('the probe reads status parts apart from an entity’s name', async ({page}, testInfo) => { + test.skip(testInfo.project.name !== 'chromium-desktop', 'The probe reads the DOM, the same in every engine.') + const stage = await openStage(page, harnessInspector(), {scenario: 'graph/npm-react-update', progress: 1, comparisonMode: 'diff'}) + await stage.frame.locator('.pk').evaluate(root => { + const probe = document.createElement('div') + probe.innerHTML = ` +
POST /Library/Movies/Added
+
RemovedAdded
+
Modified
+
Renamed
+
Removed
+
Added
` + root.append(probe) + }) + const samples = new Map((await stage.measure('samples')).map(sample => [sample.id, sample])) + const status = (id: string) => samples.get(id)?.status + // The entity element itself is never a status part, whatever its class. + expect(status('probe:named')).toEqual([]) + expect(samples.get('probe:badged')?.text).toBe('Removed Added') + expect(status('probe:badged')).toEqual(['Added']) + expect(status('probe:worded')).toEqual(['Modified']) + // A faded status part, or one only assistive technology hears, shows nothing. + expect(status('probe:faded')).toEqual([]) + expect(status('probe:spoken')).toEqual([]) + // A nested entity is never its parent's status part, whatever its class. + expect(status('probe:parent')).toEqual([]) +}) diff --git a/packages/renderers/testing/src/checks/catalog.ts b/packages/renderers/testing/src/checks/catalog.ts index 652c5196e..85006d368 100644 --- a/packages/renderers/testing/src/checks/catalog.ts +++ b/packages/renderers/testing/src/checks/catalog.ts @@ -129,7 +129,8 @@ export const checkDescriptions: Record = { 'Change status appears in reserved space: at every sampled progress, and in the diff, each entity has the same box with highlighting on and off.', 'status-text': 'While a change is emphasized, its entity states its status as text (Added, Removed, Modified, Renamed or Moved), visibly or for assistive technology, never by color alone.', - 'quiet-endpoints': "At both endpoints, with the host's default highlighting, no entity shows status text.", + 'quiet-endpoints': + "At both endpoints, with the host's default highlighting, no entity shows status text: no status word (Added, Removed, Modified, Renamed or Moved) in a part that states status (data-change-status, or the kit's badges, status lines and tabs), nor elsewhere in its text unless its own ID, label or data holds the word, as a route /Movies/Added does.", 'content-top': "The renderer draws no header of its own (the host shows the title, schema and counts): its first entity, canvas or marked content region (data-projections-content) starts within the stage padding of the frame top (the host's stage-inset, else 12 px, or its stage-inset-top where that is more), at full and phone width. A zoomable canvas (a scroll container marked data-zoom) counts as a whole, since it opens fitted (or at the text floor) and centres what it draws.", 'stage-insets': diff --git a/packages/renderers/testing/src/checks/comparison/endpoints.ts b/packages/renderers/testing/src/checks/comparison/endpoints.ts index 58d4b51e7..1f73b2250 100644 --- a/packages/renderers/testing/src/checks/comparison/endpoints.ts +++ b/packages/renderers/testing/src/checks/comparison/endpoints.ts @@ -1,4 +1,5 @@ // The checks of a comparison's endpoints: neutral at progress 0 and 1, with no status text. +import type {EntitySample, StageSubject} from '../../types.js' import {Skip, progressModes, limit, samples, label, type Checks} from '../support.js' import {acrossRevisions, difference, looks} from './support.js' import {statusWords} from './status.js' @@ -62,13 +63,56 @@ export const endpointChecks: Pick readonly string[]): string | undefined { + for (const text of sample.status ?? []) { + const word = statusWords.exec(text)?.[0] + if (word) return word + } + for (const [word] of sample.text.matchAll(new RegExp(statusWords.source, 'gu'))) { + const whole = new RegExp(`\\b${word}\\b`, 'u') + if (!named(sample.id).some(text => whole.test(text))) return word + } + return undefined +} + +/** + * Every string the items (entities and relationships) with an ID hold in either revision, with the ID itself: items + * indexed once, and each one's strings collected the first time a status word asks for them. + */ +function ownText(subject: Extract): (id: string) => readonly string[] { + const items = new Map() + for (const extraction of [subject.before, subject.after]) { + for (const item of [...extraction.entities, ...extraction.relationships]) items.set(item.id, [...(items.get(item.id) ?? []), item]) + } + const collected = new Map() + return id => { + let texts = collected.get(id) + if (texts) return texts + texts = [id] + // Data may nest JSON to any depth, so it is walked without recursion. + const pending = [...(items.get(id) ?? [])] + while (pending.length) { + const value = pending.pop() + if (typeof value === 'string') texts.push(value) + else if (typeof value === 'object' && value !== null) pending.push(...(Object.values(value) as unknown[])) + } + collected.set(id, texts) + return texts + } +} diff --git a/packages/renderers/testing/src/measure/entities.ts b/packages/renderers/testing/src/measure/entities.ts index 4d1b9bad7..3d795f844 100644 --- a/packages/renderers/testing/src/measure/entities.ts +++ b/packages/renderers/testing/src/measure/entities.ts @@ -19,12 +19,40 @@ export function entitySamples(root: Element = document.body): EntitySample[] { box: [round(box.left), round(box.top), round(box.width), round(box.height)], opacity: Math.round(effectiveOpacity(element) * 100) / 100, ...paintOf(element), + status: statusTexts(element), parts: [...new Set([...element.querySelectorAll('[data-entity-id]')].map(part => part.getAttribute('data-entity-id')!))], }) } return samples } +/** Parts that state a change's status: the kit's status words (`data-change-status`), badges, status lines and tabs. */ +const statusMarks = '[data-change-status], .badge, .pk-status-text, .pk-status-tab, .pk-svg-status, .pk-change-status' + +/** + * The visible text of each status part of an entity element, outermost parts only and not its nested entities'. The + * element itself is never one, whatever its class: its text is its name; nor is a nested entity. + */ +function statusTexts(element: Element): string[] { + return [...element.querySelectorAll(statusMarks)] + .filter( + part => + !part.hasAttribute('data-entity-id') && + part.parentElement?.closest(`${statusMarks}, [data-entity-id]`) === element && + !hiddenWithin(part, element), + ) + .map(part => paintOf(part).text) + .filter(text => text !== '') +} + +/** Whether a part, or anything around it up to `element`, is shown only to assistive technology (`sr-only`). */ +function hiddenWithin(part: Element, element: Element): boolean { + for (let node: Element | null = part; node && node !== element; node = node.parentElement) { + if (visuallyHidden(node, getComputedStyle(node))) return true + } + return visuallyHidden(element, getComputedStyle(element)) +} + /** IDs of visible entities: nested ones (parts) first, then units. */ export function visibleEntities(root: Element = document.body): string[] { const all = [...root.querySelectorAll('[data-entity-id]')].filter(visible) diff --git a/packages/renderers/testing/src/types.ts b/packages/renderers/testing/src/types.ts index e9ce4b23d..738d38582 100644 --- a/packages/renderers/testing/src/types.ts +++ b/packages/renderers/testing/src/types.ts @@ -169,6 +169,12 @@ export interface EntitySample { opacity: number paint: string text: string + /** + * The visible text of each part that states a change's status (`data-change-status`, or the kit's badges, status + * lines and tabs); also in `text`. `quiet-endpoints` reads a status word here as status, and one elsewhere in + * `text` as status only when the item's own ID, label and data don't hold it. + */ + status?: string[] /** IDs of entity elements nested inside it (a unit's parts), whose emphasis it may show. */ parts: string[] /** diff --git a/packages/renderers/testing/test/helpers.test.mjs b/packages/renderers/testing/test/helpers.test.mjs index 7695f9574..143851209 100644 --- a/packages/renderers/testing/test/helpers.test.mjs +++ b/packages/renderers/testing/test/helpers.test.mjs @@ -163,6 +163,56 @@ test('stable-layout compares two diffs once each frame has the height its render assert.equal(result.status, 'pass', result.details.join('\n')) }) +test('quiet-endpoints reads status from status parts, not from an item’s own name', async () => { + const route = 'endpoint:POST /Library/Movies/Added' + const named = { + mode: 'comparison', + before: {...extraction([]), entities: [{id: route, kind: 'endpoint', label: 'POST /Library/Movies/Added'}]}, + after: { + ...extraction([]), + entities: [ + {id: route, kind: 'endpoint', label: 'POST /Library/Movies/Added'}, + {id: 'member:ChangeKind.Removed', kind: 'member', label: 'Removed'}, + {id: 'job:nightly', kind: 'job', label: 'nightly', data: {steps: [{run: {when: {on: {kind: {name: 'Modified'}}}}}]}}, + ], + }, + comparison: { + protocolVersion: 0, + changes: [ + {id: 'member:ChangeKind.Removed', collection: 'entities', before: [], after: ['member:ChangeKind.Removed'], status: 'added'}, + ], + steps: [{changes: ['member:ChangeKind.Removed']}], + }, + } + const run = async drawn => { + const driver = fakeDriver(['side-by-side']).driver + driver.measure = async call => + call === 'samples' + ? drawn.map(sample => ({box: [0, 0, 10, 10], opacity: 1, paint: 'plain', parts: [], categories: {}, ...sample})) + : [] + return (await runChecks(driver, named, {checks: ['quiet-endpoints']}))[0] + } + // Labels that hold status words are names, at both endpoints. + const names = await run([ + {key: route, id: route, text: 'POST /Library/Movies/Added', status: []}, + {key: 'member:ChangeKind.Removed', id: 'member:ChangeKind.Removed', text: 'Removed', status: []}, + // However deep its data holds the word. + {key: 'job:nightly', id: 'job:nightly', text: 'nightly Modified', status: []}, + ]) + assert.equal(names.status, 'pass', names.details.join('\n')) + // A status badge fails, even beside a name holding the same word. + const badged = await run([{key: route, id: route, text: 'POST /Library/Movies/Added Added', status: ['Added']}]) + assert.equal(badged.status, 'fail') + assert.match( + badged.details[0], + /At Before \(progress 0\), "endpoint:POST \/Library\/Movies\/Added" shows "Added"; endpoints carry no status\./u, + ) + // Status text the item's name doesn't hold still fails, marked or not. + const loose = await run([{key: 'member:ChangeKind.Removed', id: 'member:ChangeKind.Removed', text: 'Removed Added', status: []}]) + assert.equal(loose.status, 'fail') + assert.match(loose.details[0], /shows "Added"/u) +}) + test('checks run per declared comparison mode', async () => { const status = results => Object.fromEntries(results.map(result => [result.id, result.status])) const diffOnly = fakeDriver(['diff'])