/**
 * Unit tests for TypeScript pi calculation using Jest
 */

import { execSync } from 'child_process';
import * as path from 'path';

const SCRIPT_PATH = path.join(__dirname, 'print_hej.ts');

function runScript(decimals: number | null = null): string {
  const cmd = decimals ? `npx ts-node ${SCRIPT_PATH} ${decimals}` : `npx ts-node ${SCRIPT_PATH}`;
  return execSync(cmd, { encoding: 'utf8' }).trim();
}

describe('Pi Calculation', () => {
  test('10 decimals', () => {
    const result = runScript(10);
    const expected = '3.1415926535';
    expect(result).toBe(expected);
  });

  test('5 decimals', () => {
    const result = runScript(5);
    const expected = '3.14159';
    expect(result).toBe(expected);
  });

  test('1 decimal', () => {
    const result = runScript(1);
    const expected = '3.1';
    expect(result).toBe(expected);
  });

  test('100 decimals', () => {
    const result = runScript(100);
    const expected = '3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679';
    expect(result).toBe(expected);
  });

  test('default (100 decimals)', () => {
    const result = runScript();
    const expected = '3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679';
    expect(result).toBe(expected);
  });

  test('invalid input (should use default 100)', () => {
    const result = runScript(NaN);
    const expected = '3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679';
    expect(result).toBe(expected);
  });

  test('10000 decimals', () => {
    const result = runScript(10000);
    // Check length: "3." + 10000 digits = 10002 characters
    expect(result.length).toBe(10002);
    expect(result.startsWith('3.14159')).toBe(true);
  });
});