|
| 1 | +import { describe, expect, it, vi, beforeAll } from 'vitest' |
| 2 | + |
| 3 | +// Mock the global Nuxt auto-import before importing the module |
| 4 | +beforeAll(() => { |
| 5 | + vi.stubGlobal( |
| 6 | + 'getShikiHighlighter', |
| 7 | + vi.fn().mockResolvedValue({ |
| 8 | + getLoadedLanguages: () => [], |
| 9 | + codeToHtml: (code: string) => `<pre><code>${code}</code></pre>`, |
| 10 | + }), |
| 11 | + ) |
| 12 | +}) |
| 13 | + |
| 14 | +// Import after mock is set up |
| 15 | +const { renderReadmeHtml } = await import('../../server/utils/readme') |
| 16 | + |
| 17 | +describe('Playground Link Extraction', () => { |
| 18 | + describe('StackBlitz', () => { |
| 19 | + it('extracts stackblitz.com links', async () => { |
| 20 | + const markdown = `Check out [Demo on StackBlitz](https://stackblitz.com/github/user/repo)` |
| 21 | + const result = await renderReadmeHtml(markdown, 'test-pkg') |
| 22 | + |
| 23 | + expect(result.playgroundLinks).toHaveLength(1) |
| 24 | + expect(result.playgroundLinks[0]).toMatchObject({ |
| 25 | + provider: 'stackblitz', |
| 26 | + providerName: 'StackBlitz', |
| 27 | + label: 'Demo on StackBlitz', |
| 28 | + url: 'https://stackblitz.com/github/user/repo', |
| 29 | + }) |
| 30 | + }) |
| 31 | + }) |
| 32 | + |
| 33 | + describe('CodeSandbox', () => { |
| 34 | + it('extracts codesandbox.io links', async () => { |
| 35 | + const markdown = `[Try it](https://codesandbox.io/s/example-abc123)` |
| 36 | + const result = await renderReadmeHtml(markdown, 'test-pkg') |
| 37 | + |
| 38 | + expect(result.playgroundLinks).toHaveLength(1) |
| 39 | + expect(result.playgroundLinks[0]).toMatchObject({ |
| 40 | + provider: 'codesandbox', |
| 41 | + providerName: 'CodeSandbox', |
| 42 | + }) |
| 43 | + }) |
| 44 | + |
| 45 | + it('extracts githubbox.com links as CodeSandbox', async () => { |
| 46 | + const markdown = `[Demo](https://githubbox.com/user/repo/tree/main/examples)` |
| 47 | + const result = await renderReadmeHtml(markdown, 'test-pkg') |
| 48 | + |
| 49 | + expect(result.playgroundLinks).toHaveLength(1) |
| 50 | + expect(result.playgroundLinks[0].provider).toBe('codesandbox') |
| 51 | + }) |
| 52 | + }) |
| 53 | + |
| 54 | + describe('Other Providers', () => { |
| 55 | + it('extracts CodePen links', async () => { |
| 56 | + const markdown = `[Pen](https://codepen.io/user/pen/abc123)` |
| 57 | + const result = await renderReadmeHtml(markdown, 'test-pkg') |
| 58 | + |
| 59 | + expect(result.playgroundLinks[0].provider).toBe('codepen') |
| 60 | + }) |
| 61 | + |
| 62 | + it('extracts Replit links', async () => { |
| 63 | + const markdown = `[Repl](https://replit.com/@user/project)` |
| 64 | + const result = await renderReadmeHtml(markdown, 'test-pkg') |
| 65 | + |
| 66 | + expect(result.playgroundLinks[0].provider).toBe('replit') |
| 67 | + }) |
| 68 | + |
| 69 | + it('extracts Gitpod links', async () => { |
| 70 | + const markdown = `[Open in Gitpod](https://gitpod.io/#https://github.com/user/repo)` |
| 71 | + const result = await renderReadmeHtml(markdown, 'test-pkg') |
| 72 | + |
| 73 | + expect(result.playgroundLinks[0].provider).toBe('gitpod') |
| 74 | + }) |
| 75 | + }) |
| 76 | + |
| 77 | + describe('Multiple Links', () => { |
| 78 | + it('extracts multiple playground links', async () => { |
| 79 | + const markdown = ` |
| 80 | +- [StackBlitz](https://stackblitz.com/example1) |
| 81 | +- [CodeSandbox](https://codesandbox.io/s/example2) |
| 82 | +` |
| 83 | + const result = await renderReadmeHtml(markdown, 'test-pkg') |
| 84 | + |
| 85 | + expect(result.playgroundLinks).toHaveLength(2) |
| 86 | + expect(result.playgroundLinks[0].provider).toBe('stackblitz') |
| 87 | + expect(result.playgroundLinks[1].provider).toBe('codesandbox') |
| 88 | + }) |
| 89 | + |
| 90 | + it('deduplicates same URL', async () => { |
| 91 | + const markdown = ` |
| 92 | +[Demo 1](https://stackblitz.com/example) |
| 93 | +[Demo 2](https://stackblitz.com/example) |
| 94 | +` |
| 95 | + const result = await renderReadmeHtml(markdown, 'test-pkg') |
| 96 | + |
| 97 | + expect(result.playgroundLinks).toHaveLength(1) |
| 98 | + }) |
| 99 | + }) |
| 100 | + |
| 101 | + describe('Non-Playground Links', () => { |
| 102 | + it('ignores regular GitHub links', async () => { |
| 103 | + const markdown = `[Repo](https://github.com/user/repo)` |
| 104 | + const result = await renderReadmeHtml(markdown, 'test-pkg') |
| 105 | + |
| 106 | + expect(result.playgroundLinks).toHaveLength(0) |
| 107 | + }) |
| 108 | + |
| 109 | + it('ignores npm links', async () => { |
| 110 | + const markdown = `[Package](https://npmjs.com/package/test)` |
| 111 | + const result = await renderReadmeHtml(markdown, 'test-pkg') |
| 112 | + |
| 113 | + expect(result.playgroundLinks).toHaveLength(0) |
| 114 | + }) |
| 115 | + }) |
| 116 | + |
| 117 | + describe('Edge Cases', () => { |
| 118 | + it('returns empty array for empty content', async () => { |
| 119 | + const result = await renderReadmeHtml('', 'test-pkg') |
| 120 | + |
| 121 | + expect(result.playgroundLinks).toEqual([]) |
| 122 | + expect(result.html).toBe('') |
| 123 | + }) |
| 124 | + |
| 125 | + it('handles badge images wrapped in links', async () => { |
| 126 | + const markdown = `[](https://stackblitz.com/example)` |
| 127 | + const result = await renderReadmeHtml(markdown, 'test-pkg') |
| 128 | + |
| 129 | + expect(result.playgroundLinks).toHaveLength(1) |
| 130 | + expect(result.playgroundLinks[0].provider).toBe('stackblitz') |
| 131 | + }) |
| 132 | + }) |
| 133 | +}) |
0 commit comments