45 lines
3.8 KiB
JavaScript
45 lines
3.8 KiB
JavaScript
const test=require('node:test'),assert=require('node:assert/strict'),vm=require('node:vm'),fs=require('node:fs');
|
||
const source=fs.readFileSync(__dirname+'/../public/static/studio.js','utf8');
|
||
class Node {
|
||
constructor(tag='div'){this.tagName=tag;this.children=[];this.attributes={};this.value='';this.classList={add(){},remove(){}};this.style={};}
|
||
append(...nodes){for(const n of nodes){n.parent=this;this.children.push(n)}}
|
||
appendChild(n){this.append(n)} replaceChildren(...ns){this.children=[];this.append(...ns)}
|
||
setAttribute(k,v){this.attributes[k]=v} focus(){} remove(){if(this.parent)this.parent.children=this.parent.children.filter(x=>x!==this)}
|
||
get lastChild(){return this.children.at(-1)}
|
||
}
|
||
function harness(){
|
||
const nodes={},sent=[],chat=new Node(),input=new Node();let response={ok:false,can_resume:true,part_name:'saved',message:'未完成'};let pollFailures=0;
|
||
const context={console,URL,setTimeout:f=>{f();return 0},Promise,busy:false,loadingName:null,pendingQuestion:null,pendingTarget:null,active:null,attachments:[],input,chat,viewer:null,
|
||
$:id=>nodes[id]??=new Node(),document:{createElement:t=>new Node(t),querySelector:()=>new Node()},
|
||
toast(){},setBusy:v=>context.busy=v,renderAttachments(){},applyPart(){},loadCatalog:async()=>{},reportMessage(){},
|
||
message:text=>{const n=new Node();n.textContent=text;chat.append(n);return n},
|
||
fetch:async(url,options={})=>{if(options.method==='POST'){sent.push(JSON.parse(options.body));return{ok:true,json:async()=>({ok:true,task_id:'task'})}};
|
||
if(pollFailures-->0)throw Error('offline');return{ok:true,json:async()=>({done:true,response})};}
|
||
};
|
||
vm.createContext(context);
|
||
for(const [start,end] of [[' function recoveryActions',' function demoQuestions'],[' async function run',' send.onclick']])vm.runInContext(source.slice(source.indexOf(start),source.indexOf(end)),context);
|
||
return{c:context,nodes,sent,chat,input,setResponse:r=>response=r,setPollFailures:n=>pollFailures=n};
|
||
}
|
||
function find(root,text){if(root.textContent===text)return root;for(const n of root.children){const r=find(n,text);if(r)return r}}
|
||
const data={part_name:'saved',question_items:[{question:'装配方式?',reason:'原始要求冲突',options:['卡扣','螺钉']},{question:'宽度按哪处?',reason:'两处不同',options:['文字','图纸']}]};
|
||
async function flush(){for(let i=0;i<40;i++)await Promise.resolve()}
|
||
test('step choices, close, composer answer continue original job; free text overrides choice',async()=>{
|
||
const h=harness();h.c.showQuestions(data);let dock=h.nodes.decisionDock;
|
||
assert.equal(find(dock,'下一步').disabled,true);assert.equal(h.sent.length,0);
|
||
find(dock,'卡扣').parent.onclick();find(dock,'下一步').onclick();
|
||
find(dock,'图纸').parent.onclick();
|
||
find(dock,'×').onclick();h.input.value='按文字宽度,保留图纸孔位';h.c.submit();await flush();
|
||
assert.equal(h.sent.length,1);assert.equal(h.sent[0].action,'resume');assert.equal(h.sent[0].part_name,'saved');
|
||
assert.match(h.sent[0].clarification,/卡扣/);assert.match(h.sent[0].clarification,/按文字宽度/);
|
||
assert.doesNotMatch(h.sent[0].clarification,/用户选择或补充:图纸/);
|
||
});
|
||
test('partial result accepts supplemental requirements on same saved job',async()=>{
|
||
const h=harness();h.c.recoveryActions({part_name:'partial'},new Node());h.input.value='保持孔位,简化外侧装饰';h.c.submit();await flush();
|
||
assert.equal(h.sent[0].action,'resume');assert.equal(h.sent[0].part_name,'partial');assert.equal(h.sent[0].clarification,'保持孔位,简化外侧装饰');
|
||
});
|
||
test('progress disconnection reconnects the same GET task, never duplicates generation POST',async()=>{
|
||
const h=harness();h.setPollFailures(8);await h.c.run({action:'generate',text:'box'});
|
||
assert.equal(h.sent.length,1);const reconnect=find(h.chat,'重新连接当前任务');assert.ok(reconnect);
|
||
reconnect.onclick();await flush();assert.equal(h.sent.length,1);
|
||
});
|