const { test } = require('node:test'); const assert = require('node:assert/strict'); const { validateSettings, generateSummary } = require('../src/feedback-summary'); const settings = { endpoint: 'https://llm.example/v1/chat/completions', model: 'test-model', api_key: 'test-secret' }; test('validates endpoint, model and credential input', () => { assert.equal(validateSettings(settings).model, 'test-model'); for (const endpoint of ['http://localhost:11434', 'https://user:secret@example.com', 'https://example.com/?key=secret', 'file:///etc/passwd']) { assert.throws(() => validateSettings({ ...settings, endpoint })); } assert.throws(() => validateSettings({ ...settings, model: '' })); assert.throws(() => validateSettings({ ...settings, api_key: 'secret\r\nHeader: value' })); }); test('does not call LLM without feedback', async () => { await assert.rejects(generateSummary(settings, { feedback: [] }, () => { throw new Error('Must not call'); }), /No feedback/); }); test('sends only ratings and comments and reads OpenAI-compatible output', async () => { const result = await generateSummary(settings, { scope: 'session', title: 'Session', feedback: [{ rating: 5, comment: 'Useful', contact_email: 'private@example.com', id: 'private-id' }], }, async (url, options) => { assert.equal(url, settings.endpoint); assert.equal(options.redirect, 'error'); assert.equal(options.headers.Authorization, 'Bearer test-secret'); const request = JSON.parse(options.body); assert.equal(request.model, 'test-model'); assert.ok(!options.body.includes('private@example.com')); assert.ok(!options.body.includes('private-id')); assert.deepEqual(JSON.parse(request.messages[1].content).responses, [{ rating: 5, comment: 'Useful' }]); return new Response(JSON.stringify({ choices: [{ message: { content: ' Useful session. ' } }] })); }); assert.equal(result, 'Useful session.'); }); test('disables model thinking with both supported fields', async () => { let request; await generateSummary(settings, { scope: 'event', title: 'Event', feedback: [{ rating: 4 }] }, async (_url, options) => { request = JSON.parse(options.body); return new Response(JSON.stringify({ choices: [{ message: { content: 'Summary.' } }] })); }); assert.equal(request.stream, false); assert.equal(request.reasoning_effort, 'none'); assert.deepEqual(request.chat_template_kwargs, { enable_thinking: false }); // The ignored shape must never come back — it looks like success but does nothing. assert.equal(request.thinking, undefined); assert.ok(!('max_tokens' in request)); }); test('rejects provider errors, malformed output and oversized inputs', async () => { const input = { scope: 'event', title: 'Event', feedback: [{ rating: 3 }] }; await assert.rejects(generateSummary(settings, input, async () => new Response('secret error', { status: 401 })), /HTTP 401/); await assert.rejects(generateSummary(settings, input, async () => new Response('not JSON')), /invalid JSON/); await assert.rejects(generateSummary(settings, input, async () => new Response('{}')), /invalid summary/); await assert.rejects(generateSummary(settings, { ...input, feedback: [{ rating: 1, comment: 'x'.repeat(200001) }] }), /200 KB/); });