Storing a context is not enough: the user needs to know it actually reaches Ollama, and that the messages replayed are the ones they expect. The read-only probe answers that by asking the model to recite its context and reporting how many messages were replayed, while persisting nothing.
220 lines
7.8 KiB
JavaScript
220 lines
7.8 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);
|
|
});
|
|
});
|