|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | + |
| 5 | +import * as datasets from '../src/ui/datasets/index.js'; |
| 6 | +import { allReports } from '../src/ui/reports/index.js'; |
| 7 | + |
| 8 | +/** |
| 9 | + * A report's `description` is the subtitle the console renders under its |
| 10 | + * title; its cells are the measures `values` binds. The two must name the same |
| 11 | + * quantity. The Status × Priority matrix shipped "Task counts cross-tabulated |
| 12 | + * by status and priority." over `values: ['est_hours']` — a `sum` of |
| 13 | + * `estimate_hours` — so a reader trusting the subtitle took a cell reading |
| 14 | + * 60.0 for sixty tasks when it held one task's estimated hours (#23931). |
| 15 | + * |
| 16 | + * Graded against the bound DATASET's own declarations, never against a copy of |
| 17 | + * the subtitle text: |
| 18 | + * |
| 19 | + * - a bound measure is named by its declared `label` — the name the cell's |
| 20 | + * header shows — and a `count` measure may also be named in count words; |
| 21 | + * - the converse is the defect as shipped: count words in a subtitle whose |
| 22 | + * bound measures count nothing. |
| 23 | + * |
| 24 | + * Joined reports bind their measures per block and carry no top-level |
| 25 | + * `values`, so their subtitle names no single quantity; they are out of scope |
| 26 | + * by construction, not by a skip. |
| 27 | + */ |
| 28 | + |
| 29 | +type Measure = { name: string; label?: unknown; aggregate?: string }; |
| 30 | + |
| 31 | +const MEASURES_BY_DATASET = new Map<string, readonly Measure[]>( |
| 32 | + Object.values(datasets).map((d) => [d.name, d.measures as readonly Measure[]]), |
| 33 | +); |
| 34 | + |
| 35 | +/** The words a subtitle uses to say its cells are counts. */ |
| 36 | +const COUNT_WORDS = /\b(counts?|number of)\b/i; |
| 37 | + |
| 38 | +/** The plain-string copy this pin knows how to read; an inline locale map is reported, not guessed. */ |
| 39 | +function plain(value: unknown, what: string): string { |
| 40 | + expect(typeof value, `${what} must be a plain string for this pin to read it`).toBe('string'); |
| 41 | + return value as string; |
| 42 | +} |
| 43 | + |
| 44 | +function boundMeasures(report: (typeof allReports)[number]): Measure[] { |
| 45 | + const measures = MEASURES_BY_DATASET.get(report.dataset ?? ''); |
| 46 | + expect(measures, `${report.name}: dataset '${report.dataset}' is not a showcase dataset`).toBeDefined(); |
| 47 | + return (report.values ?? []).map((name) => { |
| 48 | + const measure = measures!.find((m) => m.name === name); |
| 49 | + expect(measure, `${report.name}: measure '${name}' is not declared on '${report.dataset}'`).toBeDefined(); |
| 50 | + return measure!; |
| 51 | + }); |
| 52 | +} |
| 53 | + |
| 54 | +const graded = allReports |
| 55 | + .filter((r) => r.description !== undefined && (r.values?.length ?? 0) > 0) |
| 56 | + .map((r) => [r.name, r] as const); |
| 57 | + |
| 58 | +describe('showcase reports — the subtitle names what the cells hold', () => { |
| 59 | + it('grades the Status × Priority matrix, among the measure-bound reports', () => { |
| 60 | + expect(graded.map(([name]) => name)).toContain('showcase_status_priority_matrix'); |
| 61 | + }); |
| 62 | + |
| 63 | + it.each(graded)('%s: the description names a bound measure', (name, report) => { |
| 64 | + const description = plain(report.description, `${name}.description`).toLowerCase(); |
| 65 | + const named = boundMeasures(report).filter((m) => { |
| 66 | + const label = plain(m.label, `measure ${m.name}.label`).toLowerCase(); |
| 67 | + return description.includes(label) || (m.aggregate === 'count' && COUNT_WORDS.test(description)); |
| 68 | + }); |
| 69 | + expect( |
| 70 | + named.map((m) => m.name), |
| 71 | + `${name}: "${report.description}" names none of its bound measures ${JSON.stringify(report.values)}`, |
| 72 | + ).not.toEqual([]); |
| 73 | + }); |
| 74 | + |
| 75 | + it.each(graded)('%s: the description claims no count its cells do not hold', (name, report) => { |
| 76 | + const description = plain(report.description, `${name}.description`); |
| 77 | + const bindsACount = boundMeasures(report).some((m) => m.aggregate === 'count'); |
| 78 | + const countWord = description.match(COUNT_WORDS)?.[0]; |
| 79 | + expect( |
| 80 | + countWord === undefined || bindsACount, |
| 81 | + `${name}: "${description}" says "${countWord}", but ${JSON.stringify(report.values)} aggregate no count`, |
| 82 | + ).toBe(true); |
| 83 | + }); |
| 84 | +}); |
0 commit comments