-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathsimpleMermaidStringTest.test.ts
More file actions
202 lines (175 loc) · 6.33 KB
/
simpleMermaidStringTest.test.ts
File metadata and controls
202 lines (175 loc) · 6.33 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import { simpleMermaidStringTest, stripMarkdownMermaidFence } from './simpleMermaidStringTest'
describe('simpleMermaidStringTest', () => {
describe('bare keywords', () => {
const keywords = [
['flowchart', 'flowchart TD\n A --> B'],
['graph', 'graph LR\n A --> B'],
['sequenceDiagram', 'sequenceDiagram\n Alice->>Bob: Hi'],
['stateDiagram', 'stateDiagram-v2\n [*] --> Idle'],
['classDiagram', 'classDiagram\n class Animal'],
['erDiagram', 'erDiagram\n CUSTOMER ||--o{ ORDER : places'],
['journey', 'journey\n title My day'],
['gantt', 'gantt\n title A Gantt\n dateFormat YYYY-MM-DD'],
['pie', 'pie\n "Dogs" : 386'],
['gitGraph', 'gitGraph\n commit'],
['mindmap', 'mindmap\n root((Central))'],
['timeline', 'timeline\n title History'],
['sankey', 'sankey-beta'],
['xychart', 'xychart-beta'],
['block', 'block-beta'],
['quadrantChart', 'quadrantChart'],
['requirement', 'requirement test_req'],
['C4Context', 'C4Context\n Person(user, "User")'],
['C4Container', 'C4Container'],
['C4Component', 'C4Component'],
['C4Dynamic', 'C4Dynamic'],
['C4Deployment', 'C4Deployment'],
['packet', 'packet-beta'],
['kanban', 'kanban'],
['architecture', 'architecture-beta'],
['treemap', 'treemap-beta'],
['radar', 'radar-beta'],
['info', 'info'],
] as const
for (const [keyword, input] of keywords) {
it(`detects "${keyword}"`, () => {
expect(simpleMermaidStringTest(input)).toBe(true)
})
}
})
describe('with boilerplate stripped', () => {
it('detects keyword after YAML frontmatter', () => {
const text = '---\ntitle: test\n---\nflowchart TD\n A --> B'
expect(simpleMermaidStringTest(text)).toBe(true)
})
it('detects keyword after %%{init}%% directive', () => {
const text = '%%{init: {"theme":"dark"}}%%\nsequenceDiagram\n Alice->>Bob: Hi'
expect(simpleMermaidStringTest(text)).toBe(true)
})
it('detects keyword after %% line comments', () => {
const text = '%% this is a comment\nflowchart LR\n A --> B'
expect(simpleMermaidStringTest(text)).toBe(true)
})
it('detects keyword after frontmatter + directive + comment combined', () => {
const text = [
'---',
'title: combo',
'---',
'%%{init: {"theme":"forest"}}%%',
'%% a comment',
'stateDiagram-v2',
' [*] --> Idle',
].join('\n')
expect(simpleMermaidStringTest(text)).toBe(true)
})
it('detects keyword with leading whitespace', () => {
expect(simpleMermaidStringTest(' flowchart TD\n A --> B')).toBe(true)
expect(simpleMermaidStringTest('\t\tgantt\n title Plan')).toBe(true)
})
})
describe('markdown code fences', () => {
it('detects mermaid inside ```mermaid fence', () => {
const text = '```mermaid\nflowchart TD\n A --> B\n```'
expect(simpleMermaidStringTest(text)).toBe(true)
})
it('detects mermaid inside fence with extra backticks', () => {
const text = '````mermaid\nsequenceDiagram\n Alice->>Bob: Hi\n````'
expect(simpleMermaidStringTest(text)).toBe(true)
})
it('detects mermaid inside fence with leading whitespace', () => {
const text = ' ```mermaid\ngantt\n title Plan\n ```'
expect(simpleMermaidStringTest(text)).toBe(true)
})
it('strips fence and preserves inner boilerplate', () => {
const text = '```mermaid\n%%{init: {"theme":"dark"}}%%\nflowchart LR\n A --> B\n```'
expect(simpleMermaidStringTest(text)).toBe(true)
})
it('does not consume later non-mermaid fences in the same paste', () => {
const text = [
'```mermaid',
'flowchart TD',
' A --> B',
'```',
'',
'```javascript',
'const x = 1',
'```',
].join('\n')
expect(simpleMermaidStringTest(text)).toBe(true)
})
})
describe('negative cases', () => {
it('rejects plain English text', () => {
expect(simpleMermaidStringTest('Hello world')).toBe(false)
})
it('rejects text that merely mentions a keyword', () => {
expect(simpleMermaidStringTest('Let me think about flowcharts')).toBe(false)
expect(simpleMermaidStringTest('My flowchart TD\n A --> B')).toBe(false)
})
it('rejects empty string', () => {
expect(simpleMermaidStringTest('')).toBe(false)
})
it('rejects whitespace-only string', () => {
expect(simpleMermaidStringTest(' \n\t\n ')).toBe(false)
})
it('rejects JSON containing a keyword', () => {
expect(simpleMermaidStringTest('{"type": "flowchart", "nodes": []}')).toBe(false)
})
it('rejects HTML containing a keyword', () => {
expect(simpleMermaidStringTest('<div class="flowchart">content</div>')).toBe(false)
})
it('rejects a non-mermaid markdown code block', () => {
const text = '```javascript\nconst x = 1\n```'
expect(simpleMermaidStringTest(text)).toBe(false)
})
it('rejects a fence label that is not lowercase mermaid', () => {
const text = '```MERMAID\npie\n "A" : 1\n```'
expect(simpleMermaidStringTest(text)).toBe(false)
})
})
})
describe('stripMarkdownMermaidFence', () => {
it('extracts content from a mermaid fence', () => {
const text = '```mermaid\nflowchart TD\n A --> B\n```'
expect(stripMarkdownMermaidFence(text)).toBe('flowchart TD\n A --> B')
})
it('returns non-fenced text unchanged', () => {
const text = 'flowchart TD\n A --> B'
expect(stripMarkdownMermaidFence(text)).toBe(text)
})
it('returns non-mermaid fenced text unchanged', () => {
const text = '```javascript\nconst x = 1\n```'
expect(stripMarkdownMermaidFence(text)).toBe(text)
})
it('handles extra backticks', () => {
const text = '````mermaid\ngantt\n title Plan\n````'
expect(stripMarkdownMermaidFence(text)).toBe('gantt\n title Plan')
})
it('stops at the first closing fence when more blocks follow', () => {
const text = [
'```mermaid',
'flowchart TD',
' A --> B',
'```',
'',
'# More',
'',
'```js',
'x',
'```',
].join('\n')
expect(stripMarkdownMermaidFence(text)).toBe('flowchart TD\n A --> B')
})
it('does not close on a shorter inner fence than the opening run', () => {
const text = ['````mermaid', 'flowchart TD', ' A --> B', '```', ' note text', '````'].join(
'\n'
)
expect(stripMarkdownMermaidFence(text)).toBe(
['flowchart TD', ' A --> B', '```', ' note text'].join('\n')
)
})
it('handles CRLF line endings in the fence', () => {
const text = '```mermaid\r\nflowchart TD\r\n A --> B\r\n```'
expect(stripMarkdownMermaidFence(text)).toBe('flowchart TD\r\n A --> B')
})
})