-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmermaid_test.go
More file actions
199 lines (144 loc) · 4.23 KB
/
mermaid_test.go
File metadata and controls
199 lines (144 loc) · 4.23 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
package hype
import (
"context"
"errors"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func Test_Mermaid_Execute(t *testing.T) {
t.Parallel()
t.Run("basic graph", func(t *testing.T) {
t.Parallel()
r := require.New(t)
p := testParser(t, "testdata/mermaid/graph")
doc, err := p.ParseFile("hype.md")
r.NoError(err)
err = doc.Execute(context.Background())
r.NoError(err)
act := doc.String()
// The rendered output should contain ASCII art
r.Contains(act, "Start")
r.Contains(act, "End")
// Should have box-drawing characters or dashes for arrows
r.True(strings.Contains(act, "─") || strings.Contains(act, "-"))
})
t.Run("sequence diagram", func(t *testing.T) {
t.Parallel()
r := require.New(t)
p := testParser(t, "testdata/mermaid/sequence")
doc, err := p.ParseFile("hype.md")
r.NoError(err)
err = doc.Execute(context.Background())
r.NoError(err)
act := doc.String()
// The rendered output should contain participant names
r.Contains(act, "Alice")
r.Contains(act, "Bob")
})
t.Run("empty source", func(t *testing.T) {
t.Parallel()
r := require.New(t)
el := NewEl("code", nil)
el.Nodes = Nodes{}
_, err := NewMermaid(el)
r.Error(err)
r.Contains(err.Error(), "empty")
})
t.Run("invalid mermaid syntax", func(t *testing.T) {
t.Parallel()
r := require.New(t)
p := testParser(t, "testdata/mermaid/invalid")
doc, err := p.ParseFile("hype.md")
r.NoError(err)
err = doc.Execute(context.Background())
r.Error(err)
})
}
func Test_Mermaid_MD(t *testing.T) {
t.Parallel()
r := require.New(t)
p := testParser(t, "testdata/mermaid/graph")
doc, err := p.ParseFile("hype.md")
r.NoError(err)
err = doc.Execute(context.Background())
r.NoError(err)
md := doc.MD()
// Should be wrapped in code fences
r.Contains(md, "```")
// Should contain the rendered diagram
r.Contains(md, "Start")
}
func Test_Mermaid_String(t *testing.T) {
t.Parallel()
r := require.New(t)
p := testParser(t, "testdata/mermaid/graph")
doc, err := p.ParseFile("hype.md")
r.NoError(err)
err = doc.Execute(context.Background())
r.NoError(err)
html := doc.String()
// Should be wrapped in pre/code tags
r.Contains(html, "<pre><code")
r.Contains(html, "</code></pre>")
// Should NOT have nested pre tags (bug fix verification)
r.NotContains(html, "<pre><pre>")
r.NotContains(html, "</pre></pre>")
}
func Test_Mermaid_MarshalJSON(t *testing.T) {
t.Parallel()
r := require.New(t)
// Create a Mermaid element directly for testing
el := NewEl("code", nil)
el.Nodes = Nodes{Text("graph LR\n A --> B")}
m, err := NewMermaid(el)
r.NoError(err)
// Manually set rendered output for testing
m.Rendered = "ASCII diagram output"
b, err := m.MarshalJSON()
r.NoError(err)
json := string(b)
r.Contains(json, `"type"`)
r.Contains(json, `"source"`)
r.Contains(json, `"rendered"`)
}
func Test_Mermaid_Execute_ErrorPropagation(t *testing.T) {
t.Parallel()
t.Run("error propagates through document execute", func(t *testing.T) {
t.Parallel()
r := require.New(t)
p := testParser(t, "testdata/mermaid/invalid")
doc, err := p.ParseFile("hype.md")
r.NoError(err)
err = doc.Execute(context.Background())
r.Error(err)
// Verify it's wrapped in ExecuteError
var execErr ExecuteError
r.True(errors.As(err, &execErr), "error should be ExecuteError, got: %T", err)
// Verify the filename is preserved in the error
r.NotEmpty(execErr.Filename, "ExecuteError should contain filename")
})
t.Run("error contains mermaid context", func(t *testing.T) {
t.Parallel()
r := require.New(t)
p := testParser(t, "testdata/mermaid/invalid")
doc, err := p.ParseFile("hype.md")
r.NoError(err)
err = doc.Execute(context.Background())
r.Error(err)
// Error message should indicate mermaid rendering failure
errStr := err.Error()
r.Contains(errStr, "mermaid", "error should mention mermaid")
})
t.Run("error unwraps to original mermaid error", func(t *testing.T) {
t.Parallel()
r := require.New(t)
p := testParser(t, "testdata/mermaid/invalid")
doc, err := p.ParseFile("hype.md")
r.NoError(err)
err = doc.Execute(context.Background())
r.Error(err)
// Should be able to unwrap to get more details
r.NotNil(errors.Unwrap(err), "error should be unwrappable")
})
}