cmvr-wms/mobile-app/tests/drafts-calendar.test.mjs

43 lines
3.6 KiB
JavaScript
Raw Normal View History

import test from 'node:test';
import assert from 'node:assert/strict';
import {createDraftStore,draftKey} from '../src/core/drafts.ts';
import {calendarDate,calendarDays,dateParts} from '../src/core/calendar.ts';
function memory(){const data=new Map();return {data,get:async k=>data.get(k)??null,set:async(k,v)=>{data.set(k,v);},remove:async k=>{data.delete(k);}};}
test('drafts isolate accounts, environments and business ids and preserve unknown submit keys',async()=>{
const storage=memory(),store=createDraftStore(storage),value={quantity:'4',attempted:true,key:'original-idempotency-key',payload:{quantity:4}};
await store.save('http://dev|user-7','use',21,value);
assert.deepEqual((await store.load('http://dev|user-7','use',21)).value,value);
for(const [scope,kind,id] of [['http://prod|user-7','use',21],['http://dev|user-8','use',21],['http://dev|user-7','move',21],['http://dev|user-7','use',22]])assert.equal(await store.load(scope,kind,id),null);
assert.notEqual(draftKey('a|b','c','d'),draftKey('a','b|c','d'));
});
test('chunked drafts preserve emoji and Chinese text without exceeding secure item limits',async()=>{
const storage=memory(),store=createDraftStore(storage),value={remark:'资料📷'.repeat(2500),photos:[{id:'photo-id',name:'收货现场.jpg'}]};
await store.save('dev/user','arrival','11',value);
assert.deepEqual((await store.load('dev/user','arrival','11')).value,value);
assert.ok([...storage.data.values()].every(value=>Buffer.byteLength(value,'utf8')<2000));
await store.clear('dev/user','arrival','11');assert.equal(storage.data.size,0);
});
test('failed storage commit retains previous pending payload and clears partial chunks',async()=>{
const storage=memory(),store=createDraftStore(storage);await store.save('dev/1','use',4,{key:'original',attempted:true});
const old=new Map(storage.data),set=storage.set;let writes=0;storage.set=async(k,v)=>{if(++writes===2)throw Error('disk full');await set(k,v);};
await assert.rejects(store.save('dev/1','use',4,{key:'replacement',remark:'x'.repeat(2000)}),/disk full/);
assert.deepEqual(storage.data,old);assert.equal((await store.load('dev/1','use',4)).value.key,'original');
});
test('queued saves followed by clear cannot revive a completed business draft',async()=>{
const storage=memory(),store=createDraftStore(storage);
await Promise.all([store.save('dev/2','receive',9,{attempted:false}),store.save('dev/2','receive',9,{attempted:true,key:'stable'}),store.clear('dev/2','receive',9)]);
assert.equal(await store.load('dev/2','receive',9),null);assert.equal(storage.data.size,0);
});
test('corrupted pending drafts fail closed and credentials cannot enter persistent storage',async()=>{
const storage=memory(),store=createDraftStore(storage),key=draftKey('dev/1','move',1);
storage.data.set(key,'invalid');await assert.rejects(store.load('dev/1','move',1),/草稿索引异常/);
await assert.rejects(store.save('dev/1','move',1,{payload:{Authorization:'Bearer secret'}}),/登录凭据/);
assert.equal(storage.data.size,1);
});
test('calendar starts on Monday, includes leap day and crosses month/year correctly',()=>{
const feb=calendarDays(2024,1);assert.equal(feb[0],null);assert.equal(feb[3],'2024-02-01');assert.equal(feb.filter(Boolean).length,29);assert.ok(feb.includes('2024-02-29'));
assert.equal(calendarDays(2025,1).filter(Boolean).length,28);
assert.equal(calendarDate(2026,11,31),'2026-12-31');assert.deepEqual(dateParts('2026-12-31'),{year:2026,month:11,day:31});
for(const invalid of ['2025-02-29','2026-13-01','2026-04-31','2026-1-1','oops'])assert.equal(dateParts(invalid),null);
});