2026-10-31 14:59:30 +08:00
|
|
|
|
'use strict';
|
|
|
|
|
|
const test = require('node:test'),
|
|
|
|
|
|
assert = require('node:assert/strict'),
|
|
|
|
|
|
{ inflateRawSync, crc32 } = require('node:zlib'),
|
|
|
|
|
|
{ randomUUID } = require('node:crypto'),
|
|
|
|
|
|
fs = require('node:fs'),
|
|
|
|
|
|
os = require('node:os'),
|
|
|
|
|
|
path = require('node:path');
|
|
|
|
|
|
const { Ledger } = require('../ledger'),
|
|
|
|
|
|
{ createApp, createUser } = require('../server'),
|
|
|
|
|
|
{ inventoryMovementsFile, MIME } = require('../inventory-movements-export');
|
|
|
|
|
|
const D = '2026-10-30';
|
|
|
|
|
|
function unzip(buffer) {
|
|
|
|
|
|
assert.equal(buffer.readUInt32LE(0), 0x04034b50);
|
|
|
|
|
|
const end = buffer.length - 22;
|
|
|
|
|
|
assert.equal(buffer.readUInt32LE(end), 0x06054b50);
|
|
|
|
|
|
const count = buffer.readUInt16LE(end + 10),
|
|
|
|
|
|
start = buffer.readUInt32LE(end + 16),
|
|
|
|
|
|
size = buffer.readUInt32LE(end + 12),
|
|
|
|
|
|
files = {};
|
|
|
|
|
|
let offset = start;
|
|
|
|
|
|
for (let i = 0; i < count; i++) {
|
|
|
|
|
|
assert.equal(buffer.readUInt32LE(offset), 0x02014b50);
|
|
|
|
|
|
const length = buffer.readUInt16LE(offset + 28),
|
|
|
|
|
|
extra = buffer.readUInt16LE(offset + 30),
|
|
|
|
|
|
comment = buffer.readUInt16LE(offset + 32),
|
|
|
|
|
|
filename = buffer.subarray(offset + 46, offset + 46 + length).toString('utf8'),
|
|
|
|
|
|
local = buffer.readUInt32LE(offset + 42),
|
|
|
|
|
|
compressedSize = buffer.readUInt32LE(offset + 20),
|
|
|
|
|
|
dataSize = buffer.readUInt32LE(offset + 24),
|
|
|
|
|
|
crc = buffer.readUInt32LE(offset + 16);
|
|
|
|
|
|
assert.equal(buffer.readUInt32LE(local), 0x04034b50);
|
|
|
|
|
|
assert.equal(buffer.readUInt16LE(local + 8), 8);
|
|
|
|
|
|
assert.equal(buffer.readUInt32LE(local + 14), crc);
|
|
|
|
|
|
assert.equal(buffer.readUInt32LE(local + 18), compressedSize);
|
|
|
|
|
|
assert.equal(buffer.readUInt32LE(local + 22), dataSize);
|
|
|
|
|
|
const dataStart = local + 30 + buffer.readUInt16LE(local + 26) + buffer.readUInt16LE(local + 28),
|
|
|
|
|
|
data = inflateRawSync(buffer.subarray(dataStart, dataStart + compressedSize));
|
|
|
|
|
|
assert.equal(data.length, dataSize);
|
|
|
|
|
|
assert.equal(crc32(data), crc);
|
|
|
|
|
|
files[filename] = data.toString('utf8');
|
|
|
|
|
|
offset += 46 + length + extra + comment;
|
|
|
|
|
|
}
|
|
|
|
|
|
assert.equal(offset, start + size);
|
|
|
|
|
|
return files;
|
|
|
|
|
|
}
|
|
|
|
|
|
const decode = value =>
|
|
|
|
|
|
value.replace(/&(amp|lt|gt|quot|apos);/g, (_, name) => ({ amp: '&', lt: '<', gt: '>', quot: '"', apos: "'" })[name]);
|
|
|
|
|
|
function sheetRows(files) {
|
|
|
|
|
|
return [...files['xl/worksheets/sheet1.xml'].matchAll(/<row\b[^>]*>(.*?)<\/row>/gs)].map(match =>
|
|
|
|
|
|
[...match[1].matchAll(/<c\b([^>]*)>(.*?)<\/c>/gs)].map(cell =>
|
|
|
|
|
|
cell[1].includes('t="n"')
|
|
|
|
|
|
? Number(cell[2].match(/<v>(.*?)<\/v>/s)[1])
|
|
|
|
|
|
: decode(cell[2].match(/<t\b[^>]*>(.*?)<\/t>/s)[1]),
|
2026-12-04 08:58:53 -08:00
|
|
|
|
),
|
2026-10-31 14:59:30 +08:00
|
|
|
|
);
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-31 14:59:30 +08:00
|
|
|
|
const row = (extra = {}) => ({
|
|
|
|
|
|
date: D,
|
|
|
|
|
|
pocket: '现货',
|
|
|
|
|
|
mg: 1840,
|
|
|
|
|
|
kind: 'increase',
|
|
|
|
|
|
note: '合成库存',
|
|
|
|
|
|
actor: '合成经办人',
|
|
|
|
|
|
created: '2026-10-30T17:04:47.680Z',
|
|
|
|
|
|
manual: true,
|
|
|
|
|
|
current: true,
|
|
|
|
|
|
status: 'active',
|
|
|
|
|
|
...extra,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('真正的XLSX ZIP/XML可解压且CRC正确,中文emoji/XML字符保留,文本不变成公式,克重为带3位格式的数值', () => {
|
|
|
|
|
|
const notes = [
|
|
|
|
|
|
'=SUM(1,2)',
|
|
|
|
|
|
'+HYPERLINK("https://example.invalid")',
|
|
|
|
|
|
'-1+2',
|
|
|
|
|
|
'@SUM(A1:A2)',
|
|
|
|
|
|
'中文😀<&>\'"\n第二行\t对齐\x01',
|
|
|
|
|
|
];
|
|
|
|
|
|
const file = inventoryMovementsFile(
|
2026-12-04 08:58:53 -08:00
|
|
|
|
{
|
2026-10-31 14:59:30 +08:00
|
|
|
|
pocket: '现货',
|
|
|
|
|
|
kind: 'all',
|
|
|
|
|
|
movements: notes.map((note, i) =>
|
|
|
|
|
|
row({
|
|
|
|
|
|
note,
|
|
|
|
|
|
mg: i === 0 ? -1840 : i === 1 ? 0 : 1234,
|
|
|
|
|
|
purchase_cost: 9827346,
|
|
|
|
|
|
order_id: 'PRIVATE-ORDER-ID',
|
|
|
|
|
|
pendingRequestId: 'PRIVATE-APPROVAL-ID',
|
|
|
|
|
|
privateProfit: 991927,
|
|
|
|
|
|
}),
|
2026-12-04 08:58:53 -08:00
|
|
|
|
),
|
2026-10-31 14:59:30 +08:00
|
|
|
|
},
|
2026-12-04 08:58:53 -08:00
|
|
|
|
D,
|
|
|
|
|
|
),
|
2026-10-31 14:59:30 +08:00
|
|
|
|
files = unzip(file.buffer),
|
|
|
|
|
|
xml = Object.values(files).join('\n'),
|
|
|
|
|
|
rows = sheetRows(files);
|
|
|
|
|
|
assert.equal(file.contentType, MIME);
|
|
|
|
|
|
assert.equal(file.filename, '库存流水-现货-全部流水-2026-10-30.xlsx');
|
|
|
|
|
|
assert.equal(Object.keys(files).length, 6);
|
|
|
|
|
|
assert.ok(files['_rels/.rels'].includes('Target="xl/workbook.xml"'));
|
|
|
|
|
|
assert.ok(files['xl/_rels/workbook.xml.rels'].includes('Target="worksheets/sheet1.xml"'));
|
|
|
|
|
|
assert.match(files['xl/styles.xml'], /formatCode="\+0\.000;-0\.000;0\.000"/);
|
2026-11-08 15:47:07 +08:00
|
|
|
|
assert.deepEqual(rows[0], [
|
|
|
|
|
|
'序号',
|
|
|
|
|
|
'日期',
|
|
|
|
|
|
'货盘',
|
|
|
|
|
|
'增减克重(g)',
|
|
|
|
|
|
'类型',
|
|
|
|
|
|
'备注/原单',
|
|
|
|
|
|
'经办人',
|
|
|
|
|
|
'记录时间',
|
|
|
|
|
|
'状态',
|
|
|
|
|
|
'增减对数(对)',
|
|
|
|
|
|
]);
|
|
|
|
|
|
assert.equal(rows[1][3], -1.84);
|
|
|
|
|
|
assert.equal(rows[2][3], 0);
|
|
|
|
|
|
assert.equal(rows[3][3], 1.234);
|
|
|
|
|
|
assert.equal(rows[1][7], '2026-10-31 01:04:47');
|
2026-10-31 14:59:30 +08:00
|
|
|
|
for (let i = 0; i < notes.length; i++) {
|
|
|
|
|
|
assert.equal(rows[i + 1][5], notes[i].replace('\x01', ''));
|
|
|
|
|
|
assert.equal(typeof rows[i + 1][0], 'number');
|
|
|
|
|
|
assert.equal(typeof rows[i + 1][3], 'number');
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-31 14:59:30 +08:00
|
|
|
|
assert.doesNotMatch(xml, /<f\b|PRIVATE-|9827346|991927|purchase_cost|order_id|pendingRequestId/);
|
|
|
|
|
|
assert.match(files['xl/worksheets/sheet1.xml'], /<c r="F2" t="inlineStr">/);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('导出保留修改、删除、冲正和待审批状态,所有已知业务类型中文化,空结果只有表头', () => {
|
|
|
|
|
|
const movements = [
|
|
|
|
|
|
row({ current: false, status: 'modified' }),
|
|
|
|
|
|
row({ current: false, status: 'deleted' }),
|
|
|
|
|
|
row({ reversal: true, kind: 'manual_reversal', note: '不应导出的内部UUID' }),
|
|
|
|
|
|
row({ status: 'modified' }),
|
|
|
|
|
|
row({ status: 'modified', pendingApproval: true }),
|
|
|
|
|
|
row({ kind: 'order_cancel_reverse', manual: false }),
|
|
|
|
|
|
row({ kind: 'lock_purchase', manual: false }),
|
|
|
|
|
|
row({ kind: 'order_edit_reverse', manual: false }),
|
|
|
|
|
|
row({ kind: 'recovery', manual: false }),
|
|
|
|
|
|
];
|
|
|
|
|
|
const rows = sheetRows(unzip(inventoryMovementsFile({ pocket: '', kind: 'all', movements }, D).buffer));
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
|
rows.slice(1, 6).map(row => row[8]),
|
|
|
|
|
|
['原记录 · 已修改', '原记录 · 已删除', '更正记录 · 撤回旧记录的库存影响', '修改后有效', '修改后有效;待麦麦审批'],
|
|
|
|
|
|
);
|
|
|
|
|
|
assert.equal(rows[3][5], '撤回旧记录的库存影响');
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
|
rows.slice(6).map(row => row[4]),
|
|
|
|
|
|
['订单作废冲回', '锁价黄金入库', '改单出库冲回', '回收旧料入库'],
|
2026-11-08 15:47:07 +08:00
|
|
|
|
);
|
|
|
|
|
|
const empty = sheetRows(unzip(inventoryMovementsFile({ pocket: '金料', kind: 'transfer', movements: [] }, D).buffer));
|
|
|
|
|
|
assert.equal(empty.length, 1);
|
|
|
|
|
|
assert.equal(empty[0].length, 10);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('对数独立导出为整数,旧记录未登记与不计对数区分,成品入库类型中文化', () => {
|
|
|
|
|
|
const movements = [
|
|
|
|
|
|
row({ stockCount: 2, countUnit: 'pair', mg: 2000, kind: 'lock_finished' }),
|
|
|
|
|
|
row({ stockCount: -1, countUnit: 'pair', mg: -1100 }),
|
|
|
|
|
|
row({ stockCount: null, countUnit: 'pair' }),
|
|
|
|
|
|
row({ stockCount: null, countUnit: null }),
|
|
|
|
|
|
];
|
|
|
|
|
|
const files = unzip(inventoryMovementsFile({ pocket: '', kind: 'all', movements }, D).buffer),
|
|
|
|
|
|
rows = sheetRows(files);
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
|
rows.slice(1).map(row => row[9]),
|
|
|
|
|
|
[2, -1, '未登记', ''],
|
|
|
|
|
|
);
|
|
|
|
|
|
assert.equal(rows[1][4], '锁价成品入库');
|
|
|
|
|
|
assert.match(files['xl/worksheets/sheet1.xml'], /<c r="J2" s="0" t="n"><v>2<\/v>/);
|
|
|
|
|
|
assert.match(files['xl/worksheets/sheet1.xml'], /dimension ref="A1:J5"/);
|
|
|
|
|
|
assert.match(files['xl/worksheets/sheet1.xml'], /autoFilter ref="A1:J5"/);
|
2026-10-31 14:59:30 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
async function fixture(t) {
|
|
|
|
|
|
const l = new Ledger(':memory:'),
|
|
|
|
|
|
dir = fs.mkdtempSync(path.join(os.tmpdir(), 'maimai-inventory-xlsx-')),
|
|
|
|
|
|
actors = {};
|
|
|
|
|
|
for (const member of ['owner', 'assistant', 'assistant2']) {
|
|
|
|
|
|
const role = member === 'owner' ? 'owner' : 'assistant',
|
|
|
|
|
|
name = '合成' + member;
|
|
|
|
|
|
actors[member] = { id: createUser(l, name, role, 'synthetic-stock-export-password', member), name, role, member };
|
2026-12-04 08:58:53 -08:00
|
|
|
|
}
|
2026-10-31 14:59:30 +08:00
|
|
|
|
l.enableTeamMode();
|
|
|
|
|
|
const stock = (input = {}, member = 'owner') => {
|
|
|
|
|
|
const value = { date: D, kind: 'increase', pocket: '现货', grams: '1', note: '合成流水', ...input },
|
|
|
|
|
|
result = l.stock(value, actors[member]);
|
|
|
|
|
|
return l.one('SELECT * FROM movements WHERE group_id=? ORDER BY rowid', result.group);
|
|
|
|
|
|
};
|
|
|
|
|
|
stock({ grams: '100' });
|
|
|
|
|
|
for (let i = 0; i < 123; i++)
|
|
|
|
|
|
stock(
|
|
|
|
|
|
{ grams: '0.001', pocket: i % 4 ? '现货' : '金料', note: '完整序号' + i },
|
|
|
|
|
|
['owner', 'assistant', 'assistant2'][i % 3],
|
|
|
|
|
|
);
|
|
|
|
|
|
const original = stock({ kind: 'purchase', cost: '9327.46', note: '=中文公式外观😀<&>' }, 'assistant2'),
|
|
|
|
|
|
pending = l.updateInventoryMovement(
|
|
|
|
|
|
original.id,
|
2026-12-04 08:58:53 -08:00
|
|
|
|
{
|
2026-10-31 14:59:30 +08:00
|
|
|
|
...l.inventoryMovement(original.id, actors.assistant2).values,
|
|
|
|
|
|
revision: 1,
|
|
|
|
|
|
grams: '1',
|
|
|
|
|
|
reason: 'PRIVATE-APPROVAL-CONTENT',
|
|
|
|
|
|
},
|
|
|
|
|
|
actors.assistant2,
|
2026-11-19 00:55:38 +08:00
|
|
|
|
);
|
|
|
|
|
|
const deletion = stock({ note: '合成待审批删除' }, 'assistant'),
|
|
|
|
|
|
pendingDelete = l.deleteInventoryMovement(
|
|
|
|
|
|
deletion.id,
|
|
|
|
|
|
{ revision: 1, verified: true, reason: 'PRIVATE-DELETE-APPROVAL' },
|
|
|
|
|
|
actors.assistant,
|
2026-10-31 14:59:30 +08:00
|
|
|
|
);
|
|
|
|
|
|
const modified = stock({ note: '合成已修改' });
|
|
|
|
|
|
l.updateInventoryMovement(
|
|
|
|
|
|
modified.id,
|
|
|
|
|
|
{ ...l.inventoryMovement(modified.id, actors.owner).values, revision: 1, grams: '2' },
|
|
|
|
|
|
actors.owner,
|
2026-12-04 08:58:53 -08:00
|
|
|
|
);
|
2026-10-31 14:59:30 +08:00
|
|
|
|
const deleted = stock({ note: '合成已删除' });
|
|
|
|
|
|
l.deleteInventoryMovement(deleted.id, { revision: 1, verified: true }, actors.owner);
|
|
|
|
|
|
stock({ kind: 'transfer', to: '金料', grams: '1.234', note: '合成平移' });
|
|
|
|
|
|
const input = {
|
|
|
|
|
|
date: D,
|
|
|
|
|
|
customer: 'PRIVATE-ORDER-CUSTOMER',
|
|
|
|
|
|
salesperson: '合成开单人',
|
|
|
|
|
|
payment_account: '麦麦微信',
|
|
|
|
|
|
fee_items: [],
|
|
|
|
|
|
lines: [
|
|
|
|
|
|
{ name: 'PRIVATE-PRODUCT', unit: 'g', quantity: '0.2', pocket: '现货', payer: 'owner', cost: '1', sale: '2' },
|
2026-12-04 08:58:53 -08:00
|
|
|
|
],
|
|
|
|
|
|
},
|
2026-10-31 14:59:30 +08:00
|
|
|
|
order = l.issueOrder(input, actors.owner).id;
|
2026-12-04 08:52:50 -08:00
|
|
|
|
const app = createApp({ mode: 'trial', port: 0, dataDir: dir, ledger: l, feishuConfig: { enabled: false } });
|
|
|
|
|
|
await new Promise(resolve => app.server.listen(0, '127.0.0.1', resolve));
|
|
|
|
|
|
t.after(async () => {
|
|
|
|
|
|
await new Promise(resolve => {
|
|
|
|
|
|
app.server.closeAllConnections();
|
|
|
|
|
|
app.server.close(resolve);
|
|
|
|
|
|
});
|
|
|
|
|
|
l.close();
|
|
|
|
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
|
|
|
|
});
|
|
|
|
|
|
const base = 'http://127.0.0.1:' + app.server.address().port,
|
|
|
|
|
|
cookies = {};
|
2026-10-31 14:59:30 +08:00
|
|
|
|
for (const member of Object.keys(actors)) {
|
|
|
|
|
|
const response = await fetch(base + '/api/login', {
|
|
|
|
|
|
method: 'POST',
|
|
|
|
|
|
headers: { 'Content-Type': 'application/json', 'X-Requested-With': 'maimai-ledger', Origin: base },
|
|
|
|
|
|
body: JSON.stringify({ name: actors[member].name, password: 'synthetic-stock-export-password' }),
|
|
|
|
|
|
});
|
|
|
|
|
|
assert.equal(response.status, 200);
|
|
|
|
|
|
cookies[member] = response.headers.get('set-cookie').split(';')[0];
|
|
|
|
|
|
}
|
|
|
|
|
|
const call = (route, member = 'owner', headers = {}) =>
|
|
|
|
|
|
fetch(base + route, { headers: { ...(cookies[member] ? { Cookie: cookies[member] } : {}), ...headers } });
|
2026-11-19 00:55:38 +08:00
|
|
|
|
return { l, actors, call, pending, pendingDelete, order };
|
2026-10-31 14:59:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-12-04 12:44:30 -08:00
|
|
|
|
// 登录与导出的安全审计(及其自增序号)不属于业务数据。
|
|
|
|
|
|
function businessRows(l, name) {
|
|
|
|
|
|
if (name === 'audit')
|
|
|
|
|
|
return l.rows("SELECT * FROM audit WHERE entity NOT LIKE 'auth:%' AND entity<>'export' ORDER BY rowid");
|
|
|
|
|
|
if (name === 'sqlite_sequence') return l.rows("SELECT * FROM sqlite_sequence WHERE name<>'audit' ORDER BY rowid");
|
|
|
|
|
|
return l.rows('SELECT * FROM "' + name + '" ORDER BY rowid');
|
|
|
|
|
|
}
|
2026-10-31 14:59:30 +08:00
|
|
|
|
test('三账号HTTP下载所有筛后记录超过100条,与分页排序一致且不导出成本/订单详情/审批ID,不改数据库', async t => {
|
|
|
|
|
|
const f = await fixture(t),
|
|
|
|
|
|
path = '/api/inventory/movements/export.xlsx',
|
|
|
|
|
|
tables = f.l.rows("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name").map(row => row.name),
|
2026-12-04 12:44:30 -08:00
|
|
|
|
snapshot = () => JSON.stringify(tables.map(name => [name, businessRows(f.l, name)])),
|
2026-10-31 14:59:30 +08:00
|
|
|
|
before = snapshot();
|
|
|
|
|
|
assert.equal((await f.call(path, 'none')).status, 401);
|
|
|
|
|
|
let previous;
|
|
|
|
|
|
for (const member of ['owner', 'assistant', 'assistant2']) {
|
|
|
|
|
|
const response = await f.call(path, member);
|
|
|
|
|
|
assert.equal(response.status, 200);
|
|
|
|
|
|
assert.equal(response.headers.get('content-type'), MIME);
|
|
|
|
|
|
assert.match(decodeURIComponent(response.headers.get('content-disposition')), /库存流水-全部货盘-全部流水-/);
|
|
|
|
|
|
const buffer = Buffer.from(await response.arrayBuffer());
|
|
|
|
|
|
assert.equal(Number(response.headers.get('content-length')), buffer.length);
|
|
|
|
|
|
if (previous) assert.deepEqual(buffer, previous);
|
|
|
|
|
|
previous = buffer;
|
|
|
|
|
|
const files = unzip(buffer),
|
|
|
|
|
|
rows = sheetRows(files),
|
|
|
|
|
|
expected = f.l.inventoryMovementsExport({}, f.actors[member]).movements;
|
|
|
|
|
|
assert.ok(rows.length > 101);
|
|
|
|
|
|
assert.equal(rows.length, expected.length + 1);
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
|
rows.slice(1).map(row => row[3]),
|
|
|
|
|
|
expected.map(row => row.mg / 1000),
|
|
|
|
|
|
);
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
|
rows.slice(1).map(row => row[5]),
|
|
|
|
|
|
expected.map(row => (row.reversal || row.kind === 'manual_reversal' ? '撤回旧记录的库存影响' : row.note)),
|
|
|
|
|
|
);
|
|
|
|
|
|
assert.doesNotMatch(
|
|
|
|
|
|
Object.values(files).join('\n'),
|
|
|
|
|
|
/932746|9327\.46|PRIVATE-|purchase_cost|order_id|pendingRequestId/,
|
|
|
|
|
|
);
|
|
|
|
|
|
assert.ok(!Object.values(files).join('\n').includes(f.pending.requestId));
|
|
|
|
|
|
assert.ok(!Object.values(files).join('\n').includes(f.order));
|
|
|
|
|
|
const page = await (await f.call('/api/inventory/movements?page=2', member)).json();
|
|
|
|
|
|
assert.equal(page.shown, Math.min(100, expected.length - 100));
|
|
|
|
|
|
assert.equal(page.total, expected.length);
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
|
page.movements.map(row => row.id),
|
|
|
|
|
|
expected.slice(100, 200).map(row => row.id),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
assert.equal(snapshot(), before);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('货盘和5种类型沿用分页筛选,导出不受page限制;空筛选有表头、非法筛选和换账号拒绝', async t => {
|
|
|
|
|
|
const f = await fixture(t);
|
|
|
|
|
|
for (const pocket of ['', '现货', '金料', '君宝汇'])
|
|
|
|
|
|
for (const kind of ['all', 'increase', 'decrease', 'transfer', 'manual']) {
|
|
|
|
|
|
const query = new URLSearchParams({ pocket, kind, page: '2' }),
|
|
|
|
|
|
response = await f.call('/api/inventory/movements/export.xlsx?' + query, 'assistant');
|
|
|
|
|
|
assert.equal(response.status, 200);
|
|
|
|
|
|
const rows = sheetRows(unzip(Buffer.from(await response.arrayBuffer()))),
|
|
|
|
|
|
expected = f.l.inventoryMovementsExport({ pocket, kind }, f.actors.assistant).movements;
|
|
|
|
|
|
assert.equal(rows.length, expected.length + 1);
|
|
|
|
|
|
assert.ok(rows.slice(1).every(row => !pocket || row[2] === pocket));
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
|
rows.slice(1).map(row => row[3]),
|
|
|
|
|
|
expected.map(row => row.mg / 1000),
|
|
|
|
|
|
);
|
|
|
|
|
|
const page = await (await f.call('/api/inventory/movements?' + query, 'assistant')).json();
|
|
|
|
|
|
assert.equal(page.total, expected.length);
|
|
|
|
|
|
}
|
2026-11-19 00:55:38 +08:00
|
|
|
|
for (const query of ['kind=unknown', 'pocket=不存在', 'hideDeleted=1', 'hideDeleted=', 'hideDeleted=TRUE']) {
|
|
|
|
|
|
assert.equal((await f.call('/api/inventory/movements/export.xlsx?' + query)).status, 400);
|
|
|
|
|
|
assert.equal((await f.call('/api/inventory/movements?' + query)).status, 400);
|
|
|
|
|
|
}
|
2026-10-31 14:59:30 +08:00
|
|
|
|
assert.equal(
|
|
|
|
|
|
(await f.call('/api/inventory/movements/export.xlsx', 'assistant', { 'X-Ledger-User': f.actors.owner.id })).status,
|
|
|
|
|
|
409,
|
|
|
|
|
|
);
|
|
|
|
|
|
assert.equal((await f.call('/api/export/inventory', 'assistant')).status, 403);
|
|
|
|
|
|
});
|
2026-11-19 00:55:38 +08:00
|
|
|
|
|
|
|
|
|
|
test('三账号HTTP隐藏删除与分页、Excel共用完整筛选,保留待删除审批及有效更正,不写数据库', async t => {
|
|
|
|
|
|
const f = await fixture(t),
|
|
|
|
|
|
tables = f.l.rows("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name").map(row => row.name),
|
2026-12-04 12:44:30 -08:00
|
|
|
|
snapshot = () => JSON.stringify(tables.map(name => [name, businessRows(f.l, name)])),
|
2026-11-19 00:55:38 +08:00
|
|
|
|
before = snapshot();
|
|
|
|
|
|
const all = f.l.inventoryMovementsExport({}, f.actors.owner).movements;
|
|
|
|
|
|
assert.equal(all.filter(row => row.status === 'deleted').length, 2);
|
|
|
|
|
|
for (const member of ['owner', 'assistant', 'assistant2'])
|
|
|
|
|
|
for (const kind of ['all', 'manual']) {
|
|
|
|
|
|
const query = new URLSearchParams({ kind, hideDeleted: 'true', page: '2' }),
|
|
|
|
|
|
response = await f.call('/api/inventory/movements/export.xlsx?' + query, member);
|
|
|
|
|
|
assert.equal(response.status, 200);
|
|
|
|
|
|
const rows = sheetRows(unzip(Buffer.from(await response.arrayBuffer()))),
|
|
|
|
|
|
expected = all.filter(row => row.status !== 'deleted' && (kind === 'all' || (row.manual && !row.reversal)));
|
|
|
|
|
|
assert.ok(expected.length > 100);
|
|
|
|
|
|
assert.equal(rows.length, expected.length + 1);
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
|
rows.slice(1).map(row => row[3]),
|
|
|
|
|
|
expected.map(row => row.mg / 1000),
|
|
|
|
|
|
);
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
|
rows.slice(1).map(row => row[5]),
|
|
|
|
|
|
expected.map(row => (row.reversal ? '撤回旧记录的库存影响' : row.note)),
|
|
|
|
|
|
);
|
|
|
|
|
|
assert.ok(rows.some(row => row[5] === '合成待审批删除' && row[8].includes('待麦麦审批')));
|
|
|
|
|
|
assert.ok(!rows.some(row => row[8].includes('已删除')));
|
|
|
|
|
|
assert.ok(rows.some(row => row[8].includes('修改后有效')));
|
|
|
|
|
|
const pageResponse = await f.call('/api/inventory/movements?' + query, member);
|
|
|
|
|
|
assert.equal(pageResponse.status, 200);
|
|
|
|
|
|
const page = await pageResponse.json();
|
|
|
|
|
|
assert.equal(page.hideDeleted, true);
|
|
|
|
|
|
assert.equal(page.total, expected.length);
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
|
page.movements.map(row => row.id),
|
|
|
|
|
|
expected.slice(100, 200).map(row => row.id),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
const restored = await (await f.call('/api/inventory/movements?hideDeleted=false')).json();
|
|
|
|
|
|
assert.equal(restored.hideDeleted, false);
|
|
|
|
|
|
assert.equal(restored.total, all.length);
|
|
|
|
|
|
assert.equal(snapshot(), before);
|
|
|
|
|
|
});
|
2026-12-01 14:27:27 +08:00
|
|
|
|
|
|
|
|
|
|
test('日期区间包含起止当天,与货盘类型及隐藏删除同时筛选,Excel导出全部页且不改库存', async t => {
|
|
|
|
|
|
const f = await fixture(t),
|
|
|
|
|
|
before = f.l.inventory();
|
|
|
|
|
|
const add = (date, pocket = '现货', kind = 'increase') =>
|
|
|
|
|
|
f.l.move({ group: randomUUID(), date, pocket, mg: 1, kind, note: '日期边界测试' }, f.actors.owner);
|
|
|
|
|
|
add('2026-10-29');
|
|
|
|
|
|
add('2026-10-31');
|
|
|
|
|
|
add('2026-11-01');
|
|
|
|
|
|
add('2026-10-31', '金料');
|
|
|
|
|
|
add('2026-10-31', '现货', 'sale');
|
|
|
|
|
|
const inventory = f.l.inventory();
|
|
|
|
|
|
assert.notDeepEqual(inventory, before);
|
|
|
|
|
|
for (const range of [
|
|
|
|
|
|
{ from: D, to: '2026-10-31' },
|
|
|
|
|
|
{ from: D, to: D },
|
|
|
|
|
|
{ from: '2026-11-01' },
|
|
|
|
|
|
{ to: '2026-10-29' },
|
|
|
|
|
|
{ from: '2026-12-01', to: '2026-12-31' },
|
|
|
|
|
|
{},
|
|
|
|
|
|
]) {
|
|
|
|
|
|
const query = { ...range, pocket: '现货', kind: 'increase', hideDeleted: 'true', page: '2' },
|
|
|
|
|
|
params = new URLSearchParams(query);
|
|
|
|
|
|
const all = f.l.inventoryMovementsExport(
|
|
|
|
|
|
{ pocket: '现货', kind: 'increase', hideDeleted: true },
|
|
|
|
|
|
f.actors.owner,
|
|
|
|
|
|
).movements;
|
|
|
|
|
|
const expected = all.filter(r => (!range.from || r.date >= range.from) && (!range.to || r.date <= range.to));
|
|
|
|
|
|
const page = await (await f.call('/api/inventory/movements?' + params, 'assistant')).json();
|
|
|
|
|
|
assert.equal(page.total, expected.length);
|
|
|
|
|
|
assert.equal(page.from, range.from || '');
|
|
|
|
|
|
assert.equal(page.to, range.to || '');
|
|
|
|
|
|
const response = await f.call('/api/inventory/movements/export.xlsx?' + params, 'assistant');
|
|
|
|
|
|
assert.equal(response.status, 200);
|
|
|
|
|
|
if (range.from)
|
|
|
|
|
|
assert.ok(decodeURIComponent(response.headers.get('content-disposition')).includes(range.from + '至'));
|
|
|
|
|
|
const rows = sheetRows(unzip(Buffer.from(await response.arrayBuffer())));
|
|
|
|
|
|
assert.equal(rows.length, expected.length + 1);
|
|
|
|
|
|
assert.deepEqual(
|
|
|
|
|
|
rows.slice(1).map(r => r[1]),
|
|
|
|
|
|
expected.map(r => r.date),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
for (const query of ['from=2026-10-31&to=2026-10-30', 'from=2026-02-30', 'to=bad', 'from=2026-2-01'])
|
|
|
|
|
|
for (const endpoint of ['', '/export.xlsx'])
|
|
|
|
|
|
assert.equal((await f.call('/api/inventory/movements' + endpoint + '?' + query)).status, 400);
|
|
|
|
|
|
assert.deepEqual(f.l.inventory(), inventory);
|
|
|
|
|
|
});
|