Files
BlackAngelTV 6ebf550eb0 feat(backend): bound the messages kept per project
A project that lives for months would otherwise grow the SQLite file without
limit, even though the model only ever reads the most recent messages. Pruning
the oldest rows on every write keeps a project to a fixed budget without
touching what the context is built from.
2026-10-05 21:39:45 +02:00

299 lines
10 KiB
JavaScript

import assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import { api, readSse, 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,
);
});
it('recites the stored context without persisting anything', async () => {
const p = await newProject('Recall');
for (const content of ['Le frontend est écrit en Rust', 'Le backend répond en SSE']) {
await api(srv.base, 'POST', `/api/projects/${p.id}/messages`, {
token: alice.token,
body: { content },
});
}
const res = await fetch(`${srv.base}/api/projects/${p.id}/context/recall`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${alice.token}` },
body: JSON.stringify({}),
});
assert.equal(res.status, 200);
const events = await readSse(res);
const done = events.find((e) => e.event === 'done');
assert.ok(events.some((e) => e.event === 'delta'), 'expected the model to answer');
// The model really received the two stored messages.
assert.equal(done.data.contextMessages, 2);
assert.equal(done.data.reply, 'Bonjour Tony, je suis Jarvis.');
// A probe must leave the context untouched.
const msgs = await api(srv.base, 'GET', `/api/projects/${p.id}/messages`, { token: alice.token });
assert.equal(msgs.json.messages.length, 2);
});
it('recital needs a token', async () => {
const p = await newProject('Recall auth');
const res = await fetch(`${srv.base}/api/projects/${p.id}/context/recall`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({}),
});
assert.equal(res.status, 401);
});
});
describe('context retention', () => {
let srv;
let user;
before(async () => {
// A tiny cap so the pruning is observable without writing 500 messages.
srv = await startServer({ maxMessagesPerProject: 4 });
user = await registerUser(srv.base, 'carol@x.io');
});
after(async () => {
await srv.close();
});
async function cappedProject() {
const res = await api(srv.base, 'POST', '/api/projects', {
token: user.token,
body: { name: 'Capped' },
});
return res.json.project;
}
async function append(projectId, content) {
const res = await api(srv.base, 'POST', `/api/projects/${projectId}/messages`, {
token: user.token,
body: { content },
});
assert.equal(res.status, 201);
}
it('keeps only the last messages of a project', async () => {
const p = await cappedProject();
for (let i = 1; i <= 7; i++) await append(p.id, `message ${i}`);
const msgs = await api(srv.base, 'GET', `/api/projects/${p.id}/messages?limit=50`, {
token: user.token,
});
assert.deepEqual(
msgs.json.messages.map((m) => m.content),
['message 4', 'message 5', 'message 6', 'message 7'],
);
// Nothing older remains, so pagination stops there.
assert.equal(msgs.json.hasMore, false);
const project = await api(srv.base, 'GET', `/api/projects/${p.id}`, { token: user.token });
assert.equal(project.json.project.messageCount, 4);
});
it('also caps what a chat exchange appends', async () => {
const p = await cappedProject();
for (let i = 1; i <= 4; i++) await append(p.id, `message ${i}`);
// One exchange adds a question and an answer: the two oldest go away.
const chat = await api(srv.base, 'POST', `/api/projects/${p.id}/chat`, {
token: user.token,
body: { message: 'Hi' },
});
assert.equal(chat.status, 200);
const msgs = await api(srv.base, 'GET', `/api/projects/${p.id}/messages?limit=50`, {
token: user.token,
});
assert.deepEqual(
msgs.json.messages.map((m) => m.content),
['message 3', 'message 4', 'Hi', 'Bonjour Tony, je suis Jarvis.'],
);
});
it('never trims another project', async () => {
const a = await cappedProject();
const b = await cappedProject();
for (let i = 1; i <= 6; i++) await append(a.id, `a${i}`);
await append(b.id, 'b1');
const kept = await api(srv.base, 'GET', `/api/projects/${b.id}/messages`, { token: user.token });
assert.equal(kept.json.messages.length, 1);
});
});