import fs from 'node:fs' import assert from 'node:assert/strict' // Offline generation only. Publishing remains an explicit, separately checked operation. const [sourcePath, outputPath] = process.argv.slice(2) if (!sourcePath || !outputPath) throw new Error('Usage: node build_completion_update.mjs ') const snapshot = JSON.parse(fs.readFileSync(sourcePath, 'utf8').replace(/^\uFEFF/, '')) const graph = typeof snapshot.flowData === 'string' ? JSON.parse(snapshot.flowData) : snapshot const helper = fs.readFileSync(new URL('./phase-planner.js', import.meta.url), 'utf8') const original = structuredClone(graph) const byId = new Map(graph.nodes.map(node => [node.id, node])) const modes = node => node.properties.nodeParams?.find(parameter => parameter.name === 'mode')?.input const planningNodes = graph.nodes.filter(node => modes(node)) assert.equal(planningNodes.length, 72, 'Unexpected phase planner layout; review before applying') for (const node of planningNodes) { const code = node.properties.nodeParams.find(parameter => parameter.name === 'code') assert(code?.input.includes('function confirmedMoves('), 'Unrecognized planning implementation') code.input = helper } const approaches = planningNodes.filter(node => ['rx_scan','j6_scan','offset_after_restore'].includes(modes(node))) assert.equal(approaches.length, 20) for (const planner of approaches) { const field = modes(planner) === 'offset_after_restore' ? 'retreatMoves' : 'hoverMoves' const loops = graph.nodes.filter(node => node.type === 'loop' && node.properties.nodeParams?.some(parameter => parameter.name === 'loopNum' && parameter.quote?.[0] === planner.id && parameter.quote?.[3] === field)) assert.equal(loops.length, 1, `Expected one approach loop for ${planner.properties.name}`) const moves = loops[0].children.map(id => byId.get(id)).filter(node => node.properties.action === 'ARM_MOVE_TO_POINT') assert.equal(moves.length, 1) const parameters = moves[0].properties.nodeParams assert.equal(parameters.find(parameter => parameter.name === 'userCoordinateName')?.input, '__builtin_base__') assert.equal(Number(parameters.find(parameter => parameter.name === 'blendRadius')?.input), 0) const existing = parameters.find(parameter => parameter.name === 'confirmShortMoveCompletion') if (existing) existing.input = true else parameters.push({ name: 'confirmShortMoveCompletion', type: 'input', input: true, required: false, disabled: true }) } assert.equal(graph.nodes.length, original.nodes.length) assert.deepEqual(graph.edges, original.edges) fs.writeFileSync(outputPath, JSON.stringify(graph, null, 2)) console.log(JSON.stringify({ phasePlanners: planningNodes.length, guardedApproaches: approaches.length, nodes: graph.nodes.length, deviceCommandsSent: 0 }))