import { describe, it, expect } from 'vitest';
import { jwtVerify } from 'jose';
import { buildApp } from '../src/app.js';
import { MemoryStore } from '../src/device-grant/store.js';
import { env } from '../src/env.js';
const secret = new TextEncoder().encode(env.JWT_SECRET);
describe('Device Authorization Grant', () => {
    it('completes the full flow', async () => {
        const store = new MemoryStore();
        const app = await buildApp({ deviceGrantStore: store });
        const start = await app.inject({
            method: 'POST',
            url: '/auth/device',
            payload: { sessionId: 'session-test-12345' },
        });
        expect(start.statusCode).toBe(200);
        const startBody = start.json();
        expect(startBody.device_code).toBeDefined();
        expect(startBody.user_code).toMatch(/^[A-Z0-9]+$/);
        expect(startBody.qr_data_url).toContain('data:image/png');
        const pending = await app.inject({
            method: 'POST',
            url: '/auth/device/token',
            payload: { device_code: startBody.device_code },
        });
        expect(pending.statusCode).toBe(400);
        expect(pending.json().error).toBe('authorization_pending');
        const approve = await app.inject({
            method: 'POST',
            url: '/auth/device/approve',
            payload: { user_code: startBody.user_code, user_id: 'user-123' },
        });
        expect(approve.statusCode).toBe(200);
        const token = await app.inject({
            method: 'POST',
            url: '/auth/device/token',
            payload: { device_code: startBody.device_code },
        });
        expect(token.statusCode).toBe(200);
        const tokenBody = token.json();
        expect(tokenBody.token_type).toBe('Bearer');
        const verified = await jwtVerify(tokenBody.access_token, secret);
        expect(verified.payload.sub).toBe('user-123');
        await app.close();
    });
});
//# sourceMappingURL=device-grant.test.js.map