2026-12-05 09:09:38 -08:00
|
|
|
// 三个审查反例分别从真实页面办理。每侧每例使用独立的 SQLite 账本。
|
|
|
|
|
import assert from 'node:assert/strict';
|
|
|
|
|
import fs from 'node:fs';
|
|
|
|
|
import os from 'node:os';
|
|
|
|
|
import path from 'node:path';
|
|
|
|
|
import { createRequire } from 'node:module';
|
|
|
|
|
import { DatabaseSync } from 'node:sqlite';
|
|
|
|
|
import { chromium } from 'playwright';
|
|
|
|
|
import { Session } from './lib/client.js';
|
|
|
|
|
import { app, git, reference, fixedReference } from './lib/write-reference.mjs';
|
|
|
|
|
import { action, postFrom, section } from './writes/operations-helpers.mjs';
|
|
|
|
|
|
|
|
|
|
const day = '2026-11-20';
|
|
|
|
|
const clockStart = day + 'T02:00:00Z';
|
|
|
|
|
const temp = fs.mkdtempSync(path.join(os.tmpdir(), 'ledger-recovery-confirmations-'));
|
|
|
|
|
const oldApp = fixedReference(temp, process.env.LEDGER_REFERENCE_APP);
|
|
|
|
|
const field = (form, name) => form.locator('[name="' + name + '"]');
|
|
|
|
|
const submit = form => form.locator('button[type=submit]').click();
|
|
|
|
|
|
|
|
|
|
async function prepareExchange(session) {
|
|
|
|
|
await session.post('owner', '/api/stock', {
|
|
|
|
|
kind: 'increase',
|
|
|
|
|
date: day,
|
|
|
|
|
pocket: '现货',
|
|
|
|
|
grams: '20',
|
|
|
|
|
note: '复核库存',
|
|
|
|
|
});
|
|
|
|
|
const original = await session.post('owner', '/api/recoveries', {
|
|
|
|
|
date: day,
|
|
|
|
|
mode: 'exchange',
|
|
|
|
|
customer: '复核置换单',
|
|
|
|
|
customer_type: 'new',
|
|
|
|
|
salesperson: '麦麦',
|
|
|
|
|
member: 'owner',
|
|
|
|
|
payer: 'owner',
|
|
|
|
|
payment_account: '助理微信',
|
|
|
|
|
verified: true,
|
|
|
|
|
buy_rate: '0',
|
|
|
|
|
old_lines: [{ name: '旧料', incoming_weight: '1', melted_weight: '1', purity: '100' }],
|
|
|
|
|
exchange: {
|
|
|
|
|
name: '足金999',
|
|
|
|
|
quantity: '1',
|
|
|
|
|
pocket: '现货',
|
|
|
|
|
gold_price: '0',
|
|
|
|
|
labor_rate: '200',
|
|
|
|
|
labor_mode: 'total',
|
|
|
|
|
depreciation_rate: '0',
|
|
|
|
|
cost_labor: { mode: 'total', amount: '15' },
|
|
|
|
|
cost_payer: 'owner',
|
|
|
|
|
discount: '0',
|
|
|
|
|
rounding_mode: 'cent',
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
assert.ok(original.id);
|
|
|
|
|
return original.id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function ledger(db, id) {
|
|
|
|
|
const row = db.prepare('SELECT data, order_id, revision FROM recoveries WHERE id=?').get(id);
|
|
|
|
|
const payments = db
|
|
|
|
|
.prepare("SELECT account, amount FROM payments WHERE order_id=? AND kind='receipt' ORDER BY rowid")
|
|
|
|
|
.all(row.order_id);
|
|
|
|
|
return {
|
|
|
|
|
revision: row.revision,
|
|
|
|
|
net: JSON.parse(row.data).netSettlement,
|
|
|
|
|
payments: payments.map(p => [p.account, p.amount]),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function openEdit(ctx, id) {
|
|
|
|
|
const { page } = ctx;
|
|
|
|
|
await section(page, ctx, 'recoveries');
|
|
|
|
|
await page
|
|
|
|
|
.locator('[data-action=recovery-open][data-id="' + id + '"]')
|
|
|
|
|
.first()
|
|
|
|
|
.click();
|
|
|
|
|
await action(page.locator('#recovery-dialog[open]'), 'recovery-edit-open').click();
|
|
|
|
|
const form = page.locator('#recovery-form[data-recovery-edit=true]');
|
|
|
|
|
await field(form, 'change_reason').fill('复核工费与收款人');
|
|
|
|
|
return form;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function changedAmount(ctx) {
|
|
|
|
|
const { session, page, db } = ctx;
|
|
|
|
|
const id = await prepareExchange(session);
|
|
|
|
|
const url = '/api/recoveries/' + id + '/update';
|
|
|
|
|
const form = await openEdit(ctx, id);
|
|
|
|
|
await field(form, 'verified').check();
|
|
|
|
|
await field(form, 'labor_rate').fill('250');
|
|
|
|
|
const verified = await field(form, 'verified').isChecked();
|
|
|
|
|
if (verified) await postFrom(page, url, () => submit(form));
|
|
|
|
|
else {
|
|
|
|
|
await submit(form);
|
|
|
|
|
await page.waitForLoadState('networkidle');
|
|
|
|
|
}
|
|
|
|
|
const beforeConfirmation = ledger(db, id);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
{ verified, writes: ctx.posts.filter(p => p === url).length, beforeConfirmation },
|
|
|
|
|
{
|
|
|
|
|
verified: false,
|
|
|
|
|
writes: 0,
|
|
|
|
|
beforeConfirmation: { revision: 1, net: 20000, payments: [['助理微信', 20000]] },
|
|
|
|
|
},
|
|
|
|
|
'勾选后改价须重新核实,重新勾选前原账不动',
|
|
|
|
|
);
|
|
|
|
|
await field(form, 'verified').check();
|
|
|
|
|
await postFrom(page, url, () => submit(form));
|
2026-12-05 09:11:55 -08:00
|
|
|
assert.equal(ctx.posts.filter(p => p === url).length, 1, '重新核实后只提交一次');
|
2026-12-05 09:09:38 -08:00
|
|
|
const afterConfirmation = ledger(db, id);
|
|
|
|
|
assert.deepEqual(afterConfirmation, {
|
|
|
|
|
revision: 2,
|
|
|
|
|
net: 25000,
|
|
|
|
|
payments: [
|
|
|
|
|
['助理微信', 20000],
|
|
|
|
|
['助理微信', -20000],
|
|
|
|
|
['助理微信', 25000],
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
return { verified, beforeConfirmation, afterConfirmation };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function depositDraft(ctx) {
|
|
|
|
|
const { session, page } = ctx;
|
|
|
|
|
await section(page, ctx, 'recoveries');
|
|
|
|
|
const open = () => action(page, 'customer-material-new').click();
|
|
|
|
|
await open();
|
|
|
|
|
const form = page.locator('#customer-material-form');
|
|
|
|
|
await field(form, 'grams').fill('2.000');
|
|
|
|
|
await field(form, 'memo').fill('复核寄存袋');
|
|
|
|
|
await field(form, 'verified').check();
|
|
|
|
|
await action(page, 'customer-material-back').click();
|
|
|
|
|
await open();
|
|
|
|
|
const restored = {
|
|
|
|
|
grams: await field(form, 'grams').inputValue(),
|
|
|
|
|
memo: await field(form, 'memo').inputValue(),
|
|
|
|
|
verified: await field(form, 'verified').isChecked(),
|
|
|
|
|
};
|
|
|
|
|
const url = '/api/customer-materials';
|
|
|
|
|
if (restored.verified) await postFrom(page, url, () => submit(form));
|
|
|
|
|
else {
|
|
|
|
|
await submit(form);
|
|
|
|
|
await page.waitForLoadState('networkidle');
|
|
|
|
|
}
|
|
|
|
|
const before = await session.get('owner', url);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
{ restored, writes: ctx.posts.filter(p => p === url).length, rows: before.rows.length, mg: before.totalMg },
|
|
|
|
|
{
|
|
|
|
|
restored: { grams: '2.000', memo: '复核寄存袋', verified: false },
|
|
|
|
|
writes: 0,
|
|
|
|
|
rows: 0,
|
|
|
|
|
mg: 0,
|
|
|
|
|
},
|
|
|
|
|
'恢复寄存资料后须重新确认,确认前不能写入寄存台账',
|
|
|
|
|
);
|
|
|
|
|
await field(form, 'verified').check();
|
|
|
|
|
await postFrom(page, url, () => submit(form));
|
2026-12-05 09:11:55 -08:00
|
|
|
assert.equal(ctx.posts.filter(p => p === url).length, 1, '重新核实后只提交一次');
|
2026-12-05 09:09:38 -08:00
|
|
|
const after = await session.get('owner', url);
|
|
|
|
|
assert.equal(after.rows.length, 1);
|
|
|
|
|
assert.equal(after.totalMg, 2000);
|
|
|
|
|
return { restored, rows: after.rows.length, mg: after.totalMg };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function inactiveRecipient(ctx) {
|
|
|
|
|
const { session, db, page } = ctx;
|
|
|
|
|
const id = await prepareExchange(session);
|
|
|
|
|
const { revision } = await session.get('owner', '/api/employees');
|
|
|
|
|
await session.post('owner', '/api/employees/assistant/deactivate', { revision, verified: true });
|
|
|
|
|
const form = await openEdit(ctx, id);
|
|
|
|
|
const recipient = field(form, 'payment_recipient');
|
|
|
|
|
const value = await recipient.inputValue();
|
|
|
|
|
const label = await recipient.locator('option:checked').innerText();
|
|
|
|
|
await field(form, 'verified').check();
|
|
|
|
|
await submit(form);
|
|
|
|
|
await form.locator('#recovery-form-error').getByText('请选择有效的收付款方式。').waitFor();
|
|
|
|
|
const original = ledger(db, id);
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
{ value, label, writes: ctx.posts.filter(p => p.endsWith('/update')).length, original },
|
|
|
|
|
{
|
|
|
|
|
value: '助理',
|
|
|
|
|
label: '娜娜(已停用 · 原记录)',
|
|
|
|
|
writes: 0,
|
|
|
|
|
original: { revision: 1, net: 20000, payments: [['助理微信', 20000]] },
|
|
|
|
|
},
|
|
|
|
|
'停用收款人应按历史资料展示,提交沿用旧版校验',
|
|
|
|
|
);
|
|
|
|
|
return { value, label, original };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function exercise(browser, version, scenario) {
|
|
|
|
|
const root = version === 'old' ? oldApp : app;
|
|
|
|
|
const { startServer } = createRequire(import.meta.url)(path.join(root, 'e2e/lib/server.js'));
|
|
|
|
|
const dataDir = fs.mkdtempSync(path.join(temp, version + '-'));
|
|
|
|
|
const server = startServer({ dataDir, clockStart });
|
|
|
|
|
let context, db;
|
|
|
|
|
try {
|
|
|
|
|
const { base } = await server.ready();
|
|
|
|
|
const session = new Session({ base, server });
|
|
|
|
|
await session.day(day);
|
|
|
|
|
await session.post('owner', '/api/setup', {
|
|
|
|
|
ownerPassword: 'recovery-check-owner',
|
|
|
|
|
assistantPassword: 'recovery-check-staff',
|
|
|
|
|
assistant2Password: 'recovery-check-other',
|
|
|
|
|
});
|
|
|
|
|
context = await browser.newContext({ timezoneId: 'Asia/Shanghai', locale: 'zh-CN' });
|
|
|
|
|
await context.addCookies([
|
|
|
|
|
{ name: 'mm_session', value: session.cookies.owner.slice('mm_session='.length), url: base },
|
|
|
|
|
]);
|
|
|
|
|
const page = await context.newPage();
|
|
|
|
|
await page.clock.setFixedTime(new Date(clockStart));
|
|
|
|
|
page.setDefaultTimeout(9000);
|
|
|
|
|
page.on('dialog', d => d.accept());
|
|
|
|
|
const posts = [],
|
|
|
|
|
errors = [];
|
|
|
|
|
page.on('request', req => {
|
|
|
|
|
if (req.method() === 'POST') posts.push(new URL(req.url()).pathname);
|
|
|
|
|
});
|
|
|
|
|
page.on('pageerror', error => errors.push(error.message));
|
|
|
|
|
db = new DatabaseSync(path.join(dataDir, 'ledger.sqlite'), { readOnly: true });
|
|
|
|
|
const result = await scenario({ page, db, session, base, version, posts });
|
|
|
|
|
assert.deepEqual(session.problems, []);
|
|
|
|
|
assert.deepEqual(errors, []);
|
|
|
|
|
return result;
|
|
|
|
|
} finally {
|
|
|
|
|
db?.close();
|
|
|
|
|
await context?.close();
|
|
|
|
|
await server.stop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
process.env.WEB_NEXT = '1';
|
|
|
|
|
let browser;
|
|
|
|
|
try {
|
|
|
|
|
browser = await chromium.launch();
|
|
|
|
|
const results = {};
|
|
|
|
|
const failures = [];
|
|
|
|
|
for (const [name, scenario] of Object.entries({ changedAmount, depositDraft, inactiveRecipient })) {
|
|
|
|
|
const both = await Promise.allSettled(['old', 'new'].map(version => exercise(browser, version, scenario)));
|
|
|
|
|
const evidence = Object.fromEntries(
|
|
|
|
|
both.map((r, i) => [i ? 'new' : 'old', r.status === 'fulfilled' ? r.value : { error: r.reason.message }]),
|
|
|
|
|
);
|
|
|
|
|
for (const [side, value] of Object.entries(evidence))
|
|
|
|
|
if (value.error) failures.push(name + ' ' + side + ': ' + value.error);
|
|
|
|
|
if (both.every(r => r.status === 'fulfilled')) assert.deepEqual(evidence.new, evidence.old, name);
|
|
|
|
|
results[name] = evidence;
|
|
|
|
|
}
|
|
|
|
|
assert.deepEqual(Object.keys(results), ['changedAmount', 'depositDraft', 'inactiveRecipient']);
|
|
|
|
|
const dir = path.join(app, 'e2e/out/recovery-confirmations');
|
|
|
|
|
fs.mkdirSync(dir, { recursive: true });
|
|
|
|
|
fs.writeFileSync(
|
|
|
|
|
path.join(dir, 'result.json'),
|
|
|
|
|
JSON.stringify({ reference, head: git('rev-parse', 'HEAD'), results, failures }, null, 2) + '\n',
|
|
|
|
|
);
|
|
|
|
|
assert.deepEqual(failures, [], '旧版与新版均须满足三项业务断言');
|
|
|
|
|
console.log('三项审查反例均通过,两端的确认状态、写入和实际账务结果一致');
|
|
|
|
|
} finally {
|
|
|
|
|
await browser?.close();
|
|
|
|
|
fs.rmSync(temp, { recursive: true, force: true });
|
|
|
|
|
}
|