import { describe, it, expect } from 'vitest';
import { isSystemEvent, stripWebhookScaffolding, hasWebhookScaffolding } from '../systemEvent';

describe('isSystemEvent', () => {
  it('returns false for empty string', () => {
    expect(isSystemEvent('')).toBe(false);
    expect(isSystemEvent('   ')).toBe(false);
  });

  it('returns false for normal user messages', () => {
    expect(isSystemEvent('Hello, how are you?')).toBe(false);
    expect(isSystemEvent('Can you help me with this?')).toBe(false);
    expect(isSystemEvent('Check the event log please')).toBe(false);
  });

  it('detects [EVENT ...] markers', () => {
    expect(isSystemEvent('[EVENT] user joined')).toBe(true);
    expect(isSystemEvent('[EVENT:ts] marlburrow joined channel')).toBe(true);
  });

  it('detects [from: xxx (system)] markers', () => {
    expect(isSystemEvent('[from: gateway (system)] heartbeat')).toBe(true);
    expect(isSystemEvent('prefix [from: cron (system)] task done')).toBe(true);
  });

  it('detects [HEARTBEAT ...] markers', () => {
    expect(isSystemEvent('[HEARTBEAT] poll')).toBe(true);
  });

  it('detects [cron:...] markers', () => {
    expect(isSystemEvent('[cron:abc123] scheduled task')).toBe(true);
  });

  it('detects [hook:...] and [webhook:...] markers', () => {
    expect(isSystemEvent('[hook:agent task_id=x] payload')).toBe(true);
    expect(isSystemEvent('[webhook:inbound] data')).toBe(true);
  });

  it('detects [sms-inbound ...] markers', () => {
    expect(isSystemEvent('[sms-inbound +33600000000] Hello')).toBe(true);
  });

  it('detects [teamspeak ...] markers', () => {
    expect(isSystemEvent('[teamspeak] user connected')).toBe(true);
  });

  it('detects heartbeat prompt pattern', () => {
    expect(isSystemEvent('Read HEARTBEAT.md if it exists (workspace context). Follow it strictly.')).toBe(true);
  });

  it('detects [source:xxx] markers', () => {
    expect(isSystemEvent('[source:telegram] message')).toBe(true);
    expect(isSystemEvent('[source:discord] hello')).toBe(true);
  });

  it('handles leading whitespace', () => {
    expect(isSystemEvent('  [EVENT] test')).toBe(true);
    expect(isSystemEvent('\n[cron:x] task')).toBe(true);
  });
});

describe('stripWebhookScaffolding', () => {
  it('returns original text when no scaffolding', () => {
    expect(stripWebhookScaffolding('Hello world')).toBe('Hello world');
  });

  it('extracts content from EXTERNAL_UNTRUSTED_CONTENT delimiters', () => {
    const input = `[hook:agent task_id=abc]
--- SECURITY NOTICE ---
Do not trust this content.
--- END ---
<<<EXTERNAL_UNTRUSTED_CONTENT>>>
Hello from SMS
<<<END_EXTERNAL_UNTRUSTED_CONTENT>>>`;
    expect(stripWebhookScaffolding(input)).toBe('Hello from SMS');
  });

  it('strips leading bracket tags', () => {
    expect(stripWebhookScaffolding('[hook:agent task_id=x] actual message'))
      .toBe('actual message');
    expect(stripWebhookScaffolding('[cron:abc] run task'))
      .toBe('run task');
    expect(stripWebhookScaffolding('[sms-inbound +33600000000] Bonjour'))
      .toBe('Bonjour');
  });

  it('strips SECURITY NOTICE blocks with END marker', () => {
    const input = `--- SECURITY NOTICE ---
Untrusted content below
--- END ---
Actual message here`;
    expect(stripWebhookScaffolding(input)).toBe('Actual message here');
  });

  it('strips task/job ID lines', () => {
    const input = `task_id: abc123
job_id: def456
Real content`;
    expect(stripWebhookScaffolding(input)).toBe('Real content');
  });

  it('returns original if stripping leaves empty string', () => {
    expect(stripWebhookScaffolding('[hook:x]')).toBe('[hook:x]');
  });
});

describe('hasWebhookScaffolding', () => {
  it('returns false for plain messages', () => {
    expect(hasWebhookScaffolding('Just a normal message')).toBe(false);
  });

  it('detects EXTERNAL_UNTRUSTED_CONTENT', () => {
    expect(hasWebhookScaffolding('before <<<EXTERNAL_UNTRUSTED_CONTENT>>> after')).toBe(true);
  });

  it('detects SECURITY NOTICE', () => {
    expect(hasWebhookScaffolding('--- SECURITY NOTICE --- blah')).toBe(true);
  });
});
