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, listColumns, searchCards, } from '../src/database/repositories/project-repository.js'; import { createApiToken } from '../src/database/repositories/web-session-repository.js'; import { createPage } from '../src/database/repositories/wiki-repository.js'; import { hashToken } from '../src/web/auth/session.js'; import { createHandler } from '../src/web/server.js'; /** * Global search behind the command palette (M19). * * Gitea is deliberately left unconfigured here: the point is that the local * sources answer on their own, so a forge that is down costs the palette its * repositories and nothing else. */ const GUILD = ''; const TOKEN = 'fws_search-test'; describe('global search', () => { let app; let board; let columns; before(async () => { initDatabase(':memory:'); createApiToken({ name: 'recherche', tokenHash: hashToken(TOKEN), prefix: TOKEN.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(() => { getDatabase().exec('DELETE FROM project_boards; DELETE FROM wiki_pages;'); board = createBoard({ guildId: GUILD, scope: 'org', name: 'Organisation' }); columns = listColumns(board.id); }); const search = (term) => fetch(`http://127.0.0.1:${app.address().port}/api/search?q=${encodeURIComponent(term)}`, { headers: { Authorization: `Bearer ${TOKEN}` }, }); const results = (term) => search(term).then((response) => response.json()); const card = (title, overrides = {}) => addCard({ boardId: board.id, columnId: columns[0].id, title, ...overrides }); const page = (title, overrides = {}) => createPage({ slug: title.toLowerCase().replaceAll(' ', '-'), title, published: true, category: 'Guides', ...overrides, }); it('refuses a visitor without a session', async () => { const response = await fetch(`http://127.0.0.1:${app.address().port}/api/search?q=test`); assert.equal(response.status, 401); }); /** One or two letters match half the organization, which helps nobody. */ it('stays silent on a term too short to mean anything', async () => { card('Pterodactyl'); assert.deepEqual((await results('p')).results, []); }); it('finds a card and says which board it is on', async () => { card('Corriger le démarrage'); const [found] = (await results('démarrage')).results; assert.equal(found.kind, 'card'); assert.equal(found.title, 'Corriger le démarrage'); assert.equal(found.subtitle, 'Organisation · À faire'); assert.equal(found.href, `#/projects/${board.id}`); }); it('finds a published wiki page at its public address', async () => { page('Installer FWS Arch', { summary: 'Le guide pas à pas.' }); const [found] = (await results('Installer')).results; assert.equal(found.kind, 'wiki'); assert.equal(found.href, '/wiki/installer-fws-arch'); assert.equal(found.external, true); }); it('leaves an unpublished page out of the results', async () => { page('Brouillon interne', { published: false }); assert.deepEqual((await results('Brouillon')).results, []); }); /** The legal pages live at the root, not under /wiki. */ it('sends a root page to its own address', async () => { page('Conditions', { slug: 'terms-of-service' }); const [found] = (await results('Conditions')).results; assert.equal(found.href, '/terms-of-service'); }); it('gathers several kinds under one answer', async () => { card('Sauvegarde automatique'); page('Sauvegarde et restauration'); const kinds = (await results('Sauvegarde')).results.map((entry) => entry.kind); assert.deepEqual(kinds.slice().sort(), ['card', 'wiki']); }); it('puts the local results first', async () => { card('Wiki à relire'); page('Wiki public'); assert.equal((await results('Wiki')).results[0].kind, 'card'); }); it('answers with nothing rather than failing when nothing matches', async () => { const body = await results('introuvable-xyz'); assert.deepEqual(body.results, []); assert.equal(body.term, 'introuvable-xyz'); }); /** Gitea is not configured here; the palette must still answer. */ it('answers although the forge is absent', async () => { card('Déploiement'); const response = await search('Déploiement'); assert.equal(response.status, 200); assert.equal((await response.json()).results.length, 1); }); }); /** The query behind the palette's card results, exercised without HTTP. */ describe('searching the cards directly', () => { let board; let columns; before(() => initDatabase(':memory:')); after(() => closeDatabase()); beforeEach(() => { getDatabase().exec('DELETE FROM project_boards;'); board = createBoard({ guildId: GUILD, scope: 'org', name: 'Organisation' }); columns = listColumns(board.id); }); const put = (title, overrides = {}) => addCard({ boardId: board.id, columnId: columns[0].id, title, ...overrides }); it('matches the title', () => { put('Corriger le démarrage'); put('Documenter le wiki'); assert.deepEqual( searchCards(GUILD, 'démarrage').map((entry) => entry.title), ['Corriger le démarrage'], ); }); it('matches the body, the labels and the issue', () => { put('Sans rapport', { body: 'contient pterodactyl' }); put('Étiquetée', { labels: ['urgent'] }); put('Liée', { issueRef: 'FWS/FWS-Arch#42' }); assert.equal(searchCards(GUILD, 'pterodactyl').length, 1); assert.equal(searchCards(GUILD, 'urgent').length, 1); assert.equal(searchCards(GUILD, '#42').length, 1); }); /** A finished card is still a result, just never the first one. */ it('puts the finished cards last', () => { addCard({ boardId: board.id, columnId: columns.at(-1).id, title: 'sujet fini' }); put('sujet en cours'); assert.deepEqual( searchCards(GUILD, 'sujet').map((entry) => entry.title), ['sujet en cours', 'sujet fini'], ); }); it('honours the limit', () => { for (let index = 0; index < 5; index += 1) put(`sujet ${index}`); assert.equal(searchCards(GUILD, 'sujet', 2).length, 2); }); });