-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetadata_test.go
More file actions
373 lines (332 loc) · 11.9 KB
/
Copy pathmetadata_test.go
File metadata and controls
373 lines (332 loc) · 11.9 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
* Copyright 2025 - 2026 Zigflow authors <https://github.com/zigflow/schema/graphs/contributors>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package schema
import (
"testing"
"github.com/google/jsonschema-go/jsonschema"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// isOpenSchema returns true when s represents an open (additionalProperties:
// true) schema. trueSchema() returns &jsonschema.Schema{} (Not == nil).
// falseSchema() returns &jsonschema.Schema{Not: &jsonschema.Schema{}} (Not != nil).
func isOpenSchema(s *jsonschema.Schema) bool {
return s != nil && s.Not == nil
}
// resolvedTestSchema builds and resolves the schema once for use in
// validation tests.
func resolvedTestSchema(t *testing.T) *jsonschema.Resolved {
t.Helper()
s, err := BuildSchema("1.0.0", "json")
require.NoError(t, err)
resolved, err := s.Resolve(nil)
require.NoError(t, err)
return resolved
}
// withDocumentMetadata returns a copy of minimalWorkflow() with the given
// map merged into document.metadata.
func withDocumentMetadata(meta map[string]any) map[string]any {
doc := minimalWorkflow()
document := doc["document"].(map[string]any)
document["metadata"] = meta
return doc
}
// withTaskMetadata returns a copy of minimalWorkflow() where the single
// task body has the given map as its metadata.
func withTaskMetadata(meta map[string]any) map[string]any {
doc := minimalWorkflow()
tasks := doc["do"].([]any)
taskItem := tasks[0].(map[string]any)
for taskName, rawBody := range taskItem {
body := rawBody.(map[string]any)
body["metadata"] = meta
taskItem[taskName] = body
}
return doc
}
// --- definition-presence and shape tests -------------------------------------
// TestMetadataDefinitionsPresent verifies that all three metadata-related
// definitions are registered in buildDefinitions().
func TestMetadataDefinitionsPresent(t *testing.T) {
defs := buildDefinitions()
for _, key := range []string{defCommonMetadata, defDocumentMetadata, defTaskMetadata} {
assert.Contains(t, defs, key, "buildDefinitions() must contain %q", key)
}
}
// TestCommonMetadataDefinitionShape verifies that commonMetadataDefinition
// declares activityOptions with all its expected sub-properties, and that the
// definition is intentionally open.
func TestCommonMetadataDefinitionShape(t *testing.T) {
def := commonMetadataDefinition
assert.Equal(t, typeObject, def.Type)
assert.True(t, isOpenSchema(def.AdditionalProperties),
"commonMetadata must be open (additionalProperties: true)")
require.Contains(t, def.Properties, propActivityOptions)
actOpts := def.Properties[propActivityOptions]
// All expected activity-options sub-keys must be present.
for _, key := range []string{
propDisableEager,
propHeartbeatTimeout,
propRetryPolicy,
"scheduleToCloseTimeout",
"scheduleToStartTimeout",
propStartToCloseTimeout,
propSummary,
} {
assert.Contains(t, actOpts.Properties, key,
"activityOptions must have property %q", key)
}
assert.Equal(t, typeBoolean, actOpts.Properties[propDisableEager].Type)
assert.Equal(t, SchemaRef("duration"), actOpts.Properties[propHeartbeatTimeout].Ref)
assert.Equal(t, SchemaRef("duration"), actOpts.Properties["scheduleToCloseTimeout"].Ref)
assert.Equal(t, SchemaRef("duration"), actOpts.Properties["scheduleToStartTimeout"].Ref)
assert.Equal(t, SchemaRef("duration"), actOpts.Properties[propStartToCloseTimeout].Ref)
assert.Equal(t, typeString, actOpts.Properties[propSummary].Type)
}
// TestRetryPolicyDefinitionShape verifies that retryPolicy inside
// activityOptions declares its expected properties and is intentionally
// closed (additionalProperties: false) so that unknown retry keys are
// rejected.
func TestRetryPolicyDefinitionShape(t *testing.T) {
actOpts := commonMetadataDefinition.Properties[propActivityOptions]
require.Contains(t, actOpts.Properties, propRetryPolicy)
rp := actOpts.Properties[propRetryPolicy]
assert.Equal(t, typeObject, rp.Type)
assert.False(t, isOpenSchema(rp.AdditionalProperties),
"retryPolicy must be closed (additionalProperties: false)")
for _, key := range []string{
"backoffCoefficient",
"initialInterval",
propMaximumAttempts,
"maximumInterval",
"nonRetryableErrorTypes",
} {
assert.Contains(t, rp.Properties, key, "retryPolicy must have property %q", key)
}
assert.Equal(t, "number", rp.Properties["backoffCoefficient"].Type)
assert.Equal(t, typeInteger, rp.Properties[propMaximumAttempts].Type)
assert.Equal(t, SchemaRef("duration"), rp.Properties["initialInterval"].Ref)
assert.Equal(t, SchemaRef("duration"), rp.Properties["maximumInterval"].Ref)
nonRetryable := rp.Properties["nonRetryableErrorTypes"]
assert.Equal(t, typeArray, nonRetryable.Type)
require.NotNil(t, nonRetryable.Items)
assert.Equal(t, typeString, nonRetryable.Items.Type)
}
// TestDocumentMetadataDefinitionShape verifies that documentMetadataDefinition
// declares all expected Zigflow schedule/document metadata keys and is
// intentionally open.
func TestDocumentMetadataDefinitionShape(t *testing.T) {
def := documentMetadataDefinition
assert.Equal(t, typeObject, def.Type)
assert.True(t, isOpenSchema(def.AdditionalProperties),
"documentMetadata must be open (additionalProperties: true)")
for _, key := range []string{
propCanMaxHistory,
propScheduleWorkflowName,
propScheduleID,
propScheduleInput,
} {
assert.Contains(t, def.Properties, key,
"documentMetadata must have property %q", key)
}
assert.Equal(t, typeInteger, def.Properties[propCanMaxHistory].Type)
assert.Equal(t, typeString, def.Properties[propScheduleWorkflowName].Type)
assert.Equal(t, typeString, def.Properties[propScheduleID].Type)
assert.Equal(t, typeArray, def.Properties[propScheduleInput].Type)
}
// TestTaskMetadataDefinitionShape verifies that taskMetadataDefinition
// declares __zigflow_id and heartbeat, and is intentionally open.
func TestTaskMetadataDefinitionShape(t *testing.T) {
def := taskMetadataDefinition
assert.Equal(t, typeObject, def.Type)
assert.True(t, isOpenSchema(def.AdditionalProperties),
"taskMetadata must be open (additionalProperties: true)")
require.Contains(t, def.Properties, propZigflowID)
assert.Equal(t, typeString, def.Properties[propZigflowID].Type)
require.Contains(t, def.Properties, propHeartbeat)
assert.Equal(t, SchemaRef("duration"), def.Properties[propHeartbeat].Ref)
}
// TestTaskBaseMetadataAllOf verifies that taskBase wires metadata through
// both commonMetadata and taskMetadata.
func TestTaskBaseMetadataAllOf(t *testing.T) {
meta := taskBaseDefinition.Properties[propMetadata]
require.NotNil(t, meta, "taskBase must have a metadata property")
refs := schemaRefs(meta.AllOf)
assert.Contains(t, refs, SchemaRef(defCommonMetadata))
assert.Contains(t, refs, SchemaRef("taskMetadata"))
}
// TestDocumentMetadataAllOf verifies that the document.metadata property in
// the root schema wires through both commonMetadata and documentMetadata.
func TestDocumentMetadataAllOf(t *testing.T) {
meta := schemaProperties[propDocument].Properties[propMetadata]
require.NotNil(t, meta, "document must have a metadata property")
refs := schemaRefs(meta.AllOf)
assert.Contains(t, refs, SchemaRef(defCommonMetadata))
assert.Contains(t, refs, SchemaRef(defDocumentMetadata))
}
// --- validation tests ---------------------------------------------------------
// TestDocumentMetadata_Validation exercises the full schema validator against
// various document.metadata objects.
func TestDocumentMetadata_Validation(t *testing.T) {
resolved := resolvedTestSchema(t)
tests := []struct {
name string
meta map[string]any
expectError bool
}{
// ---- valid cases ----
{
name: "scheduleWorkflowName string is accepted",
meta: map[string]any{"scheduleWorkflowName": "my-workflow"},
},
{
name: "scheduleId string is accepted",
meta: map[string]any{"scheduleId": "my-schedule-id"},
},
{
name: "scheduleInput array is accepted",
meta: map[string]any{"scheduleInput": []any{map[string]any{"key": "val"}}},
},
{
name: "canMaxHistoryLength integer is accepted",
meta: map[string]any{propCanMaxHistory: 100},
},
{
name: "unknown metadata key is accepted (open schema)",
meta: map[string]any{"arbitrary-user-key": "anything"},
},
{
name: "activityOptions with valid startToCloseTimeout is accepted",
meta: map[string]any{
propActivityOptions: map[string]any{
"startToCloseTimeout": map[string]any{propSeconds: 30},
},
},
},
{
name: "activityOptions with valid retryPolicy is accepted",
meta: map[string]any{
propActivityOptions: map[string]any{
propRetryPolicy: map[string]any{"maximumAttempts": 3},
},
},
},
// ---- invalid cases ----
{
name: "scheduleWorkflowName as integer is rejected",
meta: map[string]any{"scheduleWorkflowName": 42},
expectError: true,
},
{
name: "canMaxHistoryLength as string is rejected",
meta: map[string]any{propCanMaxHistory: "not-an-int"},
expectError: true,
},
{
name: "activityOptions.disableEagerExecution as string is rejected",
meta: map[string]any{propActivityOptions: map[string]any{propDisableEager: "yes"}},
expectError: true,
},
{
name: "unknown key in retryPolicy is rejected (closed schema)",
meta: map[string]any{
propActivityOptions: map[string]any{
propRetryPolicy: map[string]any{"unknownRetryKey": "val"},
},
},
expectError: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
doc := withDocumentMetadata(tc.meta)
err := resolved.Validate(doc)
if tc.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}
// TestTaskMetadata_Validation exercises the full schema validator against
// various task metadata objects embedded in a set task.
func TestTaskMetadata_Validation(t *testing.T) {
resolved := resolvedTestSchema(t)
tests := []struct {
name string
meta map[string]any
expectError bool
}{
// ---- valid cases ----
{
name: "__zigflow_id string is accepted",
meta: map[string]any{propZigflowID: "abc-123"},
},
{
name: "heartbeat duration object is accepted",
meta: map[string]any{propHeartbeat: map[string]any{propSeconds: 30}},
},
{
name: "unknown task metadata key is accepted (open schema)",
meta: map[string]any{"arbitrary-task-key": "anything"},
},
{
name: "activityOptions with valid heartbeatTimeout is accepted",
meta: map[string]any{
propActivityOptions: map[string]any{
propHeartbeatTimeout: map[string]any{propMinutes: 1},
},
},
},
// ---- invalid cases ----
{
name: "__zigflow_id as integer is rejected",
meta: map[string]any{propZigflowID: 99},
expectError: true,
},
{
name: "heartbeat as plain string is rejected",
meta: map[string]any{propHeartbeat: "30s"},
expectError: true,
},
{
name: "unknown key in retryPolicy is rejected (closed schema)",
meta: map[string]any{
propActivityOptions: map[string]any{
propRetryPolicy: map[string]any{"bogusKey": true},
},
},
expectError: true,
},
{
name: "activityOptions.disableEagerExecution as string is rejected",
meta: map[string]any{propActivityOptions: map[string]any{propDisableEager: "true"}},
expectError: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
doc := withTaskMetadata(tc.meta)
err := resolved.Validate(doc)
if tc.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}