'use strict'; const { createBrowserContext } = require('./fixtures/browser-context.cjs'); const test = require('node:test'), assert = require('node:assert/strict'), fs = require('node:fs'), path = require('node:path'), vm = require('node:vm'); const Calc = require('../public/calculations'), Cards = require('../public/credit-card-fees'); const settings = { version: 1, revision: 3, storeName: '测试珠宝', receiptSubtitle: '销售单', contact: '测试电话', members: [ { member: 'owner', label: '店东', loginName: 'owner-test' }, { member: 'assistant', label: '阿一', loginName: 'one-test' }, { member: 'assistant2', label: '阿二', loginName: 'two-test' }, ], creditCardRatePermille: 9, profitSharePercent: 30, profitShareEffectiveMonth: '2000-01', feeDefaults: { certificate: '8.50', packaging: '7', shipping: '12', packagingPayer: 'assistant2' }, }; function harness() { const nodes = new Map(), handlers = {}, windowHandlers = {}; let confirmResult = false; const context = createBrowserContext({ console, URLSearchParams, FeeEstimates: require('../public/fee-estimates'), LedgerCalc: Calc, CreditCardFees: Cards, setInterval() {}, setTimeout() {}, clearTimeout() {}, window: { scrollTo() {}, confirm() { return confirmResult; }, addEventListener(type, handler) { windowHandlers[type] = handler; }, }, document: { title: '', querySelector: selector => nodes.get(selector) || null, querySelectorAll: () => [], addEventListener(type, handler, capture) { (handlers[type] ??= []).push({ handler, capture }); }, }, localStorage: { getItem() { return null; }, setItem() {}, removeItem() {}, }, sessionStorage: { getItem() { return null; }, setItem() {}, }, __settings: structuredClone(settings), }); const run = code => vm.runInContext(code, context), json = code => JSON.parse(JSON.stringify(run(code))); for (const filename of [ 'price-locks-ui.js', 'price-lock-changes-ui.js', 'order-edit-ui.js', 'recovery-changes-ui.js', 'recoveries-ui.js', 'team-funds-ui.js', 'material-pools-ui.js', 'shop-settings-ui.js', 'app.js', ]) run(fs.readFileSync(path.join(__dirname, '../public', filename), 'utf8').replace(/\ninit\(\);\s*$/, '')); run( "session={user:{id:'test-owner',member:'owner',role:'owner',label:'店东',name:'原登录名'},brand:{storeName:'测试珠宝'},loginOptions:__settings.members};state={today:'2026-11-14',user:session.user,shopSettings:__settings,members:__settings.members,accounts:['麦麦微信','助理微信','助理2微信','麦麦信用卡'],printing:{store:'测试珠宝',subtitle:'销售单',contact:'测试电话'},mode:'demo'};pending=()=>null;toast=()=>{};", ); function form() { const values = json('shopSettingsValues()'), fields = Object.fromEntries(Object.entries(values).map(([key, value]) => [key, { value: String(value) }])); const form = { dataset: { revision: '3', baseline: JSON.stringify(values) }, elements: { namedItem: key => fields[key] }, querySelector: () => ({ disabled: false }), }; nodes.set('#shop-settings-form', form); context.__form = form; return { form, fields }; } return { context, nodes, handlers, windowHandlers, run, json, form, confirm(value) { confirmResult = value; }, }; } test('仅店主显示店铺默认配置卡,成员资料由员工管理维护', () => { const h = harness(), html = h.run('shopSettingsHTML()'); for (const name of [ 'storeName', 'receiptSubtitle', 'contact', 'creditCardRatePermille', 'profitSharePercent', 'profitShareEffectiveMonth', 'certificate', 'packaging', 'shipping', 'packagingPayer', ]) assert.ok(html.includes('name="' + name + '"'), name); assert.doesNotMatch(html, /name="(?:owner|assistant2?)Label"|name="(?:owner|assistant2?)LoginName"/); assert.match(html, /保存店铺配置/); assert.match(html, /name="profitShareEffectiveMonth" value="2026-12"/); assert.doesNotMatch(html, /value="2000-01"|profitShareHistory/); h.run("state.user.role='assistant'"); assert.equal(h.run('shopSettingsHTML()'), ''); }); test('保存店铺配置使用revision且不提交员工资料;未改分成时不重复写入历史计划', () => { const h = harness(), { fields } = h.form(); fields.storeName.value = '新店名'; const data = h.json('shopSettingsInput(__form)'); assert.equal(data.revision, 3); assert.equal(data.storeName, '新店名'); assert.equal(Object.hasOwn(data, 'members'), false); assert.deepEqual(data.feeDefaults, settings.feeDefaults); assert.equal(Object.hasOwn(data, 'profitSharePercent'), false); assert.equal(Object.hasOwn(data, 'profitShareEffectiveMonth'), false); fields.profitSharePercent.value = '40'; assert.deepEqual(h.json('shopSettingsInput(__form)').profitSharePercent, 40); assert.equal(h.json('shopSettingsInput(__form)').profitShareEffectiveMonth, '2026-12'); fields.profitShareEffectiveMonth.value = '2026-10'; assert.throws(() => h.run('shopSettingsInput(__form)'), /本月或以后/); }); test('营业时间原值不随其他字段提交;两项留空清除,跨日有效', () => { const h = harness(); h.run("state.shopSettings.businessHours={open:'09:00',close:'18:00'}"); const { fields } = h.form(); const input = () => h.json('shopSettingsInput(__form)'); assert.equal(fields.businessOpen.value, '09:00'); assert.equal(fields.businessClose.value, '18:00'); assert.equal(Object.hasOwn(input(), 'businessHours'), false); fields.businessOpen.value = '20:30'; fields.businessClose.value = '02:00'; assert.deepEqual(input().businessHours, { open: '20:30', close: '02:00' }); fields.businessClose.value = '20:30'; assert.throws(input, /开始和结束时间不能相同/); fields.businessOpen.value = ''; assert.throws(input, /请完整填写开始和结束时间/); fields.businessClose.value = ''; assert.equal(input().businessHours, null); }); test('店铺配置校验阻止错误费率、过大默认费及非店主保存', () => { const h = harness(), { fields } = h.form(); fields.creditCardRatePermille.value = '0.5'; assert.throws(() => h.run('shopSettingsInput(__form)'), /整数/); fields.creditCardRatePermille.value = '0'; assert.equal(h.json('shopSettingsInput(__form)').creditCardRatePermille, 0); fields.shipping.value = '1000000.01'; assert.throws(() => h.run('shopSettingsInput(__form)'), /最多1000000元/); fields.shipping.value = '0'; h.run("state.user.role='assistant'"); assert.throws(() => h.run('shopSettingsInput(__form)'), /店主账号/); }); test('成员、收款与品牌显示配置姓名,保留legacy账户键与自由文本原文', () => { const h = harness(); assert.equal(h.run('accountDisplayName("麦麦微信")'), '店东微信'); assert.equal(h.run('accountDisplayName("助理2支付宝")'), '阿二支付宝'); assert.equal(h.run('accountDisplayName("客户说麦麦微信付款")'), '客户说麦麦微信付款'); assert.deepEqual(h.json('teamMemberOptions()'), [ ['owner', '店东'], ['assistant', '阿一'], ['assistant2', '阿二'], ]); assert.deepEqual(h.json('peopleParts()'), [ ['assistant1', '阿一人数'], ['assistant2', '阿二人数'], ]); assert.equal(h.run('priceLockPayerName("assistant2")'), '阿二'); assert.equal(h.run('materialPayerName("owner")'), '店东'); assert.equal(h.run('currentSalesperson()'), '店东'); const html = h.run('accountFields("麦麦信用卡")'); assert.match(html, /