-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfrontmatter_command_test.go
More file actions
194 lines (186 loc) · 4.93 KB
/
frontmatter_command_test.go
File metadata and controls
194 lines (186 loc) · 4.93 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
package markdown
import (
"testing"
"github.com/kitproj/coding-context-cli/pkg/codingcontext/urn"
"gopkg.in/yaml.v3"
)
// mustParseURN is a test helper that parses a URN string and panics on error
func mustParseURNCommand(s string) *urn.URN {
u, ok := urn.Parse([]byte(s))
if !ok {
panic("failed to parse URN: " + s)
}
return u
}
func TestCommandFrontMatter_Marshal(t *testing.T) {
tests := []struct {
name string
command CommandFrontMatter
want string
}{
{
name: "minimal command",
command: CommandFrontMatter{},
want: "{}\n",
},
{
name: "command with standard id, name, description",
command: CommandFrontMatter{
BaseFrontMatter: BaseFrontMatter{
URN: mustParseURNCommand("urn:agents:command:standard"),
Name: "Standard Command",
Description: "This is a standard command with metadata",
},
},
want: `urn: urn:agents:command:standard
name: Standard Command
description: This is a standard command with metadata
`,
},
{
name: "command with expand false",
command: CommandFrontMatter{
BaseFrontMatter: BaseFrontMatter{
URN: mustParseURNCommand("urn:agents:command:no-expand"),
Name: "No Expand Command",
Description: "Command with expansion disabled",
},
ExpandParams: func() *bool {
b := false
return &b
}(),
},
want: `urn: urn:agents:command:no-expand
name: No Expand Command
description: Command with expansion disabled
expand: false
`,
},
{
name: "command with selectors",
command: CommandFrontMatter{
BaseFrontMatter: BaseFrontMatter{
URN: mustParseURNCommand("urn:agents:command:selector"),
Name: "Selector Command",
Description: "Command with selectors",
},
Selectors: map[string]any{
"database": "postgres",
"feature": "auth",
},
},
want: `urn: urn:agents:command:selector
name: Selector Command
description: Command with selectors
selectors:
database: postgres
feature: auth
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := yaml.Marshal(&tt.command)
if err != nil {
t.Fatalf("Marshal() error = %v", err)
}
if string(got) != tt.want {
t.Errorf("Marshal() = %q, want %q", string(got), tt.want)
}
})
}
}
func TestCommandFrontMatter_Unmarshal(t *testing.T) {
tests := []struct {
name string
yaml string
want CommandFrontMatter
wantErr bool
}{
{
name: "command with standard id, name, description",
yaml: `urn: urn:agents:command:named
name: Named Command
description: A command with standard fields
`,
want: CommandFrontMatter{
BaseFrontMatter: BaseFrontMatter{
URN: mustParseURNCommand("urn:agents:command:named"),
Name: "Named Command",
Description: "A command with standard fields",
},
},
},
{
name: "command with expand false",
yaml: `urn: urn:agents:command:no-expand
name: No Expand
description: No expansion
expand: false
`,
want: CommandFrontMatter{
BaseFrontMatter: BaseFrontMatter{
URN: mustParseURNCommand("urn:agents:command:no-expand"),
Name: "No Expand",
Description: "No expansion",
},
ExpandParams: func() *bool {
b := false
return &b
}(),
},
},
{
name: "command with selectors",
yaml: `urn: urn:agents:command:selector
name: Selector Command
description: Has selectors
selectors:
database: postgres
feature: auth
`,
want: CommandFrontMatter{
BaseFrontMatter: BaseFrontMatter{
URN: mustParseURNCommand("urn:agents:command:selector"),
Name: "Selector Command",
Description: "Has selectors",
},
Selectors: map[string]any{
"database": "postgres",
"feature": "auth",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var got CommandFrontMatter
err := yaml.Unmarshal([]byte(tt.yaml), &got)
if (err != nil) != tt.wantErr {
t.Fatalf("Unmarshal() error = %v, wantErr %v", err, tt.wantErr)
}
if err != nil {
return
}
// Compare fields individually
if (got.URN == nil) != (tt.want.URN == nil) {
t.Errorf("URN = %v, want %v", got.URN, tt.want.URN)
} else if got.URN != nil && tt.want.URN != nil && !got.URN.Equal(tt.want.URN) {
t.Errorf("URN = %q, want %q", got.URN, tt.want.URN)
}
if got.Name != tt.want.Name {
t.Errorf("Name = %q, want %q", got.Name, tt.want.Name)
}
if got.Description != tt.want.Description {
t.Errorf("Description = %q, want %q", got.Description, tt.want.Description)
}
if (got.ExpandParams == nil) != (tt.want.ExpandParams == nil) {
t.Errorf("ExpandParams nil mismatch: got %v, want %v", got.ExpandParams == nil, tt.want.ExpandParams == nil)
} else if got.ExpandParams != nil && tt.want.ExpandParams != nil {
if *got.ExpandParams != *tt.want.ExpandParams {
t.Errorf("ExpandParams = %v, want %v", *got.ExpandParams, *tt.want.ExpandParams)
}
}
})
}
}