Files
FWS-DiscordBot/tests/projects-import.test.js
nocodeandClaude Opus 5 1c21f6ff53 feat(projects): pull Gitea issues onto any board
The import only existed on a repository board and always landed in the first
column. The organization board is exactly where issues from several
repositories belong together, so the button now lives on every board and
asks which repository, which column, and whether closed issues come too.

An issue already on the board is skipped rather than duplicated, so the
button survives being pressed twice — which the tests check against a
stand-in forge, along with the due date and labels carried over.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-24 14:03:06 +02:00

146 lines
5.0 KiB
JavaScript

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');
});
});