169 lines
5.1 KiB
Lua
169 lines
5.1 KiB
Lua
local createLibTest = require('/apis/libtest');
|
|
local createAiHelloWorld = require('/apis/libaihelloworld');
|
|
|
|
local testlib = createLibTest({ ... });
|
|
|
|
local function fakeSettings(initial)
|
|
local values = initial or {};
|
|
local saveCount = 0;
|
|
|
|
return {
|
|
get = function(key)
|
|
return values[key];
|
|
end,
|
|
set = function(key, value)
|
|
values[key] = value;
|
|
end,
|
|
unset = function(key)
|
|
values[key] = nil;
|
|
end,
|
|
save = function()
|
|
saveCount = saveCount + 1;
|
|
end,
|
|
values = values,
|
|
saveCount = function()
|
|
return saveCount;
|
|
end,
|
|
};
|
|
end
|
|
|
|
local function response(code, body)
|
|
return {
|
|
getResponseCode = function()
|
|
return code;
|
|
end,
|
|
readAll = function()
|
|
return body;
|
|
end,
|
|
close = function() end,
|
|
};
|
|
end
|
|
|
|
local function fakeHttp(result)
|
|
local calls = {};
|
|
return {
|
|
post = function(url, body, headers)
|
|
calls[#calls + 1] = { url = url, body = body, headers = headers };
|
|
if type(result) == 'function' then
|
|
return result(url, body, headers);
|
|
end
|
|
return result;
|
|
end,
|
|
calls = calls,
|
|
};
|
|
end
|
|
|
|
testlib.test('askHello posts ping prompt and saves session id', function()
|
|
local settingsStub = fakeSettings({
|
|
['opencc.proxy_url'] = 'https://proxy.example/',
|
|
['opencc.proxy_token'] = 'secret',
|
|
});
|
|
local httpStub = fakeHttp(response(200, textutils.serializeJSON({
|
|
sessionId = 'ses_123',
|
|
reply = 'pong',
|
|
truncated = false,
|
|
})));
|
|
local ai = createAiHelloWorld({ http = httpStub, settings = settingsStub });
|
|
|
|
local ok, result = ai.askHello();
|
|
|
|
testlib.assertTrue(ok, tostring(result));
|
|
testlib.assertEquals(result.reply, 'pong');
|
|
testlib.assertEquals(result.sessionId, 'ses_123');
|
|
testlib.assertEquals(settingsStub.values['opencc.session_id'], 'ses_123');
|
|
testlib.assertEquals(settingsStub.saveCount(), 1);
|
|
|
|
testlib.assertEquals(#httpStub.calls, 1);
|
|
local call = httpStub.calls[1];
|
|
testlib.assertEquals(call.url, 'https://proxy.example/ask');
|
|
testlib.assertEquals(call.headers['Authorization'], 'Bearer secret');
|
|
testlib.assertEquals(call.headers['Content-Type'], 'application/json');
|
|
|
|
local request = textutils.unserializeJSON(call.body);
|
|
testlib.assertEquals(request.prompt, 'reply with exactly: pong');
|
|
testlib.assertEquals(request.sessionId, nil);
|
|
end);
|
|
|
|
testlib.test('askHello includes existing session id', function()
|
|
local settingsStub = fakeSettings({
|
|
['opencc.proxy_url'] = 'https://proxy.example',
|
|
['opencc.proxy_token'] = 'secret',
|
|
['opencc.session_id'] = 'ses_old',
|
|
});
|
|
local httpStub = fakeHttp(response(200, textutils.serializeJSON({
|
|
sessionId = 'ses_old',
|
|
reply = 'pong',
|
|
})));
|
|
local ai = createAiHelloWorld({ http = httpStub, settings = settingsStub });
|
|
|
|
local ok = ai.askHello();
|
|
|
|
testlib.assertTrue(ok);
|
|
local request = textutils.unserializeJSON(httpStub.calls[1].body);
|
|
testlib.assertEquals(request.sessionId, 'ses_old');
|
|
end);
|
|
|
|
testlib.test('askHello rejects missing proxy url', function()
|
|
local settingsStub = fakeSettings({ ['opencc.proxy_token'] = 'secret' });
|
|
local httpStub = fakeHttp(response(200, '{}'));
|
|
local ai = createAiHelloWorld({ http = httpStub, settings = settingsStub });
|
|
|
|
local ok, err = ai.askHello();
|
|
|
|
testlib.assertTrue(not ok);
|
|
testlib.assertTrue(string.find(err, 'opencc.proxy_url', 1, true));
|
|
testlib.assertEquals(#httpStub.calls, 0);
|
|
end);
|
|
|
|
testlib.test('askHello rejects missing proxy token', function()
|
|
local settingsStub = fakeSettings({ ['opencc.proxy_url'] = 'https://proxy.example' });
|
|
local httpStub = fakeHttp(response(200, '{}'));
|
|
local ai = createAiHelloWorld({ http = httpStub, settings = settingsStub });
|
|
|
|
local ok, err = ai.askHello();
|
|
|
|
testlib.assertTrue(not ok);
|
|
testlib.assertTrue(string.find(err, 'opencc.proxy_token', 1, true));
|
|
testlib.assertEquals(#httpStub.calls, 0);
|
|
end);
|
|
|
|
testlib.test('askHello maps proxy unreachable', function()
|
|
local settingsStub = fakeSettings({
|
|
['opencc.proxy_url'] = 'https://proxy.example',
|
|
['opencc.proxy_token'] = 'secret',
|
|
});
|
|
local httpStub = fakeHttp(nil);
|
|
local ai = createAiHelloWorld({ http = httpStub, settings = settingsStub });
|
|
|
|
local ok, err = ai.askHello();
|
|
|
|
testlib.assertTrue(not ok);
|
|
testlib.assertTrue(string.find(err, 'proxy injoignable', 1, true));
|
|
end);
|
|
|
|
testlib.test('askHello maps 401 response', function()
|
|
local settingsStub = fakeSettings({
|
|
['opencc.proxy_url'] = 'https://proxy.example',
|
|
['opencc.proxy_token'] = 'secret',
|
|
});
|
|
local httpStub = fakeHttp(response(401, textutils.serializeJSON({ error = 'unauthorized' })));
|
|
local ai = createAiHelloWorld({ http = httpStub, settings = settingsStub });
|
|
|
|
local ok, err = ai.askHello();
|
|
|
|
testlib.assertTrue(not ok);
|
|
testlib.assertEquals(err, 'token invalide');
|
|
end);
|
|
|
|
testlib.test('clearSession unsets persisted session id', function()
|
|
local settingsStub = fakeSettings({ ['opencc.session_id'] = 'ses_old' });
|
|
local ai = createAiHelloWorld({ http = fakeHttp(nil), settings = settingsStub });
|
|
|
|
ai.clearSession();
|
|
|
|
testlib.assertEquals(settingsStub.values['opencc.session_id'], nil);
|
|
testlib.assertEquals(settingsStub.saveCount(), 1);
|
|
end);
|
|
|
|
testlib.run();
|