import assert from"node:assert/strict";import crypto from"node:crypto";import fs from"node:fs";import os from"node:os";import path from"node:path";import{execFileSync}from"node:child_process";import{Worker}from"node:worker_threads";import{fileURLToPath,pathToFileURL}from"node:url";const root=path.resolve(path.dirname(fileURLToPath(import.meta.url)),".."),build=path.resolve(process.argv[2]??path.join(root,"freecad-source/build-web/configure/bin")),out=fs.mkdtempSync(path.join(os.tmpdir(),"robot-python-facade-"));execFileSync(path.join(root,"web/node_modules/.bin/tsc"),["--target","ES2022","--module","ESNext","--moduleResolution","Bundler","--strict","--skipLibCheck","--outDir",out,"--declaration","false","--noEmit","false","web/src/facade-envelope.ts","web/src/robot-python-facade.ts"],{cwd:root,stdio:"inherit"});const f=await import(pathToFileURL(path.join(out,"robot-python-facade.js"))),e=await import(pathToFileURL(path.join(out,"facade-envelope.js"))),w=new Worker(path.join(root,"tools/robot-python-facade-smoke-worker.mjs"),{workerData:{buildDirectory:build,facadePath:pathToFileURL(path.join(out,"robot-python-facade.js")).href,envelopePath:pathToFileURL(path.join(out,"facade-envelope.js")).href}}),recv=()=>new Promise((r,j)=>{const h=v=>{w.off("error",j);r(v)};w.once("message",h);w.once("error",j)}),ready=await recv(),sr=f.createRobotPythonRequest("robot-python-success");w.postMessage(sr);const success=await recv();w.postMessage(f.createRobotPythonRequest("robot-python-error",{...f.ROBOT_PYTHON_FIXTURE,apiNames:["Missing"]}));const expected=await recv();w.postMessage(sr);const dup=await recv();const cancel=f.createRobotPythonRequest("robot-python-cancel"),cp=new Promise((r,j)=>{const h=v=>{if(v.requestId===cancel.requestId){w.off("error",j);r(v)}};w.on("message",h);w.once("error",j)});w.postMessage(cancel);w.postMessage(e.createCancel("robot-python-cancel-control",cancel.requestId,"robot-python"));const cancelled=await cp;w.postMessage(f.createRobotPythonRequest("robot-python-after"));const after=await recv();await w.terminate();assert.equal(ready.kind,"ready");assert.equal(success.kind,"result");assert.equal(success.result.apiCount,3);assert.equal(expected.error.code,"invalid_fixture");assert.equal(dup.error.code,"duplicate_request");assert.equal(cancelled.error.code,"cancelled");assert.equal(after.kind,"result");const trace={ready,success,expected,dup,cancelled,after},report={schema:"freecad-web-p6-robot-action-03-facade/v1",taskId:"FW-P6-ROB-ACT-03-E",module:"Robot",action:f.ROBOT_PYTHON_COMMAND,result:"pass",trace,traceSha256:crypto.createHash("sha256").update(JSON.stringify(trace)).digest("hex"),sequencing:{requestCount:6,responseCount:6,maxActive:1,cancelAccepted:true}};fs.writeFileSync(path.join(root,"docs/p6/robot-action-03-facade.json"),JSON.stringify(report,null,2)+"\n");fs.writeFileSync(path.join(root,"docs/freecad-p6-robot-action-03-e-facade.log"),JSON.stringify({result:"pass",requests:6,cancel:cancelled.error.code})+"\n");console.log(JSON.stringify({result:"pass",requests:6,cancel:cancelled.error.code}));