import { describe, expect, it, vi } from 'vitest';
import { GatewayHermesRuntimeTransport } from './hermes-runtime.transport.js';

const scope = {
  actorId: 'owner-1',
  tenantId: 'tenant-1',
  channelId: 'cli',
  correlationId: 'correlation-1',
};

describe('GatewayHermesRuntimeTransport', () => {
  it('preserves a configured path prefix and authenticates the concrete runtime request', async () => {
    const fetchFn = vi
      .fn()
      .mockResolvedValue(new Response(JSON.stringify(['session.list']), { status: 200 }));
    const transport = new GatewayHermesRuntimeTransport(
      'https://runtime.example.test/hermes',
      'test-service-token',
      fetchFn,
    );

    await expect(transport.capabilities(scope)).resolves.toEqual(['session.list']);

    expect(fetchFn).toHaveBeenCalledWith(
      new URL('https://runtime.example.test/hermes/capabilities'),
      expect.objectContaining({
        headers: expect.objectContaining({
          authorization: 'Bearer test-service-token',
          'x-mosaic-channel-id': 'cli',
        }),
      }),
    );
  });

  it('rejects non-loopback HTTP runtime endpoints before sending identity headers', async () => {
    const fetchFn = vi.fn();
    const transport = new GatewayHermesRuntimeTransport(
      'http://runtime.example.test/hermes',
      'test-service-token',
      fetchFn,
    );

    await expect(transport.capabilities(scope)).rejects.toThrow('requires HTTPS');
    expect(fetchFn).not.toHaveBeenCalled();
  });
});
