2026-10-25 14:19:24 +08:00
|
|
|
'use strict';
|
2026-12-05 07:38:51 -08:00
|
|
|
const { createBrowserContext } = require('./fixtures/browser-context.cjs');
|
2026-10-25 14:19:24 +08:00
|
|
|
const test = require('node:test'),
|
|
|
|
|
assert = require('node:assert/strict'),
|
|
|
|
|
fs = require('node:fs'),
|
|
|
|
|
path = require('node:path'),
|
|
|
|
|
vm = require('node:vm'),
|
|
|
|
|
{ randomUUID } = require('node:crypto');
|
|
|
|
|
const Calc = require('../public/calculations'),
|
|
|
|
|
Fees = require('../public/fee-estimates'),
|
|
|
|
|
Templates = require('../public/order-templates');
|
|
|
|
|
const D = '2026-10-25',
|
|
|
|
|
members = ['owner', 'assistant', 'assistant2'];
|
|
|
|
|
function pair(a = 'owner', b = 'assistant', extra = {}) {
|
|
|
|
|
return {
|
|
|
|
|
id: a + ':' + b,
|
|
|
|
|
members: [a, b],
|
|
|
|
|
net: 10000,
|
|
|
|
|
obligationNet: 15000,
|
|
|
|
|
receivedForNet: 20000,
|
|
|
|
|
advancedNet: -5000,
|
|
|
|
|
offset: 5000,
|
|
|
|
|
transferred: 5000,
|
|
|
|
|
due: 10000,
|
|
|
|
|
fromMember: a,
|
|
|
|
|
toMember: b,
|
|
|
|
|
direction: 'transfer',
|
|
|
|
|
period: { obligationNet: 15000, transferred: 5000 },
|
|
|
|
|
entries: [],
|
|
|
|
|
...extra,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
function report(self = 'owner') {
|
2026-12-04 08:58:53 -08:00
|
|
|
return {
|
|
|
|
|
from: D,
|
|
|
|
|
to: D,
|
2026-10-25 14:19:24 +08:00
|
|
|
revision: 'revision-1',
|
|
|
|
|
members: members.map(id => ({ id, name: id })),
|
|
|
|
|
balances: [],
|
|
|
|
|
pairs: [pair('owner', 'assistant'), pair('owner', 'assistant2'), pair('assistant', 'assistant2')].filter(
|
|
|
|
|
row => self === 'owner' || row.members.includes(self),
|
2026-12-04 08:58:53 -08:00
|
|
|
),
|
2026-10-25 14:19:24 +08:00
|
|
|
transfers: [],
|
|
|
|
|
};
|
2026-12-04 08:58:53 -08:00
|
|
|
}
|
2026-10-25 14:19:24 +08:00
|
|
|
function harness(member = 'owner') {
|
|
|
|
|
const nodes = new Map(),
|
|
|
|
|
saved = new Map(),
|
2026-12-04 08:58:53 -08:00
|
|
|
posts = [],
|
2026-10-25 14:19:24 +08:00
|
|
|
handlers = {};
|
2026-11-06 01:29:35 +08:00
|
|
|
const user = {
|
2026-10-25 14:19:24 +08:00
|
|
|
id: 'synthetic-' + member,
|
2026-12-04 08:58:53 -08:00
|
|
|
member,
|
2026-10-25 14:19:24 +08:00
|
|
|
name: member === 'owner' ? '麦麦' : member === 'assistant' ? '助理1' : '助理2',
|
|
|
|
|
role: member === 'owner' ? 'owner' : 'assistant',
|
2026-12-04 08:58:53 -08:00
|
|
|
};
|
2026-12-05 07:38:51 -08:00
|
|
|
const context = createBrowserContext({
|
2026-10-25 14:19:24 +08:00
|
|
|
console,
|
|
|
|
|
URLSearchParams,
|
|
|
|
|
LedgerCalc: Calc,
|
|
|
|
|
FeeEstimates: Fees,
|
|
|
|
|
OrderTemplates: Templates,
|
|
|
|
|
crypto: { randomUUID },
|
|
|
|
|
setInterval() {},
|
|
|
|
|
setTimeout() {},
|
|
|
|
|
clearTimeout() {},
|
|
|
|
|
window: { scrollTo() {} },
|
|
|
|
|
document: {
|
|
|
|
|
querySelector: q => nodes.get(q) || null,
|
|
|
|
|
querySelectorAll: () => [],
|
|
|
|
|
addEventListener: (type, fn) => (handlers[type] ??= []).push(fn),
|
2026-12-04 08:58:53 -08:00
|
|
|
},
|
2026-10-25 14:19:24 +08:00
|
|
|
FormData: class {
|
|
|
|
|
constructor(form) {
|
|
|
|
|
return Object.entries(form.values || {});
|
2026-12-04 08:58:53 -08:00
|
|
|
}
|
|
|
|
|
},
|
2026-10-25 14:19:24 +08:00
|
|
|
localStorage: {
|
|
|
|
|
getItem: k => saved.get(k) || null,
|
|
|
|
|
setItem: (k, v) => saved.set(k, String(v)),
|
|
|
|
|
removeItem: k => saved.delete(k),
|
2026-12-04 08:58:53 -08:00
|
|
|
},
|
2026-10-25 14:19:24 +08:00
|
|
|
sessionStorage: {
|
2026-12-04 08:58:53 -08:00
|
|
|
getItem() {
|
2026-10-25 14:19:24 +08:00
|
|
|
return null;
|
2026-12-04 08:58:53 -08:00
|
|
|
},
|
2026-10-25 14:19:24 +08:00
|
|
|
setItem() {},
|
2026-12-04 08:58:53 -08:00
|
|
|
},
|
2026-10-25 14:19:24 +08:00
|
|
|
__user: user,
|
|
|
|
|
__posts: posts,
|
2026-12-04 08:58:53 -08:00
|
|
|
});
|
2026-10-25 14:19:24 +08:00
|
|
|
const run = code => vm.runInContext(code, context),
|
|
|
|
|
json = code => JSON.parse(JSON.stringify(run(code)));
|
|
|
|
|
for (const file of ['material-pools-ui.js', 'daily-funds-ui.js', 'cost-funding-ui.js', 'team-funds-ui.js', 'app.js'])
|
|
|
|
|
run(fs.readFileSync(path.join(__dirname, '../public', file), 'utf8').replace(/\ninit\(\);\s*$/, ''));
|
2026-12-04 08:58:53 -08:00
|
|
|
run(
|
2026-10-25 14:19:24 +08:00
|
|
|
`session={user:__user,mode:'demo'};state={user:__user,mode:'demo',members:[{member:'owner'},{member:'assistant'},{member:'assistant2'}],today:'${D}',accounts:['麦麦信用卡','助理信用卡','助理2信用卡'],pockets:['现货','金料']};toast=function(){};showPending=function(){};api=async function(url,data,key){__posts.push({url,data,key});return {saved:true};};`,
|
|
|
|
|
);
|
|
|
|
|
return { run, json, context, nodes, saved, posts, handlers, user };
|
|
|
|
|
}
|
|
|
|
|
test('三方分项付款人与成本汇总支持助理2,缺失或伪造付款人仍拒绝', () => {
|
|
|
|
|
assert.deepEqual(Calc.normalizeCostPayers({ material: 'assistant2', labor: 'assistant' }), {
|
|
|
|
|
material: 'assistant2',
|
|
|
|
|
labor: 'assistant',
|
|
|
|
|
});
|
|
|
|
|
const order = {
|
|
|
|
|
status: 'delivered',
|
|
|
|
|
lines: [
|
2026-12-04 08:58:53 -08:00
|
|
|
{
|
2026-10-25 14:19:24 +08:00
|
|
|
name: '测试款',
|
|
|
|
|
payer: 'assistant2',
|
|
|
|
|
costParts: { material: 5000, labor: 500, accessories: 200, other: 0 },
|
|
|
|
|
costPayers: { material: 'assistant2', labor: 'owner' },
|
2026-12-04 08:58:53 -08:00
|
|
|
},
|
|
|
|
|
],
|
2026-10-25 14:19:24 +08:00
|
|
|
expenses: [{ id: 'fee', date: D, note: '快递', payer: 'assistant2', amount: 600 }],
|
|
|
|
|
pendingFees: [],
|
|
|
|
|
};
|
|
|
|
|
const before = JSON.stringify(order),
|
|
|
|
|
summary = Calc.orderCosts(order);
|
|
|
|
|
assert.equal(summary.byPayer.assistant2.total, 5800);
|
|
|
|
|
assert.equal(summary.byPayer.owner.total, 500);
|
|
|
|
|
assert.equal(summary.total.total, 6300);
|
|
|
|
|
assert.equal(JSON.stringify(order), before);
|
|
|
|
|
assert.throws(() => Calc.costPayers({ material: 'assistant2', labor: 'unknown' }), /付款人/);
|
|
|
|
|
});
|
|
|
|
|
test('日购料与锁价的三方分摊逐分守恒,第三人分项不丢失', () => {
|
|
|
|
|
const base = {
|
|
|
|
|
name: '合成黄金',
|
|
|
|
|
payer: 'owner',
|
|
|
|
|
quantity: 1000,
|
|
|
|
|
costParts: { material: 101, labor: 0, accessories: 0, other: 0 },
|
|
|
|
|
costPayers: { material: 'owner', labor: 'assistant2' },
|
2026-12-04 08:58:53 -08:00
|
|
|
};
|
2026-10-25 14:19:24 +08:00
|
|
|
const pooled = {
|
2026-12-04 08:58:53 -08:00
|
|
|
...base,
|
2026-10-25 14:19:24 +08:00
|
|
|
costMaterial: { mode: 'pool', poolId: D },
|
|
|
|
|
materialPool: {
|
|
|
|
|
averageRate: 101,
|
2026-11-03 01:15:41 +08:00
|
|
|
allocations: [
|
2026-10-25 14:19:24 +08:00
|
|
|
{ payer: 'owner', amount: 34 },
|
|
|
|
|
{ payer: 'assistant', amount: 34 },
|
|
|
|
|
{ payer: 'assistant2', amount: 33 },
|
2026-12-04 08:58:53 -08:00
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
};
|
2026-10-25 14:19:24 +08:00
|
|
|
let result = Calc.orderCosts({ status: 'delivered', lines: [pooled] });
|
|
|
|
|
assert.equal(result.byPayer.assistant2.goods, 33);
|
2026-11-03 01:15:41 +08:00
|
|
|
assert.equal(result.total.goods, 101);
|
|
|
|
|
const locked = {
|
|
|
|
|
...base,
|
|
|
|
|
materialAllocations: [
|
|
|
|
|
{ source: 'original', payer: 'assistant2', amount: 100, quantity: 999, rate: 100 },
|
2026-10-25 14:19:24 +08:00
|
|
|
{ source: 'extra', payer: 'assistant', amount: 1, quantity: 1, rate: 1000 },
|
|
|
|
|
],
|
2026-12-04 08:58:53 -08:00
|
|
|
};
|
2026-10-25 14:19:24 +08:00
|
|
|
result = Calc.orderCosts({ status: 'delivered', lines: [locked] });
|
|
|
|
|
assert.equal(result.byPayer.assistant2.goods, 100);
|
|
|
|
|
assert.throws(
|
|
|
|
|
() =>
|
|
|
|
|
Calc.orderCosts({
|
|
|
|
|
status: 'delivered',
|
|
|
|
|
lines: [{ ...pooled, materialPool: { allocations: [{ payer: 'assistant2', amount: 100 }] } }],
|
2026-11-03 01:15:41 +08:00
|
|
|
}),
|
2026-10-25 14:19:24 +08:00
|
|
|
/分项成本/,
|
2026-11-03 01:15:41 +08:00
|
|
|
);
|
2026-10-25 14:19:24 +08:00
|
|
|
});
|
|
|
|
|
test('助理2开单归属、默认收款与快递跟随本人,包装仍默认麦麦', () => {
|
2026-11-03 01:15:41 +08:00
|
|
|
const h = harness('assistant2');
|
|
|
|
|
assert.equal(h.run('currentMember()'), 'assistant2');
|
|
|
|
|
assert.equal(h.run('teamDefaultPayer()'), 'assistant2');
|
|
|
|
|
const account = h.run('accountFields()');
|
|
|
|
|
assert.match(account, /<option value="助理2" selected>小昊/);
|
|
|
|
|
assert.match(account, /<option value="助理" >娜娜/);
|
|
|
|
|
assert.match(h.run('orderMemberField()'), /name="member" value="assistant2"/);
|
2026-10-25 14:19:24 +08:00
|
|
|
const defaults = Fees.sync(
|
2026-12-04 08:58:53 -08:00
|
|
|
[],
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
charges: [
|
2026-11-03 01:15:41 +08:00
|
|
|
{ id: randomUUID(), name: '包装' },
|
2026-10-25 14:19:24 +08:00
|
|
|
{ id: randomUUID(), name: '证书' },
|
2026-12-04 08:58:53 -08:00
|
|
|
],
|
|
|
|
|
},
|
|
|
|
|
],
|
2026-10-25 14:19:24 +08:00
|
|
|
{ addShipping: true, defaultPayer: 'assistant2' },
|
2026-12-04 08:58:53 -08:00
|
|
|
);
|
2026-11-03 01:15:41 +08:00
|
|
|
assert.equal(defaults.find(row => row.key === 'shipping').payer, 'assistant2');
|
|
|
|
|
assert.equal(defaults.find(row => row.note === '包装').payer, 'owner');
|
|
|
|
|
assert.equal(defaults.find(row => row.note === '证书').payer, 'assistant2');
|
|
|
|
|
assert.deepEqual(h.json('peopleParts()'), [['assistant2', '小昊人数']]);
|
2026-12-04 08:58:53 -08:00
|
|
|
});
|
2026-10-25 14:19:24 +08:00
|
|
|
test('老板代录明确选择三人,归属与开单人文本分别保存,助理不能选择其他归属', () => {
|
|
|
|
|
const owner = harness();
|
2026-11-03 01:15:41 +08:00
|
|
|
const html = owner.run('orderMemberField("assistant2")');
|
|
|
|
|
assert.match(html, /<select name="member">/);
|
|
|
|
|
for (const label of ['麦麦', '娜娜', '小昊']) assert.ok(html.includes('>' + label + '</option>'));
|
|
|
|
|
const assistant = harness('assistant');
|
|
|
|
|
assert.doesNotMatch(assistant.run('orderMemberField()'), /<select/);
|
|
|
|
|
assert.match(assistant.run('orderMemberField()'), /娜娜/);
|
|
|
|
|
assert.deepEqual(owner.json('teamMemberOptions()'), [
|
|
|
|
|
['owner', '麦麦'],
|
|
|
|
|
['assistant', '娜娜'],
|
|
|
|
|
['assistant2', '小昊'],
|
|
|
|
|
]);
|
2026-10-25 14:19:24 +08:00
|
|
|
});
|
|
|
|
|
test('购料只录实际付款,旧草稿分配忽略并保留五项原值,不再校验承担合计', () => {
|
|
|
|
|
const h = harness('assistant2'),
|
|
|
|
|
input = {
|
|
|
|
|
date: D,
|
|
|
|
|
grams: '1',
|
|
|
|
|
total: '100',
|
|
|
|
|
payer: 'assistant2',
|
|
|
|
|
note: '合成旧草稿',
|
|
|
|
|
allocations: [{ member: 'owner', amount: '99' }],
|
|
|
|
|
allocationManual: true,
|
|
|
|
|
};
|
|
|
|
|
h.saved.set('mm-material-purchase-' + h.user.id, JSON.stringify(input));
|
|
|
|
|
h.context.__input = input;
|
|
|
|
|
const html = h.run('materialPurchaseForm()');
|
|
|
|
|
assert.doesNotMatch(html, /这笔料款由谁承担|allocation_|material-allocations|全额承担/);
|
|
|
|
|
for (const [name, value] of Object.entries(input).slice(0, 5))
|
|
|
|
|
assert.ok(
|
|
|
|
|
html.includes(name === 'payer' ? '<option value="assistant2" selected>' : `name="${name}" value="${value}"`),
|
|
|
|
|
);
|
|
|
|
|
assert.match(html, /付款当天记支出/);
|
|
|
|
|
assert.match(html, /未用完的料结转/);
|
|
|
|
|
assert.match(html, /实际耗用自动计算料费/);
|
|
|
|
|
assert.match(html, /不再重复记现金支出/);
|
|
|
|
|
const expected = { date: D, grams: '1', total: '100', payer: 'assistant2', note: '合成旧草稿' };
|
|
|
|
|
assert.deepEqual(h.json('validateMaterialPurchase(__input)'), expected);
|
|
|
|
|
assert.deepEqual(input.allocations, [{ member: 'owner', amount: '99' }]);
|
|
|
|
|
h.context.__form = {
|
|
|
|
|
elements: Object.fromEntries(Object.entries(expected).map(([name, value]) => [name, { value }])),
|
|
|
|
|
querySelector() {
|
|
|
|
|
throw new Error('不应读取已删除的承担输入');
|
2026-12-04 08:58:53 -08:00
|
|
|
},
|
2026-10-25 14:19:24 +08:00
|
|
|
};
|
|
|
|
|
h.nodes.set('#material-purchase-form', h.context.__form);
|
|
|
|
|
assert.deepEqual(h.json('materialPurchaseData(__form)'), expected);
|
|
|
|
|
h.run('rememberMaterialPurchaseDraft()');
|
|
|
|
|
assert.deepEqual(JSON.parse(h.saved.get('mm-material-purchase-' + h.user.id)), expected);
|
|
|
|
|
});
|
|
|
|
|
test('旧购料响应只显示实际付款人,编辑不依赖已移除的承担DOM', async () => {
|
|
|
|
|
const h = harness('assistant2'),
|
|
|
|
|
id = randomUUID(),
|
|
|
|
|
purchase = {
|
|
|
|
|
id,
|
|
|
|
|
date: D,
|
|
|
|
|
mg: 1000,
|
|
|
|
|
amount: 10000,
|
|
|
|
|
actualpayer: 'assistant2',
|
|
|
|
|
revision: 2,
|
|
|
|
|
note: '合成历史购料',
|
|
|
|
|
allocations: [{ member: 'owner', amount: 10000 }],
|
|
|
|
|
},
|
2026-11-03 01:15:41 +08:00
|
|
|
dialog = {
|
2026-10-25 14:19:24 +08:00
|
|
|
open: false,
|
|
|
|
|
innerHTML: '',
|
|
|
|
|
showModal() {
|
|
|
|
|
this.open = true;
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
h.context.__purchase = purchase;
|
|
|
|
|
h.nodes.set('#material-purchase-dialog', dialog);
|
|
|
|
|
h.run('api=async function(){return {purchase:__purchase};};');
|
|
|
|
|
await h.run(`showMaterialPurchaseChange('${id}')`);
|
|
|
|
|
assert.match(dialog.innerHTML, /id="material-purchase-edit-form"/);
|
|
|
|
|
assert.match(dialog.innerHTML, /<option value="assistant2" selected>小昊/);
|
|
|
|
|
assert.doesNotMatch(dialog.innerHTML, /allocation_|material-allocations|承担/);
|
|
|
|
|
h.context.__pool = { date: D, purchases: [purchase], usages: [] };
|
|
|
|
|
const html = h.run('materialPoolDetailHTML(__pool)');
|
2026-11-03 01:15:41 +08:00
|
|
|
assert.match(html, /<td>小昊<\/td>/);
|
2026-10-25 14:19:24 +08:00
|
|
|
assert.doesNotMatch(html, /承担分配|承担 ¥|allocation_/);
|
|
|
|
|
});
|
|
|
|
|
test('购料新增与编辑无需承担,响应丢失后原操作可安全重试', async () => {
|
|
|
|
|
const h = harness('assistant2'),
|
|
|
|
|
input = { date: D, grams: '1', total: '100', payer: 'assistant2', note: '' },
|
|
|
|
|
id = randomUUID(),
|
|
|
|
|
form = {
|
|
|
|
|
elements: Object.fromEntries(Object.entries(input).map(([name, value]) => [name, { value }])),
|
|
|
|
|
dataset: { id, revision: '2', kind: 'edit' },
|
|
|
|
|
querySelector() {
|
|
|
|
|
throw new Error('不应读取已删除的承担输入');
|
2026-12-04 08:58:53 -08:00
|
|
|
},
|
2026-10-25 14:19:24 +08:00
|
|
|
};
|
|
|
|
|
h.context.__form = form;
|
|
|
|
|
h.nodes.set('#material-purchase-error', { innerHTML: '' });
|
2026-11-06 01:29:35 +08:00
|
|
|
h.nodes.set('#material-purchase-change-error', { innerHTML: '' });
|
2026-10-25 14:19:24 +08:00
|
|
|
h.run(
|
|
|
|
|
'finishMaterialPurchaseSave=async function(){return {saved:true};};finishMaterialPurchaseChange=async function(){return {saved:true};};api=async function(url,data,key){__posts.push({url,data,key});throw new Error("响应丢失");};',
|
|
|
|
|
);
|
|
|
|
|
await assert.rejects(h.run('submitMaterialPurchase(__form)'), /提交结果待确认/);
|
|
|
|
|
const job = JSON.parse(h.saved.get('mm-pending-' + h.user.id));
|
|
|
|
|
assert.deepEqual(job.data, input);
|
|
|
|
|
assert.equal(job.url, '/api/material-purchases');
|
|
|
|
|
h.context.__job = job;
|
|
|
|
|
h.run('api=async function(url,data,key){__posts.push({url,data,key});return {saved:true};};');
|
|
|
|
|
await h.run('mutate(__job.url,__job.data,__job)');
|
|
|
|
|
assert.equal(h.posts[0].key, h.posts[1].key);
|
|
|
|
|
assert.deepEqual(JSON.parse(JSON.stringify(h.posts[0].data)), JSON.parse(JSON.stringify(h.posts[1].data)));
|
|
|
|
|
assert.equal(h.saved.size, 0);
|
2026-11-06 01:29:35 +08:00
|
|
|
h.context.__purchase = { id, revision: 2 };
|
|
|
|
|
h.run('materialPurchaseEntry={purchase:__purchase,kind:"edit",user:state.user.id};');
|
|
|
|
|
await h.run('submitMaterialPurchaseChange(__form)');
|
|
|
|
|
assert.equal(h.posts[2].url, '/api/material-purchases/' + id + '/edit');
|
|
|
|
|
assert.deepEqual(JSON.parse(JSON.stringify(h.posts[2].data)), { ...input, revision: 2 });
|
2026-12-04 08:58:53 -08:00
|
|
|
});
|
2026-11-06 01:29:35 +08:00
|
|
|
test('团队往来展示两人方向、已转与剩余,历史利润交款不混入新债务', () => {
|
|
|
|
|
const h = harness();
|
|
|
|
|
h.context.__report = report();
|
|
|
|
|
assert.equal(h.json('validateTeamFunds(__report)').pairs.length, 3);
|
|
|
|
|
h.context.__pair = pair('assistant', 'assistant2', {
|
|
|
|
|
net: -2500,
|
2026-12-04 08:58:53 -08:00
|
|
|
due: 2500,
|
2026-11-06 01:29:35 +08:00
|
|
|
fromMember: 'assistant2',
|
|
|
|
|
toMember: 'assistant',
|
|
|
|
|
obligationNet: 2500,
|
|
|
|
|
transferred: 5000,
|
|
|
|
|
});
|
|
|
|
|
const html = h.run('teamPairHTML(__pair)');
|
|
|
|
|
assert.match(html, /小昊 → 娜娜 ¥25.00/);
|
|
|
|
|
for (const text of ['代收', '垫付', '抵扣', '实际已转', '剩余待转', '利润留在店里']) assert.ok(html.includes(text));
|
|
|
|
|
assert.doesNotMatch(html, /利润月底交|麦麦全转助理/);
|
|
|
|
|
const assistant = harness('assistant2');
|
|
|
|
|
assistant.context.__report = report('assistant2');
|
|
|
|
|
assert.doesNotThrow(() => assistant.run('validateTeamFunds(__report)'));
|
|
|
|
|
assistant.context.__report.pairs.push(pair('owner', 'assistant'));
|
|
|
|
|
assert.throws(() => assistant.run('validateTeamFunds(__report)'), /其他成员/);
|
|
|
|
|
});
|
|
|
|
|
test('老板旧转账待恢复操作仍校验确认,丢失响应保留原操作号且恢复不重复登记', async () => {
|
|
|
|
|
const h = harness();
|
|
|
|
|
h.context.__report = report();
|
|
|
|
|
h.run(
|
|
|
|
|
`teamFundsReport=__report;teamTransferPair={pair:__report.pairs[0],revision:__report.revision,user:state.user.id,date:'${D}'};finishTeamTransferSave=async function(){return {saved:true};};`,
|
|
|
|
|
);
|
|
|
|
|
const form = {
|
|
|
|
|
dataset: {},
|
2026-11-14 01:16:35 +08:00
|
|
|
values: { date: D, fromMember: 'owner', toMember: 'assistant', amount: '50', note: '合成转账' },
|
2026-12-04 08:58:53 -08:00
|
|
|
};
|
2026-11-14 01:16:35 +08:00
|
|
|
h.context.__form = form;
|
|
|
|
|
h.nodes.set('#team-transfer-error', { innerHTML: '' });
|
|
|
|
|
await assert.rejects(h.run('submitTeamTransfer(__form)'), /勾选确认/);
|
|
|
|
|
assert.equal(h.posts.length, 0);
|
2026-10-25 14:19:24 +08:00
|
|
|
form.values.verified = 'on';
|
2026-11-14 01:16:35 +08:00
|
|
|
h.run(`api=async function(url,data,key){__posts.push({url,data,key});throw new Error('响应丢失');};`);
|
|
|
|
|
await assert.rejects(h.run('submitTeamTransfer(__form)'), /提交结果待确认/);
|
2026-11-06 01:29:35 +08:00
|
|
|
const job = JSON.parse(h.saved.get('mm-pending-' + h.user.id));
|
|
|
|
|
assert.equal(job.url, '/api/team-funds/transfers');
|
|
|
|
|
assert.equal(job.data.verified, true);
|
|
|
|
|
assert.equal(h.posts.length, 1);
|
|
|
|
|
h.run(`api=async function(url,data,key){__posts.push({url,data,key});return {saved:true};};`);
|
|
|
|
|
h.context.__job = job;
|
|
|
|
|
await h.run('mutate(__job.url,__job.data,__job)');
|
|
|
|
|
assert.equal(h.posts.length, 2);
|
|
|
|
|
assert.equal(h.posts[0].key, h.posts[1].key);
|
|
|
|
|
assert.deepEqual(JSON.parse(JSON.stringify(h.posts[0].data)), JSON.parse(JSON.stringify(h.posts[1].data)));
|
|
|
|
|
assert.equal(h.saved.size, 0);
|
2026-12-04 08:58:53 -08:00
|
|
|
});
|
2026-11-06 01:29:35 +08:00
|
|
|
function profitReport() {
|
2026-12-04 08:58:53 -08:00
|
|
|
return {
|
2026-11-06 01:29:35 +08:00
|
|
|
basis: 'orderDate',
|
|
|
|
|
month: '2026-10',
|
|
|
|
|
from: '2026-10-01',
|
|
|
|
|
date: D,
|
|
|
|
|
rows: ['assistant', 'assistant2'].map(member => ({
|
2026-12-04 08:58:53 -08:00
|
|
|
member,
|
2026-11-06 01:29:35 +08:00
|
|
|
name: member,
|
|
|
|
|
recordCount: 1,
|
2026-10-25 14:19:24 +08:00
|
|
|
orderCount: 1,
|
2026-11-06 01:29:35 +08:00
|
|
|
afterSaleCount: 0,
|
|
|
|
|
draftCount: 0,
|
|
|
|
|
sales: 10000,
|
|
|
|
|
knownTotalCost: 6000,
|
|
|
|
|
totalCost: 6000,
|
|
|
|
|
knownProfit: 4000,
|
|
|
|
|
profit: 4000,
|
|
|
|
|
unknownCostCount: 0,
|
|
|
|
|
unknownProfitCount: 0,
|
|
|
|
|
incomplete: false,
|
2026-12-04 08:58:53 -08:00
|
|
|
shareRate: 30,
|
2026-11-06 01:29:35 +08:00
|
|
|
estimatedShare: 1200,
|
2026-12-04 08:58:53 -08:00
|
|
|
})),
|
|
|
|
|
};
|
|
|
|
|
}
|
2026-11-06 01:29:35 +08:00
|
|
|
test('团队页默认本月,仅月份利润分成简表,不渲染旧资金余额与转账', async () => {
|
|
|
|
|
const h = harness(),
|
2026-10-25 14:19:24 +08:00
|
|
|
main = { innerHTML: '' },
|
2026-11-06 01:29:35 +08:00
|
|
|
content = { innerHTML: '', setAttribute() {} };
|
|
|
|
|
h.nodes.set('#main', main);
|
|
|
|
|
h.nodes.set('#team-funds-content', content);
|
|
|
|
|
h.context.__profits = profitReport();
|
|
|
|
|
h.run('tab="team-funds";api=async function(url){__posts.push({url});return __profits;};');
|
|
|
|
|
await h.run('renderTeamFunds()');
|
|
|
|
|
assert.match(main.innerHTML, /type="month"/);
|
|
|
|
|
assert.match(main.innerHTML, /name="month" value="2026-10"/);
|
|
|
|
|
assert.equal(h.posts[0].url, '/api/team-profits?month=2026-10');
|
|
|
|
|
assert.match(content.innerHTML, /娜娜/);
|
2026-11-03 01:15:41 +08:00
|
|
|
assert.match(content.innerHTML, /小昊/);
|
2026-11-06 01:29:35 +08:00
|
|
|
for (const text of ['月度净利润', '分成比例', '预计分成', '¥40.00', '30%', '¥12.00'])
|
2026-11-03 01:15:41 +08:00
|
|
|
assert.ok(content.innerHTML.includes(text));
|
2026-11-06 01:29:35 +08:00
|
|
|
assert.doesNotMatch(
|
|
|
|
|
main.innerHTML + content.innerHTML,
|
|
|
|
|
/实际转账|资金净增减|剩余待转|此前累计|历史两人|data-pair|name="from"|name="to"/,
|
2026-12-04 08:58:53 -08:00
|
|
|
);
|
2026-11-06 01:29:35 +08:00
|
|
|
h.context.__profits.rows[0] = {
|
|
|
|
|
...h.context.__profits.rows[0],
|
|
|
|
|
profit: null,
|
|
|
|
|
incomplete: true,
|
|
|
|
|
unknownProfitCount: 1,
|
2026-10-25 14:19:24 +08:00
|
|
|
estimatedShare: null,
|
2026-12-04 08:58:53 -08:00
|
|
|
};
|
2026-10-25 14:19:24 +08:00
|
|
|
await h.run('loadTeamFunds()');
|
2026-11-06 01:29:35 +08:00
|
|
|
assert.match(content.innerHTML, /待核算/);
|
|
|
|
|
assert.match(content.innerHTML, /已核算部分 ¥40.00/);
|
2026-12-04 08:58:53 -08:00
|
|
|
});
|
2026-11-06 01:29:35 +08:00
|
|
|
test('两名助理没有团队导航或详情跳转,手工触发仍拒绝且每日往来可读', async () => {
|
|
|
|
|
for (const member of ['assistant', 'assistant2']) {
|
2026-10-25 14:19:24 +08:00
|
|
|
const h = harness(member),
|
2026-11-06 01:29:35 +08:00
|
|
|
nav = { dataset: { tab: 'team-funds' }, hidden: false, disabled: false, classList: { toggle() {} } };
|
|
|
|
|
h.context.document.querySelectorAll = selector => (selector === 'nav button' ? [nav] : []);
|
|
|
|
|
h.run('renderReport=async function(){};tab="team-funds";');
|
|
|
|
|
await h.run('render()');
|
|
|
|
|
assert.equal(nav.hidden, true);
|
|
|
|
|
assert.equal(nav.disabled, true);
|
2026-10-25 14:19:24 +08:00
|
|
|
assert.equal(h.run('tab'), 'report');
|
2026-11-14 01:16:35 +08:00
|
|
|
await assert.rejects(h.run('renderTeamFunds()'), /仅店主/);
|
|
|
|
|
await assert.rejects(h.run('loadTeamFunds()'), /仅店主/);
|
2026-11-06 01:29:35 +08:00
|
|
|
await assert.rejects(h.run('submitTeamTransfer({})'), /仅店主/);
|
2026-10-25 17:50:26 +08:00
|
|
|
assert.throws(() => h.run('showTeamTransfer("owner:assistant")'), /仅店主/);
|
2026-10-25 14:19:24 +08:00
|
|
|
assert.equal(h.posts.length, 0);
|
2026-10-25 17:50:26 +08:00
|
|
|
h.context.__order = { member };
|
|
|
|
|
assert.doesNotMatch(h.run('orderTeamFundsHTML(__order)'), /goto-team-funds|团队账目/);
|
|
|
|
|
h.context.__report = report(member);
|
|
|
|
|
assert.ok(h.run('teamFundsBodyHTML(validateTeamFunds(__report),{compact:true})').includes('剩余待转'));
|
2026-12-04 08:58:53 -08:00
|
|
|
}
|
2026-10-25 14:19:24 +08:00
|
|
|
const h = harness(),
|
2026-10-25 17:50:26 +08:00
|
|
|
nav = { dataset: { tab: 'team-funds' }, hidden: true, disabled: true, classList: { toggle() {} } };
|
2026-11-06 01:29:35 +08:00
|
|
|
h.context.document.querySelectorAll = selector => (selector === 'nav button' ? [nav] : []);
|
|
|
|
|
h.run('tab="team-funds";renderTeamFunds=async function(){};');
|
2026-10-25 14:19:24 +08:00
|
|
|
await h.run('render()');
|
2026-11-06 01:29:35 +08:00
|
|
|
assert.equal(nav.hidden, false);
|
|
|
|
|
assert.equal(nav.disabled, false);
|
2026-12-04 08:58:53 -08:00
|
|
|
});
|
2026-10-25 17:50:26 +08:00
|
|
|
test('工费与附加费用不再显示报销抵用,加载开单也不读取报销来源', () => {
|
|
|
|
|
const h = harness('assistant2');
|
|
|
|
|
assert.equal(h.run('reimbursementFundingHTML("labor")'), '');
|
|
|
|
|
assert.equal(h.run('reimbursementFundingHTML("fee")'), '');
|
|
|
|
|
for (const html of [
|
|
|
|
|
h.run('costFields()'),
|
|
|
|
|
h.run('feeRowHTML({key:"shipping",note:"快递",amount:"8",payer:"assistant2"})'),
|
|
|
|
|
])
|
|
|
|
|
assert.doesNotMatch(html, /cost-funding|使用已批准报销款|成本付款来源|fundingAmount|刷新已报销款/);
|
|
|
|
|
h.nodes.set('#order-form', {});
|
|
|
|
|
h.run('loadReimbursementFundingOptions(true)');
|
|
|
|
|
assert.equal(h.posts.length, 0);
|
|
|
|
|
});
|
|
|
|
|
test('历史报销引用以隐藏资料保留原金额和版本,付款人继续锁定且坏资料不静默丢失', () => {
|
|
|
|
|
const h = harness('assistant2'),
|
|
|
|
|
reference = { reimbursementId: randomUUID(), itemId: randomUUID(), amount: '100.01', revision: 3 },
|
|
|
|
|
payer = { value: 'assistant2', disabled: false };
|
|
|
|
|
h.context.__reference = reference;
|
|
|
|
|
for (const component of ['labor', 'fee']) {
|
|
|
|
|
h.context.__component = component;
|
|
|
|
|
const html = h.run('reimbursementFundingHTML(__component,__reference)');
|
|
|
|
|
assert.match(html, /<input type="hidden" class="cost-funding"/);
|
|
|
|
|
assert.doesNotMatch(html, /<details|<select|fundingAmount|报销款/);
|
|
|
|
|
const encoded = html
|
|
|
|
|
.match(/data-reference="([^"]*)"/)[1]
|
|
|
|
|
.replace(/"/g, '"')
|
|
|
|
|
.replace(/&/g, '&');
|
|
|
|
|
const container = { dataset: { component, reference: encoded }, closest: () => ({ querySelector: () => payer }) };
|
|
|
|
|
h.context.__container = container;
|
|
|
|
|
assert.deepEqual(h.json('readReimbursementFunding(__container)'), reference);
|
|
|
|
|
h.context.document.querySelectorAll = () => [container];
|
|
|
|
|
h.run('syncReimbursementFundingControls()');
|
|
|
|
|
assert.equal(payer.value, 'assistant2');
|
|
|
|
|
assert.equal(payer.disabled, true);
|
|
|
|
|
container.dataset.reference = 'broken';
|
|
|
|
|
assert.throws(() => h.run('readReimbursementFunding(__container)'), /资料缺失/);
|
|
|
|
|
}
|
2026-10-25 14:19:24 +08:00
|
|
|
});
|
2026-11-03 01:15:41 +08:00
|
|
|
test('小昊人数视图只展示本人,老板保留两项,不把未提供当0', () => {
|
|
|
|
|
const h = harness('assistant2');
|
|
|
|
|
h.context.__summary = {
|
|
|
|
|
from: D,
|
|
|
|
|
date: D,
|
|
|
|
|
assistant2: {
|
|
|
|
|
total: 0,
|
|
|
|
|
endTotal: 0,
|
|
|
|
|
baselineTotal: null,
|
|
|
|
|
netChange: null,
|
|
|
|
|
netChangeReason: 'missing-previous',
|
|
|
|
|
recordedDays: 1,
|
|
|
|
|
baselineDate: null,
|
2026-12-04 08:58:53 -08:00
|
|
|
},
|
2026-11-03 01:15:41 +08:00
|
|
|
};
|
|
|
|
|
const html = h.run('peopleCountHTML(__summary)');
|
|
|
|
|
assert.match(html, /小昊当日总人数/);
|
|
|
|
|
assert.match(html, /0 人(已记录)/);
|
|
|
|
|
assert.match(html, /暂无上次记录/);
|
|
|
|
|
assert.doesNotMatch(html, /娜娜当日总人数/);
|
|
|
|
|
const owner = harness();
|
|
|
|
|
assert.deepEqual(owner.json('peopleParts()'), [
|
|
|
|
|
['assistant1', '娜娜人数'],
|
|
|
|
|
['assistant2', '小昊人数'],
|
|
|
|
|
]);
|
2026-10-25 14:19:24 +08:00
|
|
|
});
|
|
|
|
|
test('三人模板保留助理2付款人与收款账户,不继承已批准抵用身份', () => {
|
|
|
|
|
const id = randomUUID(),
|
|
|
|
|
preset = Templates.fromDraft({
|
|
|
|
|
payment_account: '助理2微信',
|
|
|
|
|
credit_card_fee_payer: 'assistant2',
|
|
|
|
|
lines: [
|
2026-12-04 08:58:53 -08:00
|
|
|
{
|
2026-10-25 14:19:24 +08:00
|
|
|
name: '测试款',
|
|
|
|
|
unit: 'g',
|
|
|
|
|
quantity: '1',
|
|
|
|
|
payer: 'assistant2',
|
|
|
|
|
pricingMode: 'manual',
|
|
|
|
|
baseSale: '100',
|
|
|
|
|
costParts: { material: '10', labor: '5', accessories: '0', other: '0' },
|
|
|
|
|
costPayers: { material: 'assistant2', labor: 'assistant2' },
|
|
|
|
|
costFundings: { labor: { reimbursementId: id, itemId: id, amount: '5', revision: 1 } },
|
2026-12-04 08:58:53 -08:00
|
|
|
},
|
|
|
|
|
],
|
2026-10-25 14:19:24 +08:00
|
|
|
fee_items: [],
|
|
|
|
|
});
|
|
|
|
|
assert.equal(preset.payment_account, '助理2微信');
|
|
|
|
|
assert.equal(preset.lines[0].costPayers.labor, 'assistant2');
|
|
|
|
|
assert.equal(preset.lines[0].costFundings, undefined);
|
|
|
|
|
});
|
2026-12-04 12:09:54 +08:00
|
|
|
test('店员设置页只保留本人密码,不展示店铺配置、打印、成员、备份及同步', () => {
|
|
|
|
|
for (const member of ['assistant', 'assistant2']) {
|
|
|
|
|
const h = harness(member),
|
|
|
|
|
main = { innerHTML: '' };
|
|
|
|
|
h.nodes.set('#main', main);
|
|
|
|
|
h.run("state={...state,mode:'production',printing:null,backup:null,sync:null};renderSettings();");
|
|
|
|
|
assert.match(main.innerHTML, /修改密码/);
|
|
|
|
|
assert.match(main.innerHTML, /id="password-form"/);
|
|
|
|
|
assert.match(main.innerHTML, /autocomplete="current-password"/);
|
|
|
|
|
assert.doesNotMatch(
|
|
|
|
|
main.innerHTML,
|
|
|
|
|
/客户打印模板|店铺配置|员工管理|成员账号|数据库与备份|飞书查看副本|切换娜娜|print-settings/,
|
|
|
|
|
);
|
|
|
|
|
h.run("state.mode='demo';renderSettings()");
|
|
|
|
|
assert.match(main.innerHTML, /演示账号不提供密码修改/);
|
|
|
|
|
assert.doesNotMatch(main.innerHTML, /password-form|切换娜娜/);
|
|
|
|
|
}
|
2026-11-03 01:15:41 +08:00
|
|
|
});
|
|
|
|
|
test('娜娜和小昊统一显示,姓名与账户转换不更改选项值、提交账户或历史开单人', () => {
|
|
|
|
|
const h = harness('assistant2');
|
|
|
|
|
assert.deepEqual(h.json('teamMemberOptions()'), [
|
|
|
|
|
['owner', '麦麦'],
|
|
|
|
|
['assistant', '娜娜'],
|
|
|
|
|
['assistant2', '小昊'],
|
|
|
|
|
]);
|
|
|
|
|
assert.equal(h.run('accountDisplayName("助理微信")'), '娜娜微信');
|
|
|
|
|
assert.equal(h.run('accountDisplayName("助理2支付宝")'), '小昊支付宝');
|
|
|
|
|
assert.equal(h.run('accountDisplayName("历史助理2")'), '历史助理2');
|
|
|
|
|
const options = h.run('opts(["助理微信","助理2支付宝"],"助理2支付宝")');
|
|
|
|
|
assert.match(options, /<option value="助理微信" >娜娜微信/);
|
|
|
|
|
assert.match(options, /<option value="助理2支付宝" selected>小昊支付宝/);
|
|
|
|
|
h.context.__form = {
|
|
|
|
|
querySelector(selector) {
|
|
|
|
|
return selector.includes('payment_recipient')
|
|
|
|
|
? { value: '助理2' }
|
|
|
|
|
: selector.includes('payment_method')
|
|
|
|
|
? { value: '支付宝' }
|
|
|
|
|
: null;
|
2026-12-04 08:58:53 -08:00
|
|
|
},
|
2026-11-03 01:15:41 +08:00
|
|
|
};
|
|
|
|
|
assert.equal(h.run('readPaymentAccount(__form)'), '助理2支付宝');
|
|
|
|
|
assert.match(h.run('field("开单人","salesperson","历史开单人助理2")'), /value="历史开单人助理2"/);
|
2026-12-04 02:45:54 +08:00
|
|
|
h.nodes.set('#shell', { hidden: false });
|
|
|
|
|
h.nodes.set('#login', { innerHTML: '' });
|
|
|
|
|
h.run("session.mode='trial';session.setup=false;login();");
|
|
|
|
|
const login = h.nodes.get('#login').innerHTML;
|
|
|
|
|
assert.match(login, /登录账号/);
|
|
|
|
|
assert.match(login, /<select name="member"[^>]*autocomplete="username"/);
|
|
|
|
|
assert.doesNotMatch(login, /<input[^>]*name="name"/);
|
|
|
|
|
assert.doesNotMatch(login, />助理[12]<\/option>/);
|
2026-10-25 14:19:24 +08:00
|
|
|
});
|