47 lines
3.1 KiB
JavaScript
47 lines
3.1 KiB
JavaScript
'use strict';
|
|
const test=require('node:test'),assert=require('node:assert/strict');
|
|
const {Ledger}=require('../ledger'),{createApp,createUser}=require('../server');
|
|
const {accessWindow}=require('../business-hours');
|
|
const at=time=>Date.parse('2026-12-04T'+time+':00+08:00');
|
|
test('北京时间开门当分可用,关门当分停止,包括跨午夜营业',()=>{
|
|
const hours={open:'22:00',close:'02:00'};
|
|
assert.equal(accessWindow(hours,at('00:01')).closesAt,at('02:00'));
|
|
assert.equal(accessWindow(hours,at('02:00')).state,'closed');
|
|
assert.equal(accessWindow(hours,at('21:59')).state,'closed');
|
|
assert.equal(accessWindow(hours,at('22:00')).closesAt,at('02:00')+86400000);
|
|
});
|
|
test('全设备同一营业时段:留空只准店主设置,关门同时拦截登录、既有会话、打印和写入',async t=>{
|
|
const ledger=new Ledger(':memory:'),password='synthetic-hours-only';
|
|
for(const member of ['owner','assistant'])createUser(ledger,member,member,password,member);
|
|
let now=at('09:00');
|
|
const app=createApp({mode:'trial',ledger,hoursPolicy:true,hoursClock:()=>now,feishuConfig:{}});
|
|
await new Promise(resolve=>app.server.listen(0,'127.0.0.1',resolve));
|
|
t.after(async()=>{await new Promise(resolve=>app.server.close(resolve));ledger.close();});
|
|
const base='http://127.0.0.1:'+app.server.address().port;
|
|
const call=(route,cookie,data)=>fetch(base+route,{method:data?'POST':'GET',headers:{...(cookie&&{Cookie:cookie}),...(data&&{Origin:base,'Content-Type':'application/json','X-Requested-With':'maimai-ledger'})},body:data&&JSON.stringify(data)});
|
|
const sign=member=>call('/api/login',null,{member,password});
|
|
const token=res=>res.headers.get('set-cookie').split(';')[0];
|
|
assert.doesNotMatch(await (await call('/')).text(),/<option value="assistant">/);
|
|
assert.equal((await sign('assistant')).status,403);
|
|
const owner=token(await sign('owner'));
|
|
assert.match(await (await call('/',owner)).text(),/action="\/api\/business-hours\/setup"/);
|
|
assert.equal((await call('/api/state',owner)).status,403);
|
|
const setting={revision:0,businessHours:{open:'09:00',close:'18:00'}};
|
|
assert.notEqual((await call('/api/business-hours/setup',null,setting)).status,200);
|
|
assert.equal((await call('/api/business-hours/setup',owner,setting)).status,200);
|
|
assert.equal((await call('/api/business-hours/setup',owner,setting)).status,409);
|
|
const staff=token(await sign('assistant'));
|
|
for(const cookie of [staff,owner]){
|
|
assert.equal((await call('/api/state',cookie)).status,200);
|
|
assert.match(await (await call('/',cookie)).text(),/business-hours-lock\.js/);
|
|
assert.equal((await (await call('/api/access-window',cookie)).json()).window.closesAt,at('18:00'));
|
|
}
|
|
now=at('18:00');
|
|
for(const cookie of [staff,owner]){
|
|
for(const route of ['/api/session','/api/state','/print/example','/api/access-window'])assert.equal((await call(route,cookie)).status,403);
|
|
assert.equal((await call('/api/shop-settings',cookie,{revision:1,businessHours:null})).status,403);
|
|
}
|
|
assert.equal((await sign('owner')).status,403);
|
|
const closed=await call('/');assert.equal(closed.status,403);assert.match(await closed.text(),/非营业时间/);
|
|
});
|