-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathai-monitoring-tmpl_test.go
66 lines (54 loc) · 1.75 KB
/
ai-monitoring-tmpl_test.go
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
package main
import (
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io"
"os"
"strings"
"testing"
)
func Test_RenderAiCompatDoc(t *testing.T) {
t.Run("successfully renders document", func(t *testing.T) {
expected, err := os.ReadFile("testdata/ai-compat.expected.md")
require.Nil(t, err)
builder := strings.Builder{}
err = RenderAiCompatDoc("testdata/ai-compat.json", &builder)
assert.Nil(t, err)
assert.Equal(t, string(expected), builder.String())
})
t.Run("errors if cannot open json file", func(t *testing.T) {
err := RenderAiCompatDoc("testdata/bad-file.does.not.exist", io.Discard)
assert.ErrorContains(t, err, "could not read descriptor json file")
})
t.Run("errors if json is bad", func(t *testing.T) {
currFS := appFS
t.Cleanup(func() {
appFS = currFS
})
appFS = afero.NewMemMapFs()
file, err := appFS.Create("foo.json")
require.Nil(t, err)
io.WriteString(file, `{"bad":json`)
err = RenderAiCompatDoc("foo.json", io.Discard)
assert.ErrorContains(t, err, "could not parse descriptor json file")
})
t.Run("errors if template is invalid", func(t *testing.T) {
curTmplString := aiMonitoringTmplString
t.Cleanup(func() {
aiMonitoringTmplString = curTmplString
})
aiMonitoringTmplString = "{{bad}"
err := RenderAiCompatDoc("testdata/ai-compat.json", io.Discard)
assert.ErrorContains(t, err, "could not load template")
})
t.Run("errors if rendering fails", func(t *testing.T) {
curTmplString := aiMonitoringTmplString
t.Cleanup(func() {
aiMonitoringTmplString = curTmplString
})
aiMonitoringTmplString = "{{.Text}}"
err := RenderAiCompatDoc("testdata/ai-compat.json", io.Discard)
assert.ErrorContains(t, err, "failed to render template")
})
}