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