|
| 1 | +import { test, expect, type Page } from '@playwright/test'; |
| 2 | +import { compile } from 'tailwindcss'; |
| 3 | +import { readFileSync } from 'node:fs'; |
| 4 | +import { fileURLToPath } from 'node:url'; |
| 5 | +import path from 'node:path'; |
| 6 | +import { uiRoot } from '../scripts/ui-root.mjs'; |
| 7 | + |
| 8 | +/** |
| 9 | + * GEOMETRY pin for `record:path` in a narrow record drawer at a desktop |
| 10 | + * viewport (objectstack#34696). |
| 11 | + * |
| 12 | + * The desktop row and the compact path used to switch on the VIEWPORT alone, |
| 13 | + * so a 380 px record drawer at a 1280 px viewport drew the desktop row: seven |
| 14 | + * equal shares of about 33 px each, which read as 'P…', 'Pro…', 'Ne…'. The |
| 15 | + * path's root is now a Tailwind container and each row stacks `sm` on the |
| 16 | + * container's `@sm`, so that drawer draws the compact path, while a wide page |
| 17 | + * draws the desktop row exactly as before, and a viewport below `sm` keeps the |
| 18 | + * compact path whatever the path's width. |
| 19 | + * |
| 20 | + * A Playwright spec and not a vitest test for the reason |
| 21 | + * `record-header-title-width.spec.ts` gives: jsdom has no layout engine, so a |
| 22 | + * width read there is 0 and an assertion on it is vacuous. The class-shape |
| 23 | + * half is pinned beside the renderer in `record-path.compact-34595.test.tsx`. |
| 24 | + * |
| 25 | + * REAL, read out of `record-path.tsx` at run time: the root's class, both |
| 26 | + * rows' switch classes, the desktop row's group and stage classes, the label |
| 27 | + * and rail base strings and the named PHONE_* constants, plus real Tailwind |
| 28 | + * compiled from the installed `tailwindcss` for exactly those candidates. |
| 29 | + * FIXTURE: the stage labels, and the element trees, transcribed from the two |
| 30 | + * rows. |
| 31 | + */ |
| 32 | + |
| 33 | +const REPO_ROOT = uiRoot(); |
| 34 | +const PATH_FILE = 'packages/plugin-detail/src/renderers/record-path.tsx'; |
| 35 | +const pathSrc = readFileSync(path.join(REPO_ROOT, PATH_FILE), 'utf8'); |
| 36 | + |
| 37 | +/** One string out of the source, asserting tokens so a drifted match throws. */ |
| 38 | +function grab(re: RegExp, mustContain: string[]): string { |
| 39 | + const m = pathSrc.match(re); |
| 40 | + if (!m) throw new Error(`${PATH_FILE}: nothing matched ${re}`); |
| 41 | + const cls = m[1]; |
| 42 | + for (const token of mustContain) { |
| 43 | + if (!cls.split(/\s+/).includes(token)) { |
| 44 | + throw new Error( |
| 45 | + `${PATH_FILE}: ${re} matched "${cls}", which does not carry "${token}" — the extraction ` + |
| 46 | + 'walked onto the wrong string, so this measurement would be meaningless.', |
| 47 | + ); |
| 48 | + } |
| 49 | + } |
| 50 | + return cls; |
| 51 | +} |
| 52 | + |
| 53 | +const CLS = { |
| 54 | + root: grab(/<div className=\{cn\('([^']+)', className\)\} \{\.\.\.designer\}>/, ['@container', 'w-full']), |
| 55 | + desktop: grab(/\{\/\* Desktop[^\n]*\*\/\}\s*<div\s+className="([^"]+)"/, ['hidden', 'sm:@sm:flex']), |
| 56 | + forwardGroup: grab(/<div className="([^"]+)" style=\{shareOf\(forwardStages\.length\)\}>/, ['flex', 'min-w-0']), |
| 57 | + lostGroup: grab(/<div\s+className="([^"]+)"\s+style=\{shareOf\(lostStages\.length\)\}/, ['flex', 'border-l']), |
| 58 | + stageItem: grab(/className=\{cn\('(flex flex-col gap-1\.5)', listLine/, ['flex-col']), |
| 59 | + stageShare: grab(/forwardStages\.map[\s\S]*?className: '([^']+)'/, ['flex-1', 'min-w-0']), |
| 60 | + ordinaryLabel: grab(/labelClassName: stageTerminals\[idx\] \? TERMINAL_LABEL_CLASS : '([^']+)'/, ['truncate']), |
| 61 | + terminalLabel: grab(/const TERMINAL_LABEL_CLASS = '([^']+)'/, ['[overflow-wrap:anywhere]']), |
| 62 | + label: grab(/const labelClass = [\s\S]*?cn\(\s*'([^']+)'/, ['block', 'min-w-0', 'text-xs']), |
| 63 | + rail: grab(/const railClass = [\s\S]*?cn\(\s*'([^']+)'/, ['w-full']), |
| 64 | + compact: grab(/\{\/\* Mobile:[^\n]*\*\/\}\s*<div\s+className="([^"]+)"/, ['sm:@sm:hidden']), |
| 65 | + railRow: grab(/const PHONE_RAIL_ROW_CLASS = '([^']+)'/, ['flex', 'w-full']), |
| 66 | + railSegment: grab(/const PHONE_RAIL_SEGMENT_CLASS = '([^']+)'/, ['flex-1', 'min-w-0']), |
| 67 | + summary: grab(/const PHONE_SUMMARY_CLASS =\s*'([^']+)'/, ['min-h-11', 'w-full']), |
| 68 | + summaryStage: grab(/const PHONE_SUMMARY_STAGE_CLASS = '([^']+)'/, ['[overflow-wrap:anywhere]']), |
| 69 | +}; |
| 70 | + |
| 71 | +/** The opportunity path the card measured: five open stages, won, then lost. */ |
| 72 | +const FORWARD = ['Prospecting', 'Qualification', 'Needs Analysis', 'Proposal', 'Negotiation', 'Closed Won']; |
| 73 | +const LOST = ['Closed Lost']; |
| 74 | +const CURRENT = 'Negotiation'; |
| 75 | + |
| 76 | +/** The tree both rows draw, with the switch classes the source gives them (or the old ones). */ |
| 77 | +const fixture = (cls: { root: string; desktop: string; compact: string }) => { |
| 78 | + const stage = (label: string, terminal: boolean) => |
| 79 | + `<div role="listitem" class="${CLS.stageItem} ${CLS.stageShare}">` + |
| 80 | + `<span class="${CLS.rail} bg-muted"></span>` + |
| 81 | + `<span data-label="${label}" data-ordinary="${terminal ? '' : 'yes'}" class="${CLS.label} font-normal ${ |
| 82 | + terminal ? CLS.terminalLabel : CLS.ordinaryLabel |
| 83 | + }">${label}</span></div>`; |
| 84 | + const forward = FORWARD.map((l, i) => stage(l, i === FORWARD.length - 1)).join(''); |
| 85 | + const lost = LOST.map((l) => stage(l, true)).join(''); |
| 86 | + const total = FORWARD.length + LOST.length; |
| 87 | + return ( |
| 88 | + `<div id="root" class="${cls.root}">` + |
| 89 | + `<div id="desktop" role="list" class="${cls.desktop}">` + |
| 90 | + `<div class="${CLS.forwardGroup}" style="flex:${FORWARD.length} 1 0%">${forward}</div>` + |
| 91 | + `<div class="${CLS.lostGroup}" style="flex:${LOST.length} 1 0%">${lost}</div>` + |
| 92 | + '</div>' + |
| 93 | + `<div id="compact" class="${cls.compact}">` + |
| 94 | + `<div id="rails" aria-hidden="true" class="${CLS.railRow}">` + |
| 95 | + Array.from({ length: total }, (_, i) => |
| 96 | + `<span class="${CLS.rail} bg-muted ${CLS.railSegment}${i === FORWARD.length ? ' ml-1' : ''}"></span>`, |
| 97 | + ).join('') + |
| 98 | + '</div>' + |
| 99 | + `<button id="summary" type="button" class="${CLS.summary}">` + |
| 100 | + `<span class="shrink-0">Step 5 of ${FORWARD.length}</span><span aria-hidden="true">·</span>` + |
| 101 | + `<span id="current" class="${CLS.summaryStage}">${CURRENT}</span>` + |
| 102 | + '<svg class="ml-auto h-4 w-4 shrink-0" width="16" height="16"></svg>' + |
| 103 | + '</button>' + |
| 104 | + '</div>' + |
| 105 | + '</div>' |
| 106 | + ); |
| 107 | +}; |
| 108 | + |
| 109 | +const NOW = { root: CLS.root, desktop: CLS.desktop, compact: CLS.compact }; |
| 110 | +/** The switch as it stood before this card: viewport only, no container. */ |
| 111 | +const BEFORE = { |
| 112 | + root: CLS.root.replace('@container', '').trim(), |
| 113 | + desktop: CLS.desktop.replace('sm:@sm:flex', 'sm:flex'), |
| 114 | + compact: CLS.compact.replace('sm:@sm:hidden', 'sm:hidden'), |
| 115 | +}; |
| 116 | + |
| 117 | +let css = ''; |
| 118 | +test.beforeAll(async () => { |
| 119 | + const twEntry = fileURLToPath(import.meta.resolve('tailwindcss/index.css')); |
| 120 | + const compiler = await compile('@import "tailwindcss";', { |
| 121 | + base: path.dirname(twEntry), |
| 122 | + loadStylesheet: async (id: string, base: string) => { |
| 123 | + const file = id === 'tailwindcss' ? twEntry : path.resolve(base, id); |
| 124 | + return { path: file, base: path.dirname(file), content: readFileSync(file, 'utf8') }; |
| 125 | + }, |
| 126 | + }); |
| 127 | + css = compiler.build( |
| 128 | + [...Object.values(CLS), ...Object.values(BEFORE), 'bg-muted font-normal ml-1 shrink-0 ml-auto h-4 w-4'] |
| 129 | + .join(' ') |
| 130 | + .split(/\s+/) |
| 131 | + .filter(Boolean), |
| 132 | + ); |
| 133 | + if (!css.includes('@container')) { |
| 134 | + throw new Error('Tailwind produced no container query — `sm:@sm:flex` would be inert.'); |
| 135 | + } |
| 136 | +}); |
| 137 | + |
| 138 | +type Reading = { |
| 139 | + desktopShown: boolean; |
| 140 | + compactShown: boolean; |
| 141 | + labels: { label: string; ordinary: boolean; width: number; clipped: boolean; top: number; left: number }[]; |
| 142 | + current: { clipped: boolean; right: number }; |
| 143 | + railsFit: boolean; |
| 144 | + drawerRight: number; |
| 145 | +}; |
| 146 | + |
| 147 | +async function readPath( |
| 148 | + page: Page, |
| 149 | + opts: { viewport: number; container: number; cls?: typeof NOW; forceDesktop?: boolean }, |
| 150 | +): Promise<Reading> { |
| 151 | + await page.setViewportSize({ width: opts.viewport, height: 900 }); |
| 152 | + const force = opts.forceDesktop ? '#desktop{display:flex!important}' : ''; |
| 153 | + await page.setContent( |
| 154 | + `<style>html,body{margin:0}body{font:14px sans-serif}${css}${force}</style>` + |
| 155 | + `<div id="drawer" style="width:${opts.container}px">${fixture(opts.cls ?? NOW)}</div>`, |
| 156 | + { waitUntil: 'load' }, |
| 157 | + ); |
| 158 | + await page.evaluate(() => document.fonts?.ready); |
| 159 | + return page.evaluate(() => { |
| 160 | + const round = (n: number) => Math.round(n * 100) / 100; |
| 161 | + const shown = (id: string) => getComputedStyle(document.getElementById(id)!).display !== 'none'; |
| 162 | + const cur = document.getElementById('current')!; |
| 163 | + const rails = document.getElementById('rails')!; |
| 164 | + return { |
| 165 | + desktopShown: shown('desktop'), |
| 166 | + compactShown: shown('compact'), |
| 167 | + labels: Array.from(document.querySelectorAll<HTMLElement>('#desktop [data-label]')).map((el) => { |
| 168 | + const r = el.getBoundingClientRect(); |
| 169 | + return { |
| 170 | + label: el.dataset.label!, |
| 171 | + ordinary: el.dataset.ordinary === 'yes', |
| 172 | + width: round(r.width), |
| 173 | + clipped: el.scrollWidth > el.clientWidth, |
| 174 | + top: round(r.top), |
| 175 | + left: round(r.left), |
| 176 | + }; |
| 177 | + }), |
| 178 | + current: { clipped: cur.scrollWidth > cur.clientWidth, right: round(cur.getBoundingClientRect().right) }, |
| 179 | + railsFit: rails.scrollWidth <= rails.clientWidth, |
| 180 | + drawerRight: round(document.getElementById('drawer')!.getBoundingClientRect().right), |
| 181 | + }; |
| 182 | + }); |
| 183 | +} |
| 184 | + |
| 185 | +test.describe('record:path in a narrow drawer at a desktop viewport (objectstack#34696)', () => { |
| 186 | + test('the defect this pins: at 380px the desktop row gives each stage under 50px and cuts the long names', async ({ page }) => { |
| 187 | + // Forced on, the desktop row shows what the drawer drew before: not vacuous. |
| 188 | + const r = await readPath(page, { viewport: 1280, container: 380, forceDesktop: true }); |
| 189 | + const ordinary = r.labels.filter((l) => l.ordinary); |
| 190 | + expect(ordinary).toHaveLength(FORWARD.length - 1); |
| 191 | + for (const l of ordinary) expect(l.width, `${l.label} got ${l.width}px`).toBeLessThan(50); |
| 192 | + for (const name of ['Prospecting', 'Qualification', 'Needs Analysis', 'Negotiation']) { |
| 193 | + expect(ordinary.find((l) => l.label === name)?.clipped, `${name} fit whole in the desktop row at 380px`).toBe(true); |
| 194 | + } |
| 195 | + }); |
| 196 | + |
| 197 | + for (const container of [360, 380]) { |
| 198 | + test(`a ${container}px drawer at a 1280px viewport draws the compact path, the current stage whole`, async ({ page }) => { |
| 199 | + const r = await readPath(page, { viewport: 1280, container }); |
| 200 | + expect(r.desktopShown, 'the desktop row still draws in the narrow drawer').toBe(false); |
| 201 | + expect(r.compactShown, 'the compact path is hidden in the narrow drawer').toBe(true); |
| 202 | + expect(r.current.clipped, `${CURRENT} is cut`).toBe(false); |
| 203 | + expect(r.current.right).toBeLessThanOrEqual(r.drawerRight + 0.5); |
| 204 | + expect(r.railsFit, 'the rail spills out of its row').toBe(true); |
| 205 | + }); |
| 206 | + } |
| 207 | + |
| 208 | + test('a viewport below `sm` keeps the compact path even in a path wider than `@sm`', async ({ page }) => { |
| 209 | + const r = await readPath(page, { viewport: 600, container: 560 }); |
| 210 | + expect(r.desktopShown).toBe(false); |
| 211 | + expect(r.compactShown).toBe(true); |
| 212 | + }); |
| 213 | + |
| 214 | + test('CONTROL — a 900px path at a 1280px viewport draws the desktop row, every label readable', async ({ page }) => { |
| 215 | + const r = await readPath(page, { viewport: 1280, container: 900 }); |
| 216 | + expect(r.desktopShown, 'the wide page lost its desktop row').toBe(true); |
| 217 | + expect(r.compactShown, 'the wide page drew the compact path').toBe(false); |
| 218 | + for (const l of r.labels) { |
| 219 | + expect(l.clipped, `${l.label} is cut on the wide page`).toBe(false); |
| 220 | + expect(l.width).toBeGreaterThan(80); |
| 221 | + } |
| 222 | + }); |
| 223 | + |
| 224 | + test('CONTROL — the wide desktop row is pixel-identical to the viewport-only switch it replaces', async ({ page }) => { |
| 225 | + for (const container of [900, 1200]) { |
| 226 | + const now = await readPath(page, { viewport: 1280, container, cls: NOW }); |
| 227 | + const before = await readPath(page, { viewport: 1280, container, cls: BEFORE }); |
| 228 | + expect(before.desktopShown).toBe(true); |
| 229 | + expect(now.labels, `the desktop row moved at ${container}px`).toEqual(before.labels); |
| 230 | + } |
| 231 | + }); |
| 232 | +}); |
0 commit comments