import { describe, it, expect } from 'vitest';
import { renderMarkdown } from './markdown.js';

// ── inline() protect-verbatim-regions regression (site-a2ui/FINDINGS.md
//    "No inline-flow construct" — root cause: emphasis regexes ran BEFORE
//    code-span extraction, so any code span containing `*`/`_` mangled) ──

describe('renderMarkdown — inline code spans survive markdown-unsafe content', () => {
  it('code span containing an asterisk is not treated as emphasis', () => {
    const html = renderMarkdown('Use `--a-chrome-*` for chrome tokens.');
    expect(html).toContain('<code>--a-chrome-*</code>');
    expect(html).not.toContain('<em>');
    expect(html).not.toContain('<strong>');
  });

  it('code span containing underscores is not treated as emphasis', () => {
    const html = renderMarkdown('Call `foo_bar_baz()` to run it.');
    expect(html).toContain('<code>foo_bar_baz()</code>');
    expect(html).not.toContain('<em>');
  });

  it('code span containing double-asterisks is not treated as bold', () => {
    const html = renderMarkdown('The pattern `**not-bold**` is literal.');
    expect(html).toContain('<code>**not-bold**</code>');
    expect(html).not.toContain('<strong>');
  });

  it('bold, italic, code, and link combine correctly within one run', () => {
    const html = renderMarkdown('This is **bold**, this is *italic*, this is `code_with_*` and this is [a link](https://example.com).');
    expect(html).toContain('<strong>bold</strong>');
    expect(html).toContain('<em>italic</em>');
    expect(html).toContain('<code>code_with_*</code>');
    expect(html).toContain('<a href="https://example.com">a link</a>');
  });

  it('adjacent code spans do not bleed into each other', () => {
    const html = renderMarkdown('First `a_b` then `c*d` then plain _italic_ text.');
    expect(html).toContain('<code>a_b</code>');
    expect(html).toContain('<code>c*d</code>');
    expect(html).toContain('<em>italic</em>');
  });

  it('HTML-unsafe characters inside a code span are still escaped', () => {
    const html = renderMarkdown('Use `<script>` tags carefully.');
    expect(html).toContain('<code>&lt;script&gt;</code>');
  });
});
