import { describe, expect, it } from 'vitest';
import { SSH_DEFAULTS, SSH_CONSOLE_DEFAULTS, mergeSshConfig } from './config.js';

describe('ssh/config', () => {
  it('defaults to disabled with safe values', () => {
    expect(SSH_DEFAULTS.enabled).toBe(false);
    expect(SSH_DEFAULTS.allowPrivateAddresses).toBe(false);
    expect(SSH_DEFAULTS.adminBypassesGrants).toBe(true);
    expect(SSH_DEFAULTS.callTimeoutSeconds).toBeGreaterThan(0);
    expect(SSH_DEFAULTS.maxOutputBytes).toBeGreaterThan(0);
    expect(SSH_DEFAULTS.abuseFailureThreshold).toBeGreaterThan(0);
  });

  it('merges partial overrides on top of defaults', () => {
    const merged = mergeSshConfig({ enabled: true, callTimeoutSeconds: 60 });
    expect(merged.enabled).toBe(true);
    expect(merged.callTimeoutSeconds).toBe(60);
    expect(merged.allowPrivateAddresses).toBe(SSH_DEFAULTS.allowPrivateAddresses);
    expect(merged.maxOutputBytes).toBe(SSH_DEFAULTS.maxOutputBytes);
  });

  it('returns defaults when partial is undefined', () => {
    expect(mergeSshConfig(undefined)).toEqual(SSH_DEFAULTS);
  });

  it('returns defaults when partial is empty', () => {
    expect(mergeSshConfig({})).toEqual(SSH_DEFAULTS);
  });

  it('does not mutate SSH_DEFAULTS when merging', () => {
    const before = { ...SSH_DEFAULTS };
    mergeSshConfig({ enabled: true });
    expect(SSH_DEFAULTS).toEqual(before);
  });

  describe('console.maxSessionsPerTask / maxSessionsPerUser', () => {
    it('defaults maxSessionsPerTask to 5', () => {
      expect(SSH_CONSOLE_DEFAULTS.maxSessionsPerTask).toBe(5);
    });

    it('defaults maxSessionsPerUser to 20', () => {
      expect(SSH_CONSOLE_DEFAULTS.maxSessionsPerUser).toBe(20);
    });

    it('parses max_sessions_per_task override via mergeSshConfig', () => {
      const merged = mergeSshConfig({ console: { maxSessionsPerTask: 3 } as any });
      expect(merged.console.maxSessionsPerTask).toBe(3);
    });

    it('falls back to default maxSessionsPerTask (5) when omitted', () => {
      const merged = mergeSshConfig({ console: { enabled: true } as any });
      expect(merged.console.maxSessionsPerTask).toBe(5);
    });

    it('falls back to default maxSessionsPerUser (20) when omitted', () => {
      const merged = mergeSshConfig({ console: { enabled: true } as any });
      expect(merged.console.maxSessionsPerUser).toBe(20);
    });

    it('parses max_sessions_per_user override via mergeSshConfig', () => {
      const merged = mergeSshConfig({ console: { maxSessionsPerUser: 0 } as any });
      expect(merged.console.maxSessionsPerUser).toBe(0);
    });
  });
});
