仅为 npm run format 的输出;黄金对照、旧库兼容、浏览器流程与单元测试全部通过且零差异。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
537 lines
21 KiB
JavaScript
537 lines
21 KiB
JavaScript
'use strict';
|
|
const { test } = require('node:test'),
|
|
assert = require('node:assert/strict'),
|
|
{ randomUUID } = require('node:crypto');
|
|
const fs = require('node:fs'),
|
|
os = require('node:os'),
|
|
path = require('node:path'),
|
|
{ DatabaseSync } = require('node:sqlite');
|
|
const { Ledger } = require('../ledger'),
|
|
{ createApp } = require('../server');
|
|
const D = '2026-09-30',
|
|
NEXT = '2026-10-01',
|
|
SALE = '2026-10-02';
|
|
const owner = { id: 'purchase-edit-owner', name: '合成麦麦', role: 'owner' },
|
|
assistant = { id: 'purchase-edit-assistant', name: '合成助理', role: 'assistant' };
|
|
const item = (extra = {}) => ({
|
|
name: '足金999',
|
|
unit: 'g',
|
|
quantity: '3',
|
|
pocket: '',
|
|
payer: 'owner',
|
|
sale: '3000',
|
|
costParts: { material: '', labor: '0', accessories: '0', other: '0' },
|
|
costMaterial: { mode: 'pool', poolId: D },
|
|
...extra,
|
|
});
|
|
const contents = l =>
|
|
JSON.stringify(
|
|
l
|
|
.rows("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name")
|
|
.map(({ name }) => [name, l.rows('SELECT * FROM "' + name + '"')]),
|
|
);
|
|
function fixture(t) {
|
|
const l = new Ledger(':memory:');
|
|
t.after(() => l.close());
|
|
const run = (payload, fn, key = randomUUID(), actor = assistant) => l.transact(key, actor, payload, fn);
|
|
const buy = (extra = {}, key = randomUUID()) => {
|
|
const p = { date: D, grams: '10', total: '9000', payer: 'owner', note: '合成原始凭据', ...extra };
|
|
return run({ action: 'buy', p }, () => l.createMaterialPurchase(p, assistant), key);
|
|
};
|
|
const detail = id => l.materialPurchaseDetail(id).purchase;
|
|
const edit = (id, extra = {}, key = randomUUID(), actor = assistant) => {
|
|
const before = detail(id),
|
|
p = {
|
|
revision: before.revision,
|
|
date: before.date,
|
|
grams: (before.mg / 1000).toFixed(3),
|
|
total: (before.amount / 100).toFixed(2),
|
|
payer: before.payer,
|
|
note: before.note,
|
|
...extra,
|
|
};
|
|
return run({ action: 'edit', id, p }, () => l.editMaterialPurchase(id, p, actor), key, actor);
|
|
};
|
|
const remove = (id, extra = {}, key = randomUUID(), actor = assistant) => {
|
|
const p = { revision: detail(id).revision, ...extra };
|
|
return run({ action: 'delete', id, p }, () => l.deleteMaterialPurchase(id, p, actor), key, actor);
|
|
};
|
|
const issue = (line = item(), extra = {}) => {
|
|
const p = {
|
|
date: SALE,
|
|
customer: '合成客户',
|
|
salesperson: '合成开单人',
|
|
payment_account: '助理微信',
|
|
fee_items: [],
|
|
lines: [line],
|
|
...extra,
|
|
};
|
|
return run(p, () => l.issueOrder(p, assistant)).id;
|
|
};
|
|
return { l, run, buy, detail, edit, remove, issue };
|
|
}
|
|
test('旧购料表增量增加版本及软删除字段,原始字段不改写且重开仍可修改', t => {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'maimai-material-purchase-migration-')),
|
|
file = path.join(dir, 'ledger.sqlite');
|
|
t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
|
|
const legacy = new DatabaseSync(file),
|
|
id = randomUUID();
|
|
legacy.exec(
|
|
`CREATE TABLE material_purchases(id TEXT PRIMARY KEY,pool_id TEXT NOT NULL,date TEXT NOT NULL,mg INTEGER NOT NULL CHECK(mg>0),amount INTEGER NOT NULL CHECK(amount>0),payer TEXT NOT NULL CHECK(payer IN ('owner','assistant')),note TEXT NOT NULL,actor TEXT NOT NULL,created TEXT NOT NULL)`,
|
|
);
|
|
legacy
|
|
.prepare(
|
|
'INSERT INTO material_purchases(id,pool_id,date,mg,amount,payer,note,actor,created) VALUES (?,?,?,?,?,?,?,?,?)',
|
|
)
|
|
.run(id, D, D, 10000, 900000, 'assistant', '旧凭据保持原样', '原录入人', '2026-09-30T02:00:00.000Z');
|
|
const before = legacy.prepare('SELECT * FROM material_purchases').get();
|
|
legacy.close();
|
|
let l = new Ledger(file);
|
|
try {
|
|
assert.deepEqual(
|
|
l.one('SELECT id,pool_id,date,mg,amount,payer,note,actor,created FROM material_purchases WHERE id=?', id),
|
|
before,
|
|
);
|
|
const row = l.materialPurchaseDetail(id).purchase;
|
|
assert.equal(row.revision, 1);
|
|
assert.equal(row.deleted_at, '');
|
|
assert.equal(row.updated, '');
|
|
} finally {
|
|
l.close();
|
|
}
|
|
l = new Ledger(file);
|
|
try {
|
|
l.transact(randomUUID(), assistant, { id }, () =>
|
|
l.editMaterialPurchase(
|
|
id,
|
|
{ revision: 1, date: D, grams: '11', total: '9900', payer: 'owner', note: '核对后更正' },
|
|
assistant,
|
|
),
|
|
);
|
|
const after = l.materialPurchaseDetail(id).purchase;
|
|
assert.equal(after.revision, 2);
|
|
assert.equal(after.actor, before.actor);
|
|
assert.equal(after.created, before.created);
|
|
assert.equal(after.mg, 11000);
|
|
assert.equal(l.one('PRAGMA integrity_check').integrity_check, 'ok');
|
|
} finally {
|
|
l.close();
|
|
}
|
|
});
|
|
|
|
test('修改克重金额重算整池成本和付款人份额,原收款、客户单、库存与锁价单不变', t => {
|
|
const f = fixture(t),
|
|
first = f.buy();
|
|
f.buy({ grams: '20', total: '18120', payer: 'assistant' });
|
|
const id = f.issue();
|
|
const manual = f.issue(
|
|
item({
|
|
name: '手填黄金',
|
|
costParts: { material: '2000', labor: '30', accessories: '0', other: '0' },
|
|
costMaterial: { mode: 'total' },
|
|
}),
|
|
);
|
|
const lockInput = {
|
|
date: D,
|
|
customer: '合成锁价客户',
|
|
salesperson: '合成开单人',
|
|
line: { name: '足金挂件', unit: 'g', quantity: '2.000', goldPrice: '1000', laborRate: '20', laborMode: 'gram' },
|
|
material: { rate: '900', payer: 'owner', verified: true },
|
|
payment: { amount: '1000', account: '助理微信', verified: true },
|
|
};
|
|
f.run(lockInput, () => f.l.createPriceLock(lockInput, assistant));
|
|
const untouched = Object.fromEntries(
|
|
['payments', 'expenses', 'settlements', 'movements', 'price_locks', 'price_lock_events'].map(table => [
|
|
table,
|
|
f.l.rows('SELECT * FROM ' + table),
|
|
]),
|
|
);
|
|
const publicBefore = f.l.publicOrder(id),
|
|
manualBefore = f.l.detail(manual),
|
|
purchaseBefore = f.detail(first.id);
|
|
const result = f.edit(first.id, { grams: '12', total: '11400', note: '核实原始凭据' }),
|
|
o = f.l.detail(id),
|
|
pool = result.pool;
|
|
assert.equal(result.previousPoolId, D);
|
|
assert.equal(result.poolId, D);
|
|
assert.equal(pool.totalMg, 32000);
|
|
assert.equal(pool.totalAmount, 2952000);
|
|
assert.equal(pool.averageRate, 92250);
|
|
assert.equal(o.goodsCost, 276750);
|
|
assert.equal(o.profit, 23250);
|
|
assert.deepEqual(o.lines[0].materialPool.allocations, [{ payer: 'owner', amount: 276750 }]);
|
|
assert.equal(pool.consumedCost, 276750);
|
|
assert.equal(pool.remainingCost, 2675250);
|
|
assert.equal(pool.remainingMg, 29000);
|
|
assert.deepEqual(f.l.publicOrder(id), publicBefore);
|
|
assert.deepEqual(f.l.detail(manual), manualBefore);
|
|
for (const [table, rows] of Object.entries(untouched))
|
|
assert.deepEqual(f.l.rows('SELECT * FROM ' + table), rows, table);
|
|
assert.equal(f.detail(first.id).actor, purchaseBefore.actor);
|
|
assert.equal(f.detail(first.id).created, purchaseBefore.created);
|
|
assert.equal(f.detail(first.id).revision, 2);
|
|
const history = f.l.materialPurchaseDetail(first.id).history;
|
|
assert.equal(history[0].action, '修改实际购料');
|
|
assert.equal(history[0].actor, assistant.name);
|
|
assert.ok(history[0].created);
|
|
assert.deepEqual(history[0].detail.before, purchaseBefore);
|
|
assert.deepEqual(history[0].detail.after, f.detail(first.id));
|
|
});
|
|
|
|
test('只改付款人保持总成本利润,无差额利润流水,分摊与双方应结同步更新', t => {
|
|
const f = fixture(t),
|
|
purchase = f.buy(),
|
|
id = f.issue(),
|
|
before = f.l.detail(id),
|
|
journal = f.l.rows('SELECT * FROM journal');
|
|
const result = f.edit(purchase.id, { payer: 'assistant' }, randomUUID(), owner),
|
|
after = f.l.detail(id);
|
|
assert.equal(after.goodsCost, before.goodsCost);
|
|
assert.equal(after.profit, before.profit);
|
|
assert.equal(after.ownerCost, 0);
|
|
assert.equal(after.revision, before.revision + 1);
|
|
assert.deepEqual(after.lines[0].materialPool.allocations, [{ payer: 'assistant', amount: 270000 }]);
|
|
assert.deepEqual(f.l.rows('SELECT * FROM journal'), journal);
|
|
assert.equal(result.pool.byPayer.assistant.remaining, 630000);
|
|
assert.equal(after.settlementDue, 30000);
|
|
assert.equal(f.l.reportByOrderDate(SALE).settlementDue, -600000);
|
|
});
|
|
|
|
test('软删除最后一笔已耗用采购使全部订单待补,保留采购原文与完整删除审计', t => {
|
|
const f = fixture(t),
|
|
purchase = f.buy(),
|
|
a = f.issue(),
|
|
b = f.issue(),
|
|
before = f.detail(purchase.id),
|
|
payments = f.l.rows('SELECT * FROM payments');
|
|
const result = f.remove(purchase.id),
|
|
raw = f.detail(purchase.id),
|
|
pool = f.l.materialPoolDetail(D);
|
|
assert.equal(result.deleted, true);
|
|
assert.equal(raw.revision, 2);
|
|
assert.ok(raw.deleted_at);
|
|
assert.equal(raw.updated, raw.deleted_at);
|
|
assert.equal(raw.mg, before.mg);
|
|
assert.equal(raw.amount, before.amount);
|
|
assert.equal(raw.actor, before.actor);
|
|
assert.equal(raw.created, before.created);
|
|
assert.equal(f.l.rows('SELECT * FROM material_purchases').length, 1);
|
|
assert.equal(pool.purchases.length, 0);
|
|
assert.equal(pool.totalAmount, 0);
|
|
assert.equal(pool.totalMg, 0);
|
|
assert.equal(pool.usedMg, 6000);
|
|
assert.equal(pool.shortageMg, 6000);
|
|
assert.equal(pool.status, 'pending');
|
|
assert.equal(pool.remainingCost, null);
|
|
for (const id of [a, b]) {
|
|
const o = f.l.detail(id);
|
|
assert.equal(o.materialCostPending, true);
|
|
assert.equal(o.goodsCost, null);
|
|
assert.equal(o.profit, null);
|
|
assert.equal(o.settlementDue, null);
|
|
}
|
|
const report = f.l.reportByOrderDate(SALE);
|
|
assert.equal(report.pendingMaterialCount, 2);
|
|
assert.equal(report.profit, 0);
|
|
assert.equal(report.knownProfit, 0);
|
|
assert.equal(report.summary.unknownProfitCount, 2);
|
|
assert.ok(report.journal.every(row => row.goods === null && row.profit === null));
|
|
assert.deepEqual(f.l.rows('SELECT * FROM payments'), payments);
|
|
assert.equal(f.l.materialPoolReporting({ date: D }).purchases.length, 0);
|
|
assert.equal(f.l.materialPools({ date: D }).summary.purchaseCount, 0);
|
|
const event = f.l.materialPurchaseDetail(purchase.id).history[0];
|
|
assert.equal(event.action, '删除实际购料');
|
|
assert.deepEqual(event.detail.before, before);
|
|
assert.deepEqual(event.detail.after, raw);
|
|
const frozen = contents(f.l);
|
|
assert.throws(
|
|
() => f.edit(purchase.id),
|
|
error => error.status === 409 && /已删除/.test(error.message),
|
|
);
|
|
assert.throws(
|
|
() => f.remove(purchase.id),
|
|
error => error.status === 409,
|
|
);
|
|
assert.equal(contents(f.l), frozen);
|
|
});
|
|
|
|
test('删除未使用采购后不再出现空池,删除已用尽采购后仍保留待补用量', t => {
|
|
const f = fixture(t),
|
|
unused = f.buy();
|
|
f.remove(unused.id);
|
|
assert.equal(f.l.materialPools({ date: D }).rows.length, 0);
|
|
assert.equal(f.l.materialPoolSettlement().pendingCount, 0);
|
|
const exhausted = f.buy({ grams: '3', total: '2700' });
|
|
f.issue();
|
|
assert.equal(f.l.materialPoolDetail(D).status, 'exhausted');
|
|
f.remove(exhausted.id);
|
|
assert.equal(f.l.materialPoolDetail(D).status, 'pending');
|
|
assert.equal(f.l.materialPools({ date: D }).rows.length, 1);
|
|
});
|
|
|
|
test('修改采购日期分别重算两池,不迁移旧订单用量,补回原日期后自动恢复', t => {
|
|
const f = fixture(t),
|
|
purchase = f.buy(),
|
|
old = f.issue(),
|
|
next = f.issue(item({ costMaterial: { mode: 'pool', poolId: NEXT } }));
|
|
const cash = f.l.rows('SELECT * FROM payments'),
|
|
stock = f.l.rows('SELECT * FROM movements');
|
|
const result = f.edit(purchase.id, { date: NEXT });
|
|
assert.equal(result.poolId, NEXT);
|
|
assert.equal(result.previousPoolId, D);
|
|
assert.equal(f.l.materialPoolDetail(D).status, 'pending');
|
|
assert.equal(f.l.detail(old).lines[0].costMaterial.poolId, D);
|
|
assert.equal(f.l.detail(old).profit, null);
|
|
assert.equal(f.l.materialPoolDetail(NEXT).usedMg, 3000);
|
|
assert.equal(f.l.detail(next).goodsCost, 270000);
|
|
assert.equal(f.l.detail(next).profit, 30000);
|
|
assert.equal(f.l.materialPoolReporting({ date: D }).purchases.length, 0);
|
|
assert.equal(f.l.materialPoolReporting({ date: NEXT }).purchases.length, 1);
|
|
f.buy({ grams: '3', total: '2760' });
|
|
assert.equal(f.l.detail(old).goodsCost, 276000);
|
|
assert.equal(f.l.detail(old).profit, 24000);
|
|
assert.deepEqual(f.l.rows('SELECT * FROM payments'), cash);
|
|
assert.deepEqual(f.l.rows('SELECT * FROM movements'), stock);
|
|
});
|
|
|
|
test('采购修改与删除校验日期、克重、金额、付款人、备注、身份和精确版本,失败无写入', t => {
|
|
const f = fixture(t),
|
|
purchase = f.buy();
|
|
for (const patch of [
|
|
{ date: '2026-02-30' },
|
|
{ date: '2099-01-01' },
|
|
{ grams: '0' },
|
|
{ grams: '-1' },
|
|
{ grams: '0.0001' },
|
|
{ grams: '100000001' },
|
|
{ total: '0' },
|
|
{ total: '1.001' },
|
|
{ total: '1000000001' },
|
|
{ payer: 'someone' },
|
|
{ note: '字'.repeat(201) },
|
|
]) {
|
|
const before = contents(f.l);
|
|
assert.throws(
|
|
() => f.edit(purchase.id, patch),
|
|
error => error.status === 400,
|
|
JSON.stringify(patch),
|
|
);
|
|
assert.equal(contents(f.l), before);
|
|
}
|
|
for (const revision of [0, 2, '1', null, 1.1]) {
|
|
const before = contents(f.l);
|
|
assert.throws(
|
|
() => f.edit(purchase.id, { revision }),
|
|
error => error.status === 409,
|
|
);
|
|
assert.throws(
|
|
() => f.remove(purchase.id, { revision }),
|
|
error => error.status === 409,
|
|
);
|
|
assert.equal(contents(f.l), before);
|
|
}
|
|
const before = contents(f.l),
|
|
outsider = { id: 'outsider', name: '无权限', role: 'viewer' };
|
|
assert.throws(
|
|
() => f.edit(purchase.id, {}, randomUUID(), outsider),
|
|
error => error.status === 403,
|
|
);
|
|
assert.throws(
|
|
() => f.remove(purchase.id, {}, randomUUID(), outsider),
|
|
error => error.status === 403,
|
|
);
|
|
assert.throws(
|
|
() => f.l.editMaterialPurchase(randomUUID(), { revision: 1 }, assistant),
|
|
error => error.status === 404,
|
|
);
|
|
assert.throws(
|
|
() => f.l.deleteMaterialPurchase(purchase.id, null, assistant),
|
|
error => error.status === 400,
|
|
);
|
|
assert.equal(contents(f.l), before);
|
|
});
|
|
|
|
test('修改按目标日累计校验上限且不重复累加被替换原笔', t => {
|
|
const f = fixture(t),
|
|
huge = f.buy({ grams: '100000000', total: '1000000000' });
|
|
f.edit(huge.id, { note: '只更正凭据' });
|
|
assert.equal(f.detail(huge.id).revision, 2);
|
|
const next = f.buy({ date: NEXT, grams: '1', total: '1' }),
|
|
before = contents(f.l);
|
|
assert.throws(() => f.edit(next.id, { date: D }), /累计超出支持范围/);
|
|
assert.equal(contents(f.l), before);
|
|
assert.throws(() => f.edit(huge.id, { date: NEXT }), /累计超出支持范围/);
|
|
assert.equal(contents(f.l), before);
|
|
});
|
|
|
|
test('编辑删除网络重试返回原结果,旧版本和被复用操作号阻止覆盖', t => {
|
|
const f = fixture(t),
|
|
purchase = f.buy(),
|
|
editKey = randomUUID(),
|
|
patch = { revision: 1, total: '10000' },
|
|
first = f.edit(purchase.id, patch, editKey);
|
|
assert.equal(JSON.stringify(f.edit(purchase.id, patch, editKey)), JSON.stringify(first));
|
|
assert.equal(f.detail(purchase.id).revision, 2);
|
|
f.edit(purchase.id, { total: '11000' });
|
|
const before = contents(f.l);
|
|
assert.equal(JSON.stringify(f.edit(purchase.id, patch, editKey)), JSON.stringify(first));
|
|
assert.equal(contents(f.l), before);
|
|
assert.throws(
|
|
() => f.edit(purchase.id, { revision: 1, total: '12000' }),
|
|
error => error.status === 409,
|
|
);
|
|
assert.throws(
|
|
() => f.edit(purchase.id, { ...patch, total: '12000' }, editKey),
|
|
error => error.status === 409,
|
|
);
|
|
const deleteKey = randomUUID(),
|
|
revision = f.detail(purchase.id).revision,
|
|
deleted = f.remove(purchase.id, { revision }, deleteKey);
|
|
assert.equal(JSON.stringify(f.remove(purchase.id, { revision }, deleteKey)), JSON.stringify(deleted));
|
|
assert.equal(JSON.stringify(f.edit(purchase.id, patch, editKey)), JSON.stringify(first));
|
|
assert.equal(f.detail(purchase.id).revision, revision + 1);
|
|
assert.equal(
|
|
f.l.rows("SELECT * FROM audit WHERE entity=? AND action='删除实际购料'", 'material-purchase:' + purchase.id).length,
|
|
1,
|
|
);
|
|
});
|
|
|
|
test('重算和最后审计发生故障时采购、订单、利润、同步版本及操作号完整回滚', t => {
|
|
const f = fixture(t),
|
|
purchase = f.buy();
|
|
f.issue();
|
|
const before = contents(f.l),
|
|
originalJournal = f.l.journal;
|
|
f.l.journal = function (...args) {
|
|
originalJournal.apply(this, args);
|
|
throw new Error('合成成本故障');
|
|
};
|
|
try {
|
|
assert.throws(() => f.edit(purchase.id, { total: '10000' }), /合成成本故障/);
|
|
assert.throws(() => f.remove(purchase.id), /合成成本故障/);
|
|
} finally {
|
|
f.l.journal = originalJournal;
|
|
}
|
|
assert.equal(contents(f.l), before);
|
|
const originalAudit = f.l.audit;
|
|
f.l.audit = function (...args) {
|
|
originalAudit.apply(this, args);
|
|
if (args[0].startsWith('material-purchase:')) throw new Error('合成审计故障');
|
|
};
|
|
try {
|
|
assert.throws(
|
|
() =>
|
|
f.l.editMaterialPurchase(
|
|
purchase.id,
|
|
{ revision: 1, date: D, grams: '10', total: '10000', payer: 'owner', note: '' },
|
|
assistant,
|
|
),
|
|
/合成审计故障/,
|
|
);
|
|
assert.throws(() => f.l.deleteMaterialPurchase(purchase.id, { revision: 1 }, assistant), /合成审计故障/);
|
|
} finally {
|
|
f.l.audit = originalAudit;
|
|
}
|
|
assert.equal(contents(f.l), before);
|
|
assert.equal(f.l.db.isTransaction, false);
|
|
});
|
|
|
|
test('HTTP修改删除需要登录CSRF有效操作号,双方角色可用,冲突与重试保持幂等', async t => {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'maimai-material-purchase-edit-http-')),
|
|
app = createApp({ mode: 'demo', dataDir: dir, 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);
|
|
});
|
|
app.ledger.close();
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
});
|
|
const base = 'http://127.0.0.1:' + app.server.address().port;
|
|
let cookie = '';
|
|
const call = (route, data, key = randomUUID(), extra = {}) =>
|
|
fetch(base + route, {
|
|
method: data === undefined ? 'GET' : 'POST',
|
|
headers: {
|
|
Cookie: cookie,
|
|
...(data === undefined
|
|
? {}
|
|
: {
|
|
'Content-Type': 'application/json',
|
|
Origin: base,
|
|
'X-Requested-With': 'maimai-ledger',
|
|
'Idempotency-Key': key,
|
|
}),
|
|
...extra,
|
|
},
|
|
...(data === undefined ? {} : { body: JSON.stringify(data) }),
|
|
});
|
|
const id = randomUUID();
|
|
assert.equal((await call('/api/material-purchases/' + id)).status, 401);
|
|
assert.equal((await call('/api/material-purchases/' + id + '/edit', { revision: 1 })).status, 401);
|
|
assert.equal((await call('/api/material-purchases/' + id + '/delete', { revision: 1 })).status, 401);
|
|
const assistantLogin = await call('/api/demo-login', { role: 'assistant' });
|
|
cookie = assistantLogin.headers.get('set-cookie').split(';')[0];
|
|
const createdResponse = await call('/api/material-purchases', {
|
|
date: D,
|
|
grams: '10',
|
|
total: '9000',
|
|
payer: 'owner',
|
|
allocations: [{ member: 'owner', amount: '9000' }],
|
|
note: 'HTTP合成凭据',
|
|
});
|
|
assert.equal(createdResponse.status, 200);
|
|
const created = await createdResponse.json(),
|
|
route = '/api/material-purchases/' + created.id;
|
|
const detailResponse = await call(route);
|
|
assert.equal(detailResponse.status, 200);
|
|
const detail = await detailResponse.json();
|
|
assert.equal(detail.purchase.revision, 1);
|
|
assert.equal(detail.purchase.deleted_at, '');
|
|
assert.equal(detail.history.length, 1);
|
|
for (const secret of ['salt', 'hash', 'password', 'sessions'])
|
|
assert.ok(!JSON.stringify(detail).includes('"' + secret + '"'));
|
|
const edit = {
|
|
revision: 1,
|
|
date: D,
|
|
grams: '12',
|
|
total: '11000',
|
|
payer: 'assistant',
|
|
allocations: [{ member: 'assistant', amount: '11000' }],
|
|
note: '核对实付',
|
|
};
|
|
assert.equal((await call(route + '/edit', edit, randomUUID(), { Origin: 'https://invalid.example' })).status, 403);
|
|
assert.equal((await call(route + '/delete', { revision: 1 }, randomUUID(), { 'X-Requested-With': '' })).status, 403);
|
|
assert.equal((await call(route + '/edit', edit, '')).status, 400);
|
|
assert.equal(app.ledger.materialPurchaseDetail(created.id).purchase.revision, 1);
|
|
const key = randomUUID(),
|
|
responses = await Promise.all([call(route + '/edit', edit, key), call(route + '/edit', edit, key)]);
|
|
assert.deepEqual(
|
|
responses.map(r => r.status),
|
|
[200, 200],
|
|
);
|
|
const first = await responses[0].json();
|
|
assert.deepEqual(await responses[1].json(), first);
|
|
assert.equal((await call(route + '/edit', { ...edit, total: '12000' })).status, 409);
|
|
assert.equal((await call(route + '/delete', { revision: 1 })).status, 409);
|
|
const login = await call('/api/demo-login', { role: 'owner' });
|
|
cookie = login.headers.get('set-cookie').split(';')[0];
|
|
const deleteKey = randomUUID(),
|
|
removed = await call(route + '/delete', { revision: 2 }, deleteKey);
|
|
assert.equal(removed.status, 200);
|
|
const removedValue = await removed.json();
|
|
assert.equal(removedValue.deleted, true);
|
|
assert.deepEqual(await (await call(route + '/delete', { revision: 2 }, deleteKey)).json(), removedValue);
|
|
assert.equal((await call(route + '/edit', { ...edit, revision: 3 })).status, 409);
|
|
assert.equal((await call(route + '/delete', { revision: 3 })).status, 409);
|
|
const final = await (await call(route)).json();
|
|
assert.equal(final.purchase.revision, 3);
|
|
assert.ok(final.purchase.deleted_at);
|
|
assert.equal(final.history.length, 3);
|
|
assert.equal(final.pool.purchases.length, 0);
|
|
assert.equal((await call('/api/material-purchases/' + id)).status, 404);
|
|
assert.equal((await call(route + '/edit')).status, 404);
|
|
assert.equal(app.ledger.rows('SELECT * FROM material_purchases').length, 1);
|
|
});
|