import { afterAll, beforeEach, describe, expect, it } from 'vitest';
import { ensureRepos } from '../src/domain/issue-index.js';
import { acceptSubmission } from '../src/domain/submissions.js';
import { createInvite } from '../src/domain/workspaces.js';
import { parseTriageConfig, saveTriageConfig, setApiKey, setGitHubTriageEnabled, setTriageEnabled } from '../src/triage/settings.js';
import { form, get, grant, makeAdmin, makeApp, makeWorkspace, resetDb, testDb, type Admin, type ConfiguredWorkspace, type TestEnv } from './helpers.js';
const KEY = 'sk-ant-test-key-0123456789abcdef';
let env: TestEnv;
let ws: ConfiguredWorkspace;
let mod: Admin;
const wid = () => ws.workspace.id;
const w = () => `/w/${ws.workspace.id}`;
const page = (url: string, who: Admin = mod) => env.server.inject({ method: 'GET', url: `${w()}${url}`, ...get(who) });
beforeEach(async () => {
await resetDb();
env = await makeApp();
ws = await makeWorkspace(env);
mod = await makeAdmin(env, 'mia');
await env.server.inject({ method: 'POST', url: `/invite/${await createInvite(env.db, wid(), ws.owner.user.id, 'moderator')}`, ...form(mod) });
});
afterAll(async () => {
await testDb().end();
});
describe('old addresses', () => {
it('move for good on GET and HEAD, keeping the query string', async () => {
const cases: [string, string][] = [
['/tickets/triage', '/triage/actions'],
['/tickets/triage?x=1', '/triage/actions?x=1'],
['/tickets/triage/labels', '/triage/labels'],
['/tickets/triage/prompt/full', '/triage/prompt/full'],
['/tickets/triage/runs/0b7e5d2c-1111-4222-8333-444455556666?show=changed', '/triage/runs/0b7e5d2c-1111-4222-8333-444455556666?show=changed'],
['/tickets/triage/activity?source=review', '/triage/activity?source=review'],
['/tickets/triage/bulk/status', '/triage/bulk/status'],
['/tickets/issues', '/issues'],
['/tickets/issues?state=closed&q=crash', '/issues?state=closed&q=crash'],
['/tickets/issues/triage?show=all', '/issues/github?show=all'],
['/tickets/issues/sync', '/issues/sync'],
['/tickets/issues/123/45', '/issues/123/45'],
['/tickets/triage/', '/triage/actions'],
['/tickets/issues/', '/issues'],
];
for (const [from, to] of cases) {
const res = await page(from, ws.owner);
expect(res.statusCode, from).toBe(301);
expect(res.headers.location, from).toBe(w() + to);
}
const head = await env.server.inject({ method: 'HEAD', url: `${w()}/tickets/issues/triage`, ...get(ws.owner) });
expect(head.statusCode).toBe(301);
expect(head.headers.location).toBe(`${w()}/issues/github`);
// The new pages are there.
for (const url of ['/triage', '/triage/actions', '/issues', '/issues/github', '/issues/sync']) expect((await page(url, ws.owner)).statusCode, url).toBe(200);
});
it('send forms on with 307, so the body is posted again to the new address', async () => {
await setApiKey(env.db, env.deps.box, wid(), KEY, ws.owner.user.id);
const cases: [string, string][] = [
['/tickets/triage', '/triage/actions'],
['/tickets/triage/enabled', '/triage/enabled'],
['/tickets/triage/runs/abc/apply', '/triage/runs/abc/apply'],
['/tickets/issues/sync', '/issues/sync'],
['/tickets/issues/123/45/triage/apply', '/issues/123/45/triage/apply'],
];
for (const [from, to] of cases) {
const res = await env.server.inject({ method: 'POST', url: `${w()}${from}`, ...form(ws.owner, { set: '1', enabled: '1' }) });
expect(res.statusCode, from).toBe(307);
expect(res.headers.location, from).toBe(w() + to);
}
// Following it does what the old address did.
const moved = await env.server.inject({ method: 'POST', url: `${w()}/tickets/triage/enabled`, ...form(ws.owner, { set: '1', enabled: '1' }) });
const res = await env.server.inject({ method: 'POST', url: moved.headers.location as string, ...form(ws.owner, { set: '1', enabled: '1' }) });
expect(res.statusCode).toBe(302);
expect(res.headers.location).toBe(`${w()}/triage/actions`);
expect((await env.db.query('SELECT enabled FROM triage_settings WHERE workspace_id = $1', [wid()])).rows[0].enabled).toBe(true);
// A form without its token is still refused first.
expect((await env.server.inject({ method: 'POST', url: `${w()}/tickets/triage/enabled`, headers: get(ws.owner).headers, payload: {} })).statusCode).toBe(403);
});
it('stay inside the workspace, whatever the rest of the address is', async () => {
const res = await page('/tickets/triage/%2F%2Fexample.com', ws.owner);
expect(res.statusCode).toBe(301);
expect(res.headers.location?.startsWith(`${w()}/triage/`)).toBe(true);
});
});
describe('sidebar', () => {
it('groups pages into Setup, Tools and Workspace, and highlights the tool of the page', async () => {
const overview = (await page('', ws.owner)).body;
const labels = [...overview.matchAll(/
([^<]+)<\/div>/g)].map((m) => m[1]);
expect(labels).toEqual(['Setup', 'Tools', 'Workspace']);
const tools = [...overview.matchAll(/class="nav-item"[^>]*>.*?
([^<]+)<\/span>/g)].map((m) => m[1]);
expect(tools).toEqual(['Overview', 'GitHub', 'Discord', 'Apps & repositories', 'Analytics', 'Repo status', 'Discord tickets', 'GitHub issues', 'AI triage', 'Moderation', 'Contributor roles', 'Announcements', 'Bot features', 'MCP', 'Members & permissions', 'Settings', 'Deployment', 'Server health']);
const active: [string, string][] = [
['/tickets', '/tickets'], ['/tickets/files', '/tickets'], ['/tickets/settings', '/tickets'],
['/issues/sync', '/issues'], ['/issues/github', '/issues'], ['/repos', '/repos'], ['/repos/settings', '/repos'], ['/analytics', '/analytics'],
['/triage/overview', '/triage/overview'], ['/triage', '/triage/overview'], ['/triage/labels', '/triage/overview'], ['/triage/prompt/full', '/triage/overview'], ['/triage/activity', '/triage/overview'],
['/contributors/roles', '/contributors'], ['/apps', '/apps'], ['/members', '/members'],
];
for (const [url, item] of active) {
const body = (await page(url, ws.owner)).body;
expect(body, url).toContain(`href="${w()}${item}" class="nav-item" aria-current="page"`);
expect(body.match(/aria-current="page"/g)?.length, url).toBeGreaterThanOrEqual(1);
expect(body.match(/class="nav-item" aria-current="page"/g), url).toHaveLength(1);
}
});
it('opens each tool on the first page the person can see, and locks tools they can’t', async () => {
await grant(env, wid(), mod.user.id, { 'triage.activity': 'view', 'issues-sync': 'view' });
const body = (await page('/tickets')).body;
expect(body).toContain(`href="${w()}/tickets" class="nav-item" aria-current="page"`);
expect(body).toContain(`href="${w()}/issues/sync" class="nav-item"`);
expect(body).toContain(`href="${w()}/triage/activity" class="nav-item"`);
expect(body).toContain('');
expect(body).toContain('title="Only admins can open Members & permissions. Ask an admin if you need access."');
// A tool's own address lands on that page too.
for (const [url, to] of [['/triage', '/triage/activity'], ['/issues', '/issues/sync']]) {
const res = await page(url!);
expect(res.statusCode, url).toBe(302);
expect(res.headers.location, url).toBe(w() + to);
}
expect((await page('/contributors')).statusCode).toBe(403);
expect((await page('/triage/actions')).statusCode).toBe(403);
});
});
describe('tool headers', () => {
it('show every page as a tab, locked when the person can’t open it, and say View only', async () => {
await grant(env, wid(), mod.user.id, { 'triage.activity': 'view', 'triage.prompt': 'view', files: 'edit' });
const activity = (await page('/triage/activity')).body;
expect(activity).toContain('AI triage
');
expect(activity).toContain('