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

48 lines
2.7 KiB
JavaScript

const {test}=require('node:test');
const assert=require('node:assert/strict');
const fs=require('node:fs');
const os=require('node:os');
const path=require('node:path');
let startPreview;
try { ({startPreview}=require('../../scripts/preview.cjs')); } catch(error) { if(error.code!=='MODULE_NOT_FOUND')throw error; }
test('isolated preview signs in locally with fictional workspace data and survives restart',async t=>{
assert.equal(typeof startPreview,'function','isolated preview runtime not implemented');
const dir=fs.mkdtempSync(path.join(os.tmpdir(),'framework-preview-'));
let runtime;
t.after(async()=>{if(runtime)await runtime.close();fs.rmSync(dir,{recursive:true,force:true});});
runtime=await startPreview({stateDir:dir,port:0});
assert.equal(runtime.server.address().address,'127.0.0.1');
const base='http://127.0.0.1:'+runtime.server.address().port;
const status=await (await fetch(base+'/api/bootstrap/status')).json();
assert.equal(status.preview,true);
const login=await fetch(base+'/api/preview-login',{method:'POST',headers:{Origin:base,'Content-Type':'application/json'},body:'{}'});
assert.equal(login.status,200);
const cookie=login.headers.get('set-cookie').split(';')[0];
const response=await fetch(base+'/api/workspace',{headers:{Cookie:cookie}});
assert.equal(response.status,200);
const data=await response.json();
assert.ok(data.exceptions.length>=2);
assert.ok(data.exceptions.every(item=>item.title.includes('演示')));
assert.equal(data.capabilities.automaticProcessing,false);
assert.equal(data.capabilities.remoteLogin,false);
assert.equal(data.connections[0].status,'disconnected');
assert.ok(!JSON.stringify(data).includes('passwordHash'));
const ids=data.exceptions.map(item=>item.id);
await runtime.close();runtime=null;
runtime=await startPreview({stateDir:dir,port:0});
assert.deepEqual(runtime.workspace.snapshot(runtime.store.users()).exceptions.map(item=>item.id),ids);
});
test('rule preview can configure IMAP history without enabling polling or SMTP',async t=>{
const dir=fs.mkdtempSync(path.join(os.tmpdir(),'history-preview-'));
const runtime=await startPreview({stateDir:dir,port:0,mailHistory:true});
t.after(async()=>{await runtime.close();fs.rmSync(dir,{recursive:true,force:true});});
const base='http://127.0.0.1:'+runtime.server.address().port;
const login=await fetch(base+'/api/preview-login',{method:'POST',headers:{Origin:base,'Content-Type':'application/json'},body:'{}'});
const cookie=login.headers.get('set-cookie').split(';')[0];
const result=await fetch(base+'/api/mail',{headers:{Cookie:cookie}});
assert.equal(result.status,200);assert.equal((await result.json()).historyOnly,true);
assert.equal(runtime.mail.timer,undefined);assert.equal(runtime.mail.startTimer,undefined);
});