test(backend): cover auth, projects and streaming with a mocked Ollama
Tests run without any external service; the mock streams NDJSON chunks so the SSE path is exercised end to end.
This commit is contained in:
1 parent
32dd462be5
commit
877df963f9
6 files changed
+622
No files matched your search
@@ -0,0 +1,180 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { after, before, describe, it } from 'node:test';
|
||||
|
||||
import { api, registerUser, startServer } from './helpers.js';
|
||||
|
||||
describe('projects and context', () => {
|
||||
let srv;
|
||||
let alice;
|
||||
let bob;
|
||||
|
||||
before(async () => {
|
||||
srv = await startServer();
|
||||
alice = await registerUser(srv.base, 'alice@x.io');
|
||||
bob = await registerUser(srv.base, 'bob@x.io');
|
||||
});
|
||||
|
||||
after(async () => {
|
||||
await srv.close();
|
||||
});
|
||||
|
||||
async function newProject(name = 'Home') {
|
||||
const res = await api(srv.base, 'POST', '/api/projects', {
|
||||
token: alice.token,
|
||||
body: { name, description: 'Home automation' },
|
||||
});
|
||||
assert.equal(res.status, 201);
|
||||
return res.json.project;
|
||||
}
|
||||
|
||||
it('creates and lists its projects', async () => {
|
||||
const p = await newProject('Created');
|
||||
assert.equal(p.name, 'Created');
|
||||
assert.equal(p.messageCount, 0);
|
||||
|
||||
const list = await api(srv.base, 'GET', '/api/projects', { token: alice.token });
|
||||
assert.equal(list.status, 200);
|
||||
assert.ok(list.json.projects.some((x) => x.id === p.id));
|
||||
});
|
||||
|
||||
it('requires a token', async () => {
|
||||
assert.equal((await api(srv.base, 'GET', '/api/projects')).status, 401);
|
||||
assert.equal((await api(srv.base, 'POST', '/api/projects', { body: { name: 'x' } })).status, 401);
|
||||
});
|
||||
|
||||
it('validates the project name', async () => {
|
||||
assert.equal(
|
||||
(await api(srv.base, 'POST', '/api/projects', { token: alice.token, body: { name: '' } })).status,
|
||||
400,
|
||||
);
|
||||
});
|
||||
|
||||
it('isolates projects between users', async () => {
|
||||
const p = await newProject('Private');
|
||||
|
||||
// Bob does not see Alice's project…
|
||||
const bobList = await api(srv.base, 'GET', '/api/projects', { token: bob.token });
|
||||
assert.equal(bobList.json.projects.some((x) => x.id === p.id), false);
|
||||
|
||||
// …and gets a 404 on every operation.
|
||||
assert.equal((await api(srv.base, 'GET', `/api/projects/${p.id}`, { token: bob.token })).status, 404);
|
||||
assert.equal((await api(srv.base, 'GET', `/api/projects/${p.id}/messages`, { token: bob.token })).status, 404);
|
||||
assert.equal(
|
||||
(await api(srv.base, 'PATCH', `/api/projects/${p.id}`, { token: bob.token, body: { name: 'pirate' } })).status,
|
||||
404,
|
||||
);
|
||||
assert.equal((await api(srv.base, 'DELETE', `/api/projects/${p.id}`, { token: bob.token })).status, 404);
|
||||
assert.equal(
|
||||
(await api(srv.base, 'POST', `/api/projects/${p.id}/chat`, { token: bob.token, body: { message: 'hi' } })).status,
|
||||
404,
|
||||
);
|
||||
});
|
||||
|
||||
it('renames then deletes a project', async () => {
|
||||
const p = await newProject('To rename');
|
||||
|
||||
const renamed = await api(srv.base, 'PATCH', `/api/projects/${p.id}`, {
|
||||
token: alice.token,
|
||||
body: { name: 'Renamed' },
|
||||
});
|
||||
assert.equal(renamed.json.project.name, 'Renamed');
|
||||
|
||||
assert.equal((await api(srv.base, 'DELETE', `/api/projects/${p.id}`, { token: alice.token })).status, 204);
|
||||
assert.equal((await api(srv.base, 'GET', `/api/projects/${p.id}`, { token: alice.token })).status, 404);
|
||||
});
|
||||
|
||||
it('handles the context (append, order, clear)', async () => {
|
||||
const p = await newProject('Context');
|
||||
|
||||
await api(srv.base, 'POST', `/api/projects/${p.id}/messages`, {
|
||||
token: alice.token,
|
||||
body: { role: 'system', content: 'Initial context' },
|
||||
});
|
||||
await api(srv.base, 'POST', `/api/projects/${p.id}/messages`, {
|
||||
token: alice.token,
|
||||
body: { content: 'Hello Jarvis' },
|
||||
});
|
||||
|
||||
const msgs = await api(srv.base, 'GET', `/api/projects/${p.id}/messages`, { token: alice.token });
|
||||
assert.equal(msgs.json.messages.length, 2);
|
||||
assert.equal(msgs.json.messages[0].content, 'Initial context');
|
||||
assert.equal(msgs.json.messages[1].role, 'user');
|
||||
|
||||
assert.equal(
|
||||
(await api(srv.base, 'POST', `/api/projects/${p.id}/messages`, { token: alice.token, body: { content: '' } })).status,
|
||||
400,
|
||||
);
|
||||
|
||||
const cleared = await api(srv.base, 'DELETE', `/api/projects/${p.id}/messages`, { token: alice.token });
|
||||
assert.equal(cleared.json.deleted, 2);
|
||||
});
|
||||
|
||||
it('paginates the context', async () => {
|
||||
const p = await newProject('Pagination');
|
||||
for (let i = 1; i <= 5; i++) {
|
||||
await api(srv.base, 'POST', `/api/projects/${p.id}/messages`, {
|
||||
token: alice.token,
|
||||
body: { content: `message ${i}` },
|
||||
});
|
||||
}
|
||||
|
||||
const page1 = await api(srv.base, 'GET', `/api/projects/${p.id}/messages?limit=2`, { token: alice.token });
|
||||
assert.deepEqual(
|
||||
page1.json.messages.map((m) => m.content),
|
||||
['message 4', 'message 5'],
|
||||
);
|
||||
assert.equal(page1.json.hasMore, true);
|
||||
|
||||
const page2 = await api(srv.base, 'GET', `/api/projects/${p.id}/messages?limit=2&before=${page1.json.oldestId}`, {
|
||||
token: alice.token,
|
||||
});
|
||||
assert.deepEqual(
|
||||
page2.json.messages.map((m) => m.content),
|
||||
['message 2', 'message 3'],
|
||||
);
|
||||
assert.equal(page2.json.hasMore, true);
|
||||
|
||||
const page3 = await api(srv.base, 'GET', `/api/projects/${p.id}/messages?limit=2&before=${page2.json.oldestId}`, {
|
||||
token: alice.token,
|
||||
});
|
||||
assert.deepEqual(
|
||||
page3.json.messages.map((m) => m.content),
|
||||
['message 1'],
|
||||
);
|
||||
assert.equal(page3.json.hasMore, false);
|
||||
});
|
||||
|
||||
it('non-streamed chat: reply and context persistence', async () => {
|
||||
const p = await newProject('Chat');
|
||||
|
||||
const res = await api(srv.base, 'POST', `/api/projects/${p.id}/chat`, {
|
||||
token: alice.token,
|
||||
body: { message: 'Hi' },
|
||||
});
|
||||
|
||||
assert.equal(res.status, 200);
|
||||
assert.equal(res.json.reply, 'Bonjour Tony, je suis Jarvis.');
|
||||
assert.equal(res.json.messages.length, 2);
|
||||
|
||||
const msgs = await api(srv.base, 'GET', `/api/projects/${p.id}/messages`, { token: alice.token });
|
||||
assert.equal(msgs.json.messages.length, 2);
|
||||
assert.equal(msgs.json.messages[0].content, 'Hi');
|
||||
assert.equal(msgs.json.messages[1].role, 'assistant');
|
||||
|
||||
const stats = await api(srv.base, 'GET', `/api/projects/${p.id}/stats`, { token: alice.token });
|
||||
assert.equal(stats.json.stats.messageCount, 2);
|
||||
});
|
||||
|
||||
it('Ollama error: no orphan message', async () => {
|
||||
const p = await newProject('Error');
|
||||
const res = await api(srv.base, 'POST', `/api/projects/${p.id}/chat`, {
|
||||
token: alice.token,
|
||||
body: { message: 'boom', model: 'boom' },
|
||||
});
|
||||
assert.ok(res.status >= 400);
|
||||
assert.equal(
|
||||
(await api(srv.base, 'GET', `/api/projects/${p.id}/messages`, { token: alice.token })).json.messages.length,
|
||||
0,
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user