-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_test.go
More file actions
98 lines (95 loc) · 2.39 KB
/
Copy pathparse_test.go
File metadata and controls
98 lines (95 loc) · 2.39 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
package mneme
import "testing"
func TestParseExtraction(t *testing.T) {
tests := []struct {
name string
raw string
want []string // expected texts in order
}{
{
name: "clean object",
raw: `{"memory":[{"id":"0","text":"Alice drives a Ferrari","attributed_to":"user"}]}`,
want: []string{"Alice drives a Ferrari"},
},
{
name: "fenced json",
raw: "```json\n{\"memory\":[{\"id\":\"0\",\"text\":\"fact one\"}]}\n```",
want: []string{"fact one"},
},
{
name: "fenced no language tag",
raw: "```\n{\"memory\":[{\"id\":\"0\",\"text\":\"fact\"}]}\n```",
want: []string{"fact"},
},
{
name: "prose wrapped",
raw: `Sure! Here are the facts: {"memory":[{"id":"0","text":"prose fact"}]} Let me know if you need more.`,
want: []string{"prose fact"},
},
{
name: "empty memory list",
raw: `{"memory":[]}`,
want: nil,
},
{
name: "bare array fallback",
raw: `[{"id":"0","text":"array fact"}]`,
want: []string{"array fact"},
},
{
name: "drops empty text",
raw: `{"memory":[{"id":"0","text":""},{"id":"1","text":" "},{"id":"2","text":"keeper"}]}`,
want: []string{"keeper"},
},
{
name: "trims whitespace",
raw: `{"memory":[{"id":"0","text":" spaced fact "}]}`,
want: []string{"spaced fact"},
},
{
name: "garbage returns nil",
raw: `I could not produce JSON, sorry.`,
want: nil,
},
{
name: "truncated json returns nil",
raw: `{"memory":[{"id":"0","text":"unterminated`,
want: nil,
},
{
name: "empty string",
raw: ``,
want: nil,
},
{
name: "braces inside string value",
raw: `{"memory":[{"id":"0","text":"uses {curly} braces"}]}`,
want: []string{"uses {curly} braces"},
},
{
name: "multiple facts order preserved",
raw: `{"memory":[{"id":"0","text":"first"},{"id":"1","text":"second"},{"id":"2","text":"third"}]}`,
want: []string{"first", "second", "third"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := parseExtraction(tt.raw)
if len(got) != len(tt.want) {
t.Fatalf("got %d facts %v, want %d %v", len(got), texts(got), len(tt.want), tt.want)
}
for i := range tt.want {
if got[i].Text != tt.want[i] {
t.Errorf("fact[%d] = %q, want %q", i, got[i].Text, tt.want[i])
}
}
})
}
}
func texts(fs []extractedFact) []string {
out := make([]string, len(fs))
for i, f := range fs {
out[i] = f.Text
}
return out
}