"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const api_1 = require("@opentelemetry/api");
const vitest_1 = require("vitest");
const socket_js_1 = require("./socket.js");
const socketWithClientAuth_js_1 = require("./socketWithClientAuth.js");
const PING_TIMEOUT_CLOSE_REASON = 'Ping timeout';
(0, vitest_1.describe)('ISocket', () => {
    (0, vitest_1.it)('preserves the transport-close marker when rejecting pending requests with a reason', async () => {
        const ws = {
            addEventListener: vitest_1.vi.fn(),
            close: vitest_1.vi.fn(),
            send: vitest_1.vi.fn()
        };
        const socket = new socket_js_1.ISocket(ws, {}, [], { onClose: vitest_1.vi.fn() });
        const pendingRequest = socket.request('editor.ping', {});
        socket.closeAndRejectPendingRequests(PING_TIMEOUT_CLOSE_REASON);
        await (0, vitest_1.expect)(pendingRequest).rejects.toMatchObject({
            code: 8,
            message: `[internal] Connection closed: ${PING_TIMEOUT_CLOSE_REASON}`
        });
        (0, vitest_1.expect)(ws.close).toHaveBeenCalledWith(socket_js_1.INTENTIONAL_CLOSE_CODE, PING_TIMEOUT_CLOSE_REASON);
    });
});
(0, vitest_1.describe)('ISocketWithClientAuth', () => {
    (0, vitest_1.it)('still sends a refreshed token when an older in-flight request resolves after setAuthorization', async () => {
        let messageHandler;
        const sentMessages = [];
        const ws = {
            addEventListener: vitest_1.vi.fn((event, handler) => {
                if (event === 'message') {
                    messageHandler = handler;
                }
            }),
            close: vitest_1.vi.fn(),
            send: vitest_1.vi.fn((message) => {
                sentMessages.push(JSON.parse(message));
            })
        };
        const socket = new socketWithClientAuth_js_1.ISocketWithClientAuth(ws, 'Bearer old-token', {}, [], api_1.trace.getTracer('test'), { onClose: vitest_1.vi.fn() });
        const firstRequest = socket.request('editor.ping', {});
        (0, vitest_1.expect)(sentMessages[0].request.setAuthorization).toBe('Bearer old-token');
        socket.setAuthorization('Bearer new-token');
        messageHandler?.({
            data: JSON.stringify({
                response: { id: 1, payload: undefined, error: null }
            })
        });
        await firstRequest;
        const secondRequest = socket.request('editor.ping', {});
        (0, vitest_1.expect)(sentMessages[1].request.setAuthorization).toBe('Bearer new-token');
        messageHandler?.({
            data: JSON.stringify({
                response: { id: 2, payload: undefined, error: null }
            })
        });
        await secondRequest;
    });
});
//# sourceMappingURL=socket.test.js.map