|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { render } from 'vitest-browser-react' |
| 3 | + |
| 4 | +import type { PreviewContent } from '@/renderer/src/hooks/useCodePreview' |
| 5 | + |
| 6 | +/** |
| 7 | + * Build a text preview payload without pulling in the IPC hook. |
| 8 | + * @param overrides - File metadata/content fields relevant to the preview. |
| 9 | + * @returns PreviewContent for FileContent's `text` branch. |
| 10 | + */ |
| 11 | +function makeTextContent( |
| 12 | + overrides: Partial<PreviewContent & { content: string }> = {}, |
| 13 | +): PreviewContent { |
| 14 | + const content = 'content' in overrides ? overrides.content : '# Skill\n' |
| 15 | + return { |
| 16 | + kind: 'text', |
| 17 | + data: { |
| 18 | + name: 'SKILL.md', |
| 19 | + content: content ?? '# Skill\n', |
| 20 | + extension: '.md', |
| 21 | + lineCount: content?.split('\n').length ?? 1, |
| 22 | + }, |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +describe('FileContent Markdown modes', () => { |
| 27 | + it('renders Markdown files in code mode first, then switches to Reading Mode', async () => { |
| 28 | + const { FileContent } = await import('./FileContent') |
| 29 | + const screen = await render( |
| 30 | + <FileContent |
| 31 | + content={makeTextContent({ |
| 32 | + content: |
| 33 | + '---\nname: install\n---\n# Install\n\n- [x] Link agents\n\n```ts\nconst ok = true\n```', |
| 34 | + })} |
| 35 | + />, |
| 36 | + ) |
| 37 | + |
| 38 | + await expect |
| 39 | + .element(screen.getByRole('radio', { name: /Show Markdown source/i })) |
| 40 | + .toBeInTheDocument() |
| 41 | + expect(screen.getByRole('heading', { name: 'Install' }).query()).toBeNull() |
| 42 | + |
| 43 | + await screen.getByRole('radio', { name: /Show rendered Markdown/i }).click() |
| 44 | + |
| 45 | + await expect |
| 46 | + .element(screen.getByRole('heading', { name: 'Install' })) |
| 47 | + .toBeInTheDocument() |
| 48 | + await expect.element(screen.getByText('Link agents')).toBeInTheDocument() |
| 49 | + expect(screen.getByText('name: install').query()).toBeNull() |
| 50 | + }) |
| 51 | + |
| 52 | + it('keeps Markdown that starts with a horizontal rule in Reading Mode', async () => { |
| 53 | + const { FileContent } = await import('./FileContent') |
| 54 | + const screen = await render( |
| 55 | + <FileContent |
| 56 | + content={makeTextContent({ |
| 57 | + content: '---\n# Keep Me\n---\n\nVisible body', |
| 58 | + })} |
| 59 | + />, |
| 60 | + ) |
| 61 | + |
| 62 | + await screen.getByRole('radio', { name: /Show rendered Markdown/i }).click() |
| 63 | + |
| 64 | + await expect |
| 65 | + .element(screen.getByRole('heading', { name: 'Keep Me' })) |
| 66 | + .toBeInTheDocument() |
| 67 | + await expect.element(screen.getByText('Visible body')).toBeInTheDocument() |
| 68 | + }) |
| 69 | + |
| 70 | + it('renders language-less code fences as block code without AST attributes', async () => { |
| 71 | + const { FileContent } = await import('./FileContent') |
| 72 | + const screen = await render( |
| 73 | + <FileContent |
| 74 | + content={makeTextContent({ |
| 75 | + content: |
| 76 | + '# Skill\n\n```\nnpx skills list --json\n```\n\nInline `skill` stays compact.', |
| 77 | + })} |
| 78 | + />, |
| 79 | + ) |
| 80 | + |
| 81 | + await screen.getByRole('radio', { name: /Show rendered Markdown/i }).click() |
| 82 | + |
| 83 | + const blockCode = screen.getByText(/npx skills list --json/).query() |
| 84 | + const inlineCode = screen.getByText('skill', { exact: true }).query() |
| 85 | + |
| 86 | + expect(blockCode).toBeInstanceOf(HTMLElement) |
| 87 | + expect(inlineCode).toBeInstanceOf(HTMLElement) |
| 88 | + expect(blockCode?.closest('pre')).toBeInstanceOf(HTMLPreElement) |
| 89 | + expect(inlineCode?.closest('pre')).toBeNull() |
| 90 | + expect(screen.container.querySelector('[node]')).toBeNull() |
| 91 | + }) |
| 92 | + |
| 93 | + it('renders language-tagged code fences as block code', async () => { |
| 94 | + const { FileContent } = await import('./FileContent') |
| 95 | + const screen = await render( |
| 96 | + <FileContent |
| 97 | + content={makeTextContent({ |
| 98 | + content: '# Skill\n\n```ts\nconst ok = true\n```', |
| 99 | + })} |
| 100 | + />, |
| 101 | + ) |
| 102 | + |
| 103 | + await screen.getByRole('radio', { name: /Show rendered Markdown/i }).click() |
| 104 | + |
| 105 | + const blockCode = screen.getByText(/const ok = true/).query() |
| 106 | + |
| 107 | + expect(blockCode).toBeInstanceOf(HTMLElement) |
| 108 | + expect(blockCode?.closest('pre')).toBeInstanceOf(HTMLPreElement) |
| 109 | + }) |
| 110 | + |
| 111 | + it('locks Reading Mode to vertical scrolling when Markdown is wider than the pane', async () => { |
| 112 | + const { FileContent } = await import('./FileContent') |
| 113 | + const wideInline = 'very-long-inline-token-'.repeat(30) |
| 114 | + const wideBlock = 'wide command '.repeat(40) |
| 115 | + const screen = await render( |
| 116 | + <FileContent |
| 117 | + content={makeTextContent({ |
| 118 | + content: `# Wide\n\n\`${wideInline}\`\n\n\`\`\`\n${wideBlock}\n\`\`\``, |
| 119 | + })} |
| 120 | + />, |
| 121 | + ) |
| 122 | + |
| 123 | + await screen.getByRole('radio', { name: /Show rendered Markdown/i }).click() |
| 124 | + |
| 125 | + const scrollPane = screen.container.querySelector( |
| 126 | + '[data-markdown-reading-scroll]', |
| 127 | + ) |
| 128 | + expect(scrollPane).toBeInstanceOf(HTMLElement) |
| 129 | + const pane = scrollPane as HTMLElement |
| 130 | + |
| 131 | + // Programmatic scroll mirrors trackpad horizontal gestures in the renderer. |
| 132 | + expect(pane.scrollWidth).toBeGreaterThan(pane.clientWidth) |
| 133 | + pane.scrollTo({ left: 240 }) |
| 134 | + expect(pane.scrollLeft).toBe(0) |
| 135 | + }) |
| 136 | + |
| 137 | + it('adds a bottom spacer after source code so the final line can breathe', async () => { |
| 138 | + const { FileContent } = await import('./FileContent') |
| 139 | + const screen = await render( |
| 140 | + <FileContent |
| 141 | + content={makeTextContent({ |
| 142 | + content: Array.from( |
| 143 | + { length: 48 }, |
| 144 | + (_, index) => `line ${index + 1}`, |
| 145 | + ).join('\n'), |
| 146 | + })} |
| 147 | + />, |
| 148 | + ) |
| 149 | + |
| 150 | + const scrollPane = screen.container.querySelector( |
| 151 | + '[data-file-preview-scroll]', |
| 152 | + ) |
| 153 | + const spacer = screen.container.querySelector( |
| 154 | + '[data-file-preview-bottom-spacer]', |
| 155 | + ) |
| 156 | + |
| 157 | + expect(scrollPane).toBeInstanceOf(HTMLElement) |
| 158 | + expect(spacer).toBeInstanceOf(HTMLElement) |
| 159 | + expect(scrollPane?.lastElementChild).toBe(spacer) |
| 160 | + }) |
| 161 | +}) |
0 commit comments