Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

8.4 KB
Raw Blame
import { describe, it, expect, vi } from 'vitest';
import { render, screen } from '@testing-library/react';
import MarkdownOutput from './MarkdownOutput';

it('keeps intraword underscores literal instead of consuming them as emphasis', () => {
  render(<MarkdownOutput content="QUALITY_AUDIT_JSON: done" />);
  expect(screen.getByText('QUALITY_AUDIT_JSON: done')).toBeInTheDocument();
});

it('still renders a genuine word-boundary _emphasis_ span as italic', () => {
  render(<MarkdownOutput content="this is _important_ text" />);
  const em = screen.getByText('important');
  expect(em.tagName).toBe('EM');
  expect(screen.getByText(/this is/)).toBeInTheDocument();
});

// MarkdownOutput renders inside cards, drawers and tab panels — never as the
// page's own outline — so a `#` line in stored model output must not emit a
// real `h1`/`h2` (#7264). Source levels shift into a band under `baseLevel`
// (default 3) while H_STYLES keeps indexing the SOURCE `#` count, so only the
// tag name moves; the in-card visual hierarchy is unchanged.

const SOURCE_STYLES = {
  1: 'text-base font-bold text-white mt-3 mb-1',
  2: 'text-sm font-bold text-white mt-3 mb-1',
  3: 'text-xs font-semibold text-port-accent mt-2 mb-1',
  4: 'text-xs font-semibold text-gray-300 mt-2 mb-0.5',
};

describe('MarkdownOutput heading levels', () => {
  it('shifts # to h3 at the default baseLevel', () => {
    render(<MarkdownOutput content={'# Title'} />);
    const heading = screen.getByRole('heading', { level: 3, name: 'Title' });
    expect(heading.tagName).toBe('H3');
  });

  it('shifts ## and ### to h4 and h5', () => {
    render(<MarkdownOutput content={'## Sub\n\n### Detail'} />);
    expect(screen.getByRole('heading', { level: 4, name: 'Sub' }).tagName).toBe('H4');
    expect(screen.getByRole('heading', { level: 5, name: 'Detail' }).tagName).toBe('H5');
  });

  it('shifts #### to h6', () => {
    render(<MarkdownOutput content={'#### Deep'} />);
    expect(screen.getByRole('heading', { level: 6, name: 'Deep' }).tagName).toBe('H6');
  });

  it('clamps ##### and ###### at h6 instead of emitting an invalid tag', () => {
    const { container } = render(<MarkdownOutput content={'##### Five\n\n###### Six'} />);
    expect(screen.getByRole('heading', { level: 6, name: 'Five' }).tagName).toBe('H6');
    expect(screen.getByRole('heading', { level: 6, name: 'Six' }).tagName).toBe('H6');
    expect(container.querySelector('h7')).toBeNull();
  });

  it('never emits h1 or h2 at the default baseLevel', () => {
    const { container } = render(
      <MarkdownOutput content={'# A\n\n## B\n\n### C\n\n#### D\n\n##### E\n\n###### F'} />,
    );
    expect(container.querySelector('h1, h2')).toBeNull();
  });

  it('keeps H_STYLES indexed by the source # count, not the emitted tag', () => {
    render(<MarkdownOutput content={'# One\n\n## Two\n\n### Three\n\n#### Four'} />);
    expect(screen.getByRole('heading', { level: 3, name: 'One' }).className).toBe(SOURCE_STYLES[1]);
    expect(screen.getByRole('heading', { level: 4, name: 'Two' }).className).toBe(SOURCE_STYLES[2]);
    expect(screen.getByRole('heading', { level: 5, name: 'Three' }).className).toBe(SOURCE_STYLES[3]);
    expect(screen.getByRole('heading', { level: 6, name: 'Four' }).className).toBe(SOURCE_STYLES[4]);
  });

  it('shifts the whole band when a consumer passes baseLevel', () => {
    render(<MarkdownOutput content={'# One\n\n## Two\n\n### Three'} baseLevel={4} />);
    expect(screen.getByRole('heading', { level: 4, name: 'One' }).tagName).toBe('H4');
    expect(screen.getByRole('heading', { level: 5, name: 'Two' }).tagName).toBe('H5');
    // baseLevel 4 + source 3 - 1 = 6 — already at the ceiling.
    expect(screen.getByRole('heading', { level: 6, name: 'Three' }).tagName).toBe('H6');
  });
});

