2026-12-04 03:20:49 +08:00
|
|
|
'use strict';
|
|
|
|
|
const test = require('node:test'),
|
|
|
|
|
assert = require('node:assert/strict'),
|
|
|
|
|
fs = require('node:fs'),
|
|
|
|
|
path = require('node:path'),
|
|
|
|
|
vm = require('node:vm');
|
|
|
|
|
const script = fs
|
|
|
|
|
.readFileSync(path.join(__dirname, '../public/print.js'), 'utf8')
|
|
|
|
|
.split('// 以点击页面“打印”为已打印依据;')[1];
|
|
|
|
|
function fixture(fetcher, { printed = false } = {}) {
|
|
|
|
|
const state = {
|
|
|
|
|
kind: 'order',
|
|
|
|
|
id: 'synthetic-order',
|
|
|
|
|
user: 'original-user',
|
|
|
|
|
printed,
|
|
|
|
|
revision: 0,
|
|
|
|
|
sourceRevision: 5,
|
|
|
|
|
},
|
|
|
|
|
handlers = {},
|
|
|
|
|
calls = [],
|
|
|
|
|
messages = [];
|
|
|
|
|
const label = { dataset: { state: JSON.stringify(state) }, textContent: printed ? '✓ 已打印' : '未打印' },
|
|
|
|
|
retry = { hidden: true, disabled: false, addEventListener: (key, fn) => (handlers[key] = fn) },
|
|
|
|
|
error = { textContent: '' };
|
|
|
|
|
const context = vm.createContext({
|
|
|
|
|
window: {},
|
|
|
|
|
document: {
|
|
|
|
|
getElementById: id =>
|
|
|
|
|
({ 'document-print-state': label, 'retry-document-print': retry, 'document-print-error': error })[id],
|
|
|
|
|
},
|
|
|
|
|
crypto: { randomUUID: () => 'same-operation' },
|
|
|
|
|
AbortSignal,
|
|
|
|
|
localStorage: { setItem: (key, value) => messages.push(JSON.parse(value)) },
|
|
|
|
|
fetch: async (url, options) => {
|
|
|
|
|
calls.push({ url, ...options });
|
|
|
|
|
return fetcher(url, options, calls.length);
|
2026-12-04 08:58:53 -08:00
|
|
|
},
|
2026-12-04 03:20:49 +08:00
|
|
|
});
|
|
|
|
|
vm.runInContext('// 以点击页面“打印”为已打印依据;' + script, context);
|
|
|
|
|
return {
|
|
|
|
|
calls,
|
|
|
|
|
messages,
|
|
|
|
|
label,
|
|
|
|
|
retry,
|
|
|
|
|
error,
|
|
|
|
|
click: () => context.window.recordDocumentPrint(),
|
|
|
|
|
retrySave: () => handlers.click(),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
const response = (status, body) => ({ ok: status === 200, status, json: async () => body });
|
|
|
|
|
const success = () =>
|
|
|
|
|
response(200, { printed: true, revision: 1, sourceRevision: 5, kind: 'order', id: 'synthetic-order' });
|
|
|
|
|
test('仅打开预览不写入;打印后自动记录,多次点击不反转状态', async () => {
|
|
|
|
|
const f = fixture(success);
|
|
|
|
|
assert.equal(f.calls.length, 0);
|
|
|
|
|
await f.click();
|
|
|
|
|
assert.equal(f.calls.length, 1);
|
|
|
|
|
assert.equal(JSON.parse(f.calls[0].body).printed, true);
|
|
|
|
|
assert.equal(f.calls[0].headers['X-Ledger-User'], 'original-user');
|
|
|
|
|
assert.equal(f.label.textContent, '✓ 已打印');
|
|
|
|
|
assert.equal(f.messages[0].user, 'original-user');
|
|
|
|
|
await f.click();
|
|
|
|
|
assert.equal(f.calls.length, 1);
|
|
|
|
|
});
|
|
|
|
|
test('响应丢失复用操作号自动重试,不重复记录', async () => {
|
|
|
|
|
const f = fixture((url, options, n) => {
|
|
|
|
|
if (n === 1) throw Error('网络断开');
|
|
|
|
|
return success();
|
|
|
|
|
});
|
|
|
|
|
await f.click();
|
|
|
|
|
assert.equal(f.calls.length, 2);
|
|
|
|
|
assert.equal(f.calls[0].body, f.calls[1].body);
|
|
|
|
|
assert.equal(f.calls[0].headers['Idempotency-Key'], f.calls[1].headers['Idempotency-Key']);
|
|
|
|
|
assert.equal(f.retry.hidden, true);
|
|
|
|
|
assert.equal(f.label.textContent, '✓ 已打印');
|
|
|
|
|
});
|
|
|
|
|
test('持续中断显示失败与重试入口,不伪造已打印;恢复仍用原操作', async () => {
|
|
|
|
|
const f = fixture((url, options, n) => {
|
|
|
|
|
if (n < 3) throw Error('连接失败');
|
|
|
|
|
return success();
|
|
|
|
|
});
|
|
|
|
|
await f.click();
|
|
|
|
|
assert.equal(f.label.textContent, '未打印');
|
|
|
|
|
assert.equal(f.retry.hidden, false);
|
|
|
|
|
await f.click();
|
|
|
|
|
assert.equal(f.label.textContent, '✓ 已打印');
|
|
|
|
|
assert.equal(f.calls[0].headers['Idempotency-Key'], f.calls[2].headers['Idempotency-Key']);
|
|
|
|
|
});
|
|
|
|
|
test('其他预览已记录时读取最新状态,单据变动或切换账号则要求刷新', async () => {
|
|
|
|
|
const f = fixture((url, options) => (options.method ? response(409, { error: '状态已变化' }) : success()));
|
|
|
|
|
await f.click();
|
|
|
|
|
assert.equal(f.label.textContent, '✓ 已打印');
|
|
|
|
|
assert.equal(f.calls.length, 2);
|
|
|
|
|
const stale = fixture((url, options) =>
|
|
|
|
|
options.method ? response(409, { error: '单据已变化' }) : response(200, { printed: true, sourceRevision: 6 }),
|
|
|
|
|
);
|
|
|
|
|
await stale.click();
|
|
|
|
|
assert.equal(stale.label.textContent, '未打印');
|
|
|
|
|
assert.match(stale.error.textContent, /刷新/);
|
|
|
|
|
assert.equal(stale.retry.hidden, true);
|
|
|
|
|
const switched = fixture(() => response(409, { code: 'ACCOUNT_CHANGED', error: '账号已切换' }));
|
|
|
|
|
await switched.click();
|
|
|
|
|
assert.equal(switched.calls.length, 1);
|
|
|
|
|
assert.match(switched.error.textContent, /刷新/);
|
|
|
|
|
assert.equal(switched.messages.length, 0);
|
|
|
|
|
});
|