import assert from 'node:assert/strict'; import { createServer } from 'node:http'; import { after, before, beforeEach, describe, it } from 'node:test'; /** * Importing Gitea issues onto a board (M18). * * A stand-in forge answers on a real socket, and the environment is set before * the modules that read it are loaded — hence the dynamic imports. */ let issues = []; const forge = createServer((request, response) => { if (!request.url.includes('/issues')) { response.writeHead(404).end(); return; } const state = new URL(request.url, 'http://x').searchParams.get('state'); const body = state === 'all' ? issues : issues.filter((issue) => issue.state === 'open'); response.writeHead(200, { 'Content-Type': 'application/json' }).end(JSON.stringify(body)); }); await new Promise((resolve) => forge.listen(0, '127.0.0.1', resolve)); process.env.GITEA_URL = `http://127.0.0.1:${forge.address().port}`; process.env.GITEA_TOKEN = 'jeton-de-test'; process.env.GITEA_OWNER = 'FWS'; const { closeDatabase, getDatabase, initDatabase } = await import('../src/database/index.js'); const { createBoard, listCards, listColumns } = await import( '../src/database/repositories/project-repository.js' ); const { createApiToken } = await import('../src/database/repositories/web-session-repository.js'); const { hashToken } = await import('../src/web/auth/session.js'); const { createHandler } = await import('../src/web/server.js'); const TOKEN = 'fws_import-test'; describe('importing issues from Gitea', () => { let app; let board; let columns; before(async () => { initDatabase(':memory:'); createApiToken({ name: 'import', tokenHash: hashToken(TOKEN), prefix: 'fws_impo', accessLevel: 3 }); app = createServer(createHandler({ client: null })); await new Promise((resolve) => app.listen(0, '127.0.0.1', resolve)); }); after(async () => { await new Promise((resolve) => app.close(resolve)); await new Promise((resolve) => forge.close(resolve)); closeDatabase(); }); beforeEach(() => { getDatabase().exec('DELETE FROM project_boards;'); board = createBoard({ guildId: '', scope: 'org', name: 'Organisation' }); columns = listColumns(board.id); issues = [ { number: 1, title: 'Corriger le démarrage', state: 'open', body: 'Détails', labels: [{ name: 'bug' }] }, { number: 2, title: 'Documenter le wiki', state: 'open', body: null, labels: [] }, { number: 3, title: 'Ancienne demande', state: 'closed', body: null, labels: [] }, ]; }); const call = (path, body) => fetch(`http://127.0.0.1:${app.address().port}${path}`, { method: 'POST', headers: { Authorization: `Bearer ${TOKEN}`, 'Content-Type': 'application/json' }, body: JSON.stringify(body), }); const importInto = (body = {}) => call(`/api/projects/${board.id}/import`, { repository: 'FWS/FWS-Arch', ...body }).then((response) => response.json(), ); it('creates one card per open issue', async () => { const result = await importInto(); assert.equal(result.imported, 2); assert.equal(result.column, 'À faire'); assert.deepEqual( listCards(board.id) .map((card) => card.title) .sort(), ['Corriger le démarrage', 'Documenter le wiki'], ); }); it('records the issue each card came from', async () => { await importInto(); const card = listCards(board.id).find((entry) => entry.title === 'Corriger le démarrage'); assert.equal(card.issue_ref, 'FWS/FWS-Arch#1'); assert.deepEqual(card.labels, ['bug']); assert.match(card.body, /Détails/); }); /** The button has to survive being pressed twice. */ it('skips what is already on the board', async () => { await importInto(); const second = await importInto(); assert.equal(second.imported, 0); assert.equal(second.skipped, 2); assert.equal(listCards(board.id).length, 2); }); it('brings the closed issues in when asked', async () => { const result = await importInto({ state: 'all' }); assert.equal(result.imported, 3); }); it('imports into the chosen column', async () => { const review = columns.find((column) => column.name === 'En revue'); const result = await importInto({ columnId: review.id }); assert.equal(result.column, 'En revue'); assert.ok(listCards(board.id).every((card) => card.column_id === review.id)); }); /** Importing straight into « Terminé » is never what anyone means. */ it('never imports into a column of another board', async () => { const other = createBoard({ guildId: '', scope: 'repo', repository: 'FWS/site', name: 'Site' }); const foreign = listColumns(other.id)[0]; const result = await importInto({ columnId: foreign.id }); assert.equal(result.column, 'À faire'); assert.equal(listCards(other.id).length, 0); }); it('reports what it found even when nothing is new', async () => { issues = []; const result = await importInto(); assert.equal(result.found, 0); assert.equal(result.imported, 0); assert.equal(result.repository, 'FWS/FWS-Arch'); }); });