2026-10-22 12:59:56 +08:00
|
|
|
'use strict';
|
|
|
|
|
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 { createApp } = require('../server');
|
|
|
|
|
function businessSnapshot(ledger) {
|
|
|
|
|
const tables = [
|
|
|
|
|
'orders',
|
|
|
|
|
'payments',
|
|
|
|
|
'expenses',
|
|
|
|
|
'journal',
|
|
|
|
|
'movements',
|
|
|
|
|
'settlements',
|
|
|
|
|
'sync',
|
|
|
|
|
'closes',
|
|
|
|
|
'inventory_snapshots',
|
|
|
|
|
'price_locks',
|
|
|
|
|
'price_lock_events',
|
|
|
|
|
'material_purchases',
|
|
|
|
|
'material_pools',
|
|
|
|
|
'material_pool_usages',
|
|
|
|
|
'material_pool_settlements',
|
|
|
|
|
];
|
|
|
|
|
return Object.fromEntries(
|
|
|
|
|
tables
|
|
|
|
|
.filter(name => ledger.one("SELECT name FROM sqlite_master WHERE type='table' AND name=?", name))
|
|
|
|
|
.map(name => [name, ledger.rows('SELECT * FROM ' + name)]),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
async function fixture(t) {
|
|
|
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'maimai-print-settings-')),
|
|
|
|
|
app = createApp({ mode: 'demo', dataDir: dir, port: 0 });
|
|
|
|
|
await new Promise(resolve => app.server.listen(0, '127.0.0.1', resolve));
|
2026-12-04 08:52:50 -08:00
|
|
|
t.after(async () => {
|
|
|
|
|
await new Promise(resolve => {
|
|
|
|
|
app.server.closeAllConnections();
|
|
|
|
|
app.server.close(resolve);
|
|
|
|
|
});
|
|
|
|
|
app.ledger.close();
|
|
|
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
|
|
|
});
|
2026-10-22 12:59:56 +08:00
|
|
|
const base = 'http://127.0.0.1:' + app.server.address().port;
|
|
|
|
|
let cookie = '';
|
|
|
|
|
const call = (route, input) =>
|
|
|
|
|
fetch(base + route, {
|
|
|
|
|
method: input ? 'POST' : 'GET',
|
|
|
|
|
headers: {
|
|
|
|
|
Cookie: cookie,
|
|
|
|
|
...(input
|
2026-12-04 08:58:53 -08:00
|
|
|
? {
|
2026-10-22 12:59:56 +08:00
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
Origin: base,
|
|
|
|
|
'X-Requested-With': 'maimai-ledger',
|
|
|
|
|
'Idempotency-Key': randomUUID(),
|
2026-12-04 08:58:53 -08:00
|
|
|
}
|
2026-10-22 12:59:56 +08:00
|
|
|
: {}),
|
|
|
|
|
},
|
|
|
|
|
...(input ? { body: JSON.stringify(input) } : {}),
|
|
|
|
|
});
|
|
|
|
|
cookie = (await call('/api/demo-login', { role: 'owner' })).headers.get('set-cookie').split(';')[0];
|
|
|
|
|
return { app, call };
|
|
|
|
|
}
|
|
|
|
|
test('读取旧打印设置只规范化返回241×140,不回写旧meta或业务账', async t => {
|
|
|
|
|
const { app, call } = await fixture(t),
|
|
|
|
|
old = {
|
|
|
|
|
store: '历史店名',
|
|
|
|
|
subtitle: '历史标题',
|
|
|
|
|
width: 300,
|
|
|
|
|
height: 400,
|
|
|
|
|
offsetX: 2,
|
|
|
|
|
offsetY: 3,
|
|
|
|
|
contact: '测试联系信息',
|
|
|
|
|
rows: 5,
|
|
|
|
|
rowHeight: 12,
|
|
|
|
|
contentTop: 14,
|
|
|
|
|
};
|
|
|
|
|
app.ledger.setMeta('printing', old);
|
|
|
|
|
const before = businessSnapshot(app.ledger),
|
|
|
|
|
audit = app.ledger.rows('SELECT * FROM audit');
|
|
|
|
|
const response = await call('/api/state');
|
|
|
|
|
assert.equal(response.status, 200);
|
|
|
|
|
const state = await response.json();
|
|
|
|
|
assert.deepEqual(state.printing, { ...old, width: 241, height: 140 });
|
|
|
|
|
assert.deepEqual(app.ledger.meta('printing'), old);
|
|
|
|
|
assert.deepEqual(businessSnapshot(app.ledger), before);
|
|
|
|
|
assert.deepEqual(app.ledger.rows('SELECT * FROM audit'), audit);
|
|
|
|
|
const html = await (await call('/print/' + state.orders[0].id)).text();
|
|
|
|
|
assert.match(html, /@page\{size:241mm 140mm;margin:0\}/);
|
|
|
|
|
assert.match(html, /历史店名/);
|
|
|
|
|
assert.match(html, /data-layout-width-mm="217" data-layout-height-mm="115"/);
|
|
|
|
|
assert.deepEqual(app.ledger.meta('printing'), old);
|
|
|
|
|
assert.deepEqual(businessSnapshot(app.ledger), before);
|
|
|
|
|
});
|
|
|
|
|
test('新表单不带尺寸和旧客户端只改尺寸均保存固定纸型,保留可调设置及所有业务账', async t => {
|
|
|
|
|
const { app, call } = await fixture(t),
|
|
|
|
|
old = {
|
|
|
|
|
store: '原店名',
|
|
|
|
|
subtitle: '保留标题',
|
|
|
|
|
contact: '保留联系信息',
|
|
|
|
|
width: 140,
|
|
|
|
|
height: 241,
|
|
|
|
|
offsetX: 3,
|
|
|
|
|
offsetY: 4,
|
|
|
|
|
contentTop: 16,
|
|
|
|
|
rowHeight: 11,
|
|
|
|
|
rows: 6,
|
|
|
|
|
};
|
|
|
|
|
app.ledger.setMeta('printing', old);
|
|
|
|
|
const before = businessSnapshot(app.ledger);
|
|
|
|
|
const input = { store: '更新店名', offsetY: 5 };
|
|
|
|
|
assert.equal((await call('/api/printing', input)).status, 200);
|
2026-11-14 01:16:35 +08:00
|
|
|
const expected = { ...old, ...input, width: 241, height: 140 },
|
|
|
|
|
{ store, subtitle, contact, ...layout } = expected;
|
|
|
|
|
assert.deepEqual(app.ledger.meta('printing'), layout);
|
|
|
|
|
assert.equal(app.ledger.shopSettings().storeName, store);
|
|
|
|
|
assert.equal(app.ledger.shopSettings().receiptSubtitle, subtitle);
|
|
|
|
|
assert.equal(app.ledger.shopSettings().contact, contact);
|
2026-10-22 12:59:56 +08:00
|
|
|
for (const sizes of [
|
|
|
|
|
{ width: 300, height: 400 },
|
|
|
|
|
{ width: '无效旧宽度', height: -1 },
|
|
|
|
|
]) {
|
|
|
|
|
assert.equal((await call('/api/printing', sizes)).status, 200);
|
2026-11-14 01:16:35 +08:00
|
|
|
assert.deepEqual(app.ledger.meta('printing'), layout);
|
2026-10-22 12:59:56 +08:00
|
|
|
const state = await (await call('/api/state')).json();
|
|
|
|
|
assert.deepEqual(state.printing, expected);
|
|
|
|
|
}
|
|
|
|
|
assert.deepEqual(businessSnapshot(app.ledger), before);
|
|
|
|
|
});
|