import { describe, it, expect } from 'vitest';
import {
  buildSoulTemplateVars,
  buildUserTemplateVars,
  buildToolsTemplateVars,
} from '../../src/template/builders.js';

describe('buildSoulTemplateVars', () => {
  it('builds direct style correctly', () => {
    const vars = buildSoulTemplateVars({
      agentName: 'Jarvis',
      communicationStyle: 'direct',
    });
    expect(vars.AGENT_NAME).toBe('Jarvis');
    expect(vars.BEHAVIORAL_PRINCIPLES).toContain('Clarity over performance theater');
    expect(vars.COMMUNICATION_STYLE).toContain('Be direct, concise, and concrete');
  });

  it('builds friendly style correctly', () => {
    const vars = buildSoulTemplateVars({
      communicationStyle: 'friendly',
    });
    expect(vars.BEHAVIORAL_PRINCIPLES).toContain('Be helpful and approachable');
    expect(vars.COMMUNICATION_STYLE).toContain('Be warm and conversational');
  });

  it('builds formal style correctly', () => {
    const vars = buildSoulTemplateVars({
      communicationStyle: 'formal',
    });
    expect(vars.BEHAVIORAL_PRINCIPLES).toContain('Maintain professional, structured');
    expect(vars.COMMUNICATION_STYLE).toContain('Use professional, structured language');
  });

  it('appends accessibility to principles', () => {
    const vars = buildSoulTemplateVars({
      communicationStyle: 'direct',
      accessibility: 'ADHD-friendly chunking',
    });
    expect(vars.BEHAVIORAL_PRINCIPLES).toContain('6. ADHD-friendly chunking.');
  });

  it('does not append accessibility when "none"', () => {
    const vars = buildSoulTemplateVars({
      communicationStyle: 'direct',
      accessibility: 'none',
    });
    expect(vars.BEHAVIORAL_PRINCIPLES).not.toContain('6.');
  });

  it('formats custom guardrails', () => {
    const vars = buildSoulTemplateVars({
      customGuardrails: 'Never auto-commit',
    });
    expect(vars.CUSTOM_GUARDRAILS).toBe('- Never auto-commit');
  });

  it('uses defaults when config is empty', () => {
    const vars = buildSoulTemplateVars({});
    expect(vars.AGENT_NAME).toBe('Assistant');
    expect(vars.ROLE_DESCRIPTION).toBe('execution partner and visibility engine');
  });
});

describe('buildUserTemplateVars', () => {
  it('maps all fields', () => {
    const vars = buildUserTemplateVars({
      userName: 'Jason',
      pronouns: 'He/Him',
      timezone: 'America/Chicago',
    });
    expect(vars.USER_NAME).toBe('Jason');
    expect(vars.PRONOUNS).toBe('He/Him');
    expect(vars.TIMEZONE).toBe('America/Chicago');
  });

  it('uses defaults for missing fields', () => {
    const vars = buildUserTemplateVars({});
    expect(vars.PRONOUNS).toBe('They/Them');
    expect(vars.TIMEZONE).toBe('UTC');
  });
});

describe('buildToolsTemplateVars', () => {
  it('builds git providers table', () => {
    const vars = buildToolsTemplateVars({
      gitProviders: [{ name: 'GitHub', url: 'https://github.com', cli: 'gh', purpose: 'OSS' }],
    });
    expect(vars.GIT_PROVIDERS_TABLE).toContain('| GitHub |');
    expect(vars.GIT_PROVIDERS_TABLE).toContain('`gh`');
  });

  it('uses default table when no providers', () => {
    const vars = buildToolsTemplateVars({});
    expect(vars.GIT_PROVIDERS_TABLE).toContain('add your git providers here');
  });
});
