2026-10-17 19:50:24 +08:00
'use strict' ;
2026-10-23 00:38:04 +08:00
const { historicalAction } = require ( './fixtures/legacy-cancellation.cjs' );
2026-10-17 19:50:24 +08:00
const { test } = require ( 'node:test' ), assert = require ( 'node:assert/strict' );
const { randomUUID } = require ( 'node:crypto' ), fs = require ( 'node:fs' ), os = require ( 'node:os' ), path = require ( 'node:path' );
const { Ledger , today } = require ( '../ledger' ),{ createApp } = require ( '../server' );
const actor = { id : 'daily-owner' , name : '测试麦麦' , role : 'owner' }, assistant = { id : 'daily-assistant' , name : '测试助理' , role : 'assistant' };
const D = '2026-09-30' , D2 = '2026-10-01' , D3 = '2026-10-02' ;
const line = ( extra = {})=>({ name : '合成商品' , unit : 'g' , quantity : '1.200' , pocket : '' , payer : 'owner' , sale : '1200' , costParts : { material : '980' , labor : '20' , accessories : '0' , other : '0' },... extra });
function fixture ( t ){
const ledger = new Ledger ( ':memory:' ); t . after (()=> ledger . close ());
const run = ( payload , fn , key = randomUUID ())=> ledger . transact ( key , actor , payload , fn );
const save = ( extra = {}, issue = false )=>{
2026-10-23 09:49:36 +08:00
const input = { date : D , customer : '合成客户' , salesperson : '合成开单人' , note : '仅用于测试' , payment_account : '助理微信' , lines : [ line ()], fee_items : [],... extra };
2026-10-17 19:50:24 +08:00
return run ( input ,()=> issue ? ledger . issueOrder ( input , actor ) : ledger . saveOrder ( input , actor )). id ;
};
const action = ( id , kind , extra = {}, key = randomUUID ())=>{
2026-10-23 00:38:04 +08:00
const input = { date : D ,... extra }; return run ({ id , kind , input },()=> historicalAction ( ledger , id , kind , input , actor ), key );
2026-10-17 19:50:24 +08:00
};
const report = ( date = D , from = date )=> ledger . reportByOrderDate ( date , from );
const close = ( date = D , basis = 'orderDate' )=> run ({ date , basis },()=> ledger . closeDay ( date , actor , basis ));
return { ledger , run , save , action , report , close };
}
function sameBook ( f , date = D , from = date ){
const report = f . report ( date , from ), book = f . ledger . orderBook ({ date , from , status : 'all' });
assert . equal ( report . basis , 'orderDate' ); assert . deepEqual ( report . summary , book . summary ); assert . deepEqual ( report . orderRows , book . rows );
assert . deepEqual ( report . accounts , book . receiptsByOrderDate . accounts ); assert . deepEqual ( report . receiptsByOrderDate , book . receiptsByOrderDate );
assert . equal ( report . sales , book . summary . sale ); assert . equal ( report . profit , book . summary . profit );
assert . equal ( report . goods + report . expense , book . summary . totalCost ); assert . equal ( report . totalCost , book . summary . totalCost );
for ( const payer of [ 'owner' , 'assistant' ]) assert . equal ( report . payerCosts [ payer ]. total , book . summary [ payer + 'Cost' ]);
return report ;
}
function databaseContents ( ledger ){ return JSON . stringify ( ledger . rows ( "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name" ). map (({ name })=>[ name , ledger . rows ( 'SELECT * FROM "' + name + '"' )]));}
test ( '开单日对账与订单簿一致,双方货品及实际费用分开,按克结果和计算式保留' , t =>{
const f = fixture ( t ), id = f . save ({ lines : [ line ({ quantity : '2.200' , sale : '2300' , costMaterial : { mode : 'gram' , rate : '900.01' }, costLabor : { mode : 'gram' , rate : '20' }, costParts : { material : '0' , labor : '0' , accessories : '5' , other : '0' }}),
line ({ name : '合成银饰' , unit : '件' , quantity : '2' , payer : 'assistant' , sale : '200' , costParts : { material : '80' , labor : '10' , accessories : '0' , other : '0' }})],
fee_items : [{ key : 'shipping' , note : '快递费' , amount : '6' , payer : 'assistant' },{ key : 'certificate' , note : '证书' , amount : '4' , payer : 'owner' }]}, true );
const r = sameBook ( f ), owner = r . payerCosts . owner , helper = r . payerCosts . assistant ;
assert . equal ( r . sales , 250000 ); assert . equal ( r . goods , 211902 ); assert . equal ( r . expense , 1000 ); assert . equal ( r . profit , 37098 );
assert . equal ( owner . goods , 202902 ); assert . equal ( owner . paidFees , 400 ); assert . equal ( owner . total , 203302 );
assert . equal ( helper . goods , 9000 ); assert . equal ( helper . paidFees , 600 ); assert . equal ( helper . total , 9600 );
assert . equal ( owner . missingCount , 0 ); assert . equal ( helper . pendingCount , 0 );
const material = owner . items . find ( item => item . component === 'material' );
assert . equal ( material . amount , 198002 ); assert . deepEqual ( material . calculation ,{ rate : 90001 , quantity : 2200 });
const labor = owner . items . find ( item => item . component === 'labor' ); assert . equal ( labor . amount , 4400 ); assert . deepEqual ( labor . calculation ,{ rate : 2000 , quantity : 2200 });
for ( const item of [... owner . items ,... helper . items ]){ assert . equal ( item . order_id , id ); assert . equal ( item . date , D ); assert . equal ( item . paper_number , f . ledger . detail ( id ). paper_number ); assert . equal ( item . status , 'recorded' );}
assert . equal ( r . profit , f . ledger . detail ( id ). profit ); assert . equal ( r . profit , r . sales - owner . total - helper . total );
});
test ( '实际费用的商品关联仅在原项目唯一匹配时保留,未知来源按整单费用展示' , t =>{
const f = fixture ( t ), charge = randomUUID (), id = f . save ({ lines : [ line ({ charges : [{ id : charge , name : '特殊包装' , amount : '10' }]})],
fee_items : [{ key : 'charge:' + charge , note : '特殊包装' , amount : '3' , payer : 'assistant' },{ key : 'box' , note : '包装' , amount : '0' , payer : 'owner' }]}, true );
f . action ( id , 'expense' ,{ date : D2 , amount : '2' , payer : 'owner' , note : '额外实付' });
const r = sameBook ( f ), item = r . payerCosts . assistant . items . find ( item => item . kind === 'paid' );
assert . equal ( item . source , `商品 1 · 合成商品 · 实付 ${ D } ` ); assert . equal ( item . paidDate , D ); assert . equal ( item . date , D );
const extra = r . payerCosts . owner . items . find ( item => item . name === '额外实付' ); assert . equal ( extra . source , `整单费用 · 实付 ${ D2 } ` ); assert . equal ( extra . paidDate , D2 ); assert . equal ( extra . date , D );
const zero = r . payerCosts . owner . items . find ( item => item . name === '包装' ); assert . equal ( zero . kind , 'zero' ); assert . equal ( zero . amount , 0 ); assert . equal ( zero . status , 'recorded' );
assert . equal ( r . expense , 500 ); assert . equal ( r . payerCosts . owner . paidFees , 200 );
});
test ( '旧单未分项货品总成本仍归原付款人,分币不变,不伪造分项' , t =>{
const f = fixture ( t ); f . save ({ lines : [ line ({ unit : '件' , quantity : '1' , costParts : undefined , cost : '12.34' , sale : '50' , payer : 'assistant' })],
fee_items : [{ key : 'box' , note : '包装' , amount : '0.01' , payer : 'owner' }]}, true );
const r = sameBook ( f ), item = r . payerCosts . assistant . items [ 0 ];
assert . equal ( item . component , 'unallocated' ); assert . equal ( item . amount , 1234 ); assert . equal ( item . calculation , null );
assert . equal ( r . payerCosts . assistant . goods , 1234 ); assert . equal ( r . payerCosts . owner . total , 1 ); assert . equal ( r . totalCost , 1235 ); assert . equal ( r . profit , 3765 );
assert . equal ( r . summary . legacyUnallocatedCount , 1 ); assert . equal ( r . orderRows [ 0 ]. material , null ); assert . equal ( r . summary . partial , true );
});
test ( '缺项保留null,草稿和预估费用不算实际支出,按付款人显示待补与未入账数量' , t =>{
const f = fixture ( t ), old = f . save ({ lines : [ line ({ costParts : { material : '' , labor : '0' , accessories : '5' , other : '0' }})],
fee_items : [{ key : 'shipping' , note : '待核快递' , amount : '6' , payer : 'assistant' , status : 'estimated' },{ key : 'box' , note : '待补包装' , amount : '' , payer : 'owner' , status : 'estimated' }]});
f . action ( old , 'confirm' ); f . action ( old , 'expense' ,{ amount : '24' , payer : 'assistant' , note : '实际证书费' });
const draft = f . save ({ customer : '草稿不算支出' , lines : [ line ({ payer : 'assistant' , sale : '9999' })], fee_items : [{ key : 'shipping' , amount : '30' , payer : 'assistant' , status : 'estimated' }]});
const r = sameBook ( f ), owner = r . payerCosts . owner , helper = r . payerCosts . assistant ;
assert . equal ( r . goods , 500 ); assert . equal ( r . expense , 2400 ); assert . equal ( r . totalCost , 2900 ); assert . equal ( r . profit , 0 ); assert . equal ( r . sales , 120000 );
assert . equal ( owner . total , 500 ); assert . equal ( owner . missingCount , 1 ); assert . equal ( owner . pendingCount , 1 ); assert . equal ( helper . total , 2400 ); assert . equal ( helper . missingCount , 0 ); assert . equal ( helper . pendingCount , 1 );
assert . equal ( owner . items . find ( item => item . component === 'material' ). amount , null ); assert . equal ( owner . items . find ( item => item . component === 'material' ). status , 'missing' );
assert . equal ( helper . items . find ( item => item . kind === 'pending' ). amount , 600 ); assert . equal ( helper . items . find ( item => item . kind === 'pending' ). status , 'pending' );
assert . ok ([... owner . items ,... helper . items ]. every ( item => item . order_id !== draft ));
assert . equal ( r . summary . unknownCosts , 3 ); assert . equal ( r . summary . unknownProfitCount , 1 ); assert . equal ( r . orderRows . find ( row => row . id === old ). totalCost , null );
});
test ( '跨天及跨月尾款和费用归开单日,日月空日合计一致,旧流水日期接口保持原状' , t =>{
const f = fixture ( t ), id = f . save ({ lines : [ line ({ sale : '100' , costParts : { material : '50' , labor : '0' , accessories : '0' , other : '0' }})]});
f . action ( id , 'confirm' ); f . action ( id , 'receipt' ,{ amount : '40' , account : '助理微信' , verified : true });
f . action ( id , 'receipt' ,{ date : D2 , amount : '60' , account : '麦麦支付宝' , verified : true });
f . action ( id , 'expense' ,{ date : D2 , amount : '8' , payer : 'assistant' , note : '跨月快递' }); f . action ( id , 'deliver' ,{ date : D2 , verified : true });
f . action ( id , 'expense' ,{ date : D3 , amount : '2' , payer : 'owner' , note : '跨月包装' });
const cash = f . save ({ date : D2 , customer : '次日开单' , payment_account : '助理现金' , lines : [ line ({ sale : '30' , costParts : { material : '10' , labor : '0' , accessories : '0' , other : '0' }})]}, true );
const r = sameBook ( f ); assert . equal ( r . sales , 10000 ); assert . equal ( r . profit , 4000 ); assert . equal ( r . expense , 1000 ); assert . equal ( r . receiptsByOrderDate . receipt , 10000 );
assert . equal ( r . payments . length , 2 ); assert . ok ( r . payments . every ( payment => payment . order_id === id && payment . order_date === D ));
assert . ok ( r . payerCosts . owner . items . some ( item => item . paidDate === D3 ));
const next = sameBook ( f , D2 ); assert . deepEqual ( next . orderRows . map ( row => row . id ),[ cash ]); assert . equal ( next . receiptsByOrderDate . receipt , 3000 ); assert . equal ( next . expense , 0 );
const september = sameBook ( f , D , '2026-09-01' ); assert . equal ( september . sales , r . sales ); assert . equal ( september . receiptsByOrderDate . receipt , 10000 ); assert . equal ( september . expense , 1000 );
const october = sameBook ( f , D3 , D2 ); assert . equal ( october . sales , 3000 ); assert . equal ( october . receiptsByOrderDate . receipt , 3000 ); assert . equal ( october . expense , 0 );
const combined = sameBook ( f , D3 , D ); for ( const key of [ 'sales' , 'goods' , 'expense' , 'profit' ]) assert . equal ( combined [ key ], r [ key ] + next [ key ]);
const empty = sameBook ( f , D3 ); assert . equal ( empty . sales , 0 ); assert . equal ( empty . expense , 0 ); assert . equal ( empty . profit , 0 ); assert . deepEqual ( empty . orderRows ,[]); assert . equal ( empty . receiptsByOrderDate . receipt , 0 );
assert . equal ( f . ledger . report ( D ). sales , 0 ); assert . equal ( f . ledger . report ( D ). accounts . find ( item => item . account === '助理微信' ). receipt , 4000 );
assert . equal ( f . ledger . report ( D2 ). sales , 13000 ); assert . equal ( f . ledger . report ( D3 ). expense , 200 );
});
test ( '退货取消归原开单日冲回货品及销售,实际费用保留,客户退款不再扣一次利润' , t =>{
const f = fixture ( t ), returned = f . save ({ fee_items : [{ key : 'shipping' , note : '已付运费' , amount : '8' , payer : 'assistant' }]}, true );
f . action ( returned , 'return' ,{ date : D2 , reason : '合成退货' , verified : true }); f . action ( returned , 'refund' ,{ date : D2 , amount : '1200' , account : '助理微信' , verified : true });
const cancelled = f . save ({ lines : [ line ({ payer : 'assistant' })]}); f . action ( cancelled , 'confirm' ); f . action ( cancelled , 'expense' ,{ amount : '2' , payer : 'owner' , note : '已付打样' });
f . action ( cancelled , 'receipt' ,{ amount : '100' , account : '麦麦现金' , verified : true }); f . action ( cancelled , 'cancel' ,{ date : D2 , reason : '合成取消' }); f . action ( cancelled , 'refund' ,{ date : D2 , amount : '100' , account : '麦麦现金' , verified : true });
const r = sameBook ( f ); assert . equal ( r . sales , 0 ); assert . equal ( r . goods , 0 ); assert . equal ( r . expense , 1000 ); assert . equal ( r . profit , - 1000 );
assert . equal ( r . payerCosts . owner . total , 200 ); assert . equal ( r . payerCosts . assistant . total , 800 ); assert . ok ( Object . values ( r . payerCosts ). every ( payer => payer . items . every ( item => item . kind !== 'goods' )));
assert . equal ( r . receiptsByOrderDate . receipt , 130000 ); assert . equal ( r . receiptsByOrderDate . refund , 130000 ); assert . equal ( r . receiptsByOrderDate . net , 0 );
assert . equal ( sameBook ( f , D2 ). profit , 0 ); assert . equal ( sameBook ( f , D2 ). receiptsByOrderDate . refund , 0 );
assert . equal ( r . returns , 1 ); assert . equal ( r . summary . cancelledCount , 1 );
});
test ( '跨日连续更正费用及付款人只计最新有效项,撤销后原项与冲回项不重复' , t =>{
const f = fixture ( t ), id = f . save ({ fee_items : [{ key : 'shipping' , note : '快递' , amount : '6' , payer : 'assistant' }]}, true );
let target = f . ledger . detail ( id ). expenses [ 0 ]. id ;
for ( const [ amount , payer , ownerFees , assistantFees ] of [[ '8' , 'owner' , 800 , 0 ],[ '5' , 'assistant' , 0 , 500 ],[ '0' , 'assistant' , 0 , 0 ]]){
f . action ( id , 'correct' ,{ date : D2 , type : 'expense' , target , amount , payer , reason : '合成更正' , verified : true });
const r = sameBook ( f ); assert . equal ( r . payerCosts . owner . paidFees , ownerFees ); assert . equal ( r . payerCosts . assistant . paidFees , assistantFees );
assert . equal ( r . profit , 120000 - 100000 - ownerFees - assistantFees ); assert . equal ( r . profit , f . ledger . detail ( id ). profit );
assert . equal ( Object . values ( r . payerCosts ). flatMap ( p => p . items ). filter ( item => item . kind === 'paid' ). length , amount === '0' ? 0 : 1 );
if ( amount !== '0' ) target = f . ledger . detail ( id ). expenses . find ( item => item . amount === Number ( amount ) * 100 ). id ;
}
assert . equal ( sameBook ( f , D2 ). expense , 0 ); assert . equal ( f . ledger . detail ( id ). expenses . length , 6 );
});
test ( '读取对账和快照状态不改订单、审计、同步、操作号或快照,日期验证保持一致' , t =>{
const f = fixture ( t ); f . save ({}, true ); f . close ();
const before = databaseContents ( f . ledger ), changes = f . ledger . one ( 'SELECT total_changes() AS count' ). count ;
const first = f . report (); assert . deepEqual ( f . report (), first ); sameBook ( f ); f . report ( D , '2026-09-01' ); f . report ( D2 );
for ( const [ date , from ] of [[ '2026-02-30' , undefined ],[ '2099-01-01' , undefined ],[ D , D2 ],[ '' , undefined ]]) assert . throws (()=> f . ledger . reportByOrderDate ( date , from ));
assert . equal ( f . ledger . one ( 'SELECT total_changes() AS count' ). count , changes ); assert . equal ( databaseContents ( f . ledger ), before );
});
test ( '开单日快照遇跨日尾款、交付、补费及收款更正会过期,重存同日覆盖并保留库存快照' , t =>{
const f = fixture ( t ), id = f . save ({ lines : [ line ({ sale : '100' , costParts : { material : '50' , labor : '0' , accessories : '0' , other : '0' }})]});
f . action ( id , 'confirm' ); f . action ( id , 'receipt' ,{ amount : '40' , account : '助理微信' , verified : true });
const first = f . close (); assert . equal ( first . basis , 'orderDate' ); assert . equal ( f . report (). close . stale , false ); assert . equal ( first . reportRevision , f . report (). reportRevision );
const deposit = f . ledger . detail ( id ). payments [ 0 ]. id ;
f . action ( id , 'receipt' ,{ date : D2 , amount : '60' , account : '助理现金' , verified : true }); assert . equal ( f . ledger . one ( 'SELECT stale FROM closes WHERE date=?' , D ). stale , 0 ); assert . equal ( f . report (). close . stale , true );
f . close (); assert . equal ( f . report (). close . stale , false );
f . action ( id , 'deliver' ,{ date : D2 , verified : true }); assert . equal ( f . report (). close . stale , true ); f . close ();
f . action ( id , 'expense' ,{ date : D3 , amount : '8' , payer : 'assistant' , note : '售后快递' }); assert . equal ( f . report (). close . stale , true ); f . close ();
f . action ( id , 'correct' ,{ date : D3 , type : 'payment' , target : deposit , amount : '40' , account : '助理支付宝' , reason : '账户更正' , verified : true });
const corrected = f . report (); assert . equal ( corrected . close . stale , true ); assert . match ( corrected . close . staleReason , /开单日/ );
assert . equal ( corrected . accounts . find ( row => row . account === '助理微信' ). receipt , 0 ); assert . equal ( corrected . accounts . find ( row => row . account === '助理支付宝' ). receipt , 4000 );
f . close (); assert . equal ( f . report (). close . stale , false );
assert . equal ( f . ledger . rows ( 'SELECT * FROM closes' ). length , 1 ); assert . equal ( f . ledger . inventorySnapshots (). snapshots . length , 1 ); assert . deepEqual ( first . snapshot . inventory , f . ledger . inventorySnapshot ( D ). inventory );
assert . equal ( f . report ( D , '2026-09-01' ). close , null , '单日快照不能冒充整月快照' );
});
test ( '纯复核、打印、库存及其他开单日数据和交款变化不使本日财务快照过期' , t =>{
const f = fixture ( t ), id = f . save ({}, true ); f . close (); const revision = f . report (). reportRevision ;
f . action ( id , 'review' ,{ revision : f . ledger . detail ( id ). revision }); f . action ( id , 'print' );
f . run ({ stock : true },()=> f . ledger . stock ({ kind : 'opening' , date : D , pocket : '现货' , grams : '5' , note : '合成独立库存' }, actor ));
f . run ({ inventory : true },()=> f . ledger . saveInventorySnapshot ( D , actor ));
f . save ({ date : D2 }, true ); f . action ( id , 'settlement' ,{ date : D2 , amount : '1000' , bucket : 'cost' , direction : 'toOwner' , note : '实际交款' , verified : true });
assert . equal ( f . ledger . report ( D ). close . stale , true , '保留旧流水口径的原过期规则' );
assert . equal ( f . report (). reportRevision , revision ); assert . equal ( f . report (). close . stale , false ); assert . equal ( f . report (). close . staleReason , null );
});
test ( '旧口径快照在新对账中明确待重核,读取不迁移,保存新口径后恢复有效' , t =>{
const f = fixture ( t ); f . save ({}, true ); f . run ({ close : true },()=> f . ledger . closeDay ( D , actor ));
const before = databaseContents ( f . ledger ), r = f . report (); assert . equal ( r . close . stale , true ); assert . equal ( r . close . basis , 'entryDate' ); assert . match ( r . close . staleReason , /口径已更新/ ); assert . equal ( databaseContents ( f . ledger ), before );
f . close (); assert . equal ( f . report (). close . basis , 'orderDate' ); assert . equal ( f . report (). close . stale , false );
assert . throws (()=> f . ledger . closeDay ( D , assistant , 'orderDate' ), /麦麦账号/ ); assert . throws (()=> f . ledger . closeDay ( D , actor , 'wrong' ), /口径无效/ );
});
test ( 'HTTP新对账要求登录,助理可读,旧接口兼容,快照保留owner权限、CSRF及幂等' , async t =>{
const dir = fs . mkdtempSync ( path . join ( os . tmpdir (), 'maimai-daily-report-test-' ));
const app = createApp ({ mode : 'demo' , port : 0 , dataDir : dir , ledger : new Ledger ( ':memory:' ), feishuConfig : { enabled : false }});
await new Promise ( resolve => app . server . listen ( 0 , '127.0.0.1' , resolve ));
t . after ( async ()=>{ await new Promise ( resolve => app . server . close ( resolve )); app . ledger . close (); fs . rmSync ( dir ,{ recursive : true , force : true });});
const base = 'http://127.0.0.1:' + app . server . address (). port ; let cookie = '' ;
const call = ( route , data , key = randomUUID (), origin = base )=> fetch ( base + route ,{ method : data ? 'POST' : 'GET' , headers : {...( cookie ? { Cookie : cookie } : {}),...( data ? { 'Content-Type' : 'application/json' , Origin : origin , 'X-Requested-With' : 'maimai-ledger' , 'Idempotency-Key' : key } : {} )},...( data ? { body : JSON . stringify ( data )} : {})});
assert . equal (( await call ( '/api/report?basis=orderDate' )). status , 401 );
const login = await call ( '/api/demo-login' ,{ role : 'assistant' }); cookie = login . headers . get ( 'set-cookie' ). split ( ';' )[ 0 ];
const before = databaseContents ( app . ledger ), changes = app . ledger . one ( 'SELECT total_changes() AS count' ). count ;
const response = await call ( '/api/report?basis=orderDate' ), r = await response . json (); assert . equal ( response . status , 200 ); assert . equal ( r . basis , 'orderDate' ); assert . equal ( r . summary . orderCount , 1 ); assert . equal ( r . sales , 120000 );
assert . equal ( r . payerCosts . owner . goods , 100000 ); assert . equal ( r . payerCosts . owner . items [ 0 ]. component , 'unallocated' ); assert . equal ( r . profit , 20000 );
const legacy = await ( await call ( '/api/report' )). json (); assert . ok ( ! ( 'basis' in legacy )); assert . equal ( legacy . sales , r . sales );
for ( const route of [ '/api/report?basis=wrong' , '/api/report?basis=orderDate&date=2099-01-01' , '/api/report?basis=orderDate&date=2026-02-30' , '/api/report?basis=orderDate&from=' + D2 + '&date=' + D ]) assert . equal (( await call ( route )). status , 400 );
assert . equal ( app . ledger . one ( 'SELECT total_changes() AS count' ). count , changes ); assert . equal ( databaseContents ( app . ledger ), before );
assert . equal (( await call ( '/api/close' ,{ date : today (), basis : 'orderDate' })). status , 403 ); assert . equal ( app . ledger . rows ( 'SELECT * FROM closes' ). length , 0 );
const ownerLogin = await call ( '/api/demo-login' ,{ role : 'owner' }); cookie = ownerLogin . headers . get ( 'set-cookie' ). split ( ';' )[ 0 ];
assert . equal (( await call ( '/api/close' ,{ date : today (), basis : 'orderDate' }, randomUUID (), 'https://invalid.example' )). status , 403 );
assert . equal (( await call ( '/api/close' ,{ date : today (), basis : 'wrong' })). status , 400 );
const payload = { date : today (), basis : 'orderDate' }, key = randomUUID (), closed = await call ( '/api/close' , payload , key ); assert . equal ( closed . status , 200 ); const first = await closed . json ();
const retry = await call ( '/api/close' , payload , key ); assert . equal ( retry . status , 200 ); assert . deepEqual ( await retry . json (), first );
assert . equal ( app . ledger . rows ( 'SELECT * FROM closes' ). length , 1 ); assert . equal ( app . ledger . rows ( 'SELECT * FROM inventory_snapshots' ). length , 1 ); assert . equal ( app . ledger . rows ( 'SELECT * FROM operations WHERE id=?' , key ). length , 1 );
const saved = await ( await call ( '/api/report?basis=orderDate' )). json (); assert . equal ( saved . close . stale , false ); assert . equal ( saved . close . basis , 'orderDate' );
});