/**
 * Pure-function tests for the Telegram bots formatters.
 * No React, no network.
 */

import { describe, expect, it } from 'vitest';
import {
  formatDate,
  formatDateTime,
  statusLabel,
  statusVariant,
  tokenSetLabel,
  tokenSetVariant,
  truncate,
} from '@/features/telegram/format';
import type { TelegramBotStatus } from '@/features/telegram/types';

describe('statusLabel', () => {
  it('humanizes the status values', () => {
    expect(statusLabel('active')).toBe('Active');
    expect(statusLabel('paused')).toBe('Paused');
  });
  it('returns the input when unknown', () => {
    expect(statusLabel('weird' as TelegramBotStatus)).toBe('weird');
  });
});

describe('statusVariant', () => {
  it('maps active to success and paused to warning', () => {
    expect(statusVariant('active')).toBe('success');
    expect(statusVariant('paused')).toBe('warning');
  });
});

describe('tokenSetLabel', () => {
  it('renders Configured when set and Not set when unset', () => {
    expect(tokenSetLabel(true)).toBe('Configured');
    expect(tokenSetLabel(false)).toBe('Not set');
  });
});

describe('tokenSetVariant', () => {
  it('maps set to success and unset to warning', () => {
    expect(tokenSetVariant(true)).toBe('success');
    expect(tokenSetVariant(false)).toBe('warning');
  });
});

describe('formatDate', () => {
  it('returns em-dash for null/undefined/empty', () => {
    expect(formatDate(null)).toBe('\u2014');
    expect(formatDate(undefined)).toBe('\u2014');
    expect(formatDate('')).toBe('\u2014');
  });
  it('returns the original string for unparseable input', () => {
    expect(formatDate('not-a-date')).toBe('not-a-date');
  });
  it('formats a valid ISO date', () => {
    const out = formatDate('2026-06-18T12:00:00Z');
    expect(out).not.toMatch(/Invalid/);
    expect(out.length).toBeGreaterThan(0);
  });
});

describe('formatDateTime', () => {
  it('returns em-dash for null/undefined/empty', () => {
    expect(formatDateTime(null)).toBe('\u2014');
    expect(formatDateTime(undefined)).toBe('\u2014');
    expect(formatDateTime('')).toBe('\u2014');
  });
  it('formats a valid ISO date with a time component', () => {
    const out = formatDateTime('2026-06-18T12:34:00Z');
    expect(out).not.toMatch(/Invalid/);
    expect(out.length).toBeGreaterThan(0);
  });
});

describe('truncate', () => {
  it('returns em-dash for null/undefined/empty', () => {
    expect(truncate(null)).toBe('\u2014');
    expect(truncate(undefined)).toBe('\u2014');
    expect(truncate('')).toBe('\u2014');
  });
  it('returns the input when shorter than the cap', () => {
    expect(truncate('hello', 10)).toBe('hello');
  });
  it('truncates and adds an ellipsis when over the cap', () => {
    const out = truncate('this is a long string that will be cut off', 10);
    expect(out.endsWith('\u2026')).toBe(true);
    expect(out.length).toBe(10);
  });
  it('uses a default cap of 60', () => {
    const out = truncate('a'.repeat(80));
    expect(out.length).toBe(60);
    expect(out.endsWith('\u2026')).toBe(true);
  });
});
