import { describe, expect, it } from 'vitest';
import { resolveJobAutopilot } from '../src/server/jobAutopilot';
import { resolveBootstrapChoice } from '../src/server/bootstrapPrecedence';

describe('SET 23c resolveJobAutopilot', () => {
  it('omitted autopilot falls back to autopilotDefault', () => {
    expect(resolveJobAutopilot(undefined, true)).toBe(true);
    expect(resolveJobAutopilot(null, true)).toBe(true);
    expect(resolveJobAutopilot(undefined, false)).toBe(false);
  });

  it('explicit false stays supervised even when default is true', () => {
    expect(resolveJobAutopilot(false, true)).toBe(false);
  });

  it('explicit true enables autopilot even when default is false', () => {
    expect(resolveJobAutopilot(true, false)).toBe(true);
  });
});

describe('SET 23e / R1 MOCK_LLM precedence', () => {
  it('documents that bootstrapConnectionFromEnv delegates to resolveBootstrapChoice', async () => {
    // Source-level sanity: bootstrapConnectionFromEnv must not re-implement its
    // own if/else precedence inline — it defers to the tested pure function.
    const fs = await import('node:fs');
    const path = await import('node:path');
    const src = fs.readFileSync(
      path.resolve('src/server/index.ts'),
      'utf8',
    );
    expect(src).toContain('resolveBootstrapChoice({');
  });

  it('actual behavior: MOCK_LLM=1 wins even with a persisted, verified providers.json operator', () => {
    // The regression this guards against: booting with MOCK_LLM=1 set but a
    // real LM Studio / Claude operator already persisted from a prior run —
    // meta must report mock, and no provider verification/connection is
    // attempted against the persisted host.
    expect(resolveBootstrapChoice({
      mockLlm: true,
      hasPersistedOperator: true,
      localLlmEnv: true,
      claudeKeyPresent: true,
      claudeModelPresent: true,
    })).toBe('mock');
  });

  it('actual behavior: without MOCK_LLM, a persisted operator wins over LOCAL_LLM/Claude env', () => {
    expect(resolveBootstrapChoice({
      mockLlm: false,
      hasPersistedOperator: true,
      localLlmEnv: true,
      claudeKeyPresent: true,
      claudeModelPresent: true,
    })).toBe('persisted');
  });

  it('actual behavior: falls through to LOCAL_LLM, then Claude, then none', () => {
    expect(resolveBootstrapChoice({
      mockLlm: false, hasPersistedOperator: false, localLlmEnv: true,
      claudeKeyPresent: true, claudeModelPresent: true,
    })).toBe('local-env');
    expect(resolveBootstrapChoice({
      mockLlm: false, hasPersistedOperator: false, localLlmEnv: false,
      claudeKeyPresent: true, claudeModelPresent: true,
    })).toBe('claude-env');
    expect(resolveBootstrapChoice({
      mockLlm: false, hasPersistedOperator: false, localLlmEnv: false,
      claudeKeyPresent: true, claudeModelPresent: false,
    })).toBe('none');
    expect(resolveBootstrapChoice({
      mockLlm: false, hasPersistedOperator: false, localLlmEnv: false,
      claudeKeyPresent: false, claudeModelPresent: false,
    })).toBe('none');
  });
});
