|
| 1 | +import { describe, it, expect, vi } from 'vitest' |
| 2 | +import { renderContent } from '@/content-render/index' |
| 3 | + |
| 4 | +describe('code-header plugin', () => { |
| 5 | + describe('copilot language code blocks', () => { |
| 6 | + it('should render basic copilot code block without header (no copy meta)', async () => { |
| 7 | + const markdown = '```copilot\nImprove the variable names in this function\n```' |
| 8 | + |
| 9 | + const html = await renderContent(markdown) |
| 10 | + |
| 11 | + // Should keep copilot as the language (not convert to text without copy meta) |
| 12 | + expect(html).toContain('language-copilot') |
| 13 | + // Should NOT wrap in code-example div since no copy meta |
| 14 | + expect(html).not.toContain('code-example') |
| 15 | + // Should NOT have header since no copy meta |
| 16 | + expect(html).not.toContain('<header') |
| 17 | + }) |
| 18 | + |
| 19 | + it('should render copilot code block with copy button when copy meta is present', async () => { |
| 20 | + const markdown = '```copilot copy\nImprove the variable names in this function\n```' |
| 21 | + |
| 22 | + const html = await renderContent(markdown) |
| 23 | + |
| 24 | + // Should be wrapped in code-example div |
| 25 | + expect(html).toContain('code-example') |
| 26 | + // Should have header with copy button |
| 27 | + expect(html).toContain('<header') |
| 28 | + expect(html).toContain('js-btn-copy') |
| 29 | + expect(html).toContain('language-copilot') |
| 30 | + // Should NOT have prompt button (no prompt meta) |
| 31 | + expect(html).not.toContain('https://github.com/copilot?prompt=') |
| 32 | + }) |
| 33 | + |
| 34 | + it('should render copilot code block with prompt button only (no copy meta)', async () => { |
| 35 | + const markdown = '```copilot prompt\nImprove the variable names in this function\n```' |
| 36 | + |
| 37 | + const html = await renderContent(markdown) |
| 38 | + |
| 39 | + // Should be wrapped in code-example div |
| 40 | + expect(html).toContain('code-example') |
| 41 | + // Should have header |
| 42 | + expect(html).toContain('<header') |
| 43 | + // Should have prompt button |
| 44 | + expect(html).toContain('https://github.com/copilot?prompt=') |
| 45 | + expect(html).toContain('language-copilot') |
| 46 | + // Should NOT have copy button |
| 47 | + expect(html).not.toContain('js-btn-copy') |
| 48 | + }) |
| 49 | + |
| 50 | + it('should render copilot code block with both copy and prompt buttons when prompt meta is present', async () => { |
| 51 | + const markdown = '```copilot copy prompt\nImprove the variable names in this function\n```' |
| 52 | + |
| 53 | + const html = await renderContent(markdown) |
| 54 | + |
| 55 | + // Should be wrapped in code-example div |
| 56 | + expect(html).toContain('code-example') |
| 57 | + // Should have header with copy button |
| 58 | + expect(html).toContain('<header') |
| 59 | + expect(html).toContain('js-btn-copy') |
| 60 | + // Should have prompt button with encoded URL |
| 61 | + expect(html).toContain('https://github.com/copilot?prompt=') |
| 62 | + expect(html).toContain('Improve%20the%20variable%20names%20in%20this%20function') |
| 63 | + // Should have Copilot icon button |
| 64 | + expect(html).toContain('aria-label="Run this prompt in Copilot Chat"') |
| 65 | + expect(html).toContain('language-copilot') |
| 66 | + }) |
| 67 | + |
| 68 | + it('should render copilot code block with context reference when ref meta is present', async () => { |
| 69 | + const markdown = ` |
| 70 | +\`\`\`javascript id=js-age |
| 71 | +function logPersonsAge(a, b, c) { |
| 72 | + if (c) { |
| 73 | + console.log(a + " is " + b + " years old."); |
| 74 | + } else { |
| 75 | + console.log(a + " does not want to reveal their age."); |
| 76 | + } |
| 77 | +} |
| 78 | +\`\`\` |
| 79 | +
|
| 80 | +\`\`\`copilot copy prompt ref=js-age |
| 81 | +Improve the variable names in this function |
| 82 | +\`\`\` |
| 83 | + ` |
| 84 | + |
| 85 | + const html = await renderContent(markdown) |
| 86 | + |
| 87 | + // Should have prompt button with both code blocks in URL |
| 88 | + expect(html).toContain('https://github.com/copilot?prompt=') |
| 89 | + // Should contain encoded content from both the referenced code and the prompt |
| 90 | + expect(html).toContain('function%20logPersonsAge') |
| 91 | + expect(html).toContain('Improve%20the%20variable%20names') |
| 92 | + // Should have different aria-label indicating context |
| 93 | + expect(html).toContain('aria-label="Run this prompt with context in Copilot Chat"') |
| 94 | + }) |
| 95 | + |
| 96 | + it('should render copilot code block with prompt and ref only (no copy meta)', async () => { |
| 97 | + const markdown = ` |
| 98 | +\`\`\`javascript id=js-age |
| 99 | +function logPersonsAge(a, b, c) { |
| 100 | + if (c) { |
| 101 | + console.log(a + " is " + b + " years old."); |
| 102 | + } else { |
| 103 | + console.log(a + " does not want to reveal their age."); |
| 104 | + } |
| 105 | +} |
| 106 | +\`\`\` |
| 107 | +
|
| 108 | +\`\`\`copilot prompt ref=js-age |
| 109 | +Improve the variable names in this function |
| 110 | +\`\`\` |
| 111 | + ` |
| 112 | + |
| 113 | + const html = await renderContent(markdown) |
| 114 | + |
| 115 | + // Should have prompt button with both code blocks in URL |
| 116 | + expect(html).toContain('https://github.com/copilot?prompt=') |
| 117 | + // Should contain encoded content from both the referenced code and the prompt |
| 118 | + expect(html).toContain('function%20logPersonsAge') |
| 119 | + expect(html).toContain('Improve%20the%20variable%20names') |
| 120 | + // Should have different aria-label indicating context |
| 121 | + expect(html).toContain('aria-label="Run this prompt with context in Copilot Chat"') |
| 122 | + // Should NOT have copy button |
| 123 | + expect(html).not.toContain('js-btn-copy') |
| 124 | + }) |
| 125 | + }) |
| 126 | + |
| 127 | + describe('edge cases', () => { |
| 128 | + it('should handle missing reference gracefully and fall back to current code only', async () => { |
| 129 | + // Mock console.warn to capture warning |
| 130 | + const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}) |
| 131 | + |
| 132 | + const markdown = |
| 133 | + '```copilot copy prompt ref=nonexistent-id\nImprove the variable names in this function\n```' |
| 134 | + |
| 135 | + const html = await renderContent(markdown) |
| 136 | + |
| 137 | + // Should warn about missing reference |
| 138 | + expect(consoleWarnSpy).toHaveBeenCalledWith( |
| 139 | + expect.stringContaining("Can't find referenced code block with id=nonexistent-id"), |
| 140 | + ) |
| 141 | + |
| 142 | + // Should still render with prompt button using current code only |
| 143 | + expect(html).toContain('https://github.com/copilot?prompt=') |
| 144 | + expect(html).toContain('Improve%20the%20variable%20names%20in%20this%20function') |
| 145 | + // Should NOT contain any referenced code since none was found |
| 146 | + expect(html).not.toContain('function%20logPersonsAge') |
| 147 | + // Should have standard aria-label (not context version) |
| 148 | + expect(html).toContain('aria-label="Run this prompt in Copilot Chat"') |
| 149 | + // Should not crash or fail |
| 150 | + expect(html).toContain('code-example') |
| 151 | + |
| 152 | + // Restore console.warn |
| 153 | + consoleWarnSpy.mockRestore() |
| 154 | + }) |
| 155 | + |
| 156 | + it('should not process annotated code blocks', async () => { |
| 157 | + const markdown = `\`\`\`javascript copy annotate |
| 158 | +// This is an annotation |
| 159 | +function test() {} |
| 160 | +\`\`\`` |
| 161 | + |
| 162 | + const html = await renderContent(markdown) |
| 163 | + |
| 164 | + // Should NOT wrap in code-example div (annotated blocks are excluded) |
| 165 | + expect(html).not.toContain('code-example') |
| 166 | + }) |
| 167 | + |
| 168 | + it('should handle regular code blocks with copy', async () => { |
| 169 | + const markdown = '```javascript copy\nfunction test() {}\n```' |
| 170 | + |
| 171 | + const html = await renderContent(markdown) |
| 172 | + |
| 173 | + // Should render with copy button |
| 174 | + expect(html).toContain('code-example') |
| 175 | + expect(html).toContain('js-btn-copy') |
| 176 | + expect(html).toContain('language-javascript') |
| 177 | + }) |
| 178 | + }) |
| 179 | + |
| 180 | + describe('URL encoding', () => { |
| 181 | + it('should properly encode special characters in prompt URLs', async () => { |
| 182 | + const markdown = '```copilot copy prompt\nHow do I handle "quotes" and & symbols?\n```' |
| 183 | + |
| 184 | + const html = await renderContent(markdown) |
| 185 | + |
| 186 | + // Should encode quotes and ampersands properly |
| 187 | + expect(html).toContain('%22quotes%22') |
| 188 | + expect(html).toContain('%26%20symbols') |
| 189 | + }) |
| 190 | + |
| 191 | + it('should handle multiline prompts correctly', async () => { |
| 192 | + const markdown = `\`\`\`copilot copy prompt |
| 193 | +This is line 1 |
| 194 | +This is line 2 |
| 195 | +\`\`\`` |
| 196 | + |
| 197 | + const html = await renderContent(markdown) |
| 198 | + |
| 199 | + // Should encode newlines properly |
| 200 | + expect(html).toContain('This%20is%20line%201%0AThis%20is%20line%202') |
| 201 | + }) |
| 202 | + }) |
| 203 | +}) |
0 commit comments