The front-desk check-in set checked_in and printed badges but awarded nothing, so the +10 depended on which check-in a person happened to get. Every award path was reviewed: - welcome_checkin awards +10 (category checkin) on a genuine first check-in and returns points + points_awarded; checkin_attendee and preview_welcome_checkin return points_awarded too. - set_registration_checked_in(false) deleted all checkin points while keeping the checkins rows, and switching back on awarded nothing -- one mistaken toggle destroyed those points permanently. ON now recomputes from the rows that still exist and awards only the shortfall: reversible, still unfarmable. - The staff/Admin/@appe.at exclusion stays (product decision) but is no longer silent: three-way points sheets in the scanner, "no points" with a tooltip in Registrations, and the Contacts points modal refuses to apply rather than letting adjust_points (void) no-op quietly. Also in this change: - admin silent refresh (5s poll + 500ms after writes) across all data views; Registrations patches in place so rows never slide under the cursor - Settings "Validate AI connection" via a new sidecar POST /llm-test verdict endpoint (never leaks the key, distinguishes "no usage block" from "off") - new Sessions view between Schedule and Activity: speakers, attendance, ratings, attendee filter, and one button that summarises every session with feedback (one request each, concurrency 3, UI blocked until all settle) - provider requests disable thinking through one shared body builder sending both reasoning_effort:"none" and chat_template_kwargs.enable_thinking SQL: 113 / 57 / 24 assertions (set_registration_checked_in section 3 was rewritten, it asserted the bug). Sidecar 34 unit tests. Full e2e 202 passed, 0 failed. Migration applied locally only -- not deployed.
200 lines
11 KiB
JavaScript
200 lines
11 KiB
JavaScript
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: options.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 ? [] : (options.settingsRows || [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');
|
|
});
|
|
|
|
const okReply = { choices: [{ message: { content: 'OK' } }], usage: { completion_tokens: 1, completion_tokens_details: { reasoning_tokens: 0 } } };
|
|
|
|
test('llm-test verifies the connection and sends thinking-disabled request', async () => {
|
|
const calls = [];
|
|
const { api, writes } = fixture({ providerFetch: async (url, options) => {
|
|
calls.push({ url, options, body: JSON.parse(options.body) });
|
|
return new Response(JSON.stringify(okReply));
|
|
} });
|
|
const verdict = await api.handle(request(), '/llm-test');
|
|
assert.equal(verdict.ok, true);
|
|
assert.equal(verdict.model, 'model');
|
|
assert.equal(verdict.endpoint, config.endpoint);
|
|
assert.equal(verdict.reasoning_disabled, true);
|
|
assert.equal(verdict.content, 'OK');
|
|
assert.equal(verdict.error, null);
|
|
assert.equal(typeof verdict.latency_ms, 'number');
|
|
assert.equal(writes.length, 0);
|
|
assert.equal(calls.length, 1);
|
|
assert.equal(calls[0].url, config.endpoint);
|
|
assert.equal(calls[0].options.headers.Authorization, 'Bearer secret');
|
|
// Same body shape as generateSummary, so the test really proves summaries work.
|
|
assert.equal(calls[0].body.model, 'model');
|
|
assert.equal(calls[0].body.stream, false);
|
|
assert.equal(calls[0].body.reasoning_effort, 'none');
|
|
assert.deepEqual(calls[0].body.chat_template_kwargs, { enable_thinking: false });
|
|
assert.equal(calls[0].body.thinking, undefined);
|
|
assert.ok(!JSON.stringify(verdict).includes('secret'));
|
|
});
|
|
|
|
test('llm-test reports thinking still on without calling it a clean pass', async () => {
|
|
const { api } = fixture({ providerFetch: async () => new Response(JSON.stringify({
|
|
choices: [{ message: { content: 'OK' } }],
|
|
usage: { completion_tokens: 236, completion_tokens_details: { reasoning_tokens: 235 } },
|
|
})) });
|
|
const verdict = await api.handle(request(), '/llm-test');
|
|
assert.equal(verdict.ok, true);
|
|
assert.equal(verdict.reasoning_disabled, false);
|
|
assert.equal(verdict.reasoning_tokens, 235);
|
|
assert.match(verdict.warning, /thinking is still enabled/);
|
|
});
|
|
|
|
test('llm-test answers ok:false for unconfigured settings, provider errors and blocked endpoints', async () => {
|
|
const cases = [
|
|
{ label: 'no settings stored', options: { noSettings: true }, match: /No LLM endpoint and model are configured/ },
|
|
{ label: 'endpoint stored without a model', options: { settingsRows: [{ ...config, model: '' }] }, match: /No LLM endpoint and model are configured/ },
|
|
{ label: 'provider 503', options: { providerFetch: async () => new Response(JSON.stringify({ error: { message: 'upstream unavailable' } }), { status: 503 }) }, match: /HTTP 503.*upstream unavailable/ },
|
|
{ label: 'provider network failure', options: { providerFetch: async () => { throw new Error('ECONNREFUSED'); } }, match: /failed or timed out/ },
|
|
{ label: 'endpoint fails SSRF validation', options: { validateEndpoint: async () => { throw new Error('LLM endpoint must resolve to a public IP address'); } }, match: /must resolve to a public IP/ },
|
|
];
|
|
for (const { label, options, match } of cases) {
|
|
const { api, writes } = fixture(options);
|
|
const verdict = await api.handle(request(), '/llm-test');
|
|
assert.equal(verdict.ok, false, label);
|
|
assert.match(verdict.error, match, label);
|
|
assert.equal(verdict.model, /no settings|endpoint stored/.test(label) ? '' : 'model', label);
|
|
assert.equal(writes.length, 0, label);
|
|
assert.ok(!JSON.stringify(verdict).includes('secret'), label);
|
|
}
|
|
// A blocked endpoint must never reach the provider.
|
|
let providerCalled = false;
|
|
const blocked = fixture({ validateEndpoint: async () => { throw new Error('private'); }, providerFetch: async () => { providerCalled = true; return new Response('{}'); } });
|
|
assert.equal((await blocked.api.handle(request(), '/llm-test')).ok, false);
|
|
assert.equal(providerCalled, false);
|
|
});
|
|
|
|
test('llm-test never leaks the stored key and rejects non-POST', async () => {
|
|
const { api } = fixture({ providerFetch: async () => new Response(JSON.stringify({ error: { message: `bad key: ${'secret'}` } }), { status: 401 }) });
|
|
const verdict = await api.handle(request(), '/llm-test');
|
|
assert.equal(verdict.ok, false);
|
|
assert.match(verdict.error, /HTTP 401/);
|
|
assert.ok(!verdict.error.includes('secret'));
|
|
assert.match(verdict.error, /\[redacted\]/);
|
|
await assert.rejects(api.handle(request('GET'), '/llm-test'), error => error.status === 405);
|
|
});
|
|
|
|
test('llm-test requires an authenticated admin', async () => {
|
|
const { api } = fixture({ unauthorized: true });
|
|
await assert.rejects(api.handle(request(), '/llm-test'), error => error.status === 401);
|
|
});
|