import { describe, it, expect } from 'vitest';
import { detectContentType } from '../src/proxymodel/content-type.js';

describe('detectContentType', () => {
  it('detects JSON object', () => {
    expect(detectContentType('{"key": "value", "num": 42}')).toBe('json');
  });

  it('detects JSON array', () => {
    expect(detectContentType('[{"id": 1}, {"id": 2}]')).toBe('json');
  });

  it('detects JSON with leading whitespace', () => {
    expect(detectContentType('  \n  {"key": "value"}')).toBe('json');
  });

  it('detects XML with processing instruction', () => {
    expect(detectContentType('<?xml version="1.0"?>\n<root/>')).toBe('xml');
  });

  it('detects XML with closing tags', () => {
    expect(detectContentType('<root>\n  <child>text</child>\n</root>')).toBe('xml');
  });

  it('detects YAML with multiple key-value lines', () => {
    expect(detectContentType('name: test\nversion: 1.0\ndescription: hello')).toBe('yaml');
  });

  it('does not false-positive single colon as YAML', () => {
    // Prose that happens to have one line with a colon
    expect(detectContentType('Note: this is important.\nAnd this is more prose.')).toBe('prose');
  });

  it('detects code starting with function', () => {
    expect(detectContentType('function hello() {\n  return "world";\n}')).toBe('code');
  });

  it('detects code starting with import', () => {
    expect(detectContentType('import { foo } from "bar";\n\nconst x = 1;')).toBe('code');
  });

  it('detects code starting with class', () => {
    expect(detectContentType('class MyClass {\n  constructor() {}\n}')).toBe('code');
  });

  it('detects code starting with export', () => {
    expect(detectContentType('export default function main() {}')).toBe('code');
  });

  it('detects code starting with const', () => {
    expect(detectContentType('const handler = async (content, ctx) => {\n  return { content };\n};')).toBe('code');
  });

  it('returns prose for markdown', () => {
    expect(detectContentType('# Security Policy\n\nAll tokens MUST be rotated every 90 days.')).toBe('prose');
  });

  it('returns prose for plain text', () => {
    expect(detectContentType('This is a plain text document about security practices.')).toBe('prose');
  });

  it('returns prose for empty content', () => {
    expect(detectContentType('')).toBe('prose');
  });

  it('returns prose for whitespace-only content', () => {
    expect(detectContentType('   \n  \n  ')).toBe('prose');
  });

  it('handles large JSON arrays (Node-RED flows)', () => {
    const flows = JSON.stringify([
      { id: 'flow1', label: 'Thermostat', type: 'tab', nodes: [] },
      { id: 'flow2', label: 'Lighting', type: 'tab', nodes: [] },
    ]);
    expect(detectContentType(flows)).toBe('json');
  });

  it('detects Python code', () => {
    expect(detectContentType('def main():\n    print("hello")\n\nif __name__ == "__main__":\n    main()')).toBe('code');
  });

  it('detects shell script', () => {
    expect(detectContentType('#!/bin/bash\nset -e\necho "hello"')).toBe('code');
  });
});
