import { mkdtempSync, rmSync } from 'node:fs'; import { tmpdir } from 'node:os'; import path from 'node:path'; import { afterEach, describe, expect, it } from 'vitest'; import { createApp } from '../src/app.js'; import { bootstrapAdmin } from '../src/auth/bootstrap.js'; import { loadConfig } from '../src/config.js'; import { openAppDatabase } from '../src/db.js'; import { devEnv } from './env.js'; const NOW = '2026-10-14T18:00:00.000Z'; const ORIGIN = 'http://127.0.0.1:3000'; const dirs: string[] = []; const databases: { close: () => void }[] = []; afterEach(() => { for (const database of databases.splice(0)) database.close(); for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true }); }); describe('administrators', () => { it('asks a signed-out visitor to sign in', async () => { const { app } = await buildApp(); const page = await app.request('/admin/admins'); expect(page.status).toBe(303); expect(page.headers.get('location')).toBe('/login'); const posted = await app.request('/admin/admins', { method: 'POST', headers: { origin: ORIGIN, 'content-type': 'application/x-www-form-urlencoded' }, body: new URLSearchParams({ name: 'Ada Lovelace', email: 'ada@example.invalid' }), }); expect(posted.status).toBe(303); expect(posted.headers.get('location')).toBe('/login'); }); it('lists the first administrator and adds another who can sign in', async () => { const { app, db } = await buildApp(); const cookie = await signIn(app, db, 'coordinator@example.invalid'); const home = await app.request('/admin', { headers: { cookie } }); const homeHtml = await home.text(); expect(homeHtml).toContain('