import {
  describe,
  it,
  expect
} from 'vitest';
import remarkParse from 'remark-parse';
import { unified } from 'unified';
import { remarkStripDangerousContent } from './remark-sanitize.rules';

async function parseMarkdown(content: string) {
  const processor = unified()
    .use(remarkParse)
    .use(remarkStripDangerousContent());

  return processor.run(processor.parse(content));
}

describe('remarkStripDangerousContent', () => {
  it('removes raw html nodes from markdown', async () => {
    const tree = await parseMarkdown('Hello <script>alert(1)</script> world');

    expect(JSON.stringify(tree)).not.toContain('script');
  });

  it('clears dangerous link protocols', async () => {
    const tree = await parseMarkdown('[click](javascript:alert(1))');

    expect(JSON.stringify(tree)).not.toContain('javascript:');
  });

  it('preserves safe custom emoji image data urls', async () => {
    const tree = await parseMarkdown('![custom-emoji:party-id:party](data:image/webp;base64,abc)');

    expect(JSON.stringify(tree)).toContain('data:image/webp;base64,abc');
  });

  it('clears dangerous non-image data urls', async () => {
    const tree = await parseMarkdown('![x](data:text/html,<script>alert(1)</script>)');

    expect(JSON.stringify(tree)).not.toContain('data:text/html');
  });
});
