You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/tests/CloudInvoiceMobileLayoutTes...

128 lines
6.6 KiB
JavaScript

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('</style', '<\\/style');
const html = `<!doctype html><html><head><meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<style>${safeCss}</style></head><body>
<main style="width:auto;box-sizing:border-box;padding:20px;">
<div class="invoice-balance-card">
<div class="invoice-balance-summary">
<span class="invoice-balance-label">当前账户余额</span>
<strong id="invoices-balance">$10,551.00</strong>
<small class="invoice-balance-hint">可用于支付未结清账单</small>
</div>
<button class="button button-outline small" type="button">余额充值</button>
</div>
<form class="invoice-filter-bar" id="filter-bar">
<label class="invoice-filter-field" id="purpose-field"><span>账单类型</span><select><option>全部账单</option></select></label>
<label class="invoice-filter-field" id="status-field"><span>账单状态</span><select><option>全部状态</option></select></label>
<label class="invoice-filter-field invoice-filter-keyword" id="keyword-field"><span>账单或实例</span><input type="search"></label>
<div class="invoice-filter-actions"><button class="button button-primary small">筛选</button><button id="reset-filter" class="button button-ghost small">清除</button></div>
</form>
</main><script>
const rect = target => { const value = target.getBoundingClientRect(); return { left:value.left, right:value.right, top:value.top, bottom:value.bottom, width:value.width }; };
const card = document.querySelector('.invoice-balance-card');
document.body.dataset.metrics = encodeURIComponent(JSON.stringify({
viewport: document.documentElement.clientWidth,
scrollWidth: document.documentElement.scrollWidth,
filter: rect(document.getElementById('filter-bar')),
purpose: rect(document.getElementById('purpose-field')),
status: rect(document.getElementById('status-field')),
keyword: rect(document.getElementById('keyword-field')),
resetBackground: getComputedStyle(document.getElementById('reset-filter')).backgroundColor,
card: rect(card),
summary: rect(card.querySelector('.invoice-balance-summary')),
recharge: rect(card.querySelector('button')),
}));
</script></body></html>`;
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).'}`);