2026-12-03 17:46:23 +08:00
|
|
|
'use strict';
|
2026-12-04 08:58:53 -08:00
|
|
|
const test = require('node:test'),
|
|
|
|
|
assert = require('node:assert/strict'),
|
|
|
|
|
{ randomUUID } = require('node:crypto'),
|
|
|
|
|
fs = require('node:fs'),
|
|
|
|
|
os = require('node:os'),
|
|
|
|
|
path = require('node:path');
|
|
|
|
|
const Print = require('../print-status'),
|
|
|
|
|
{ Ledger, today } = require('../ledger'),
|
|
|
|
|
{ createUser, createApp } = require('../server');
|
|
|
|
|
function fixture(t) {
|
|
|
|
|
const l = new Ledger(':memory:'),
|
|
|
|
|
actors = {};
|
|
|
|
|
t.after(() => l.close());
|
|
|
|
|
for (const member of ['owner', 'assistant', 'assistant2']) {
|
|
|
|
|
const role = member === 'owner' ? 'owner' : 'assistant',
|
|
|
|
|
name = '合成' + member;
|
|
|
|
|
actors[member] = { id: createUser(l, name, role, 'synthetic-print-password', member), name, role, member };
|
|
|
|
|
}
|
|
|
|
|
l.enableTeamMode();
|
|
|
|
|
const run = (input, fn, member = 'assistant', key = randomUUID()) =>
|
|
|
|
|
l.scope(actors[member], () => l.transact(key, actors[member], input, fn, { syncTeamFunds: false }));
|
|
|
|
|
const order = run({}, () =>
|
|
|
|
|
l.issueOrder(
|
|
|
|
|
{
|
|
|
|
|
date: today(),
|
|
|
|
|
customer: '合成打印客户',
|
|
|
|
|
salesperson: '合成经办',
|
|
|
|
|
internal_note: '测试打印状态',
|
|
|
|
|
payment_account: '助理微信',
|
|
|
|
|
lines: [
|
|
|
|
|
{
|
|
|
|
|
name: '足金999',
|
|
|
|
|
unit: 'g',
|
|
|
|
|
quantity: '1',
|
|
|
|
|
sale: '1000',
|
|
|
|
|
payer: 'assistant',
|
|
|
|
|
pocket: '',
|
|
|
|
|
costParts: { material: '800', labor: '10', accessories: '0', other: '0' },
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
fee_items: [],
|
|
|
|
|
},
|
|
|
|
|
actors.assistant,
|
|
|
|
|
),
|
|
|
|
|
).id;
|
|
|
|
|
const mark = (kind, id, printed = true, extra = {}, member = 'assistant', key = randomUUID()) => {
|
|
|
|
|
const current = Print.read(l, kind, id, actors[member]),
|
|
|
|
|
input = { printed, revision: current.revision, sourceRevision: current.sourceRevision, ...extra };
|
|
|
|
|
return run(input, () => Print.update(l, kind, id, input, actors[member]), member, key);
|
|
|
|
|
};
|
|
|
|
|
return { l, actors, order, run, mark };
|
2026-12-03 17:46:23 +08:00
|
|
|
}
|
2026-12-04 08:58:53 -08:00
|
|
|
const snapshot = l =>
|
|
|
|
|
JSON.stringify(
|
|
|
|
|
[
|
|
|
|
|
'orders',
|
|
|
|
|
'payments',
|
|
|
|
|
'expenses',
|
|
|
|
|
'movements',
|
|
|
|
|
'journal',
|
|
|
|
|
'price_locks',
|
|
|
|
|
'material_purchases',
|
|
|
|
|
'recovery_material_batches',
|
|
|
|
|
]
|
|
|
|
|
.filter(name => l.one("SELECT name FROM sqlite_master WHERE type='table' AND name=?", name))
|
|
|
|
|
.map(name => [name, l.rows('SELECT * FROM ' + name)]),
|
|
|
|
|
);
|
|
|
|
|
test('打开预览不是已打印;记录打印和撤销不改变订单账目、复核与库存', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
{ l, order, actors } = f;
|
|
|
|
|
l.audit(order, '打开打印页', actors.assistant, { notice: '不代表纸张已输出' });
|
|
|
|
|
assert.equal(Print.read(l, 'order', order, actors.assistant).printed, false);
|
|
|
|
|
const before = snapshot(l),
|
|
|
|
|
detail = l.detail(order),
|
|
|
|
|
financialRows = l.orderBook({ date: today() }).rows;
|
|
|
|
|
assert.equal(f.mark('order', order).printed, true);
|
|
|
|
|
assert.equal(Print.read(l, 'order', order, actors.assistant).printed, true);
|
|
|
|
|
assert.equal(l.orderBook({ date: today() }).displayRows.find(row => row.id === order).printStatus.printed, true);
|
|
|
|
|
assert.equal(f.mark('order', order, false).printed, false);
|
|
|
|
|
assert.equal(snapshot(l), before);
|
|
|
|
|
assert.deepEqual(l.orderBook({ date: today() }).rows, financialRows);
|
|
|
|
|
assert.equal(l.detail(order).revision, detail.revision);
|
|
|
|
|
assert.equal(l.detail(order).reviewed_revision, detail.reviewed_revision);
|
|
|
|
|
assert.equal(
|
|
|
|
|
l.rows("SELECT * FROM audit WHERE entity=? AND action IN ('记录打印操作','改为未打印')", order).length,
|
|
|
|
|
2,
|
|
|
|
|
);
|
|
|
|
|
assert.ok(!Object.hasOwn(l.publicOrder(order), 'printStatus'), '客户单据数据不带内部打印登记');
|
2026-12-03 17:46:23 +08:00
|
|
|
});
|
2026-12-04 08:58:53 -08:00
|
|
|
test('回收、置换与关联销售单共用打印状态;店员不能操作别人的单', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
{ l, actors } = f;
|
|
|
|
|
const input = {
|
|
|
|
|
mode: 'direct',
|
|
|
|
|
date: today(),
|
|
|
|
|
customer: '合成回收客户',
|
|
|
|
|
salesperson: '合成经办',
|
|
|
|
|
internal_note: '仅合成测试',
|
|
|
|
|
old_lines: [{ name: 'Au旧料', incoming_weight: '1', melted_weight: '1', purity: '100' }],
|
|
|
|
|
buy_rate: '800',
|
|
|
|
|
payer: 'assistant',
|
|
|
|
|
payment_account: '助理微信',
|
|
|
|
|
verified: true,
|
|
|
|
|
};
|
|
|
|
|
const direct = f.run(input, () => l.createRecovery(input, actors.assistant));
|
|
|
|
|
assert.equal(f.mark('recovery', direct.id).printed, true);
|
|
|
|
|
assert.equal(Print.read(l, 'recovery', direct.id, actors.assistant).printed, true);
|
|
|
|
|
assert.equal(l.orderBook({ date: today() }).displayRows.find(row => row.id === direct.id).printStatus.printed, true);
|
|
|
|
|
const exchangeInput = {
|
|
|
|
|
...input,
|
|
|
|
|
mode: 'exchange',
|
|
|
|
|
exchange: {
|
|
|
|
|
name: '足金999',
|
|
|
|
|
quantity: '1',
|
|
|
|
|
pocket: '',
|
|
|
|
|
gold_price: '900',
|
|
|
|
|
labor_rate: '20',
|
|
|
|
|
labor_mode: 'gram',
|
|
|
|
|
depreciation_rate: '10',
|
|
|
|
|
cost_material: { mode: 'gram', rate: '800' },
|
|
|
|
|
cost_labor: { mode: 'gram', rate: '5' },
|
|
|
|
|
cost_payer: 'assistant',
|
|
|
|
|
discount: '0',
|
|
|
|
|
rounding_mode: 'cent',
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
const exchange = f.run(exchangeInput, () => l.createRecovery(exchangeInput, actors.assistant));
|
|
|
|
|
f.mark('recovery', exchange.id);
|
|
|
|
|
assert.equal(Print.read(l, 'order', exchange.order_id, actors.assistant).printed, true);
|
|
|
|
|
f.mark('order', exchange.order_id, false);
|
|
|
|
|
assert.equal(Print.read(l, 'recovery', exchange.id, actors.assistant).printed, false);
|
|
|
|
|
for (const [kind, id] of [
|
|
|
|
|
['order', f.order],
|
|
|
|
|
['order', exchange.order_id],
|
|
|
|
|
['recovery', direct.id],
|
|
|
|
|
]) {
|
|
|
|
|
assert.throws(() => Print.read(l, kind, id, actors.assistant2));
|
|
|
|
|
assert.throws(() => f.mark(kind, id, true, {}, 'assistant2'));
|
|
|
|
|
assert.doesNotThrow(() => Print.read(l, kind, id, actors.owner));
|
|
|
|
|
}
|
2026-12-03 17:46:23 +08:00
|
|
|
});
|
2026-12-04 08:58:53 -08:00
|
|
|
test('旧状态和旧打印页不能覆盖新记录,重试幂等,失败事务回滚', t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
{ l, actors, order } = f,
|
|
|
|
|
original = Print.read(l, 'order', order, actors.assistant),
|
|
|
|
|
input = { printed: true, revision: 0, sourceRevision: original.sourceRevision },
|
|
|
|
|
key = randomUUID();
|
|
|
|
|
const apply = () => f.run(input, () => Print.update(l, 'order', order, input, actors.assistant), 'assistant', key);
|
|
|
|
|
assert.deepEqual(apply(), apply());
|
|
|
|
|
assert.equal(Print.read(l, 'order', order, actors.assistant).revision, 1);
|
|
|
|
|
assert.throws(() => f.mark('order', order, false, { revision: 0 }), { status: 409 });
|
|
|
|
|
l.run('UPDATE orders SET revision=revision+1 WHERE id=?', order);
|
|
|
|
|
assert.throws(() => f.mark('order', order, true, { sourceRevision: original.sourceRevision }), { status: 409 });
|
|
|
|
|
assert.throws(() =>
|
|
|
|
|
f.run({}, () => {
|
|
|
|
|
Print.update(
|
|
|
|
|
l,
|
|
|
|
|
'order',
|
|
|
|
|
order,
|
|
|
|
|
{ ...Print.read(l, 'order', order, actors.assistant), printed: false },
|
|
|
|
|
actors.assistant,
|
|
|
|
|
);
|
|
|
|
|
throw new Error('rollback');
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
assert.equal(Print.read(l, 'order', order, actors.assistant).printed, true);
|
2026-12-03 17:46:23 +08:00
|
|
|
});
|
2026-12-04 08:58:53 -08:00
|
|
|
test('HTTP打印页自动登记且无人工确认入口,接口鉴权、账号切换和重试正常', async t => {
|
|
|
|
|
const f = fixture(t),
|
|
|
|
|
dir = fs.mkdtempSync(path.join(os.tmpdir(), 'print-status-http-')),
|
|
|
|
|
app = createApp({
|
|
|
|
|
mode: 'trial',
|
|
|
|
|
ledger: f.l,
|
|
|
|
|
dataDir: dir,
|
|
|
|
|
networkAccess: { header: '', trustedProxies: [] },
|
|
|
|
|
feishuConfig: { enabled: false },
|
|
|
|
|
});
|
|
|
|
|
await new Promise(done => app.server.listen(0, '127.0.0.1', done));
|
|
|
|
|
t.after(async () => {
|
|
|
|
|
await new Promise(done => {
|
|
|
|
|
app.server.closeAllConnections();
|
|
|
|
|
app.server.close(done);
|
|
|
|
|
});
|
|
|
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
|
|
|
});
|
|
|
|
|
const base = 'http://127.0.0.1:' + app.server.address().port;
|
|
|
|
|
const login = await fetch(base + '/api/login', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json', 'X-Requested-With': 'maimai-ledger', Origin: base },
|
|
|
|
|
body: JSON.stringify({ name: f.actors.assistant.name, password: 'synthetic-print-password' }),
|
|
|
|
|
});
|
|
|
|
|
assert.equal(login.status, 200);
|
|
|
|
|
const headers = { Cookie: login.headers.get('set-cookie').split(';')[0], 'X-Ledger-User': f.actors.assistant.id },
|
|
|
|
|
url = base + '/api/print-status/order/' + f.order;
|
|
|
|
|
assert.equal((await fetch(url)).status, 401);
|
|
|
|
|
const html = await (await fetch(base + '/print/' + f.order, { headers })).text();
|
|
|
|
|
assert.doesNotMatch(html, /id="confirm-document-print"/);
|
|
|
|
|
assert.match(html, /id="retry-document-print" type="button" hidden/);
|
|
|
|
|
assert.match(html, /未打印/);
|
|
|
|
|
assert.match(html, /data-state=/);
|
|
|
|
|
const current = await (await fetch(url, { headers })).json(),
|
|
|
|
|
body = JSON.stringify({ printed: true, revision: current.revision, sourceRevision: current.sourceRevision }),
|
|
|
|
|
key = randomUUID(),
|
|
|
|
|
options = {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
...headers,
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
'X-Requested-With': 'maimai-ledger',
|
|
|
|
|
Origin: base,
|
|
|
|
|
'Idempotency-Key': key,
|
|
|
|
|
},
|
|
|
|
|
body,
|
|
|
|
|
};
|
|
|
|
|
assert.equal(
|
|
|
|
|
(await fetch(url, { ...options, headers: { ...options.headers, 'X-Ledger-User': f.actors.owner.id } })).status,
|
|
|
|
|
409,
|
|
|
|
|
);
|
|
|
|
|
assert.equal((await fetch(url, options)).status, 200);
|
|
|
|
|
assert.equal((await fetch(url, options)).status, 200);
|
|
|
|
|
assert.equal((await (await fetch(url, { headers })).json()).printed, true);
|
|
|
|
|
assert.equal(Print.read(f.l, 'order', f.order, f.actors.assistant).revision, 1);
|
2026-12-03 17:46:23 +08:00
|
|
|
});
|