import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import { buildApp } from '../../app.js';
describe('graph routes', () => {
    let app;
    let cookie;
    beforeAll(async () => {
        app = await buildApp({ config: { jwtSecret: 'test-secret-graph' } });
        await app.ready();
        const loginRes = await app.inject({
            method: 'POST',
            url: '/api/auth/login',
            payload: { username: 'admin', password: 'admin' },
        });
        const cookies = loginRes.cookies;
        const access = cookies.find((c) => c.name === 'access_token');
        const refresh = cookies.find((c) => c.name === 'refresh_token');
        cookie = `access_token=${access.value}; refresh_token=${refresh.value}`;
    });
    afterAll(async () => {
        await app.close();
    });
    it('GET /api/graph — returns graph with nodes and edges', async () => {
        const res = await app.inject({
            method: 'GET',
            url: '/api/graph',
            headers: { cookie },
        });
        expect(res.statusCode).toBe(200);
        const body = res.json();
        expect(body.nodes).toBeInstanceOf(Array);
        expect(body.edges).toBeInstanceOf(Array);
        // Should have at least the sample-squad
        expect(body.nodes.length).toBeGreaterThanOrEqual(1);
    });
    it('GET /api/graph — includes squad nodes', async () => {
        const res = await app.inject({
            method: 'GET',
            url: '/api/graph',
            headers: { cookie },
        });
        const body = res.json();
        const squads = body.nodes.filter((n) => n.type === 'squad');
        expect(squads.length).toBeGreaterThanOrEqual(1);
        const sampleSquad = squads.find((n) => n.id === 'squad:sample-squad');
        expect(sampleSquad).toBeDefined();
        expect(sampleSquad.label).toBe('Sample Squad');
    });
    it('GET /api/graph — includes agent nodes with squad ref', async () => {
        const res = await app.inject({
            method: 'GET',
            url: '/api/graph',
            headers: { cookie },
        });
        const body = res.json();
        const agents = body.nodes.filter((n) => n.type === 'agent');
        expect(agents.length).toBeGreaterThanOrEqual(1);
        const researcher = agents.find((n) => n.id === 'agent:sample-squad/researcher');
        expect(researcher).toBeDefined();
        expect(researcher.squad).toBe('sample-squad');
    });
    it('GET /api/graph — includes contains edges', async () => {
        const res = await app.inject({
            method: 'GET',
            url: '/api/graph',
            headers: { cookie },
        });
        const body = res.json();
        const containsEdges = body.edges.filter((e) => e.type === 'contains');
        expect(containsEdges.length).toBeGreaterThanOrEqual(1);
        const squadToAgent = containsEdges.find((e) => e.source === 'squad:sample-squad' && e.target === 'agent:sample-squad/researcher');
        expect(squadToAgent).toBeDefined();
    });
    it('GET /api/graph — includes pipeline edges for deliverFrom', async () => {
        const res = await app.inject({
            method: 'GET',
            url: '/api/graph',
            headers: { cookie },
        });
        const body = res.json();
        const delivers = body.edges.filter((e) => e.type === 'delivers');
        // sample-squad has deliverFrom: researcher in writer step
        const delivery = delivers.find((e) => e.source === 'agent:sample-squad/researcher' && e.target === 'agent:sample-squad/writer');
        expect(delivery).toBeDefined();
    });
    it('GET /api/graph — includes skill nodes', async () => {
        const res = await app.inject({
            method: 'GET',
            url: '/api/graph',
            headers: { cookie },
        });
        const body = res.json();
        const skills = body.nodes.filter((n) => n.type === 'skill');
        expect(skills.length).toBeGreaterThanOrEqual(1);
        const webSearch = skills.find((n) => n.id === 'skill:web_search');
        expect(webSearch).toBeDefined();
    });
    it('rejects unauthenticated requests', async () => {
        const res = await app.inject({
            method: 'GET',
            url: '/api/graph',
        });
        expect(res.statusCode).toBe(401);
    });
});
//# sourceMappingURL=graph-routes.test.js.map