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(/