Files
weiguang-jewelry-ledger/新账系统/web/test/shared.test.ts
T
黄健铭andClaude Opus 5.5 ec5889075f 新版界面(React + TypeScript + Vite + Ant Design):登录页与开单页
共用计算经构建插件直接导入 public/ 下的 UMD 源文件;请求客户端沿用旧界面的重试、
同一操作号自动确认、恢复原操作与账号切换约定;附 vitest 单元与组件测试。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-04 18:58:31 -08:00

76 lines
3.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 经 sharedUmd 插件导入的共用计算:与服务端 require 的是同一份源文件。
import { createRequire } from 'node:module';
import { describe, expect, it } from 'vitest';
import { Calc, Cards, Fees, Model } from '../src/shared';
import type { LedgerState } from '../src/shared';
const require = createRequire(import.meta.url);
const serverCalc = require('../../public/calculations.js');
const st: LedgerState = {
today: '2026-11-20',
mode: 'trial',
user: { id: 'u', name: '娜娜', role: 'assistant', member: 'assistant', operatorName: '娜娜' },
members: [
{ member: 'owner', label: '麦麦' },
{ member: 'assistant', label: '娜娜' },
],
accounts: ['麦麦微信', '助理微信', '麦麦信用卡'],
shopSettings: {},
};
let n = 0;
const uuid = () => '00000000-0000-4000-8000-' + String(++n).padStart(12, '0');
const ctx = { st, uuid };
describe('共用模块', () => {
it('浏览器构建与服务端得到相同的计算结果', () => {
const line = { pricingMode: 'auto', unit: 'g', quantity: '1.234', goldPrice: '888.88', laborMode: 'gram', laborRate: '35' };
expect(Calc.sale(line)).toEqual(serverCalc.sale(line));
expect(Calc.orderTotal([123449], 0, 'yuan').total).toBe(123400);
expect(Calc.orderTotal([123450], 0, 'yuan').total).toBe(123500);
expect(Cards.amount(100000, 6)).toBe(600);
expect(Fees.checkboxCategory('快递')).toBe('shipping');
});
it('店员开单:订单归属固定为本人,收款默认本人微信,付款人默认本人', () => {
const form = Model.newForm(ctx);
expect(form.memberLocked).toBe(true);
expect(Model.readPaymentAccount(form)).toBe('助理微信');
expect(form.lines[0].purchase?.materialPayer).toBe('assistant');
const r = Model.syncForm(form, ctx);
expect(r.draft.member).toBe('assistant');
});
it('行与合计:克重×(金价+克工费)+附加项目,整单取整到元', () => {
let form = Model.newForm(ctx);
form = Model.addCharge(form, 0, uuid);
form = Model.updateLine(form, 0, { ...form.lines[0], quantity: '1.5', goldPrice: '900', laborRate: '20' });
const r = Model.syncForm(form, ctx);
expect(r.form.lines[0].sale).toBe('1435.00');
expect(r.summary.totalText).toBe('¥1,435');
expect(r.form.lines[0].view.calculation).toBe('1.5 ×(900 + 20) + 附加 55.00 = 1,435.00 元');
});
it('附加项目默认报价不覆盖手改、0 与清空', () => {
let c = Model.newCharge({}, uuid);
c = Model.setChargeAmount(c, '0');
c = Model.changeChargeType(Model.changeChargeType(c, '快递'), '编绳');
expect(c.amount).toBe('0');
expect(Model.changeChargeType(c, '快递').amount).toBe('8');
});
it('校验:必填项与内部备注最少 3 个字符(# 计入)', () => {
const form = { ...Model.newForm(ctx), customer_type: 'new' as const };
let r = Model.syncForm(Model.updateLine(form, 0, { ...form.lines[0], quantity: '1', goldPrice: '900', laborRate: '20' }), ctx);
r = Model.syncForm(
Model.updateLine(r.form, 0, {
...r.form.lines[0],
purchase: { ...r.form.lines[0].purchase!, materialRate: '800', labor: '10' },
}),
ctx,
);
const draft = { ...r.draft, customer: '客户', internal_note: '#ab' };
expect(() => Model.validateIssueDraft(draft, st)).not.toThrow();
expect(() => Model.validateIssueDraft({ ...draft, internal_note: '# a' }, st)).toThrow(/至少填写3个字符/);
expect(() => Model.validateIssueDraft({ ...draft, customer: '' }, st)).toThrow(/客户称呼/);
expect(() => Model.validateIssueDraft({ ...draft, salesperson: ' ' }, st)).toThrow(/开单人/);
expect(() => Model.validateFormFields({ ...r.form, customer_type: '' }, st)).toThrow(/客户类型/);
});
});