const fs = require('fs'); const path = require('path'); const {pathToFileURL} = require('url'); const {chromium} = require('playwright'); const {readSummary} = require('./summary_values'); const ok = (condition, message) => { if (!condition) throw Error(message); }; const entry = () => path.join(process.cwd(), 'index.html'); async function form(page) { let amount = page.locator('input[type=number]:visible').first(); if (!await amount.count()) { const add = page.getByRole('button', {name:/add|添加|新增|记一笔/i}).first(); ok(await add.count(), 'add action missing'); await add.click(); amount = page.locator('input[type=number]:visible').first(); } const desc = page.locator('textarea:visible,input:not([type=number]):not([type=hidden]):visible').first(); const submit = page.getByRole('button', {name:/add|添加|新增|保存|确定/i}).last(); ok(await amount.count() && await desc.count() && await submit.count(), 'usable add form missing'); return {amount,desc,submit}; } async function summary(page) { const text = await page.locator('body').innerText(); return readSummary(text); } (async () => { const browser = await chromium.launch({headless:true,args:['--no-sandbox']}); try { const page = await browser.newPage({viewport:{width:375,height:740}}); await page.goto(pathToFileURL(entry()).href); await page.evaluate(() => localStorage.clear()); await page.reload(); ok(await page.evaluate(() => document.documentElement.scrollWidth <= document.documentElement.clientWidth), 'mobile page must not overflow horizontally'); const initial = await summary(page); ok(initial.expense === 0, 'new ledger total expense must start at zero'); let controls = await form(page); await controls.desc.fill('午餐'); await controls.amount.fill('37'); await controls.submit.click(); ok((await page.locator('body').innerText()).includes('午餐'), 'first saved record must be visible'); let current = await summary(page); ok(current.expense === 37 && current.balance === initial.balance-37, 'first expense must update both summaries'); controls = await form(page); await controls.desc.fill('车票'); await controls.amount.fill('13'); await controls.submit.click(); current = await summary(page); ok(current.expense === 50 && current.balance === initial.balance-50, 'multiple expenses must aggregate'); await page.reload(); const body = await page.locator('body').innerText(); ok(body.includes('午餐') && body.includes('车票'), 'both records must persist after reload'); current = await summary(page); ok(current.expense === 50 && current.balance === initial.balance-50, 'summaries must persist after reload'); controls = await form(page); await controls.desc.fill('零元'); await controls.amount.fill('0'); await controls.submit.click(); current = await summary(page); ok(current.expense === 50, 'zero-value expense must not change the ledger'); await page.reload(); ok(!(await page.locator('body').innerText()).includes('零元'), 'zero-value expense must not be stored as a record'); current = await summary(page); ok(current.expense === 50 && current.balance === initial.balance-50, 'invalid expense must not alter persisted summaries'); let total = 50; const additions = [['小额甲', 0.1], ['小额乙', 0.2], ['大额支出', Math.abs(initial.balance) + 100.37]]; for (const [description, amount] of additions) { controls = await form(page); await controls.desc.fill(description); await controls.amount.fill(amount.toFixed(2)); await controls.submit.click(); total += amount; current = await summary(page); ok(Math.abs(current.expense - total) < 0.005 && Math.abs(current.balance - (initial.balance - total)) < 0.005, 'decimal expense and negative balance must be calculated correctly'); } await page.reload(); current = await summary(page); ok(current.balance < 0 && Math.abs(current.expense - total) < 0.005, 'negative balance did not persist'); controls = await form(page); await controls.desc.fill('非法负金额'); await controls.amount.fill('-5'); await controls.submit.click(); current = await summary(page); ok(Math.abs(current.expense - total) < 0.005 && Math.abs(current.balance - (initial.balance - total)) < 0.005, 'negative expense altered a summary'); await page.reload(); ok(!(await page.locator('body').innerText()).includes('非法负金额'), 'negative expense was stored'); current = await summary(page); ok(Math.abs(current.expense - total) < 0.005 && Math.abs(current.balance - (initial.balance - total)) < 0.005, 'negative expense altered a persisted summary'); const output = '/logs/verifier/visual'; fs.mkdirSync(output, {recursive: true}); await page.screenshot({path: path.join(output, 'mobile-ledger.png'), fullPage: true}); console.log('enhanced expense ledger cases passed'); } finally { await browser.close(); } })().catch(error => { console.error(error); process.exit(1); });