import { describe, it, expect } from 'vitest';
import WebSocket from 'ws';
import { buildApp } from '../src/app.js';
import { MemoryStore } from '../src/device-grant/store.js';
describe('GET /ws', () => {
    it('accepts and acks a hello', async () => {
        const app = await buildApp({ deviceGrantStore: new MemoryStore() });
        await app.listen({ port: 0, host: '127.0.0.1' });
        const addr = app.server.address();
        if (!addr || typeof addr === 'string')
            throw new Error('no port');
        const url = `ws://127.0.0.1:${addr.port}/ws`;
        const ws = new WebSocket(url);
        const messages = [];
        await new Promise((resolve, reject) => {
            ws.on('open', () => ws.send(JSON.stringify({ type: 'hello' })));
            ws.on('message', (raw) => {
                const msg = JSON.parse(raw.toString());
                messages.push(msg);
                if (msg.type === 'ack')
                    resolve();
            });
            ws.on('error', reject);
            setTimeout(() => reject(new Error('timeout')), 3000);
        });
        expect(messages.some((m) => m.type === 'ack')).toBe(true);
        ws.close();
        await app.close();
    });
});
//# sourceMappingURL=ws.test.js.map