97 lines
3.1 KiB
Lua
97 lines
3.1 KiB
Lua
local createLibTest = require('/apis/libtest');
|
|
local createMcpComputer = require('/apis/libmcpcomputer');
|
|
|
|
local testlib = createLibTest({ ... });
|
|
|
|
local function packed(...)
|
|
return table.pack(...);
|
|
end
|
|
|
|
local function fakeOs(id, label)
|
|
return {
|
|
getComputerID = function() return id; end,
|
|
getComputerLabel = function() return label; end,
|
|
};
|
|
end
|
|
|
|
testlib.test('parseArgs accepts positional URL', function()
|
|
local mcpComputer = createMcpComputer();
|
|
local config = mcpComputer.parseArgs(packed('ws://127.0.0.1:3001'));
|
|
|
|
testlib.assertEquals(config.url, 'ws://127.0.0.1:3001');
|
|
end);
|
|
|
|
testlib.test('parseArgs accepts -url option', function()
|
|
local mcpComputer = createMcpComputer();
|
|
local config = mcpComputer.parseArgs(packed('-url', 'ws://bridge:3001'));
|
|
|
|
testlib.assertEquals(config.url, 'ws://bridge:3001');
|
|
end);
|
|
|
|
testlib.test('parseArgs rejects missing URL', function()
|
|
local mcpComputer = createMcpComputer();
|
|
local config, err = mcpComputer.parseArgs(packed());
|
|
|
|
testlib.assertEquals(config, nil);
|
|
testlib.assertTrue(string.find(err, 'missing websocket URL', 1, true));
|
|
end);
|
|
|
|
testlib.test('formatPong includes computer id and label', function()
|
|
local mcpComputer = createMcpComputer();
|
|
|
|
testlib.assertEquals(mcpComputer.formatPong(fakeOs(12, 'base-turtle')), 'pong from 12 (Label: base-turtle)');
|
|
end);
|
|
|
|
testlib.test('formatPong renders missing or empty labels as null', function()
|
|
local mcpComputer = createMcpComputer();
|
|
|
|
testlib.assertEquals(mcpComputer.formatPong(fakeOs(7, nil)), 'pong from 7 (Label: null)');
|
|
testlib.assertEquals(mcpComputer.formatPong(fakeOs(8, '')), 'pong from 8 (Label: null)');
|
|
end);
|
|
|
|
testlib.test('hello identifies the current computer', function()
|
|
local mcpComputer = createMcpComputer();
|
|
local hello = mcpComputer.hello(fakeOs(3, 'agent'));
|
|
|
|
testlib.assertEquals(hello.type, 'hello');
|
|
testlib.assertEquals(hello.computerId, 3);
|
|
testlib.assertEquals(hello.computerLabel, 'agent');
|
|
end);
|
|
|
|
testlib.test('handleRequest responds to ping', function()
|
|
local mcpComputer = createMcpComputer();
|
|
local response = mcpComputer.handleRequest({
|
|
type = 'request',
|
|
id = 'req-1',
|
|
method = 'ping',
|
|
}, fakeOs(42, 'worker'));
|
|
|
|
testlib.assertEquals(response.type, 'response');
|
|
testlib.assertEquals(response.id, 'req-1');
|
|
testlib.assertEquals(response.ok, true);
|
|
testlib.assertEquals(response.result, 'pong from 42 (Label: worker)');
|
|
end);
|
|
|
|
testlib.test('handleRequest reports unknown methods', function()
|
|
local mcpComputer = createMcpComputer();
|
|
local response = mcpComputer.handleRequest({
|
|
type = 'request',
|
|
id = 'req-2',
|
|
method = 'dance',
|
|
}, fakeOs(42, 'worker'));
|
|
|
|
testlib.assertEquals(response.type, 'response');
|
|
testlib.assertEquals(response.id, 'req-2');
|
|
testlib.assertEquals(response.ok, false);
|
|
testlib.assertEquals(response.error, 'unknown method');
|
|
end);
|
|
|
|
testlib.test('handleRequest ignores malformed frames', function()
|
|
local mcpComputer = createMcpComputer();
|
|
|
|
testlib.assertEquals(mcpComputer.handleRequest({ type = 'request', method = 'ping' }, fakeOs(1, nil)), nil);
|
|
testlib.assertEquals(mcpComputer.handleRequest({ type = 'hello' }, fakeOs(1, nil)), nil);
|
|
end);
|
|
|
|
testlib.run();
|