来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
149 lines
6.2 KiB
TypeScript
149 lines
6.2 KiB
TypeScript
// @vitest-environment jsdom
|
|
import { beforeEach, describe, expect, it } from 'vitest';
|
|
import {
|
|
type DeliveryTemplatePreferenceMap,
|
|
loadTemplatePreferenceMap,
|
|
normalizeTemplatePreferenceMap,
|
|
readPreferenceFromMap,
|
|
readTemplatePreference,
|
|
saveTemplatePreference,
|
|
writePreferenceToMap,
|
|
} from './deliveryTemplatePreference';
|
|
|
|
/**
|
|
* 「客户 → 上次用的配送单版式」记忆(本机 localStorage)。
|
|
* 需求:A 客户选了 C 模板并点打印 → 下次给 A 打印默认 C;多客户混合打印取第一个客户的记录。
|
|
*/
|
|
const KEY = 'delivery_note_customer_template_v1';
|
|
|
|
describe('deliveryTemplatePreference', () => {
|
|
beforeEach(() => {
|
|
localStorage.clear();
|
|
});
|
|
|
|
it('坏数据一律丢弃,不抛错', () => {
|
|
expect(normalizeTemplatePreferenceMap(null)).toEqual({});
|
|
expect(normalizeTemplatePreferenceMap('x')).toEqual({});
|
|
expect(normalizeTemplatePreferenceMap([1, 2])).toEqual({});
|
|
expect(normalizeTemplatePreferenceMap({ A: {} })).toEqual({});
|
|
expect(normalizeTemplatePreferenceMap({ A: { templateId: ' ' } })).toEqual({});
|
|
expect(normalizeTemplatePreferenceMap({ ' ': { templateId: 't1' } })).toEqual({});
|
|
});
|
|
|
|
it('取记录:按客户号,空号返回 null,不做模糊匹配', () => {
|
|
const map = { C001: { templateId: 'tpl-c' }, C002: { htmlTemplateId: 'ai-1' } };
|
|
expect(readPreferenceFromMap(map, 'C001')).toEqual({ templateId: 'tpl-c' });
|
|
expect(readPreferenceFromMap(map, 'C002')).toEqual({ htmlTemplateId: 'ai-1' });
|
|
expect(readPreferenceFromMap(map, 'C003')).toBeNull();
|
|
expect(readPreferenceFromMap(map, '')).toBeNull();
|
|
expect(readPreferenceFromMap(map, null)).toBeNull();
|
|
// 返回副本,避免外部改到原 map
|
|
const got = readPreferenceFromMap(map, 'C001')!;
|
|
got.templateId = 'changed';
|
|
expect(map.C001.templateId).toBe('tpl-c');
|
|
});
|
|
|
|
it('写记录:只动这个客户,不覆盖别人;空值等于删除', () => {
|
|
const base: DeliveryTemplatePreferenceMap = {
|
|
C001: { templateId: 't1' },
|
|
C002: { templateId: 't2' },
|
|
};
|
|
const next = writePreferenceToMap(base, 'C003', { htmlTemplateId: 'ai-9' });
|
|
expect(next.C001).toEqual({ templateId: 't1' });
|
|
expect(next.C002).toEqual({ templateId: 't2' });
|
|
expect(next.C003).toEqual({ htmlTemplateId: 'ai-9' });
|
|
expect(base.C003).toBeUndefined(); // 不改原对象
|
|
|
|
expect(writePreferenceToMap(next, 'C001', {}).C001).toBeUndefined();
|
|
expect(writePreferenceToMap(next, '', { templateId: 'x' })).toEqual(next);
|
|
});
|
|
|
|
it('localStorage 往返:保存后能读回,且不丢其它客户', () => {
|
|
saveTemplatePreference('C001', { templateId: 'tpl-c' });
|
|
saveTemplatePreference('C002', { htmlTemplateId: 'ai-2' });
|
|
expect(readTemplatePreference('C001')).toEqual({ templateId: 'tpl-c' });
|
|
expect(readTemplatePreference('C002')).toEqual({ htmlTemplateId: 'ai-2' });
|
|
expect(readTemplatePreference('C003')).toBeNull();
|
|
expect(loadTemplatePreferenceMap()).toEqual({
|
|
C001: { templateId: 'tpl-c' },
|
|
C002: { htmlTemplateId: 'ai-2' },
|
|
});
|
|
});
|
|
|
|
it('localStorage 里是坏 JSON 时按空处理(不影响打开弹窗)', () => {
|
|
localStorage.setItem(KEY, '{不是 JSON');
|
|
expect(loadTemplatePreferenceMap()).toEqual({});
|
|
expect(readTemplatePreference('C001')).toBeNull();
|
|
});
|
|
|
|
it('空客户号不写盘', () => {
|
|
saveTemplatePreference('', { templateId: 't1' });
|
|
saveTemplatePreference(null, { templateId: 't1' });
|
|
expect(localStorage.getItem(KEY)).toBeNull();
|
|
});
|
|
});
|
|
|
|
/**
|
|
* 打印选项(商品列 / 页眉信息 / 版式)也按客户记在本机(2026-10-06 追加)。
|
|
* 背景:弹窗里的这些勾选**不回写模板**,关闭即丢;改成按客户记下来。
|
|
*/
|
|
describe('打印选项的存取', () => {
|
|
beforeEach(() => localStorage.clear());
|
|
|
|
it('options 能随记录一起存取', () => {
|
|
saveTemplatePreference('C001', {
|
|
htmlTemplateId: 'ai-1',
|
|
options: {
|
|
selectedColumnIds: ['seq', 'sku_name', 'sku_name'],
|
|
selectedMetaFields: ['order_code', 'cycle_code'],
|
|
layout: { documentStyle: 'standard', totalAmountType: 'actual' },
|
|
},
|
|
});
|
|
const saved = readTemplatePreference('C001');
|
|
expect(saved?.htmlTemplateId).toBe('ai-1');
|
|
// 去重
|
|
expect(saved?.options?.selectedColumnIds).toEqual(['seq', 'sku_name']);
|
|
expect(saved?.options?.selectedMetaFields).toEqual(['order_code', 'cycle_code']);
|
|
expect(saved?.options?.layout).toEqual({ documentStyle: 'standard', totalAmountType: 'actual' });
|
|
});
|
|
|
|
it('只有 options、没有模板 id 的记录也要保留(版式没变但勾选变了)', () => {
|
|
saveTemplatePreference('C002', { options: { selectedMetaFields: ['customer'] } });
|
|
expect(readTemplatePreference('C002')?.options?.selectedMetaFields).toEqual(['customer']);
|
|
expect(normalizeTemplatePreferenceMap(loadTemplatePreferenceMap()).C002).toBeDefined();
|
|
});
|
|
|
|
it('坏 options 一律丢弃,不抛错', () => {
|
|
localStorage.setItem(
|
|
KEY,
|
|
JSON.stringify({
|
|
A: { options: 'nope' },
|
|
B: { options: { selectedColumnIds: [1, ' ', null], selectedMetaFields: [] } },
|
|
C: { options: { layout: [] } },
|
|
D: { templateId: 't1', options: { selectedMetaFields: ['x'] } },
|
|
})
|
|
);
|
|
const map = loadTemplatePreferenceMap();
|
|
expect(map.A).toBeUndefined();
|
|
expect(map.B).toBeUndefined();
|
|
expect(map.C).toBeUndefined();
|
|
expect(map.D?.options?.selectedMetaFields).toEqual(['x']);
|
|
});
|
|
|
|
it('空记录(既无模板 id 也无 options)等于删除', () => {
|
|
saveTemplatePreference('C003', { templateId: 't1' });
|
|
expect(readTemplatePreference('C003')?.templateId).toBe('t1');
|
|
saveTemplatePreference('C003', {});
|
|
expect(readTemplatePreference('C003')).toBeNull();
|
|
});
|
|
|
|
it('保存新 options 不会丢掉已有模板 id(读写一致)', () => {
|
|
saveTemplatePreference('C004', { templateId: 'tpl-a' });
|
|
// 模拟弹窗再保存一次(同时带模板与 options)
|
|
saveTemplatePreference('C004', { templateId: 'tpl-a', options: { selectedColumnIds: ['seq'] } });
|
|
const saved = readTemplatePreference('C004');
|
|
expect(saved?.templateId).toBe('tpl-a');
|
|
expect(saved?.options?.selectedColumnIds).toEqual(['seq']);
|
|
});
|
|
});
|