2026-10-17 19:50:24 +08:00
'use strict' ;
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 owner = { id : 'people-owner' , name : '测试麦麦' , role : 'owner' }, assistant = { id : 'people-assistant' , name : '测试助理' , role : 'assistant' };
const D = '2026-09-30' , D2 = '2026-10-01' , D3 = '2026-10-02' ;
2026-10-23 00:21:53 +08:00
const entry = ( date , count , revision , assistant1Count = null , assistant2Count = null )=>({ date , count , assistant1Count , assistant2Count , revision });
const aggregate = ( from , date , total , recordedDays , assistant1 = { total : 0 , recordedDays : 0 }, assistant2 = { total : 0 , recordedDays : 0 })=>({ from , date , total , recordedDays , assistant1 , assistant2 });
2026-10-17 19:50:24 +08:00
function fixture ( t ){
const ledger = new Ledger ( ':memory:' ); t . after (()=> ledger . close ());
2026-10-23 00:21:53 +08:00
const write = ( input , actor = owner , key = randomUUID ())=> ledger . transact ( key , actor , input ,()=> ledger . savePeopleCount ( input , actor ));
2026-10-17 19:50:24 +08:00
const save = ( date , count , revision , actor = owner , key = randomUUID ())=>{
2026-10-23 00:21:53 +08:00
return write ({ date , count , revision }, actor , key );
2026-10-17 19:50:24 +08:00
};
2026-10-23 00:21:53 +08:00
return { ledger , save , write };
2026-10-17 19:50:24 +08:00
}
function contents ( ledger , tables ){ return JSON . stringify ( tables . map ( table =>[ table , ledger . rows ( 'SELECT * FROM "' + table + '"' )]));}
function allContents ( ledger ){ return contents ( ledger , ledger . rows ( "SELECT name FROM sqlite_master WHERE type='table' ORDER BY name" ). map ( row => row . name ));}
test ( '每日人数区分未登记与0,按日期保存总数而非累加,双方修改保留版本和审计' , t =>{
2026-10-23 00:21:53 +08:00
const f = fixture ( t ); assert . deepEqual ( f . ledger . peopleCount ( D ), entry ( D , null , 0 ));
assert . deepEqual ( f . save ( D , 0 , 0 , assistant ), entry ( D , 0 , 1 )); const initial = f . ledger . one ( 'SELECT * FROM daily_people_counts WHERE date=?' , D );
2026-10-17 19:50:24 +08:00
assert . equal ( initial . actor , assistant . name ); assert . equal ( initial . revision , 1 ); assert . equal ( initial . count , 0 );
2026-10-23 00:21:53 +08:00
assert . deepEqual ( f . save ( D , 15 , 1 , owner ), entry ( D , 15 , 2 ));
assert . deepEqual ( f . save ( D , 4 , 2 , assistant ), entry ( D , 4 , 3 )); assert . equal ( f . ledger . rows ( 'SELECT * FROM daily_people_counts' ). length , 1 );
2026-10-17 19:50:24 +08:00
const record = f . ledger . one ( 'SELECT * FROM daily_people_counts WHERE date=?' , D ); assert . equal ( record . created , initial . created ); assert . ok ( record . updated >= initial . updated ); assert . equal ( record . actor , assistant . name );
2026-10-23 00:21:53 +08:00
assert . deepEqual ( f . ledger . peopleCount ( D ), entry ( D , 4 , 3 )); assert . deepEqual ( f . ledger . peopleCount ( D2 ), entry ( D2 , null , 0 ));
2026-10-17 19:50:24 +08:00
const audits = f . ledger . rows ( "SELECT * FROM audit WHERE entity=? ORDER BY id" , 'people-count:' + D ); assert . equal ( audits . length , 3 );
assert . deepEqual ( audits . map ( row => JSON . parse ( row . detail ). previousCount ),[ null , 0 , 15 ]); assert . deepEqual ( audits . map ( row => JSON . parse ( row . detail ). count ),[ 0 , 15 , 4 ]);
});
test ( '人数拒绝空白、负数、非整数、超限、非法日期和非法版本,失败不改已有记录' , t =>{
const f = fixture ( t ), before = allContents ( f . ledger );
for ( const count of [ '' , undefined , null , ' ' , true , false ,[],{}, '-1' , '1.5' , '1.0' , '1e2' , 'NaN' , Infinity , NaN , 1000001 , Number . MAX_SAFE_INTEGER + 1 ]) assert . throws (()=> f . save ( D , count , 0 ));
for ( const date of [ '' , undefined , null , '2026-02-30' , '2026-9-30' , '1999-12-31' , '2099-01-01' ]) assert . throws (()=> f . save ( date , 1 , 0 ));
for ( const revision of [ undefined , null , - 1 , 1.5 , '0' , Number . MAX_SAFE_INTEGER + 1 ]) assert . throws (()=> f . save ( D , 1 , revision ));
assert . throws (()=> f . ledger . savePeopleCount ( null , owner )); assert . throws (()=> f . ledger . savePeopleCount ([], owner )); assert . throws (()=> f . save ( D , 1 , 0 ,{... owner , role : 'customer' }), /麦麦或助理账号/ );
2026-10-23 00:21:53 +08:00
assert . equal ( allContents ( f . ledger ), before ); assert . deepEqual ( f . save ( D , '1000000' , 0 ), entry ( D , 1000000 , 1 ));
2026-10-17 19:50:24 +08:00
const existing = allContents ( f . ledger ); assert . throws (()=> f . save ( D , 9 , 0 ),{ status : 409 }); assert . throws (()=> f . save ( D2 , 9 , 1 ),{ status : 409 }); assert . equal ( allContents ( f . ledger ), existing );
});
test ( '人数操作幂等只保存一次,旧版本不能覆盖新记录,操作号内容或用户改变会冲突' , t =>{
const f = fixture ( t ), key = randomUUID (), first = f . save ( D , 7 , 0 , assistant , key ); assert . deepEqual ( f . save ( D , 7 , 0 , assistant , key ), first );
assert . equal ( f . ledger . peopleCount ( D ). revision , 1 ); assert . equal ( f . ledger . rows ( 'SELECT * FROM audit' ). length , 1 ); assert . equal ( f . ledger . rows ( 'SELECT * FROM operations' ). length , 1 );
assert . throws (()=> f . save ( D , 8 , 0 , assistant , key ),{ status : 409 }); assert . throws (()=> f . save ( D , 7 , 0 , owner , key ),{ status : 409 });
f . save ( D , 12 , 1 , owner ); const before = allContents ( f . ledger ); assert . throws (()=> f . save ( D , 3 , 1 , assistant ),{ status : 409 }); assert . equal ( allContents ( f . ledger ), before );
2026-10-23 00:21:53 +08:00
assert . deepEqual ( f . ledger . peopleCount ( D ), entry ( D , 12 , 2 ));
});
test ( '助理两项独立于原人数,空白未记录与0区分,旧请求缺省保留且显式null可清空' , t =>{
const f = fixture ( t ), first = { date : D , count : 3 , assistant1Count : 100 , assistant2Count : 0 , revision : 0 }, key = randomUUID ();
assert . deepEqual ( f . write ( first , assistant , key ), entry ( D , 3 , 1 , 100 , 0 ));
assert . deepEqual ( f . save ( D , 9 , 1 ), entry ( D , 9 , 2 , 100 , 0 ));
assert . deepEqual ( f . write ({ date : D , count : 9 , assistant1Count : null , revision : 2 }), entry ( D , 9 , 3 , null , 0 ));
assert . deepEqual ( f . write ({ date : D , count : 9 , assistant1Count : '0' , assistant2Count : ' ' , revision : 3 }), entry ( D , 9 , 4 , 0 , null ));
const before = allContents ( f . ledger ); assert . deepEqual ( f . write ( first , assistant , key ), entry ( D , 3 , 1 , 100 , 0 )); assert . equal ( allContents ( f . ledger ), before , '中断重试返回原结果,不能覆盖后来保存的三项' );
const audit = JSON . parse ( f . ledger . rows ( 'SELECT detail FROM audit ORDER BY id DESC LIMIT 1' )[ 0 ]. detail );
assert . deepEqual ([ audit . previousCount , audit . previousAssistant1Count , audit . previousAssistant2Count ],[ 9 , null , 0 ]);
assert . deepEqual ([ audit . count , audit . assistant1Count , audit . assistant2Count ],[ 9 , 0 , null ]);
assert . equal ( f . ledger . rows ( 'SELECT * FROM daily_people_counts' ). length , 1 );
});
test ( '助理人数只接受空白null或范围内整数,非法任一项和并发旧版本全部不写入' , t =>{
const f = fixture ( t ); f . write ({ date : D , count : 7 , assistant1Count : 1 , assistant2Count : 2 , revision : 0 }); const before = allContents ( f . ledger );
for ( const name of [ 'assistant1Count' , 'assistant2Count' ]) for ( const value of [ undefined , true , false ,[],{}, '-1' , '0.1' , '1.0' , '1e3' , Infinity , NaN , 1000001 , Number . MAX_SAFE_INTEGER + 1 ])
assert . throws (()=> f . write ({ date : D , count : 9 ,[ name ] : value , revision : 1 }));
assert . throws (()=> f . write ({ date : D , count : '' , assistant1Count : 0 , assistant2Count : 0 , revision : 1 }), /人数/ );
assert . throws (()=> f . write ({ date : D , count : 9 , assistant1Count : 0 , assistant2Count : 0 , revision : 0 }),{ status : 409 }); assert . equal ( allContents ( f . ledger ), before );
assert . deepEqual ( f . write ({ date : D , count : 1000000 , assistant1Count : 1000000 , assistant2Count : 1000000 , revision : 1 }), entry ( D , 1000000 , 2 , 1000000 , 1000000 ));
});
test ( '日月各项分别合计和统计已记录天数,未填不计天数,零值计天数且不受订单筛选影响' , t =>{
const f = fixture ( t ); f . write ({ date : D , count : 3 , assistant1Count : 0 , assistant2Count : null , revision : 0 }); f . write ({ date : D2 , count : 0 , assistant1Count : null , assistant2Count : 5 , revision : 0 }); f . write ({ date : D3 , count : 4 , assistant1Count : 7 , assistant2Count : 0 , revision : 0 });
const expected = aggregate ( D , D3 , 7 , 3 ,{ total : 7 , recordedDays : 2 },{ total : 5 , recordedDays : 2 });
for ( const query of [{},{ q : '不存在' },{ status : 'after_sale' },{ status : 'cancelled' }]) assert . deepEqual ( f . ledger . orderBook ({ from : D , date : D3 ,... query }). peopleCount , expected );
assert . deepEqual ( f . ledger . reportByOrderDate ( D3 , D ). peopleCount , expected );
assert . deepEqual ( f . ledger . peopleCountSummary ( D ), aggregate ( D , D , 3 , 1 ,{ total : 0 , recordedDays : 1 },{ total : 0 , recordedDays : 0 }));
assert . deepEqual ( f . ledger . peopleCountSummary ( D2 ), aggregate ( D2 , D2 , 0 , 1 ,{ total : 0 , recordedDays : 0 },{ total : 5 , recordedDays : 1 }));
assert . deepEqual ( f . ledger . peopleCountSummary ( D3 ), aggregate ( D3 , D3 , 4 , 1 ,{ total : 7 , recordedDays : 1 },{ total : 0 , recordedDays : 1 }));
2026-10-17 19:50:24 +08:00
});
test ( '日期范围人数独立于订单搜索状态,零人数为已登记,日报透传同一聚合' , t =>{
const f = fixture ( t ); f . save ( D , 3 , 0 ); f . save ( D2 , 0 , 0 ); f . save ( D3 , 4 , 0 );
2026-10-23 00:21:53 +08:00
const expected = aggregate ( D , D3 , 7 , 3 );
2026-10-17 19:50:24 +08:00
for ( const query of [{},{ q : '不存在' },{ status : 'after_sale' },{ status : 'draft' },{ q : '售后' , status : 'delivered' }]){
const b = f . ledger . orderBook ({ from : D , date : D3 ,... query }); assert . deepEqual ( b . peopleCount , expected ); assert . equal ( b . summary . recordCount , 0 ); assert . equal ( b . summary . profit , 0 );
}
assert . deepEqual ( f . ledger . reportByOrderDate ( D3 , D ). peopleCount , expected );
2026-10-23 00:21:53 +08:00
assert . deepEqual ( f . ledger . orderBook ({ date : D2 }). peopleCount , aggregate ( D2 , D2 , 0 , 1 ));
assert . deepEqual ( f . ledger . orderBook ({ date : '2026-09-29' }). peopleCount , aggregate ( '2026-09-29' , '2026-09-29' , 0 , 0 ));
assert . deepEqual ( f . ledger . orderBook ({ from : '2026-09-01' , date : D }). peopleCount , aggregate ( '2026-09-01' , D , 3 , 1 ));
assert . deepEqual ( f . ledger . orderBook ({ from : D2 , date : D3 }). peopleCount , aggregate ( D2 , D3 , 4 , 2 ));
2026-10-17 19:50:24 +08:00
});
test ( '人数登记修改不改变销售成本利润、收款、库存、财务版本、核对快照或同步' , t =>{
2026-10-23 09:49:36 +08:00
const f = fixture ( t ), input = { date : D , customer : '合成客户' , salesperson : '合成开单人' , lines : [{ name : '合成商品' , unit : 'g' , quantity : '1' , pocket : '' , payer : 'owner' , cost : '80' , sale : '100' }], fee_items : []};
2026-10-17 19:50:24 +08:00
f . ledger . transact ( randomUUID (), owner , input ,()=> f . ledger . issueOrder ( input , owner )); f . ledger . transact ( randomUUID (), owner ,{ date : D },()=> f . ledger . closeDay ( D , owner , 'orderDate' ));
const tables = [ 'orders' , 'payments' , 'expenses' , 'after_sales' , 'settlements' , 'movements' , 'journal' , 'meta' , 'sync' , 'closes' , 'inventory_snapshots' ], before = contents ( f . ledger , tables ), r = f . ledger . reportByOrderDate ( D ), book = f . ledger . orderBook ({ date : D });
2026-10-23 00:21:53 +08:00
f . save ( D , 0 , 0 ); f . write ({ date : D , count : 20 , assistant1Count : 0 , assistant2Count : 7 , revision : 1 }, assistant ); f . write ({ date : D2 , count : 9 , assistant1Count : 100 , assistant2Count : null , revision : 0 });
2026-10-17 19:50:24 +08:00
assert . equal ( contents ( f . ledger , tables ), before ); const after = f . ledger . reportByOrderDate ( D );
assert . equal ( after . reportRevision , r . reportRevision ); assert . equal ( after . close . stale , false ); assert . equal ( f . ledger . report ( D ). close . stale , false ); assert . equal ( f . ledger . inventorySnapshot ( D ). stale , false );
assert . deepEqual ( after . summary , r . summary ); assert . deepEqual ( after . orderRows , r . orderRows ); assert . deepEqual ( after . accounts , r . accounts ); assert . deepEqual ( after . payerCosts , r . payerCosts );
2026-10-23 00:21:53 +08:00
assert . deepEqual ( f . ledger . orderBook ({ date : D }). summary , book . summary ); assert . deepEqual ( after . peopleCount , aggregate ( D , D , 20 , 1 ,{ total : 0 , recordedDays : 1 },{ total : 7 , recordedDays : 1 }));
2026-10-17 19:50:24 +08:00
const readBefore = allContents ( f . ledger ), changes = f . ledger . one ( 'SELECT total_changes() AS count' ). count ;
f . ledger . peopleCount ( D ); f . ledger . peopleCount ( D3 ); f . ledger . peopleCountSummary ( D3 , D ); f . ledger . orderBook ({ date : D }); f . ledger . reportByOrderDate ( D ); assert . equal ( allContents ( f . ledger ), readBefore ); assert . equal ( f . ledger . one ( 'SELECT total_changes() AS count' ). count , changes );
});
test ( '旧库增量建人数表,0与版本重启后保留,重试不再覆盖新总人数' , t =>{
const dir = fs . mkdtempSync ( path . join ( os . tmpdir (), 'maimai-people-persist-' )), filename = path . join ( dir , 'ledger.sqlite' ); let ledger = new Ledger ( filename );
t . after (()=>{ try { ledger . close ();} catch {} fs . rmSync ( dir ,{ recursive : true , force : true });});
ledger . setMeta ( 'synthetic-existing' , 7 ); ledger . db . exec ( 'DROP TABLE daily_people_counts' ); ledger . close (); ledger = new Ledger ( filename );
2026-10-23 00:21:53 +08:00
assert . equal ( ledger . meta ( 'synthetic-existing' ), 7 ); assert . deepEqual ( ledger . peopleCount ( D ), entry ( D , null , 0 ));
2026-10-17 19:50:24 +08:00
const value = { date : D , count : 0 , revision : 0 }, key = randomUUID (), result = ledger . transact ( key , owner , value ,()=> ledger . savePeopleCount ( value , owner )); ledger . close (); ledger = new Ledger ( filename );
2026-10-23 00:21:53 +08:00
assert . deepEqual ( ledger . peopleCount ( D ), entry ( D , 0 , 1 )); assert . deepEqual ( ledger . transact ( key , owner , value ,()=> ledger . savePeopleCount ( value , owner )), result );
2026-10-17 19:50:24 +08:00
const changed = { date : D , count : 11 , revision : 1 }; ledger . transact ( randomUUID (), assistant , changed ,()=> ledger . savePeopleCount ( changed , assistant ));
2026-10-23 00:21:53 +08:00
assert . deepEqual ( ledger . transact ( key , owner , value ,()=> ledger . savePeopleCount ( value , owner )), result ); assert . deepEqual ( ledger . peopleCount ( D ), entry ( D , 11 , 2 )); assert . deepEqual ( ledger . peopleCount ( D2 ), entry ( D2 , null , 0 ));
});
test ( '已有原人数的旧表增量加两列NULL,原列原值和历史操作结果逐字保留,重开不回填助理' , t =>{
const { DatabaseSync } = require ( 'node:sqlite' ),{ createHash } = require ( 'node:crypto' );
const dir = fs . mkdtempSync ( path . join ( os . tmpdir (), 'maimai-people-columns-' )), filename = path . join ( dir , 'ledger.sqlite' ); t . after (()=> fs . rmSync ( dir ,{ recursive : true , force : true }));
const old = new DatabaseSync ( filename );
old . exec ( 'CREATE TABLE daily_people_counts(date TEXT PRIMARY KEY,count INTEGER NOT NULL CHECK(count>=0 AND count<=1000000),revision INTEGER NOT NULL,actor TEXT NOT NULL,created TEXT NOT NULL,updated TEXT NOT NULL)' );
old . prepare ( 'INSERT INTO daily_people_counts VALUES (?,?,?,?,?,?)' ). run ( D , 0 , 4 , '旧录入人' , '2026-09-30T01:00:00.000Z' , '2026-09-30T02:00:00.000Z' );
old . prepare ( 'INSERT INTO daily_people_counts VALUES (?,?,?,?,?,?)' ). run ( D2 , 17 , 2 , '另一旧录入人' , '2026-10-01T01:00:00.000Z' , '2026-10-01T01:00:00.000Z' );
const rows = old . prepare ( 'SELECT * FROM daily_people_counts ORDER BY date' ). all (), key = randomUUID (), payload = { date : D , count : 0 , revision : 3 }, oldResult = { date : D , count : 0 , revision : 4 };
old . exec ( 'CREATE TABLE operations(id TEXT PRIMARY KEY,hash TEXT NOT NULL,result TEXT NOT NULL)' );
old . prepare ( 'INSERT INTO operations VALUES (?,?,?)' ). run ( key , createHash ( 'sha256' ). update ( JSON . stringify ({ actor : owner . id , payload })). digest ( 'hex' ), JSON . stringify ( oldResult )); old . close ();
let ledger = new Ledger ( filename );
try {
assert . deepEqual ( ledger . rows ( 'SELECT date,count,revision,actor,created,updated FROM daily_people_counts ORDER BY date' ), rows );
assert . deepEqual ( ledger . peopleCount ( D ), entry ( D , 0 , 4 )); assert . deepEqual ( ledger . peopleCount ( D2 ), entry ( D2 , 17 , 2 ));
assert . deepEqual ( ledger . transact ( key , owner , payload ,()=> ledger . savePeopleCount ( payload , owner )), oldResult );
ledger . transact ( randomUUID (), owner ,{},()=> ledger . savePeopleCount ({ date : D , count : 0 , assistant1Count : 0 , assistant2Count : 8 , revision : 4 }, owner ));
assert . deepEqual ( ledger . transact ( key , owner , payload ,()=> ledger . savePeopleCount ( payload , owner )), oldResult );
} finally { ledger . close ();}
ledger = new Ledger ( filename );
try { assert . deepEqual ( ledger . peopleCount ( D ), entry ( D , 0 , 5 , 0 , 8 )); assert . deepEqual ( ledger . peopleCount ( D2 ), entry ( D2 , 17 , 2 )); assert . equal ( ledger . one ( 'PRAGMA integrity_check' ). integrity_check , 'ok' );} finally { ledger . close ();}
2026-10-17 19:50:24 +08:00
});
test ( 'HTTP人数须登录与CSRF,双方可改,相同版本并发只成功一笔,GET不补造未登记记录' , async t =>{
const dir = fs . mkdtempSync ( path . join ( os . tmpdir (), 'maimai-people-http-' )), app = createApp ({ mode : 'demo' , 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/people-count?date=' + D )). status , 401 ); assert . equal (( await call ( '/api/people-count' ,{ date : D , count : 0 , revision : 0 })). status , 401 );
let login = await call ( '/api/demo-login' ,{ role : 'assistant' }); cookie = login . headers . get ( 'set-cookie' ). split ( ';' )[ 0 ];
2026-10-23 00:21:53 +08:00
assert . deepEqual ( await ( await call ( '/api/people-count' )). json (), entry ( today (), null , 0 )); assert . equal ( app . ledger . rows ( 'SELECT * FROM daily_people_counts' ). length , 0 );
2026-10-17 19:50:24 +08:00
for ( const date of [ '2099-01-01' , '2026-02-30' , '' ]) assert . equal (( await call ( '/api/people-count?date=' + date )). status , 400 );
assert . equal (( await call ( '/api/people-count' ,{ date : D , count : 0 , revision : 0 }, randomUUID (), 'https://invalid.example' )). status , 403 );
assert . equal (( await call ( '/api/people-count' ,{ date : D , count : 1 })). status , 400 ); assert . equal (( await call ( '/api/people-count' ,{ date : D , count : 1.5 , revision : 0 })). status , 400 );
2026-10-23 00:21:53 +08:00
const key = randomUUID (), retries = await Promise . all ([ call ( '/api/people-count' ,{ date : D , count : 0 , revision : 0 }, key ), call ( '/api/people-count' ,{ date : D , count : 0 , revision : 0 }, key )]); assert . deepEqual ( retries . map ( r => r . status ),[ 200 , 200 ]); const first = await retries [ 0 ]. json (); assert . deepEqual ( await retries [ 1 ]. json (), first ); assert . deepEqual ( first , entry ( D , 0 , 1 ));
2026-10-17 19:50:24 +08:00
const writes = await Promise . all ([ call ( '/api/people-count' ,{ date : D , count : 5 , revision : 1 }), call ( '/api/people-count' ,{ date : D , count : 7 , revision : 1 })]); assert . deepEqual ( writes . map ( r => r . status ). sort (),[ 200 , 409 ]);
const saved = await writes . find ( r => r . status === 200 ). json (); assert . equal ( saved . revision , 2 ); assert . deepEqual ( await ( await call ( '/api/people-count?date=' + D )). json (), saved );
assert . equal ( app . ledger . rows ( 'SELECT * FROM daily_people_counts' ). length , 1 ); assert . equal ( app . ledger . rows ( "SELECT * FROM audit WHERE entity=?" , 'people-count:' + D ). length , 2 );
2026-10-23 00:21:53 +08:00
login = await call ( '/api/demo-login' ,{ role : 'owner' }); cookie = login . headers . get ( 'set-cookie' ). split ( ';' )[ 0 ]; const ownerWrite = await call ( '/api/people-count' ,{ date : D , count : 0 , revision : 2 }); assert . equal ( ownerWrite . status , 200 ); assert . deepEqual ( await ownerWrite . json (), entry ( D , 0 , 3 ));
2026-10-17 19:50:24 +08:00
assert . equal (( await call ( '/api/people-count' ,{ date : D , count : 20 , revision : 2 })). status , 409 );
2026-10-23 00:21:53 +08:00
const book = await ( await call ( '/api/order-book?date=' + D + '&q=none&status=after_sale' )). json (); assert . deepEqual ( book . peopleCount , aggregate ( D , D , 0 , 1 ));
2026-10-17 19:50:24 +08:00
const report = await ( await call ( '/api/report?date=' + D + '&basis=orderDate' )). json (); assert . deepEqual ( report . peopleCount , book . peopleCount ); assert . equal ( report . profit , 0 );
2026-10-23 00:21:53 +08:00
const independent = await call ( '/api/people-count' ,{ date : D , count : 0 , assistant1Count : 8 , assistant2Count : 0 , revision : 3 }); assert . deepEqual ( await independent . json (), entry ( D , 0 , 4 , 8 , 0 ));
const legacyWrite = await call ( '/api/people-count' ,{ date : D , count : 12 , revision : 4 }); assert . deepEqual ( await legacyWrite . json (), entry ( D , 12 , 5 , 8 , 0 ));
const cleared = await call ( '/api/people-count' ,{ date : D , count : 12 , assistant1Count : null , assistant2Count : '' , revision : 5 }); assert . deepEqual ( await cleared . json (), entry ( D , 12 , 6 ));
2026-10-17 19:50:24 +08:00
});