import { describe, it, expect } from 'vitest';
import { ConfigBuilder } from './config.js';

describe('ConfigBuilder', () => {
  it('creates minimal config with anthropic + webchat', () => {
    const builder = new ConfigBuilder();
    builder.setProvider('default', { provider: 'anthropic', model: 'claude-sonnet-4-20250514', api_key: 'sk-ant-test' });
    builder.setGatewayPort(3777);
    const obj = builder.build();
    expect(obj.models.default.provider).toBe('anthropic');
    expect(obj.models.default.api_key).toBe('sk-ant-test');
    expect(obj.server.port).toBe(3777);
  });

  it('adds telegram channel', () => {
    const builder = new ConfigBuilder();
    builder.setProvider('default', { provider: 'anthropic', model: 'test', api_key: 'k' });
    builder.setTelegram('123:ABC', [12345]);
    const obj = builder.build();
    expect(obj.telegram.bot_token).toBe('123:ABC');
    expect(obj.telegram.allowed_chat_ids).toEqual([12345]);
  });

  it('adds discord channel', () => {
    const builder = new ConfigBuilder();
    builder.setProvider('default', { provider: 'anthropic', model: 'test', api_key: 'k' });
    builder.setDiscord('MTIz.test', ['guild1']);
    const obj = builder.build();
    expect(obj.discord.bot_token).toBe('MTIz.test');
    expect(obj.discord.allowed_guild_ids).toEqual(['guild1']);
  });

  it('adds fast tier', () => {
    const builder = new ConfigBuilder();
    builder.setProvider('default', { provider: 'anthropic', model: 'test', api_key: 'k' });
    builder.setProvider('fast', { provider: 'anthropic', model: 'claude-haiku-4-5-20251001', api_key: 'k' });
    const obj = builder.build();
    expect(obj.models.fast.provider).toBe('anthropic');
  });

  it('serializes to valid YAML string', () => {
    const builder = new ConfigBuilder();
    builder.setProvider('default', { provider: 'ollama', model: 'llama3.3', endpoint: 'http://localhost:11434' });
    builder.setGatewayPort(3777);
    const yaml = builder.toYaml();
    expect(yaml).toContain('provider: ollama');
    expect(yaml).toContain('model: llama3.3');
    expect(yaml).toContain('port: 3777');
  });

  it('loads from existing config object', () => {
    const existing = {
      models: { default: { provider: 'openai', model: 'gpt-4.1', api_key: 'sk-test' } },
      server: { port: 9999 },
      telegram: { bot_token: '123:ABC', allowed_chat_ids: [111] },
    };
    const builder = ConfigBuilder.fromObject(existing);
    const obj = builder.build();
    expect(obj.models.default.provider).toBe('openai');
    expect(obj.server.port).toBe(9999);
    expect(obj.telegram.bot_token).toBe('123:ABC');
  });

  it('sets memory embedding config', () => {
    const builder = new ConfigBuilder();
    builder.setProvider('default', { provider: 'anthropic', model: 'test', api_key: 'k' });
    builder.setMemoryEmbedding({ provider: 'openai', api_key: 'sk-emb' });
    const obj = builder.build();
    expect(obj.memory.embedding.enabled).toBe(true);
    expect(obj.memory.embedding.provider).toBe('openai');
    expect(obj.memory.embedding.api_key).toBe('sk-emb');
  });

  it('sets sandbox enabled', () => {
    const builder = new ConfigBuilder();
    builder.setSandboxEnabled(true);
    const obj = builder.build();
    expect(obj.sandbox.enabled).toBe(true);
  });

  it('sets gateway auth token', () => {
    const builder = new ConfigBuilder();
    builder.setGatewayToken('my-secret-token');
    const obj = builder.build();
    expect(obj.server.token).toBe('my-secret-token');
  });
});
