import assert from 'node:assert/strict';
import { afterEach, beforeEach, describe, it } from 'node:test';
import { claimRuntimeLaunchBinding } from './runtime-launch-binding.js';
describe('claimRuntimeLaunchBinding', () => {
    const originalEnv = { ...process.env };
    const originalFetch = global.fetch;
    const agentConfig = {
        roleName: '',
        agentId: 'agent-a',
        projectName: 'default',
        workspacePath: 'C:/repo/demo',
        prompt: '',
        describe: '',
    };
    const serverConfig = {
        serverUrl: 'ws://server:7380/ws/agent',
        heartbeatInterval: 3000,
        reconnectInterval: 200,
        maxReconnectAttempts: -1,
        runtimeBridgeBaseUrl: 'http://127.0.0.1:18081',
    };
    beforeEach(() => {
        process.env = { ...originalEnv };
    });
    afterEach(() => {
        process.env = { ...originalEnv };
        global.fetch = originalFetch;
    });
    it('skips claim when launch binding flag is disabled', async () => {
        process.env.AWS_MCP_CLAIM_LAUNCH_BINDING = 'false';
        const result = await claimRuntimeLaunchBinding(agentConfig, serverConfig);
        assert.equal(result, null);
    });
    it('claims launch binding even when runtime token is already configured', async () => {
        delete process.env.AWS_MCP_CLAIM_LAUNCH_BINDING;
        let called = false;
        global.fetch = async () => {
            called = true;
            return {
                ok: true,
                status: 200,
                statusText: 'OK',
                json: async () => ({
                    ok: true,
                    agentId: 'agent-a',
                    runtimeAccessToken: 'binding-token',
                }),
            };
        };
        const result = await claimRuntimeLaunchBinding(agentConfig, {
            ...serverConfig,
            runtimeAccessToken: 'direct-configured-token',
        });
        assert.equal(result?.runtimeAccessToken, 'binding-token');
        assert.equal(called, true);
    });
    it('claims queued agent identity from local runtime bridge', async () => {
        delete process.env.AWS_MCP_CLAIM_LAUNCH_BINDING;
        let capturedUrl = '';
        let capturedBody;
        global.fetch = async (url, options) => {
            capturedUrl = url.toString();
            capturedBody = JSON.parse(String(options?.body));
            return {
                ok: true,
                status: 200,
                statusText: 'OK',
                json: async () => ({
                    ok: true,
                    agentId: 'agent-a',
                    runtimeAccessToken: 'token-a',
                    userId: 'user-a',
                }),
            };
        };
        const result = await claimRuntimeLaunchBinding(agentConfig, serverConfig);
        assert.equal(capturedUrl, 'http://127.0.0.1:18081/runtime/binding/claim-agent');
        assert.deepEqual(capturedBody, {
            workspacePath: 'C:/repo/demo',
            serverUrl: 'ws://server:7380/ws/agent',
        });
        assert.equal(result?.agentId, 'agent-a');
        assert.equal(result?.runtimeAccessToken, 'token-a');
    });
    it('claims by workspace path and server URL when agent id is missing', async () => {
        process.env.AWS_MCP_CLAIM_LAUNCH_BINDING = 'true';
        let capturedBody;
        global.fetch = async (_url, options) => {
            capturedBody = JSON.parse(String(options?.body));
            return {
                ok: true,
                status: 200,
                statusText: 'OK',
                json: async () => ({
                    ok: true,
                    agentId: 'agent-from-queue',
                    runtimeAccessToken: 'token-a',
                }),
            };
        };
        const result = await claimRuntimeLaunchBinding({ ...agentConfig, agentId: undefined }, serverConfig);
        assert.deepEqual(capturedBody, {
            workspacePath: 'C:/repo/demo',
            serverUrl: 'ws://server:7380/ws/agent',
        });
        assert.equal(result?.agentId, 'agent-from-queue');
    });
    it('ignores binding id and still claims by workspace path plus server URL', async () => {
        process.env.AWS_MCP_CLAIM_LAUNCH_BINDING = 'true';
        process.env.AWS_MCP_LAUNCH_BINDING_ID = 'binding-a';
        let capturedBody;
        global.fetch = async (_url, options) => {
            capturedBody = JSON.parse(String(options?.body));
            return {
                ok: true,
                status: 200,
                statusText: 'OK',
                json: async () => ({ ok: true, agentId: 'agent-a' }),
            };
        };
        const result = await claimRuntimeLaunchBinding(agentConfig, serverConfig);
        assert.deepEqual(capturedBody, {
            workspacePath: 'C:/repo/demo',
            serverUrl: 'ws://server:7380/ws/agent',
        });
        assert.equal(result?.agentId, 'agent-a');
    });
});
//# sourceMappingURL=runtime-launch-binding.test.js.map