// Tests for report_problem, with a fake GitHub: no network. import { test } from 'node:test'; import assert from 'node:assert/strict'; import { makeReporter, makeGitHubPost, MAX_TITLE_CHARS } from './report.js'; test('a report opens an issue assigned to whoever looks after the box', async () => { const calls = []; const report = makeReporter(async (path, body) => { calls.push([path, body]); return { status: 201, json: { number: 7, html_url: 'https://github.com/o/r/issues/7' } }; }, 'o/r', 'keeper'); assert.equal(await report(' Input menu closes ', 'Tried 3 presses.'), 'reported as #7: https://github.com/o/r/issues/7'); assert.deepEqual(calls, [['/repos/o/r/issues', { title: 'Input menu closes', body: 'Tried 3 presses.\n\n— reported by the agent at the TV', assignees: ['keeper'], }]]); }); test('GitHub refusing is a failure, with its reason', async () => { const report = makeReporter(async () => ({ status: 403, json: { message: 'Resource not accessible' } }), 'o/r', 'keeper'); await assert.rejects(report('t', 'd'), /403: Resource not accessible/); }); test('a report needs a title, and not a novel', async () => { const report = makeReporter(async () => assert.fail('no post'), 'o/r', 'keeper'); await assert.rejects(report('', 'd'), /title/); await assert.rejects(report('x'.repeat(MAX_TITLE_CHARS + 1), 'd'), /title over/); await assert.rejects(report('t', undefined), /details/); }); test('the post carries the token and names the client', async () => { let seen; const post = makeGitHubPost(async (url, init) => { seen = { url, ...init }; return { status: 201, json: async () => ({ number: 1 }) }; }, 's3cret'); assert.deepEqual(await post('/repos/o/r/issues', { title: 't' }), { status: 201, json: { number: 1 } }); assert.equal(seen.url, 'https://api.github.com/repos/o/r/issues'); assert.equal(seen.headers.authorization, 'Bearer s3cret'); assert.match(seen.headers['user-agent'], /pdtv26-brain/); });