Files
Void-Homelab/tests/api/companion.test.js

72 lines
3.2 KiB
JavaScript

import { describe, it, expect, beforeAll } from 'vitest';
import { fileURLToPath } from 'url';
import request from 'supertest';
import { pool } from '../../lib/db/pool.js';
import { createApp } from '../../server.js';
import { resetDb } from '../helpers/db.js';
import { migrateUp } from '../../lib/db/migrate.js';
const FAKE_CLAUDE = fileURLToPath(
new URL('../fixtures/fake-claude-draft.js', import.meta.url)
);
let app, spaceId;
beforeAll(async () => {
await resetDb(); await migrateUp();
process.env.OWNER_TOKEN = 'test-token';
({ rows: [{ id: spaceId }] } = await pool.query(
`INSERT INTO spaces(slug,name) VALUES('s','S') RETURNING id`));
app = createApp();
// Inject the fake claude binary — it emits a stream with a propose_change
// tool call and a pending_change_id in the tool_result content.
app.locals.claudeExe = process.execPath; // node
process.env.FAKE_CLAUDE_SCRIPT = FAKE_CLAUDE; // picked up by the wrapper below
// Override claudeExe to be a tiny node wrapper that runs the fixture script.
// runClaudeTurn passes all flags AFTER claudeExe, so we can't use the script
// directly as claudeExe (node can't take a script + unknown flags).
// Instead, inject a wrapper that ignores all args and just runs the fixture.
app.locals.claudeExe = process.execPath;
app.locals._claudeArgs = [FAKE_CLAUDE]; // Not used by the route — use env trick instead.
// The cleanest injection: point claudeExe at the fixture directly (it has a shebang).
// Node will exec it as a script; since it ignores all CLI args, all --flags are harmless.
app.locals.claudeExe = FAKE_CLAUDE;
});
const auth = (r) => r.set('Authorization', 'Bearer test-token');
describe('companion API', () => {
it('GET creates the conversation and returns empty history', async () => {
const res = await auth(request(app).get(`/api/spaces/${spaceId}/companion`));
expect(res.status).toBe(200);
expect(res.body.conversation_id).toBeTruthy();
expect(res.body.messages).toEqual([]);
});
it('POST /turn streams SSE events and persists messages', async () => {
const res = await auth(request(app).post(`/api/spaces/${spaceId}/companion/turn`))
.send({ text: 'make a task to validate the CSV', view: null }); // rail sends view:null
expect(res.status).toBe(200);
expect(res.headers['content-type']).toMatch(/text\/event-stream/);
// Verify SSE event types are present in the stream
expect(res.text).toMatch(/event: delta/);
expect(res.text).toMatch(/event: tool/);
expect(res.text).toMatch(/event: draft/);
expect(res.text).toMatch(/event: done/);
// Verify messages persisted: user + assistant
const { rows: msgs } = await pool.query(
`SELECT role, body, metadata FROM messages ORDER BY created_at`);
expect(msgs.map(m => m.role)).toEqual(['user', 'assistant']);
expect(msgs[1].body).toBe('Drafted a task.');
expect(msgs[1].metadata.draft_ids).toEqual(['pc-test-1']);
// NOTE: Because the fake claude does NOT actually run the real MCP server,
// NO real pending_changes row is created in this test. That code path is
// covered by the companion-stdio B1 callMcpTool test and the live B5 smoke test.
// Do NOT assert on the pending_changes table here.
});
});