Files
waggle-os/packages/server/tests/local/feedback-routes.test.ts
shumeiko.it ad2ca875cb feat(core): give Waggle its own database, apart from memory (P1.5)
Waggle kept its own tables in the memory database. Before the memory
engine can be replaced by mem0 they need a home of their own.

- add WaggleDB (<dataDir>/waggle.db). Automations, notifications, held
  actions, skill hashes, thumbs feedback, cost tracking, the profile card
  and awareness items live there. Their stores take a WaggleDB, so passing
  a memory database is a type error.
- the profile card and awareness items are now per scope ('personal' or
  'workspace:<id>') in one table each, instead of one table per .mind file
- on the first start, copy these tables from personal.mind and every
  workspace.mind (importLegacyMindData). The legacy files are only read,
  each scope is copied once and a table only while its target is empty.
- copy the logger, injection scanner, external-memory ingress guard,
  WorkspaceManager and the embedders from @waggle/hive-mind-core into
  @waggle/core, which now re-exports its own copies
- point the server, agent orchestrator and CLI at waggle.db; update the
  tests that seeded these tables in a memory database

ADR-0007 records the layout and the licensing question about the copied
files.
2026-10-23 14:31:28 +02:00

203 lines
5.9 KiB
TypeScript

/**
* Feedback REST API Route Tests
*
* Tests the two feedback endpoints:
* POST /api/feedback — record user feedback
* GET /api/feedback/stats — get feedback stats
*/
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import path from 'node:path';
import fs from 'node:fs';
import os from 'node:os';
import Fastify from 'fastify';
import { WaggleDB } from '@waggle/core';
import { feedbackRoutes } from '../../src/local/routes/feedback.js';
/** Minimal server with a real in-memory waggle.db, where feedback is stored. */
function createTestServer(db: WaggleDB) {
const server = Fastify({ logger: false });
server.decorate('waggleDb', db);
server.register(feedbackRoutes);
return { server };
}
describe('Feedback Routes', () => {
let db: WaggleDB;
let server: ReturnType<typeof Fastify>;
beforeEach(() => {
db = new WaggleDB(':memory:');
const result = createTestServer(db);
server = result.server;
});
afterEach(async () => {
await server.close();
db.close();
});
// ── POST /api/feedback ──────────────────────────────────────────────
describe('POST /api/feedback', () => {
it('stores thumbs-up feedback', async () => {
const res = await server.inject({
method: 'POST',
url: '/api/feedback',
payload: {
sessionId: 'sess-1',
messageIndex: 3,
rating: 'up',
},
});
expect(res.statusCode).toBe(200);
expect(res.json()).toEqual({ ok: true });
});
it('stores thumbs-down feedback with reason', async () => {
const res = await server.inject({
method: 'POST',
url: '/api/feedback',
payload: {
sessionId: 'sess-1',
messageIndex: 5,
rating: 'down',
reason: 'too_verbose',
detail: 'The response was way too long',
},
});
expect(res.statusCode).toBe(200);
expect(res.json()).toEqual({ ok: true });
});
it('rejects missing sessionId', async () => {
const res = await server.inject({
method: 'POST',
url: '/api/feedback',
payload: {
messageIndex: 3,
rating: 'up',
},
});
expect(res.statusCode).toBe(400);
expect(res.json().error).toContain('sessionId');
});
it('rejects invalid rating', async () => {
const res = await server.inject({
method: 'POST',
url: '/api/feedback',
payload: {
sessionId: 'sess-1',
messageIndex: 3,
rating: 'meh',
},
});
expect(res.statusCode).toBe(400);
expect(res.json().error).toContain('rating');
});
it('rejects invalid reason', async () => {
const res = await server.inject({
method: 'POST',
url: '/api/feedback',
payload: {
sessionId: 'sess-1',
messageIndex: 3,
rating: 'down',
reason: 'invalid_reason',
},
});
expect(res.statusCode).toBe(400);
expect(res.json().error).toContain('reason');
});
it('rejects negative messageIndex', async () => {
const res = await server.inject({
method: 'POST',
url: '/api/feedback',
payload: {
sessionId: 'sess-1',
messageIndex: -1,
rating: 'up',
},
});
expect(res.statusCode).toBe(400);
expect(res.json().error).toContain('messageIndex');
});
});
// ── GET /api/feedback/stats ─────────────────────────────────────────
describe('GET /api/feedback/stats', () => {
it('returns empty stats initially', async () => {
const res = await server.inject({
method: 'GET',
url: '/api/feedback/stats',
});
expect(res.statusCode).toBe(200);
const body = res.json();
expect(body.totalFeedback).toBe(0);
expect(body.positiveRate).toBe(0);
expect(body.topIssues).toEqual([]);
expect(typeof body.improvementTrend).toBe('string');
});
it('returns correct stats shape after feedback', async () => {
// Submit some feedback
await server.inject({
method: 'POST',
url: '/api/feedback',
payload: { sessionId: 's1', messageIndex: 0, rating: 'up' },
});
await server.inject({
method: 'POST',
url: '/api/feedback',
payload: { sessionId: 's1', messageIndex: 1, rating: 'up' },
});
await server.inject({
method: 'POST',
url: '/api/feedback',
payload: { sessionId: 's1', messageIndex: 2, rating: 'down', reason: 'too_verbose' },
});
const res = await server.inject({
method: 'GET',
url: '/api/feedback/stats',
});
expect(res.statusCode).toBe(200);
const body = res.json();
expect(body.totalFeedback).toBe(3);
expect(body.positiveRate).toBeCloseTo(0.67, 1);
expect(body.topIssues).toContain('too_verbose');
expect(typeof body.improvementTrend).toBe('string');
});
it('tracks top issues from negative feedback', async () => {
// Submit multiple negative feedbacks with different reasons
for (let i = 0; i < 3; i++) {
await server.inject({
method: 'POST',
url: '/api/feedback',
payload: { sessionId: 's1', messageIndex: i, rating: 'down', reason: 'wrong_answer' },
});
}
await server.inject({
method: 'POST',
url: '/api/feedback',
payload: { sessionId: 's1', messageIndex: 10, rating: 'down', reason: 'too_slow' },
});
const res = await server.inject({
method: 'GET',
url: '/api/feedback/stats',
});
const body = res.json();
expect(body.topIssues[0]).toBe('wrong_answer'); // Most common
expect(body.topIssues).toContain('too_slow');
});
});
});