Files
Jarvise-project/backend/test/stream.test.js
T
BlackAngelTV 877df963f9 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.
2026-10-05 15:01:57 +02:00

114 lines
4.0 KiB
JavaScript

import assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import { api, readSse, registerUser, startServer } from './helpers.js';
const FULL_REPLY = 'Bonjour Tony, je suis Jarvis.';
describe('streaming over SSE', () => {
let srv;
let user;
before(async () => {
srv = await startServer();
user = await registerUser(srv.base, 'stream@x.io');
});
after(async () => {
await srv.close();
});
async function newProject() {
const res = await api(srv.base, 'POST', '/api/projects', {
token: user.token,
body: { name: 'Stream' },
});
return res.json.project;
}
it('streams the answer chunk by chunk and persists at the end', async () => {
const p = await newProject();
const res = await fetch(`${srv.base}/api/projects/${p.id}/chat/stream`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${user.token}` },
body: JSON.stringify({ message: 'Hi' }),
});
assert.equal(res.status, 200);
assert.match(res.headers.get('content-type') ?? '', /text\/event-stream/);
const events = await readSse(res);
const deltas = events.filter((e) => e.event === 'delta').map((e) => e.data.content);
const done = events.find((e) => e.event === 'done');
assert.ok(deltas.length >= 5, `chunks received: ${deltas.length}`);
assert.equal(deltas.join(''), FULL_REPLY);
assert.ok(done, 'expected a done event');
assert.equal(done.data.reply, FULL_REPLY);
assert.equal(done.data.projectId, p.id);
assert.equal(done.data.messages.length, 2);
const msgs = await api(srv.base, 'GET', `/api/projects/${p.id}/messages`, { token: user.token });
assert.equal(msgs.json.messages.length, 2);
assert.equal(msgs.json.messages[1].content, FULL_REPLY);
});
it('injects the project context into the stream', async () => {
const p = await newProject();
await api(srv.base, 'POST', `/api/projects/${p.id}/messages`, {
token: user.token,
body: { role: 'system', content: 'Important note' },
});
const res = await fetch(`${srv.base}/api/projects/${p.id}/chat/stream`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${user.token}` },
body: JSON.stringify({ message: 'Follow up' }),
});
const events = await readSse(res);
assert.ok(events.some((e) => e.event === 'done'));
const msgs = await api(srv.base, 'GET', `/api/projects/${p.id}/messages`, { token: user.token });
assert.deepEqual(
msgs.json.messages.map((m) => m.content),
['Important note', 'Follow up', FULL_REPLY],
);
});
it('Ollama error before the stream: real HTTP error as JSON', async () => {
const p = await newProject();
const res = await fetch(`${srv.base}/api/projects/${p.id}/chat/stream`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${user.token}` },
body: JSON.stringify({ message: 'boom', model: 'boom' }),
});
assert.ok(res.status >= 400, `status: ${res.status}`);
assert.match(res.headers.get('content-type') ?? '', /application\/json/);
assert.ok((await res.json()).error);
const msgs = await api(srv.base, 'GET', `/api/projects/${p.id}/messages`, { token: user.token });
assert.equal(msgs.json.messages.length, 0, 'no orphan message');
});
it('stateless stream: requires authentication', async () => {
assert.equal((await fetch(`${srv.base}/api/chat/stream`, { method: 'POST' })).status, 401);
});
it('stateless stream: works', async () => {
const res = await fetch(`${srv.base}/api/chat/stream`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${user.token}` },
body: JSON.stringify({ message: 'Hello' }),
});
assert.equal(res.status, 200);
const events = await readSse(res);
const deltas = events.filter((e) => e.event === 'delta').map((e) => e.data.content);
assert.equal(deltas.join(''), FULL_REPLY);
});
});