feat: add manual feedback AI summaries and secure leaderboard scoring
This commit is contained in:
1 parent
34a3a88376
commit
6bad96ca03
37 files changed
+1640
-77
No files matched your search
@@ -0,0 +1,119 @@
|
||||
const { test } = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const { createFeedbackApi } = require('../src/feedback-api');
|
||||
const { isPublicAddress, resolveEndpoint } = require('../src/llm-fetch');
|
||||
const eventId = 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa';
|
||||
const sessionId = 'bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb';
|
||||
const config = { endpoint: 'https://llm.example/v1/chat/completions', model: 'model', api_key: 'secret' };
|
||||
const request = (method = 'POST') => ({ method, headers: { authorization: 'Bearer user-token' } });
|
||||
|
||||
function fixture(options = {}) {
|
||||
const writes = [];
|
||||
const reads = [];
|
||||
const api = createFeedbackApi({
|
||||
supabaseUrl: 'https://supabase.example', serviceRoleKey: 'server-only',
|
||||
fetchImpl: async (_url, init) => {
|
||||
assert.equal(init.headers.Authorization, 'Bearer user-token');
|
||||
return new Response(JSON.stringify({ id: 'user-id', email: '[email protected]' }), { status: options.unauthorized ? 401 : 200 });
|
||||
},
|
||||
validateEndpoint: async () => {},
|
||||
providerFetch: options.providerFetch || (async () => new Response(JSON.stringify({ choices: [{ message: { content: 'Helpful feedback summary' } }] }))),
|
||||
supabaseFetch: async (path, init) => {
|
||||
if (init) { writes.push({ path, ...init }); return null; }
|
||||
reads.push(path);
|
||||
if (path.includes('/contacts?')) return [{ is_admin: !options.notAdmin }];
|
||||
if (path.includes('/llm_settings?')) return options.noSettings ? [] : [config];
|
||||
if (path.includes('/events?')) return [{ id: eventId, name: 'Event' }];
|
||||
if (path.includes('/sessions?')) return options.wrongSession ? [] : [{ id: sessionId, title: 'Session' }];
|
||||
if (path.includes('/feedback?') || path.includes('/event_feedback?')) {
|
||||
const rows = options.empty ? [] : Array.from({ length: options.feedbackCount || 1 }, () => ({ rating: 5, comment: 'Helpful' }));
|
||||
const offset = Number(new URL(path, 'https://supabase.example').searchParams.get('offset') || 0);
|
||||
return rows.slice(offset, offset + 500);
|
||||
}
|
||||
throw new Error(`Unexpected path: ${path}`);
|
||||
},
|
||||
});
|
||||
return { api, writes, reads };
|
||||
}
|
||||
|
||||
test('rejects missing, invalid and non-admin credentials before reading feedback', async () => {
|
||||
for (const options of [{ unauthorized: true }, { notAdmin: true }]) {
|
||||
const { api, reads } = fixture(options);
|
||||
await assert.rejects(api.handle(request(), '/feedback-summary', {}), error => [401, 403].includes(error.status));
|
||||
assert.ok(!reads.some(path => path.includes('/feedback?')));
|
||||
}
|
||||
await assert.rejects(fixture().api.handle({ method: 'GET', headers: {} }, '/llm-settings'), error => error.status === 401);
|
||||
});
|
||||
|
||||
test('never returns the stored key; retains it on unchanged-endpoint edits', async () => {
|
||||
const { api, writes } = fixture();
|
||||
const read = await api.handle(request('GET'), '/llm-settings');
|
||||
assert.equal(read.has_api_key, true);
|
||||
assert.ok(!('api_key' in read));
|
||||
const saved = await api.handle(request('PUT'), '/llm-settings', { endpoint: config.endpoint, model: 'new-model' });
|
||||
assert.equal(writes[0].body.api_key, 'secret');
|
||||
assert.ok(!('api_key' in saved));
|
||||
await assert.rejects(api.handle(request('PUT'), '/llm-settings', { endpoint: 'https://different.example/completions', model: 'model' }), /Enter or clear/);
|
||||
await api.handle(request('PUT'), '/llm-settings', { ...config, api_key: '' });
|
||||
assert.equal(writes[1].body.api_key, '');
|
||||
});
|
||||
|
||||
for (const scope of ['event', 'session']) {
|
||||
test(`generates and persists isolated ${scope} feedback with provenance`, async () => {
|
||||
const { api, writes, reads } = fixture();
|
||||
const summary = await api.handle(request(), '/feedback-summary', { scope, event_id: eventId, session_id: sessionId });
|
||||
assert.equal(summary.summary, 'Helpful feedback summary');
|
||||
assert.equal(summary.response_count, 1);
|
||||
assert.equal(summary.source_fingerprint.length, 64);
|
||||
assert.equal(summary.target_id, scope === 'session' ? sessionId : eventId);
|
||||
assert.equal(writes.length, 1);
|
||||
assert.equal(writes[0].prefer, 'resolution=merge-duplicates');
|
||||
assert.ok(reads.some(path => path.includes(`/rest/v1/${scope === 'session' ? 'feedback' : 'event_feedback'}?event_id=eq.${eventId}`)));
|
||||
if (scope === 'session') assert.ok(reads.some(path => path.includes(`&session_id=eq.${sessionId}`)));
|
||||
assert.ok(!JSON.stringify(summary).includes('secret'));
|
||||
});
|
||||
}
|
||||
|
||||
test('no writes for empty feedback, absent config, wrong event or provider failure', async () => {
|
||||
for (const options of [{ empty: true }, { noSettings: true }, { wrongSession: true }, { providerFetch: async () => new Response('{}', { status: 500 }) }]) {
|
||||
const { api, writes } = fixture(options);
|
||||
await assert.rejects(api.handle(request(), '/feedback-summary', { scope: 'session', event_id: eventId, session_id: sessionId }));
|
||||
assert.equal(writes.length, 0);
|
||||
}
|
||||
});
|
||||
|
||||
test('includes feedback from every query page in the provider snapshot', async () => {
|
||||
const { api, reads } = fixture({ feedbackCount: 501, providerFetch: async (_url, options) => {
|
||||
const messages = JSON.parse(options.body).messages;
|
||||
assert.equal(JSON.parse(messages[1].content).response_count, 501);
|
||||
return new Response(JSON.stringify({ choices: [{ message: { content: 'All responses summarized' } }] }));
|
||||
} });
|
||||
const summary = await api.handle(request(), '/feedback-summary', { scope: 'event', event_id: eventId });
|
||||
assert.equal(summary.response_count, 501);
|
||||
assert.equal(reads.filter(path => path.includes('/event_feedback?')).length, 2);
|
||||
});
|
||||
|
||||
test('rejects overlapping generation and releases lock after a failure', async () => {
|
||||
let release;
|
||||
const pending = new Promise(resolve => { release = resolve; });
|
||||
let started;
|
||||
const began = new Promise(resolve => { started = resolve; });
|
||||
const { api } = fixture({ providerFetch: async () => { started(); await pending; throw new Error('fail'); } });
|
||||
const body = { scope: 'event', event_id: eventId };
|
||||
const first = api.handle(request(), '/feedback-summary', body);
|
||||
await began;
|
||||
await assert.rejects(api.handle(request(), '/feedback-summary', body), error => error.status === 409);
|
||||
release();
|
||||
await assert.rejects(first, error => error.status === 502);
|
||||
await assert.rejects(api.handle(request(), '/feedback-summary', body), error => error.status === 502);
|
||||
});
|
||||
|
||||
test('blocks private, loopback, metadata, mapped and reserved IP destinations', async () => {
|
||||
for (const address of ['127.0.0.1', '10.0.0.1', '172.16.0.1', '192.168.1.1', '169.254.169.254', '0.0.0.0', '::1', '::ffff:127.0.0.1', 'fc00::1', 'fe80::1']) {
|
||||
assert.equal(isPublicAddress(address), false, address);
|
||||
}
|
||||
assert.equal(isPublicAddress('8.8.8.8'), true);
|
||||
await assert.rejects(resolveEndpoint(config.endpoint, async () => [{ address: '127.0.0.1', family: 4 }]), /public IP/);
|
||||
const record = await resolveEndpoint(config.endpoint, async () => [{ address: '8.8.8.8', family: 4 }]);
|
||||
assert.equal(record.address, '8.8.8.8');
|
||||
});
|
||||
Reference in new issue
Block a user