refactor: organize source web and tests by business module
This commit is contained in:
1 parent
9854d2d5c2
commit
2aed328774
113 files changed
+189
-183
No files matched your search
@@ -0,0 +1,9 @@
|
||||
'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 {Store}=require('../../src/modules/dashboard/store.cjs');const {NotificationStore,NotificationService}=require('../../src/modules/notifications/notifications.cjs');const {createDashboard}=require('../../src/app/dashboard.cjs');
|
||||
test('notification HTTP exposes configurable mapping and preview without sending, with role and origin checks',async t=>{
|
||||
const dir=await fs.mkdtemp(path.join(os.tmpdir(),'notice-http-')),store=new Store(path.join(dir,'users'));store.bootstrap('admin','Test-password-123');for(const role of ['operator','viewer'])store.addUser({username:role,password:'Test-password-123',role},'admin');const notificationStore=new NotificationStore(path.join(dir,'notifications')),notifications=new NotificationService({store:notificationStore,mail:{},settings:()=>store.settings()}),server=createDashboard({store,notifications,outputDir:dir});await new Promise(r=>server.listen(0,'127.0.0.1',r));t.after(async()=>{await notifications.stop();server.closeAllConnections();await new Promise(r=>server.close(r));notificationStore.close();await fs.rm(dir,{recursive:true,force:true});});const base='http://127.0.0.1:'+server.address().port,call=(route,body,cookie,origin=base)=>fetch(base+route,{method:body===undefined?'GET':'POST',headers:{...(body===undefined?{}:{'Content-Type':'application/json',Origin:origin}),...(cookie?{Cookie:cookie}:{})},body:body===undefined?undefined:JSON.stringify(body)}),login=async username=>(await call('/api/login',{username,password:'Test-password-123'})).headers.get('set-cookie').split(';')[0];
|
||||
assert.equal((await call('/api/notifications')).status,401);const admin=await login('admin'),operator=await login('operator'),viewer=await login('viewer');const config=notificationStore.settings();assert.equal((await call('/api/notifications/settings',config,operator)).status,403);assert.equal((await call('/api/notifications/settings',config,admin,'https://evil.test')).status,403);assert.equal((await call('/api/notifications/settings',config,admin)).status,200);
|
||||
const data={id:'external-1',industry:'待配置产业',brief:'简要说明原文\n保持换行'};const preview=await(await call('/api/notifications/preview',{data},operator)).json();assert.equal(preview.event.brief,data.brief);assert.equal(notificationStore.items().length,0);
|
||||
assert.equal((await call('/api/notifications/events',{data},viewer)).status,403);assert.equal((await call('/api/notifications/events',{data},operator)).status,201);assert.equal(notificationStore.items().length,1);
|
||||
assert.equal((await call('/api/notifications',undefined,viewer)).status,200);
|
||||
});
|
||||
@@ -0,0 +1,5 @@
|
||||
'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 {Store}=require('../../src/modules/dashboard/store.cjs');const {NotificationStore,NotificationService}=require('../../src/modules/notifications/notifications.cjs');const {createDashboard}=require('../../src/app/dashboard.cjs');const {loadPlaywright,browserOptions}=require('../../src/platform/browser.cjs');
|
||||
test('notification page saves field maps and previews original text without queuing or sending',{timeout:90000},async t=>{
|
||||
const dir=await fs.mkdtemp(path.join(os.tmpdir(),'notice-ui-')),store=new Store(path.join(dir,'users'));store.bootstrap('admin','Test-password-123');const notificationStore=new NotificationStore(path.join(dir,'notifications')),notifications=new NotificationService({store:notificationStore,mail:{},settings:()=>store.settings()}),server=createDashboard({store,notifications,outputDir:dir});await new Promise(r=>server.listen(0,'127.0.0.1',r));let browser;t.after(async()=>{await browser?.close();await notifications.stop();server.closeAllConnections();await new Promise(r=>server.close(r));notificationStore.close();await fs.rm(dir,{recursive:true,force:true});});browser=await loadPlaywright().chromium.launch(browserOptions());const page=await browser.newPage({viewport:{width:1440,height:1000}});page.setDefaultTimeout(6000);const errors=[];page.on('pageerror',e=>errors.push(e.message));await page.goto('http://127.0.0.1:'+server.address().port+'/#notifications');await page.getByLabel('账号',{exact:true}).fill('admin');await page.getByLabel('密码',{exact:true}).fill('Test-password-123');await page.getByRole('button',{name:'登录',exact:true}).click();await page.getByLabel('来源名称',{exact:true}).fill('团队业务');await page.getByRole('button',{name:'保存通知配置',exact:true}).click();await page.getByText('业务通知配置已保存。',{exact:true}).waitFor();assert.equal(notificationStore.settings().sourceName,'团队业务');await page.getByLabel('来源 JSON 样本',{exact:true}).fill(JSON.stringify({id:'SAMPLE-001',industry:'同事后续配置',brief:'原始简要说明\n第二行'}));await page.getByRole('button',{name:'预览字段映射',exact:true}).click();await page.locator('#notification-preview pre').getByText('原始简要说明\n第二行',{exact:true}).waitFor();assert.equal(notificationStore.items().length,0);assert.equal(await page.locator('#notification-preview pre').evaluate(el=>{const node=el.firstChild,index=node.textContent.indexOf('\n'),range=document.createRange();range.setStart(node,0);range.setEnd(node,1);const first=range.getBoundingClientRect().top;range.setStart(node,index+1);range.setEnd(node,index+2);return range.getBoundingClientRect().top>first;}),true);await fs.mkdir(path.resolve('output/verification'),{recursive:true});await page.evaluate(()=>window.scrollTo(0,0));await page.screenshot({path:path.resolve('output/verification/notifications-desktop.png'),fullPage:true});await page.setViewportSize({width:390,height:844});assert.equal(await page.evaluate(()=>document.documentElement.scrollWidth<=innerWidth),true);assert.deepEqual(errors,[]);
|
||||
});
|
||||
@@ -0,0 +1,52 @@
|
||||
'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 {NotificationStore,NotificationService}=require('../../src/modules/notifications/notifications.cjs');const {MailStore}=require('../../src/modules/mail/store.cjs');const {MailService}=require('../../src/modules/mail/service.cjs');
|
||||
const fields={id:'flow.id',version:'flow.version',industry:'flow.industry',brief:'flow.brief',subject:'flow.subject',occurredAt:'flow.at'};
|
||||
const event={flow:{id:'FLOW-001',version:'v1',industry:'宽带',brief:'第一行原文\n第二行 <原文> & 保留',subject:'业务提醒',at:'2026-10-17T08:00:00+08:00'}};
|
||||
async function fixture(t,{uncertain=false}={}){const dir=await fs.mkdtemp(path.join(os.tmpdir(),'order-notice-'));const store=new NotificationStore(path.join(dir,'notifications'));const mailStore=new MailStore(path.join(dir,'mail'));const channel=mailStore.saveChannel({name:'工作邮箱',email:'notice@company.test',smtp:{host:'smtp.company.test',port:465,security:'tls',user:'notice',password:'Fixture-only-password'},imap:{host:'imap.company.test',port:993,security:'tls',user:'notice',password:'Fixture-only-password'},senders:['author@company.test'],enabled:false});mailStore.setState(channel.id,1,{smtp:true});const sent=[];const mail=new MailService({store:mailStore,smtpFactory:()=>({verify:async()=>{},sendMail:async payload=>{sent.push(payload);if(uncertain)throw new Error('socket disconnected');return {accepted:['interface@company.test'],rejected:[]};},close(){}})});const service=new NotificationService({store,mail,settings:()=>({schedule:{times:['09:00','17:00'],timezone:'Asia/Shanghai',weekends:true,mode:'product_line',missed:'next_window'},groups:[{productLine:'宽带',to:['interface@company.test'],cc:[]}]}),calendar:()=>[],clock:()=>new Date('2026-10-17T08:59:00+08:00')});service.saveSettings({version:1,enabled:false,sourceName:'业务流程',channelId:channel.id,fieldMap:fields,maxAttempts:3});t.after(async()=>{await service.stop();await mail.stop();store.close();mailStore.close();await fs.rm(dir,{recursive:true,force:true});});return {dir,store,service,sent,channel};}
|
||||
|
||||
test('mapped notifications copy brief text, deduplicate versions and send no attachments at a configured slot',async t=>{
|
||||
const {store,service,sent}=await fixture(t);const preview=service.preview(event);assert.equal(preview.event.brief,event.flow.brief);assert.equal(store.items().length,0);
|
||||
const item=service.ingest(event);assert.equal(service.ingest(event).id,item.id);assert.equal(store.items().length,1);
|
||||
assert.throws(()=>service.ingest({flow:{...event.flow,brief:'changed without version'}}),e=>e.status===409);
|
||||
service.saveSettings({...store.settings(),sourceName:'来源改名'});assert.equal(service.ingest(event).id,item.id);
|
||||
await service.tick(new Date('2026-10-17T09:00:01+08:00'));assert.equal(sent.length,0);
|
||||
const settings=store.settings();service.saveSettings({...settings,enabled:true});await service.tick(new Date('2026-10-17T09:00:01+08:00'));assert.equal(sent.length,1);
|
||||
assert.equal(sent[0].text,'第一行原文\n第二行 <原文> & 保留');assert.deepEqual(sent[0].attachments,[]);assert.deepEqual(sent[0].to,['interface@company.test']);assert.equal(store.items()[0].status,'sent');
|
||||
await service.tick(new Date('2026-10-17T09:00:20+08:00'));assert.equal(sent.length,1);
|
||||
});
|
||||
|
||||
test('unmapped industries cannot starve a later valid notification behind the queue read limit',async t=>{
|
||||
const {store,service,sent}=await fixture(t);for(let i=0;i<201;i++)service.ingest({flow:{...event.flow,id:'unmapped-'+i,industry:'尚未配置'}});
|
||||
service.ingest(event);service.saveSettings({...store.settings(),enabled:true});await service.tick(new Date('2026-10-17T09:00:00+08:00'));
|
||||
assert.equal(sent.length,1);assert.equal(sent[0].text,event.flow.brief);
|
||||
});
|
||||
|
||||
test('uncertain delivery remains blocked across restart and requires recorded human evidence to retry',async t=>{
|
||||
const {store,service,sent}=await fixture(t,{uncertain:true});service.ingest(event);service.saveSettings({...store.settings(),enabled:true});await service.tick(new Date('2026-10-17T09:00:00+08:00'));assert.equal(store.items()[0].status,'unknown');assert.equal(store.batches()[0].status,'unknown');
|
||||
await service.tick(new Date('2026-10-17T17:00:00+08:00'));assert.equal(sent.length,1);
|
||||
const unknownId=store.batches()[0].id;for(let index=0;index<101;index++){const item=service.ingest({flow:{...event.flow,id:'newer-'+index}}),batch=store.prepare([item],{industry:'宽带',to:['interface@company.test'],cc:[],text:'newer',subject:'newer'});store.mark(batch,'sent');}
|
||||
assert.equal(store.batches().some(batch=>batch.id===unknownId),true);
|
||||
assert.throws(()=>service.resolve(unknownId,{resolution:'retry',note:''},'admin'),e=>e.status===400);
|
||||
service.resolve(unknownId,{resolution:'sent',note:'已在 SMTP 服务端核实发送成功'},'admin');assert.equal(store.items()[0].status,'sent');
|
||||
});
|
||||
|
||||
test('an IMAP operation occupying the due slot does not consume the notification window',async t=>{
|
||||
const {store,service,sent}=await fixture(t);service.ingest(event);service.saveSettings({...store.settings(),enabled:true});
|
||||
let release;const occupied=service.mail.exclusive(store.settings().channelId,()=>new Promise(resolve=>{release=resolve;}));await Promise.resolve();
|
||||
await service.tick(new Date('2026-10-17T09:00:00+08:00'));assert.equal(sent.length,0);release();await occupied;
|
||||
await service.tick(new Date('2026-10-17T09:00:30+08:00'));assert.equal(sent.length,1);
|
||||
});
|
||||
|
||||
test('incomplete and unsafe field maps cannot enable delivery and unmapped industries stay visible',async t=>{
|
||||
const {store,service}=await fixture(t);assert.equal(service.preview({flow:{...event.flow,industry:'未配置产业'}}).valid,false);
|
||||
assert.throws(()=>service.ingest({flow:{...event.flow,brief:''}}),e=>e.status===400);
|
||||
assert.throws(()=>service.saveSettings({...store.settings(),fieldMap:{...fields,brief:'__proto__.secret'}}),e=>e.status===400);
|
||||
service.ingest({flow:{...event.flow,industry:'未配置产业'}});service.saveSettings({...store.settings(),enabled:true});await service.tick(new Date('2026-10-17T09:00:00+08:00'));assert.equal(store.items()[0].status,'blocked');
|
||||
});
|
||||
|
||||
test('field mapping supports Chinese object keys and rejects control characters in identity fields',async t=>{
|
||||
const {store,service}=await fixture(t);service.saveSettings({...store.settings(),fieldMap:{id:'来源.编号',version:'',industry:'来源.产业',brief:'来源.简要说明',subject:'',occurredAt:''}});
|
||||
const sample={来源:{编号:'FLOW-CN',产业:'宽带',简要说明:'中文字段原文'}};assert.equal(service.preview(sample).event.brief,'中文字段原文');
|
||||
assert.throws(()=>service.ingest({来源:{...sample.来源,编号:'bad\u0000identity'}}),e=>e.status===400);
|
||||
});
|
||||
Reference in new issue
Block a user