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 { addCard, createBoard, listAssignees, listCardsForAssignee, listColumns, } 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'; /** « Mes tâches » — cards gathered across every board (M19). */ const GUILD = ''; const ME = '111'; const OTHER = '222'; /** Whole days from today, in the `YYYY-MM-DD` shape the cards store. */ const dayOffset = (days) => { const date = new Date(); date.setDate(date.getDate() + days); return date.toISOString().slice(0, 10); }; let board; let columns; function seed() { getDatabase().exec('DELETE FROM project_boards;'); board = createBoard({ guildId: GUILD, scope: 'repo', repository: 'FWS/FWS-Arch', name: 'FWS Arch' }); columns = listColumns(board.id); } const put = (title, overrides = {}) => addCard({ boardId: board.id, columnId: columns[0].id, title, ...overrides }); describe('cards gathered by assignee', () => { before(() => initDatabase(':memory:')); after(() => closeDatabase()); beforeEach(seed); it('carries the column each card sits in', () => { put('Une tâche', { assigneeId: ME }); const [task] = listCardsForAssignee(GUILD, ME); assert.equal(task.column_name, 'À faire'); assert.equal(task.column_is_done, false); assert.equal(task.board_name, 'FWS Arch'); assert.equal(task.repository, 'FWS/FWS-Arch'); }); /** Finishing a card from the list is one move, so the target travels with it. */ it('names the column that would finish the card', () => { put('Une tâche', { assigneeId: ME }); const done = columns.at(-1); assert.equal(listCardsForAssignee(GUILD, ME)[0].done_column_id, done.id); }); it('marks a card already in the finished column', () => { addCard({ boardId: board.id, columnId: columns.at(-1).id, title: 'Livrée', assigneeId: ME }); assert.equal(listCardsForAssignee(GUILD, ME)[0].column_is_done, true); }); it('orders by deadline, undated last', () => { put('plus tard', { assigneeId: ME, dueAt: dayOffset(9) }); put('sans date', { assigneeId: ME }); put('demain', { assigneeId: ME, dueAt: dayOffset(1) }); assert.deepEqual( listCardsForAssignee(GUILD, ME).map((task) => task.title), ['demain', 'plus tard', 'sans date'], ); }); it('ignores what belongs to someone else', () => { put('à moi', { assigneeId: ME }); put('à l’autre', { assigneeId: OTHER }); assert.deepEqual( listCardsForAssignee(GUILD, ME).map((task) => task.title), ['à moi'], ); }); it('lists who carries cards, finished ones excluded from the open count', () => { put('ouverte', { assigneeId: ME }); addCard({ boardId: board.id, columnId: columns.at(-1).id, title: 'close', assigneeId: ME }); put('autre', { assigneeId: OTHER }); const [mine] = listAssignees(GUILD).filter((entry) => entry.assigneeId === ME); assert.equal(mine.total, 2); assert.equal(mine.open, 1); }); it('leaves unassigned cards out of the roster', () => { put('orpheline'); assert.deepEqual(listAssignees(GUILD), []); }); }); describe('my tasks over HTTP', () => { const TOKEN = 'fws_tasks-test'; const ANONYMOUS = 'fws_tasks-anon'; let app; before(async () => { initDatabase(':memory:'); createApiToken({ name: 'tasks', tokenHash: hashToken(TOKEN), prefix: TOKEN.slice(0, 12), accessLevel: ACCESS_LEVELS.STAFF, createdBy: ME, }); // A token with nobody behind it: there is no « me » to answer for. createApiToken({ name: 'anonyme', tokenHash: hashToken(ANONYMOUS), prefix: ANONYMOUS.slice(0, 12), accessLevel: ACCESS_LEVELS.STAFF, }); 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(seed); const call = (path, token = TOKEN) => fetch(`http://127.0.0.1:${app.address().port}${path}`, { headers: { Authorization: `Bearer ${token}` }, }); const tasks = (query = '') => call(`/api/me/tasks${query}`).then((response) => response.json()); it('refuses a visitor without a session', async () => { const response = await fetch(`http://127.0.0.1:${app.address().port}/api/me/tasks`); assert.equal(response.status, 401); }); it('answers with the caller’s own cards', async () => { put('à moi', { assigneeId: ME }); put('à l’autre', { assigneeId: OTHER }); const body = await tasks(); assert.equal(body.self, true); assert.equal(body.assigneeId, ME); assert.deepEqual( body.cards.map((task) => task.title), ['à moi'], ); }); it('counts what is late, due today and finished', async () => { put('en retard', { assigneeId: ME, dueAt: dayOffset(-2) }); put('aujourd’hui', { assigneeId: ME, dueAt: dayOffset(0) }); put('plus tard', { assigneeId: ME, dueAt: dayOffset(30) }); addCard({ boardId: board.id, columnId: columns.at(-1).id, title: 'faite', assigneeId: ME }); const { counts } = await tasks(); assert.deepEqual(counts, { total: 4, open: 3, done: 1, overdue: 1, dueToday: 1 }); }); /** A finished card must not be counted as late, however old its deadline. */ it('never calls a finished card late', async () => { addCard({ boardId: board.id, columnId: columns.at(-1).id, title: 'livrée en retard', assigneeId: ME, dueAt: dayOffset(-40), }); const { counts } = await tasks(); assert.equal(counts.overdue, 0); assert.equal(counts.done, 1); }); it('tells the page where each card lives and how to finish it', async () => { put('une carte', { assigneeId: ME }); const [task] = (await tasks()).cards; assert.equal(task.boardId, board.id); assert.equal(task.boardName, 'FWS Arch'); assert.equal(task.columnName, 'À faire'); assert.equal(task.isDone, false); assert.equal(task.doneColumnId, columns.at(-1).id); }); it('looks at another member when asked', async () => { put('à l’autre', { assigneeId: OTHER }); const body = await tasks(`?assignee=${OTHER}`); assert.equal(body.self, false); assert.equal(body.assigneeId, OTHER); assert.equal(body.cards.length, 1); }); it('offers the members who carry cards', async () => { put('à moi', { assigneeId: ME }); put('à l’autre', { assigneeId: OTHER }); const ids = (await tasks()).assignees.map((entry) => entry.assigneeId).sort(); assert.deepEqual(ids, [ME, OTHER]); }); it('explains itself when the caller has no identity', async () => { const response = await call('/api/me/tasks', ANONYMOUS); assert.equal(response.status, 400); assert.match((await response.json()).error, /membre/i); }); it('skips the forge when only the counters are wanted', async () => { put('liée', { assigneeId: ME, issueRef: 'FWS/FWS-Arch#1' }); assert.deepEqual((await tasks('?issues=0')).issues, {}); }); });