import { describe, expect, it, vi } from 'vitest';
import { AgentConfigRegistry } from '../agents/registry.js';
import { configSchema } from '../config/schema.js';
import { buildCouncilPreflightReport } from './preflight.js';

function makeConfig() {
  const config = configSchema.parse({
    models: {
      default: {
        provider: 'zhipuai',
        model: 'glm-4.7',
        use_oauth: true,
      },
    },
  });
  config.councils.enabled = true;
  config.councils.groups.D.arbiter_agent = 'comms';
  config.councils.groups.D.freethinker_agent = 'research';
  config.councils.groups.D.model_tier = 'default';
  config.councils.groups.P.arbiter_agent = 'comms';
  config.councils.groups.P.freethinker_agent = 'research';
  config.councils.groups.P.model_tier = 'default';
  config.councils.meta_arbiter_agent = 'code';
  config.councils.meta_model_tier = 'default';
  return config;
}

function makeRegistry(): AgentConfigRegistry {
  const registry = new AgentConfigRegistry();
  registry.register({ name: 'comms', modelTier: 'fast' });
  registry.register({ name: 'research', modelTier: 'default' });
  registry.register({ name: 'code', modelTier: 'complex' });
  return registry;
}

describe('buildCouncilPreflightReport', () => {
  it('reports zhipu endpoint/auth and stale oauth flag note', async () => {
    const config = makeConfig();
    const report = await buildCouncilPreflightReport({
      config,
      registry: makeRegistry(),
      delegateRunner: {
        delegate: vi.fn(async () => ({
          content: '{"ok":true}',
          usage: { inputTokens: 1, outputTokens: 1 },
          tier: 'default' as const,
        })),
      },
      activeTier: 'default',
      includeLiveProbe: false,
      credentialState: {
        openaiOAuth: false,
        openaiApiKeyStored: false,
        anthropicApiKeyStored: false,
        anthropicAuthTokenStored: false,
        zaiStored: true,
      },
    });

    expect(report).toContain('D.arbiter: agent=comms');
    expect(report).toContain('effective_tier=default model=zhipuai/glm-4.7');
    expect(report).toContain('endpoint=https://api.z.ai/api/paas/v4');
    expect(report).toContain('auth_mode=bearer_credential');
    expect(report).toContain('auth_source=auth.json:zai');
    expect(report).toContain('note=use_oauth/auth_mode=oauth is ignored for zhipuai');
  });

  it('includes live probe failures by tier', async () => {
    const config = makeConfig();
    const delegate = vi.fn(async () => {
      throw new Error('Connection error');
    });
    const report = await buildCouncilPreflightReport({
      config,
      registry: makeRegistry(),
      delegateRunner: { delegate },
      activeTier: 'default',
      includeLiveProbe: true,
      credentialState: {
        openaiOAuth: false,
        openaiApiKeyStored: false,
        anthropicApiKeyStored: false,
        anthropicAuthTokenStored: false,
        zaiStored: false,
      },
    });

    expect(report).toContain('Live tier probes:');
    expect(report).toContain('default: failed');
    expect(report).toContain('Connection error');
    expect(delegate).toHaveBeenCalled();
  });
});
