import assert from 'node:assert/strict'; import { execFile } from 'node:child_process'; import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; import { promisify } from 'node:util'; const execFileAsync = promisify(execFile); const root = new URL('../', import.meta.url); const css = await readFile(new URL('public/cloud/client.css', root), 'utf8'); const template = await readFile( new URL('resources/views/venix/cloud/pages/invoices.tpl', root), 'utf8', ); const module = await readFile(new URL('public/cloud/invoices.js', root), 'utf8'); const service = await readFile( new URL('app/Services/Cloud/CloudInvoicePageService.php', root), 'utf8', ); assert.match(template, /class="invoice-balance-summary"/, 'The balance label and amount must share a stable summary container.'); assert.match(template, /class="invoice-balance-hint"/, 'The balance card must explain what the wallet balance can be used for.'); assert.match(css, /\.invoice-balance-summary\s*\{/, 'The balance summary has no dedicated visual hierarchy.'); assert.match(css, /\.invoice-filter-actions \.button-ghost\s*\{[^}]*background:\s*#fff/, 'The invoice reset action must remain visibly white against the page background.'); assert.doesNotMatch(template, /type="date"|invoices-date-|date_(?:from|to)/, 'The invoice template still exposes the retired date filter.'); assert.doesNotMatch(module, /normalizeDateFilter|date_(?:from|to)|invoices-date-/, 'The invoice browser module still carries retired date-filter state.'); assert.doesNotMatch(service, /date_(?:from|to)|DateTimeImmutable/, 'The invoice query service still carries retired date-filter logic.'); assert.match(css, /@media \(max-width: 360px\)[\s\S]*?\.invoice-balance-card\s*\{[^}]*grid-template-columns:\s*1fr/, 'Very narrow screens must stack the balance action below the amount.'); const chromeCandidates = [ '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', '/Applications/Chromium.app/Contents/MacOS/Chromium', '/usr/bin/google-chrome', '/usr/bin/chromium', '/usr/bin/chromium-browser', ]; let chrome = ''; for (const candidate of chromeCandidates) { try { await execFileAsync(candidate, ['--version']); chrome = candidate; break; } catch { /* Try the next local browser. */ } } if (chrome) { const directory = await mkdtemp(join(tmpdir(), 'invoice-mobile-layout-')); const fixture = join(directory, 'fixture.html'); const safeCss = css.replaceAll('
当前账户余额 $10,551.00 可用于支付未结清账单
`; await writeFile(fixture, html); const measure = async width => { const profile = join(directory, `profile-${width}`); const { stdout } = await execFileAsync(chrome, [ '--headless=new', '--disable-gpu', '--no-sandbox', '--hide-scrollbars', `--window-size=${width},844`, `--user-data-dir=${profile}`, '--dump-dom', `file://${fixture}`, ], { maxBuffer: 8 * 1024 * 1024 }); const encoded = stdout.match(/data-metrics="([^"]+)"/)?.[1]; assert.ok(encoded, 'Headless browser did not return invoice layout metrics.'); return JSON.parse(decodeURIComponent(encoded)); }; try { // Desktop Chrome enforces a 500 CSS-pixel minimum headless window. That // still drives the real max-width:520px mobile rules; the narrower // max-width:360px fallback is locked by the static contract above. const regular = await measure(500); assert.ok(regular.scrollWidth <= regular.viewport, `Invoice page overflows horizontally (${regular.scrollWidth} > ${regular.viewport}).`); assert.ok(regular.filter.right <= regular.viewport && regular.purpose.right <= regular.viewport && regular.status.right <= regular.viewport && regular.keyword.right <= regular.viewport, 'An invoice filter control extends beyond the mobile viewport.'); assert.equal(regular.resetBackground, 'rgb(255, 255, 255)', 'The invoice reset action does not render with a white background.'); assert.ok(regular.recharge.left >= regular.summary.right - 1 && regular.recharge.right <= regular.card.right, 'The balance action is not aligned with the summary on a normal phone.'); } finally { await rm(directory, { recursive: true, force: true }); } } console.log(`Cloud invoice mobile filter and balance layout checks passed${chrome ? ' with headless browser.' : ' (browser unavailable; static contract only).'}`);