128 lines
4.1 KiB
JavaScript
128 lines
4.1 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import os from 'node:os';
|
|
import { autoWireWorkspace, CanvasAutoWiringMonitor } from '../src/gateway/canvas-auto-wiring.js';
|
|
|
|
test('autoWireWorkspace deve conectar terminais soltos ao nó concentrador e persistir atomicamente', () => {
|
|
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'maestri-autowire-'));
|
|
const workspacePath = path.join(tmpDir, 'workspace.json');
|
|
|
|
const initialWorkspace = {
|
|
id: 'ws-100',
|
|
title: 'Workspace Teste',
|
|
payload: {
|
|
nodes: [
|
|
{
|
|
id: 'node-concentrador',
|
|
type: 'terminal',
|
|
title: 'maestri-remote',
|
|
content: { terminal: { _0: { name: 'maestri-remote' } } }
|
|
},
|
|
{
|
|
id: 'node-terminal-1',
|
|
type: 'terminal',
|
|
title: 'Engenheiro #1',
|
|
content: { terminal: { _0: { name: 'engenheiro1' } } }
|
|
},
|
|
{
|
|
id: 'node-terminal-2',
|
|
type: 'terminal',
|
|
title: 'Engenheiro #2',
|
|
content: { terminal: { _0: { name: 'engenheiro2' } } }
|
|
}
|
|
],
|
|
connections: [
|
|
// terminal-1 já conectado
|
|
{ from: 'node-concentrador', to: 'node-terminal-1' }
|
|
]
|
|
}
|
|
};
|
|
|
|
fs.writeFileSync(workspacePath, JSON.stringify(initialWorkspace, null, 2), 'utf8');
|
|
|
|
// Executa autoWireWorkspace
|
|
const result = autoWireWorkspace(workspacePath, {
|
|
concentratorName: 'maestri-remote'
|
|
});
|
|
|
|
assert.equal(result.modified, true);
|
|
assert.equal(result.connectedCount, 1);
|
|
assert.equal(result.newConnections.length, 1);
|
|
assert.equal(result.newConnections[0].from, 'node-concentrador');
|
|
assert.equal(result.newConnections[0].to, 'node-terminal-2');
|
|
|
|
// Relê o arquivo e valida integridade
|
|
const updated = JSON.parse(fs.readFileSync(workspacePath, 'utf8'));
|
|
assert.equal(updated.payload.connections.length, 2);
|
|
const conn2 = updated.payload.connections.find(c => c.to === 'node-terminal-2');
|
|
assert.ok(conn2, 'Deve ter criado conexão com node-terminal-2');
|
|
assert.equal(conn2.from, 'node-concentrador');
|
|
|
|
// Segunda execução imediata não deve duplicar nem modificar o arquivo
|
|
const secondResult = autoWireWorkspace(workspacePath, {
|
|
concentratorName: 'maestri-remote'
|
|
});
|
|
assert.equal(secondResult.modified, false);
|
|
assert.equal(secondResult.connectedCount, 0);
|
|
|
|
// Limpeza
|
|
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
});
|
|
|
|
test('CanvasAutoWiringMonitor deve observar alterações no workspace.json e acionar auto-wiring com debounce', async () => {
|
|
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'maestri-autowire-mon-'));
|
|
const workspacePath = path.join(tmpDir, 'workspace.json');
|
|
|
|
const workspaceData = {
|
|
id: 'ws-monitor',
|
|
payload: {
|
|
nodes: [
|
|
{
|
|
id: 'hub',
|
|
type: 'terminal',
|
|
title: 'maestri-remote',
|
|
content: { terminal: { _0: { name: 'maestri-remote' } } }
|
|
}
|
|
],
|
|
connections: []
|
|
}
|
|
};
|
|
fs.writeFileSync(workspacePath, JSON.stringify(workspaceData, null, 2), 'utf8');
|
|
|
|
let wiringEvents = 0;
|
|
const monitor = new CanvasAutoWiringMonitor({
|
|
workspaceFilePath: workspacePath,
|
|
concentratorName: 'maestri-remote',
|
|
debounceMs: 50,
|
|
onWired: (res) => {
|
|
if (res.modified) wiringEvents++;
|
|
}
|
|
});
|
|
|
|
monitor.start();
|
|
|
|
// Adiciona novo nó terminal
|
|
workspaceData.payload.nodes.push({
|
|
id: 'novo-agente',
|
|
type: 'terminal',
|
|
title: 'Agente Novo',
|
|
content: { terminal: { _0: { name: 'agente_novo' } } }
|
|
});
|
|
fs.writeFileSync(workspacePath, JSON.stringify(workspaceData, null, 2), 'utf8');
|
|
|
|
// Aguarda debounce e processamento
|
|
await new Promise(r => setTimeout(r, 150));
|
|
|
|
monitor.stop();
|
|
|
|
assert.ok(wiringEvents >= 1, 'Deve ter acionado evento de wiring');
|
|
const finalJson = JSON.parse(fs.readFileSync(workspacePath, 'utf8'));
|
|
const conn = finalJson.payload.connections.find(c => c.to === 'novo-agente');
|
|
assert.ok(conn, 'Conexão para novo-agente deve existir no disco');
|
|
|
|
// Limpeza
|
|
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
});
|