Files
FWS-DiscordBot/tests/projects.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

352 lines
12 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import assert from 'node:assert/strict';
import { createServer } from 'node:http';
import { after, before, beforeEach, describe, it } from 'node:test';
import { ACCESS_LEVELS } from '../src/config/constants.js';
import { closeDatabase, getDatabase, initDatabase } from '../src/database/index.js';
import {
DEFAULT_COLUMNS,
addCard,
addColumn,
countCardsInColumn,
createBoard,
getBoardByRepository,
getCard,
getOrgBoard,
listBoards,
listCards,
listCardsForAssignee,
listColumns,
moveCard,
removeBoard,
removeColumn,
} from '../src/database/repositories/project-repository.js';
import { createApiToken } from '../src/database/repositories/web-session-repository.js';
import { hashToken } from '../src/web/auth/session.js';
import { createHandler } from '../src/web/server.js';
/** Kanban boards (M18). */
const GUILD = '';
const board = (overrides = {}) =>
createBoard({ guildId: GUILD, scope: 'repo', repository: 'FWS/FWS-Arch', name: 'FWS Arch', ...overrides });
const columnsOf = (id) => listColumns(id);
describe('project boards', () => {
before(() => initDatabase(':memory:'));
after(() => closeDatabase());
beforeEach(() => getDatabase().exec('DELETE FROM project_boards;'));
it('creates a board with the FWS columns', () => {
const created = board();
assert.deepEqual(
columnsOf(created.id).map((column) => column.name),
DEFAULT_COLUMNS.map((column) => column.name),
);
});
it('marks the last column as the finished one', () => {
const columns = columnsOf(board().id);
assert.equal(columns.at(-1).is_done, true);
assert.equal(columns[0].is_done, false);
});
it('carries the default limit on « En cours »', () => {
const inProgress = columnsOf(board().id).find((column) => column.name === 'En cours');
assert.equal(inProgress.wip_limit, 3);
});
it('allows one board per repository', () => {
board();
assert.throws(() => board(), /UNIQUE/);
});
it('allows a single organization board', () => {
createBoard({ guildId: GUILD, scope: 'org', name: 'Organisation' });
assert.throws(() => createBoard({ guildId: GUILD, scope: 'org', name: 'Autre' }), /UNIQUE/);
});
it('finds a board by its repository and by its scope', () => {
const repo = board();
const org = createBoard({ guildId: GUILD, scope: 'org', name: 'Organisation' });
assert.equal(getBoardByRepository(GUILD, 'FWS/FWS-Arch').id, repo.id);
assert.equal(getOrgBoard(GUILD).id, org.id);
assert.equal(listBoards(GUILD).length, 2);
});
it('takes its columns and cards with it when deleted', () => {
const created = board();
addCard({ boardId: created.id, columnId: columnsOf(created.id)[0].id, title: 'Une tâche' });
removeBoard(created.id);
assert.equal(listColumns(created.id).length, 0);
assert.equal(listCards(created.id).length, 0);
});
});
describe('cards', () => {
let created;
let columns;
before(() => initDatabase(':memory:'));
after(() => closeDatabase());
beforeEach(() => {
getDatabase().exec('DELETE FROM project_boards;');
created = board();
columns = columnsOf(created.id);
});
const add = (title, columnId = columns[0].id) => addCard({ boardId: created.id, columnId, title });
const titles = (columnId) =>
listCards(created.id)
.filter((card) => card.column_id === columnId)
.sort((a, b) => a.position - b.position)
.map((card) => card.title);
it('appends a card at the end of its column', () => {
add('un');
add('deux');
assert.deepEqual(titles(columns[0].id), ['un', 'deux']);
});
it('keeps labels as a list', () => {
const card = addCard({
boardId: created.id,
columnId: columns[0].id,
title: 'Étiquetée',
labels: ['bug', 'urgent'],
});
assert.deepEqual(getCard(card.id).labels, ['bug', 'urgent']);
});
it('moves a card to another column', () => {
const card = add('un');
add('deux');
moveCard(card.id, { columnId: columns[1].id, position: 0 });
assert.deepEqual(titles(columns[0].id), ['deux']);
assert.deepEqual(titles(columns[1].id), ['un']);
});
it('inserts at the requested rank', () => {
add('un');
add('deux');
const third = add('trois');
moveCard(third.id, { columnId: columns[0].id, position: 1 });
assert.deepEqual(titles(columns[0].id), ['un', 'trois', 'deux']);
});
/** Positions must stay contiguous, or the next drop lands somewhere odd. */
it('renumbers both columns after a move', () => {
add('un');
const second = add('deux');
add('trois');
moveCard(second.id, { columnId: columns[1].id, position: 0 });
const left = listCards(created.id).filter((card) => card.column_id === columns[0].id);
assert.deepEqual(left.map((card) => card.position).sort(), [0, 1], 'la colonne quittée garde un trou');
assert.equal(getCard(second.id).position, 0);
});
it('survives a move onto itself', () => {
const card = add('un');
add('deux');
moveCard(card.id, { columnId: columns[0].id, position: 1 });
assert.deepEqual(titles(columns[0].id), ['deux', 'un']);
});
it('counts the cards of a column', () => {
add('un');
add('deux');
assert.equal(countCardsInColumn(columns[0].id), 2);
assert.equal(countCardsInColumn(columns[1].id), 0);
});
it('lists what one member carries, soonest deadline first', () => {
addCard({
boardId: created.id,
columnId: columns[0].id,
title: 'plus tard',
assigneeId: 'u1',
dueAt: '2026-12-01',
});
addCard({
boardId: created.id,
columnId: columns[0].id,
title: 'bientôt',
assigneeId: 'u1',
dueAt: '2026-10-01',
});
addCard({ boardId: created.id, columnId: columns[0].id, title: 'sans date', assigneeId: 'u1' });
addCard({ boardId: created.id, columnId: columns[0].id, title: 'quelqu’un d’autre', assigneeId: 'u2' });
assert.deepEqual(
listCardsForAssignee(GUILD, 'u1').map((card) => card.title),
['bientôt', 'plus tard', 'sans date'],
);
});
it('drops the cards of a removed column', () => {
add('un');
const extra = addColumn({ boardId: created.id, name: 'Bloqué' });
const card = add('deux', extra.id);
removeColumn(extra.id);
assert.equal(getCard(card.id), null);
assert.deepEqual(titles(columns[0].id), ['un']);
});
});
describe('projects over HTTP', () => {
const TOKEN = 'fws_projects-test';
let app;
before(async () => {
initDatabase(':memory:');
createApiToken({
name: 'projects',
tokenHash: hashToken(TOKEN),
prefix: TOKEN.slice(0, 12),
accessLevel: ACCESS_LEVELS.OWNER,
});
app = createServer(createHandler({ client: null, setupOnly: false }));
await new Promise((resolve) => app.listen(0, '127.0.0.1', resolve));
});
after(async () => {
await new Promise((resolve) => app.close(resolve));
closeDatabase();
});
beforeEach(() => getDatabase().exec('DELETE FROM project_boards;'));
const call = (method, path, body) =>
fetch(`http://127.0.0.1:${app.address().port}${path}`, {
method,
headers: { Authorization: `Bearer ${TOKEN}`, 'Content-Type': 'application/json' },
body: body === undefined ? undefined : JSON.stringify(body),
});
it('refuses a board without a session', async () => {
const response = await fetch(`http://127.0.0.1:${app.address().port}/api/projects`);
assert.equal(response.status, 401);
});
it('creates the organization board once', async () => {
assert.equal((await call('POST', '/api/projects', { scope: 'org', name: 'Organisation' })).status, 200);
const again = await call('POST', '/api/projects', { scope: 'org', name: 'Encore' });
assert.equal(again.status, 400);
assert.match((await again.json()).error, /déjà son tableau/);
});
it('refuses a repository that is not owner/name', async () => {
const response = await call('POST', '/api/projects', { scope: 'repo', repository: 'pas-un-depot' });
assert.equal(response.status, 400);
});
it('returns a board with its columns and cards', async () => {
const created = await (await call('POST', '/api/projects', { scope: 'org', name: 'Org' })).json();
const columns = columnsOf(created.id);
await call('POST', `/api/projects/${created.id}/cards`, {
columnId: columns[0].id,
title: 'Première carte',
});
const full = await (await call('GET', `/api/projects/${created.id}`)).json();
assert.equal(full.columns.length, DEFAULT_COLUMNS.length);
assert.equal(full.columns[0].cards[0].title, 'Première carte');
});
it('refuses an issue reference that is not owner/repo#n', async () => {
const created = await (await call('POST', '/api/projects', { scope: 'org', name: 'Org' })).json();
const response = await call('POST', `/api/projects/${created.id}/cards`, {
columnId: columnsOf(created.id)[0].id,
title: 'Liée',
issueRef: 'pas-une-reference',
});
assert.equal(response.status, 400);
assert.match((await response.json()).error, /organisation\/depot#12/);
});
it('reports an overloaded column without refusing the move', async () => {
const created = await (await call('POST', '/api/projects', { scope: 'org', name: 'Org' })).json();
const columns = columnsOf(created.id);
const inProgress = columns.find((column) => column.name === 'En cours');
// Fill it to its limit of three, then push a fourth in. Each step is
// asserted, so a failure cannot cascade into a misleading one later.
const cards = [];
for (const title of ['une', 'deux', 'trois', 'quatre']) {
const response = await call('POST', `/api/projects/${created.id}/cards`, {
columnId: columns[0].id,
title,
});
assert.equal(response.status, 200, `la carte « ${title} » n'a pas été créée`);
cards.push(await response.json());
}
for (const card of cards.slice(0, 3)) {
const response = await call('POST', `/api/projects/${created.id}/cards/${card.id}/move`, {
columnId: inProgress.id,
});
assert.equal(response.status, 200, `le déplacement de « ${card.title} » a échoué`);
assert.equal(
(await response.json()).overloaded,
false,
'la limite ne doit pas être atteinte avant la 4e',
);
}
const response = await call('POST', `/api/projects/${created.id}/cards/${cards[3].id}/move`, {
columnId: inProgress.id,
});
const body = await response.json();
assert.equal(response.status, 200, 'le déplacement doit aboutir malgré la limite');
assert.equal(body.overloaded, true);
assert.equal(countCardsInColumn(inProgress.id), 4);
});
it('keeps a board from losing its last column', async () => {
const created = await (await call('POST', '/api/projects', { scope: 'org', name: 'Org' })).json();
for (const column of columnsOf(created.id).slice(0, 3)) {
await call('DELETE', `/api/projects/${created.id}/columns/${column.id}`);
}
const last = columnsOf(created.id);
assert.equal(last.length, 1);
const response = await call('DELETE', `/api/projects/${created.id}/columns/${last[0].id}`);
assert.equal(response.status, 400);
});
it('refuses to import when Gitea is not configured', async () => {
const created = await (await call('POST', '/api/projects', { scope: 'org', name: 'Org' })).json();
const response = await call('POST', `/api/projects/${created.id}/import`, {
repository: 'FWS/FWS-Arch',
});
// `GITEA_TOKEN` is unset in the tests, so the import cannot run.
assert.equal(response.status, 400);
assert.match((await response.json()).error, /Gitea/);
});
it('refuses a repository that is not owner/name', async () => {
const created = await (await call('POST', '/api/projects', { scope: 'org', name: 'Org' })).json();
const response = await call('POST', `/api/projects/${created.id}/import`, { repository: 'nope' });
assert.equal(response.status, 400);
});
it('asks for the name before deleting a board', async () => {
const created = await (await call('POST', '/api/projects', { scope: 'org', name: 'Org' })).json();
assert.equal((await call('DELETE', `/api/projects/${created.id}`, { confirm: 'faux' })).status, 400);
assert.equal((await call('DELETE', `/api/projects/${created.id}`, { confirm: 'Org' })).status, 200);
});
});