Files
order-notification-automation/tests/app/application.test.cjs
T

47 lines
5.7 KiB
JavaScript

'use strict';
const {test}=require('node:test');const assert=require('node:assert/strict');const fs=require('node:fs/promises');const os=require('node:os');const path=require('node:path');const net=require('node:net');
const {main}=require('../../src/server.cjs');const {NotificationStore}=require('../../src/modules/notifications/store.cjs');
const {NotificationService}=require('../../src/modules/notifications/service.cjs');
async function freePort(){const s=net.createServer();await new Promise(r=>s.listen(0,'127.0.0.1',r));const p=s.address().port;await new Promise(r=>s.close(r));return p;}
async function fixture(t){const dir=await fs.mkdtemp(path.join(os.tmpdir(),'order-application-'));return {stateDir:dir,quiet:true,config:{port:await freePort(),outputDir:path.join(dir,'output'),dashboard:{port:await freePort(),host:'127.0.0.1'}}};}
test('formal application never starts the retired generic notification scheduler',async t=>{
const options=await fixture(t),start=NotificationService.prototype.start;let starts=0;NotificationService.prototype.start=function(){starts++;};let app;
try{app=await main(options);assert.equal(starts,0);}finally{NotificationService.prototype.start=start;await app?.stop();await fs.rm(options.stateDir,{recursive:true,force:true});}
});
test('second main rejects before recovering or mutating current frozen business evidence',async t=>{
const options=await fixture(t),app=await main(options);t.after(async()=>{await app.stop();await fs.rm(options.stateDir,{recursive:true,force:true});});
const batch={id:'owned-frozen',status:'prepared',cutoff:new Date(Date.now()+300000).toISOString(),orderIds:[],text:'synthetic frozen evidence'};app.orderRules.put('batches',batch);
let recoveries=0;const original=NotificationStore.prototype.recover;NotificationStore.prototype.recover=function(){recoveries++;return original.call(this);};
try{await assert.rejects(main(options),e=>e.code==='ORDER_INSTANCE_OWNED');assert.equal(recoveries,0);assert.deepEqual(app.orderRules.get('batches',batch.id),batch);assert.equal(app.notifications.historyOnly,true);assert.equal(app.notifications.tick,undefined);}finally{NotificationStore.prototype.recover=original;}
});
test('validation failure and occupied dashboard port both release state ownership',async t=>{
const options=await fixture(t);t.after(()=>fs.rm(options.stateDir,{recursive:true,force:true}));await assert.rejects(main({...options,config:{...options.config,dashboard:{port:1}}}),/端口/);
const occupied=net.createServer();await new Promise(r=>occupied.listen(options.config.dashboard.port,'127.0.0.1',r));
try{await assert.rejects(main(options),e=>e.code==='EADDRINUSE');}finally{await new Promise(r=>occupied.close(r));}
const app=await main(options);await app.stop();const again=await main(options);await again.stop();
});
test('partial construction failure closes earlier databases and releases ownership',async t=>{
const options=await fixture(t);t.after(()=>fs.rm(options.stateDir,{recursive:true,force:true}));await fs.writeFile(path.join(options.stateDir,'mail'),'blocks directory initialization');
await assert.rejects(main(options));await fs.rm(path.join(options.stateDir,'mail'));
// Windows refuses this rename if the workspace database remains open.
const db=path.join(options.stateDir,'workspace','workspace.sqlite');const moved=db+'.closed';await fs.rename(db,moved);await fs.rename(moved,db);
const app=await main(options);await app.stop();
});
test('shutdown attempts every resource and releases ownership despite teardown errors',async t=>{
const options=await fixture(t);t.after(()=>fs.rm(options.stateDir,{recursive:true,force:true}));const app=await main(options);
const close=app.health.close.bind(app.health);app.health.close=()=>{close();throw new Error('health teardown fixture');};const stop=app.assistant.stop.bind(app.assistant);app.assistant.stop=async()=>{await stop();throw new Error('assistant teardown fixture');};
await assert.rejects(app.stop(),e=>e instanceof AggregateError&&e.errors.length===2);
assert.equal(app.workspace.db,null);assert.equal(app.mail.store.db,null);assert.equal(app.notifications.store.db,null);assert.equal(app.server.listening,false);assert.equal(app.dashboard.listening,false);
const next=await main(options);await next.stop();
});
test('dashboard asset initialization failure closes the already-created session store',async t=>{
const dir=await fs.mkdtemp(path.join(os.tmpdir(),'order-assets-fail-'));t.after(()=>fs.rm(dir,{recursive:true,force:true}));
const {Store}=require('../../src/modules/dashboard/store.cjs');const {SessionStore}=require('../../src/modules/dashboard/sessions.cjs');const {createDashboard}=require('../../src/app/dashboard.cjs');const Module=require('node:module');
const originalResolve=Module._resolveFilename,originalClose=SessionStore.prototype.close;let closed=0;
Module._resolveFilename=function(id,...rest){if(id==='@tabler/core/dist/css/tabler.min.css')throw new Error('fixture missing asset');return originalResolve.call(this,id,...rest);};SessionStore.prototype.close=function(){closed++;return originalClose.call(this);};
try{assert.throws(()=>createDashboard({store:new Store(path.join(dir,'dashboard')),outputDir:dir}),/fixture missing asset/);assert.equal(closed,1);}finally{Module._resolveFilename=originalResolve;SessionStore.prototype.close=originalClose;}
});
test('application wires test expiry to every manual mail submission path',async t=>{const options=await fixture(t),app=await main(options);t.after(async()=>{await app.stop();await fs.rm(options.stateDir,{recursive:true,force:true});});app.orderRules.put('stability-test',{id:'main',enabled:true,startedAt:'2026-01-01T00:00:00Z',endsAt:'2026-01-02T00:00:00Z'});assert.throws(()=>app.mail.beforeSend(),e=>e.code==='STABILITY_TEST_EXPIRED');});