-
Notifications
You must be signed in to change notification settings - Fork 328
Expand file tree
/
Copy pathschema_test.go
More file actions
196 lines (171 loc) · 4.71 KB
/
schema_test.go
File metadata and controls
196 lines (171 loc) · 4.71 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
//go:build !integration
package parser
import (
"os"
"strings"
"testing"
)
func TestValidateWithSchema(t *testing.T) {
tests := []struct {
name string
frontmatter map[string]any
schema string
context string
wantErr bool
errContains string
}{
{
name: "valid data with simple schema",
frontmatter: map[string]any{
"name": "test",
},
schema: `{
"type": "object",
"properties": {
"name": {"type": "string"}
},
"additionalProperties": false
}`,
context: "test context",
wantErr: false,
},
{
name: "invalid data with additional property",
frontmatter: map[string]any{
"name": "test",
"invalid": "value",
},
schema: `{
"type": "object",
"properties": {
"name": {"type": "string"}
},
"additionalProperties": false
}`,
context: "test context",
wantErr: true,
errContains: "additional properties 'invalid' not allowed",
},
{
name: "invalid schema JSON",
frontmatter: map[string]any{
"name": "test",
},
schema: `invalid json`,
context: "test context",
wantErr: true,
errContains: "schema validation error for test context",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validateWithSchema(tt.frontmatter, tt.schema, tt.context)
if tt.wantErr && err == nil {
t.Errorf("validateWithSchema() expected error, got nil")
return
}
if !tt.wantErr && err != nil {
t.Errorf("validateWithSchema() error = %v", err)
return
}
if tt.wantErr && err != nil && tt.errContains != "" {
if !strings.Contains(err.Error(), tt.errContains) {
t.Errorf("validateWithSchema() error = %v, expected to contain %v", err, tt.errContains)
}
}
})
}
}
func TestValidateWithSchemaAndLocation_CleanedErrorMessage(t *testing.T) {
// Test that error messages are properly cleaned of unhelpful jsonschema prefixes
frontmatter := map[string]any{
"on": "push",
"timeout_minu tes": 10, // Invalid property name with space
}
// Create a temporary test file
tempFile := "/tmp/gh-aw/test_schema_validation.md"
// Ensure the directory exists
if err := os.MkdirAll("/tmp/gh-aw", 0755); err != nil {
t.Fatalf("Failed to create temp directory: %v", err)
}
err := os.WriteFile(tempFile, []byte(`---
on: push
timeout_minu tes: 10
---
# Test workflow`), 0644)
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
defer os.Remove(tempFile)
err = ValidateMainWorkflowFrontmatterWithSchemaAndLocation(frontmatter, tempFile)
if err == nil {
t.Fatal("Expected validation error, got nil")
}
errorMsg := err.Error()
// The error message should NOT contain the unhelpful jsonschema prefixes
if strings.Contains(errorMsg, "jsonschema validation failed") {
t.Errorf("Error message should not contain 'jsonschema validation failed' prefix, got: %s", errorMsg)
}
if strings.Contains(errorMsg, "- at '': ") {
t.Errorf("Error message should not contain '- at '':' prefix, got: %s", errorMsg)
}
// The error message should contain the friendly rewritten error description
if !strings.Contains(errorMsg, "Unknown property: timeout_minu tes") {
t.Errorf("Error message should contain the validation error, got: %s", errorMsg)
}
// The error message should be formatted with location information
if !strings.Contains(errorMsg, tempFile) {
t.Errorf("Error message should contain file path, got: %s", errorMsg)
}
}
// TestGetSafeOutputTypeKeys tests extracting safe output type keys from the embedded schema
func TestGetSafeOutputTypeKeys(t *testing.T) {
keys, err := GetSafeOutputTypeKeys()
if err != nil {
t.Fatalf("GetSafeOutputTypeKeys() returned error: %v", err)
}
// Should return multiple keys
if len(keys) == 0 {
t.Error("GetSafeOutputTypeKeys() returned empty list")
}
// Should include known safe output types
expectedKeys := []string{
"create-issue",
"add-comment",
"create-discussion",
"create-pull-request",
"update-issue",
}
keySet := make(map[string]bool)
for _, key := range keys {
keySet[key] = true
}
for _, expected := range expectedKeys {
if !keySet[expected] {
t.Errorf("GetSafeOutputTypeKeys() missing expected key: %s", expected)
}
}
// Should NOT include meta-configuration fields
metaFields := []string{
"allowed-domains",
"staged",
"env",
"github-token",
"github-app",
"max-patch-size",
"jobs",
"runs-on",
"messages",
}
for _, meta := range metaFields {
if keySet[meta] {
t.Errorf("GetSafeOutputTypeKeys() should not include meta field: %s", meta)
}
}
// Keys should be sorted
for i := 1; i < len(keys); i++ {
if keys[i-1] > keys[i] {
t.Errorf("GetSafeOutputTypeKeys() keys are not sorted: %s > %s", keys[i-1], keys[i])
}
}
}