// The renderer takes a resolver, not a repository (#7676): what counts as a
// reference and where it points belongs to the caller that holds the record.
describe('linkifyText resolver', () => {
  // A stand-in for lib/issueRefs.js — deliberately not that module, so these
  // assertions pin the RENDERER contract and not the issue-ref pattern.
  const linkifyText = (text) => text.split(/(#\d+)/).map(seg =>
    /^#\d+$/.test(seg) ? { ref: seg, url: `https://forge.example.com/issues/${seg.slice(1)}` } : seg
  ).filter(Boolean);
  const link = (name) => screen.getByRole('link', { name });

  it('links resolver segments inside paragraphs, headings, list items, and bold runs', () => {
    render(<MarkdownOutput content={'Closes #1.\n\n## Fixes #2\n\n- refs #3\n\n**and #4**'} linkifyText={linkifyText} />);
    for (const n of [1, 2, 3, 4]) {
      expect(link(`#${n}`)).toHaveAttribute('href', `https://forge.example.com/issues/${n}`);
    }
  });

  it('never offers the resolver text inside a code span or a fenced block', () => {
    render(<MarkdownOutput content={'use `#7640` here\n\n```\ngrep #7640\n```'} linkifyText={linkifyText} />);
    expect(screen.queryByRole('link')).toBeNull();
  });

  it('does not let the resolver rewrite an existing markdown link', () => {
    render(<MarkdownOutput content="[#7640](https://example.com/elsewhere)" linkifyText={linkifyText} />);
    expect(link('#7640')).toHaveAttribute('href', 'https://example.com/elsewhere');
  });

  it('renders plain text when no resolver is passed', () => {
    render(<MarkdownOutput content="Closes #7640" />);
    expect(screen.queryByRole('link')).toBeNull();
    expect(screen.getByText('Closes #7640')).toBeInTheDocument();
  });
});

it('refuses a resolver destination that would not pass as a markdown link href', () => {
  // The resolver is caller-supplied, so a `javascript:`/`data:` destination must
  // not become an anchor just because it arrived through a different door.
  const hostile = (text) => [{ ref: text, url: 'javascript:alert(1)' }];
  render(<MarkdownOutput content="click me" linkifyText={hostile} />);
  expect(screen.queryByRole('link')).toBeNull();
  expect(screen.getByText('click me')).toBeInTheDocument();
});

describe('preserveLineBreaks', () => {
  const src = 'first line\nsecond line';
  it('folds soft-wrapped lines into one row by default', () => {
    const { container } = render(<MarkdownOutput content={src} />);
    expect(container.querySelector('br')).toBeNull();
  });
  it('keeps each source line on its own row when asked', () => {
    const { container } = render(<MarkdownOutput content={src} preserveLineBreaks />);
    expect(container.querySelectorAll('br')).toHaveLength(1);
    expect(container.querySelector('p').textContent).toBe('first linesecond line');
  });
});

describe('task list items', () => {
  it('renders GFM checkboxes as glyphs and strikes through the checked ones', () => {
    render(<MarkdownOutput content={'- [ ] open\n- [x] done\n- plain'} />);
    const open = screen.getByText('open').closest('li');
    const done = screen.getByText('done').closest('li');
    expect(open).toHaveTextContent('☐open');
    expect(open.className).not.toContain('line-through');
    expect(done).toHaveTextContent('☑done');
    expect(done.className).toContain('line-through');
    expect(screen.getByText('plain').className).toContain('list-disc');
  });
});

describe('resolver segments without a URL', () => {
  it('renders an onClick segment as a button and a className segment as a styled span', () => {
    const onClick = vi.fn();
    const linkifyText = (text) => text.split(/(\[\[[^\]]+\]\]|#\w+)/).filter(Boolean).map(seg => {
      if (seg.startsWith('[[')) return { ref: seg.slice(2, -2), onClick };
      if (seg.startsWith('#')) return { ref: seg, className: 'text-port-accent/70' };
      return seg;
    });
    render(<MarkdownOutput content="open [[Other Note]] #tag" linkifyText={linkifyText} />);
    const button = screen.getByRole('button', { name: 'Other Note' });
    expect(button).toHaveAttribute('type', 'button');
    button.click();
    expect(onClick).toHaveBeenCalledTimes(1);
    expect(screen.queryByRole('link')).toBeNull();
    expect(screen.getByText('#tag').className).toContain('text-port-accent/70');
  });
});

it('keeps protocol-relative paths from becoming links or image fetches', () => {
  render(<MarkdownOutput content={'[one](//evil.example/x) [two](/\\evil.example/x) ![pic](/\\evil.example/p.png) [ok](/tasks)'} />);
  expect(screen.getAllByRole('link').map((a) => a.getAttribute('href'))).toEqual(['/tasks']);
  expect(screen.queryByRole('img')).toBeNull();
  expect(screen.getByText('one')).toBeInTheDocument();
  expect(screen.getByText('two')).toBeInTheDocument();
});