import test from 'node:test' import assert from 'node:assert/strict' import fs from 'node:fs' import ts from 'typescript' test('progress accepts this client, rejects other prompts and closes on completion',()=>{ const source=fs.readFileSync(new URL('../server/utils/studio2/progress.ts',import.meta.url),'utf8') const tree=ts.createSourceFile('progress.ts',source,ts.ScriptTarget.Latest,true) const fn=tree.statements.find(n=>ts.isFunctionDeclaration(n)&&n.name.text==='watchProgress') const code=ts.transpileModule(fn.getText(tree).replace(/^export /,''),{compilerOptions:{target:ts.ScriptTarget.ES2022}}).outputText let socket,saves=0 class Socket { listeners={};constructor(){socket=this}addEventListener(name,fn){this.listeners[name]=fn}close(){this.closed=true} } const watch=new Function('WebSocket','comfyWsUrl','saveRecord',`${code};return watchProgress`)(Socket,()=>'/fake',()=>saves++) const record={promptId:'mine'},stop=watch(record,{clientId:'client'}) const send=data=>socket.listeners.message({data:JSON.stringify({type:'progress',data})}) send({prompt_id:'another',value:1,max:8});assert.equal(saves,0) send({prompt_id:'mine',value:3,max:8});assert.equal(record.progress.step,3);assert.equal(record.progress.maxStep,8) send({value:4,max:8});assert.equal(record.progress.percent,50) stop();assert.equal(socket.closed,true) })