Skip to content

Commit 2ab0c8d

Browse files
authored
fix: resolve duplicate test declarations and fix wantOperation defaults (#2737)
## Summary Fixes compilation errors introduced by merging overlapping test PRs that created duplicate test function declarations across files. ## Changes ### Duplicate test removal - **`internal/difc/evaluator_test.go`**: Removed duplicate `TestNewEvaluatorWithMode` (comprehensive table-driven version exists in `difc_test.go`) - **`internal/guard/guard_test.go`**: Removed duplicate `TestParseResourceResponse` (11 subtests) and `TestParseCollectionLabeledData` (8 subtests) — comprehensive versions with 22+ and 16+ subtests respectively exist in `wasm_response_parse_test.go` ### Test bug fixes - **`internal/guard/wasm_response_parse_test.go`**: Fixed 6 test cases that omitted `wantOperation`, causing them to default to `OperationRead` (0) while `parseResourceResponse` defaults missing operations to `OperationWrite` (1). Added `wantOperation: difc.OperationWrite` to: - `description field is optional` - `non-string tags in secrecy array are skipped` - `non-string tags in integrity array are skipped` - `secrecy value is not an array (string) defaults to empty label` - `integrity value is not an array (map) defaults to empty label` - `description field is not a string - ignored` ### Formatting - Applied `gofmt` alignment fixes to `wasm_response_parse_test.go` ## Verification `make agent-finished` passes: format ✅ build ✅ lint ✅ all tests ✅
2 parents 408f6b0 + 8b07b7b commit 2ab0c8d

3 files changed

Lines changed: 18 additions & 296 deletions

File tree

internal/difc/evaluator_test.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -776,26 +776,6 @@ func TestParseEnforcementMode(t *testing.T) {
776776
}
777777
}
778778

779-
// TestNewEvaluatorWithMode tests creating evaluator with specific mode
780-
func TestNewEvaluatorWithMode(t *testing.T) {
781-
t.Run("creates evaluator with strict mode", func(t *testing.T) {
782-
eval := NewEvaluatorWithMode(EnforcementStrict)
783-
assert.Equal(t, EnforcementStrict, eval.GetMode())
784-
})
785-
786-
t.Run("creates evaluator with propagate mode", func(t *testing.T) {
787-
eval := NewEvaluatorWithMode(EnforcementPropagate)
788-
assert.Equal(t, EnforcementPropagate, eval.GetMode())
789-
})
790-
791-
t.Run("SetMode changes mode", func(t *testing.T) {
792-
eval := NewEvaluator()
793-
assert.Equal(t, EnforcementStrict, eval.GetMode())
794-
eval.SetMode(EnforcementPropagate)
795-
assert.Equal(t, EnforcementPropagate, eval.GetMode())
796-
})
797-
}
798-
799779
// TestEvaluationResult_RequiresPropagation tests the RequiresPropagation method
800780
func TestEvaluationResult_RequiresPropagation(t *testing.T) {
801781
tests := []struct {

internal/guard/guard_test.go

Lines changed: 1 addition & 265 deletions
Original file line numberDiff line numberDiff line change
@@ -799,268 +799,4 @@ func TestIsValidAllowOnlyRepos(t *testing.T) {
799799
}
800800
}
801801

802-
func TestParseResourceResponse(t *testing.T) {
803-
t.Run("valid resource with secrecy and integrity labels and read operation", func(t *testing.T) {
804-
response := map[string]interface{}{
805-
"resource": map[string]interface{}{
806-
"description": "test resource",
807-
"secrecy": []interface{}{"private:owner/repo"},
808-
"integrity": []interface{}{"approved"},
809-
},
810-
"operation": "read",
811-
}
812-
813-
resource, operation, err := parseResourceResponse(response)
814-
require.NoError(t, err)
815-
require.NotNil(t, resource)
816-
assert.Equal(t, "test resource", resource.Description)
817-
assert.Equal(t, difc.OperationRead, operation)
818-
assert.Contains(t, resource.Secrecy.Label.GetTags(), difc.Tag("private:owner/repo"))
819-
assert.Contains(t, resource.Integrity.Label.GetTags(), difc.Tag("approved"))
820-
})
821-
822-
t.Run("write operation", func(t *testing.T) {
823-
response := map[string]interface{}{
824-
"resource": map[string]interface{}{},
825-
"operation": "write",
826-
}
827-
828-
_, operation, err := parseResourceResponse(response)
829-
require.NoError(t, err)
830-
assert.Equal(t, difc.OperationWrite, operation)
831-
})
832-
833-
t.Run("read-write operation", func(t *testing.T) {
834-
response := map[string]interface{}{
835-
"resource": map[string]interface{}{},
836-
"operation": "read-write",
837-
}
838-
839-
_, operation, err := parseResourceResponse(response)
840-
require.NoError(t, err)
841-
assert.Equal(t, difc.OperationReadWrite, operation)
842-
})
843-
844-
t.Run("missing operation defaults to write", func(t *testing.T) {
845-
response := map[string]interface{}{
846-
"resource": map[string]interface{}{"description": "no-op"},
847-
}
848-
849-
_, operation, err := parseResourceResponse(response)
850-
require.NoError(t, err)
851-
assert.Equal(t, difc.OperationWrite, operation, "missing operation should default to write (most restrictive)")
852-
})
853-
854-
t.Run("unknown operation string defaults to write", func(t *testing.T) {
855-
response := map[string]interface{}{
856-
"resource": map[string]interface{}{},
857-
"operation": "unknown-op",
858-
}
859-
860-
_, operation, err := parseResourceResponse(response)
861-
require.NoError(t, err)
862-
assert.Equal(t, difc.OperationWrite, operation)
863-
})
864-
865-
t.Run("missing resource key returns error", func(t *testing.T) {
866-
response := map[string]interface{}{
867-
"operation": "read",
868-
}
869-
870-
resource, _, err := parseResourceResponse(response)
871-
require.Error(t, err)
872-
assert.Nil(t, resource)
873-
assert.Contains(t, err.Error(), "invalid resource format")
874-
})
875-
876-
t.Run("resource not a map returns error", func(t *testing.T) {
877-
response := map[string]interface{}{
878-
"resource": "not-a-map",
879-
"operation": "read",
880-
}
881-
882-
resource, _, err := parseResourceResponse(response)
883-
require.Error(t, err)
884-
assert.Nil(t, resource)
885-
})
886-
887-
t.Run("resource without description uses empty string", func(t *testing.T) {
888-
response := map[string]interface{}{
889-
"resource": map[string]interface{}{
890-
"secrecy": []interface{}{"tag1"},
891-
},
892-
}
893-
894-
resource, _, err := parseResourceResponse(response)
895-
require.NoError(t, err)
896-
assert.Equal(t, "", resource.Description)
897-
})
898-
899-
t.Run("resource without labels has empty labels", func(t *testing.T) {
900-
response := map[string]interface{}{
901-
"resource": map[string]interface{}{
902-
"description": "minimal resource",
903-
},
904-
}
905-
906-
resource, _, err := parseResourceResponse(response)
907-
require.NoError(t, err)
908-
assert.True(t, resource.Secrecy.Label.IsEmpty(), "secrecy should be empty when not specified")
909-
assert.True(t, resource.Integrity.Label.IsEmpty(), "integrity should be empty when not specified")
910-
})
911-
912-
t.Run("non-string secrecy tags are skipped", func(t *testing.T) {
913-
response := map[string]interface{}{
914-
"resource": map[string]interface{}{
915-
"secrecy": []interface{}{"valid-tag", 42, nil, "another-valid-tag"},
916-
},
917-
}
918-
919-
resource, _, err := parseResourceResponse(response)
920-
require.NoError(t, err)
921-
tags := resource.Secrecy.Label.GetTags()
922-
assert.Len(t, tags, 2, "non-string secrecy entries should be skipped")
923-
assert.Contains(t, tags, difc.Tag("valid-tag"))
924-
assert.Contains(t, tags, difc.Tag("another-valid-tag"))
925-
})
926-
927-
t.Run("multiple secrecy and integrity tags", func(t *testing.T) {
928-
response := map[string]interface{}{
929-
"resource": map[string]interface{}{
930-
"secrecy": []interface{}{"private:org/repo1", "private:org/repo2"},
931-
"integrity": []interface{}{"approved", "merged"},
932-
},
933-
}
934-
935-
resource, _, err := parseResourceResponse(response)
936-
require.NoError(t, err)
937-
assert.Len(t, resource.Secrecy.Label.GetTags(), 2)
938-
assert.Len(t, resource.Integrity.Label.GetTags(), 2)
939-
})
940-
}
941-
942-
func TestParseCollectionLabeledData(t *testing.T) {
943-
t.Run("empty items returns empty collection", func(t *testing.T) {
944-
result, err := parseCollectionLabeledData([]interface{}{})
945-
require.NoError(t, err)
946-
require.NotNil(t, result)
947-
assert.Empty(t, result.Items)
948-
})
949-
950-
t.Run("nil items returns empty collection", func(t *testing.T) {
951-
result, err := parseCollectionLabeledData(nil)
952-
require.NoError(t, err)
953-
require.NotNil(t, result)
954-
assert.Empty(t, result.Items)
955-
})
956-
957-
t.Run("single item with labels", func(t *testing.T) {
958-
items := []interface{}{
959-
map[string]interface{}{
960-
"data": map[string]interface{}{"id": "123", "title": "Test Issue"},
961-
"labels": map[string]interface{}{
962-
"description": "issue data",
963-
"secrecy": []interface{}{"private:owner/repo"},
964-
"integrity": []interface{}{"approved"},
965-
},
966-
},
967-
}
968-
969-
result, err := parseCollectionLabeledData(items)
970-
require.NoError(t, err)
971-
require.Len(t, result.Items, 1)
972-
973-
item := result.Items[0]
974-
require.NotNil(t, item.Labels)
975-
assert.Equal(t, "issue data", item.Labels.Description)
976-
assert.Contains(t, item.Labels.Secrecy.Label.GetTags(), difc.Tag("private:owner/repo"))
977-
assert.Contains(t, item.Labels.Integrity.Label.GetTags(), difc.Tag("approved"))
978-
})
979-
980-
t.Run("item without labels field has nil labels", func(t *testing.T) {
981-
items := []interface{}{
982-
map[string]interface{}{
983-
"data": "some data",
984-
},
985-
}
986-
987-
result, err := parseCollectionLabeledData(items)
988-
require.NoError(t, err)
989-
require.Len(t, result.Items, 1)
990-
assert.Nil(t, result.Items[0].Labels)
991-
})
992-
993-
t.Run("non-map item is skipped", func(t *testing.T) {
994-
items := []interface{}{
995-
"string-not-a-map",
996-
42,
997-
map[string]interface{}{"data": "valid item"},
998-
}
999-
1000-
result, err := parseCollectionLabeledData(items)
1001-
require.NoError(t, err)
1002-
assert.Len(t, result.Items, 1, "non-map items should be skipped")
1003-
})
1004-
1005-
t.Run("multiple items with different labels", func(t *testing.T) {
1006-
items := []interface{}{
1007-
map[string]interface{}{
1008-
"data": "public item",
1009-
"labels": map[string]interface{}{
1010-
"secrecy": []interface{}{"public"},
1011-
"integrity": []interface{}{},
1012-
},
1013-
},
1014-
map[string]interface{}{
1015-
"data": "private item",
1016-
"labels": map[string]interface{}{
1017-
"secrecy": []interface{}{"private:owner/repo"},
1018-
"integrity": []interface{}{"approved"},
1019-
},
1020-
},
1021-
}
1022-
1023-
result, err := parseCollectionLabeledData(items)
1024-
require.NoError(t, err)
1025-
require.Len(t, result.Items, 2)
1026-
1027-
assert.Contains(t, result.Items[0].Labels.Secrecy.Label.GetTags(), difc.Tag("public"))
1028-
assert.Contains(t, result.Items[1].Labels.Secrecy.Label.GetTags(), difc.Tag("private:owner/repo"))
1029-
})
1030-
1031-
t.Run("item labels without secrecy or integrity use empty labels", func(t *testing.T) {
1032-
items := []interface{}{
1033-
map[string]interface{}{
1034-
"data": "item",
1035-
"labels": map[string]interface{}{
1036-
"description": "minimal labels",
1037-
},
1038-
},
1039-
}
1040-
1041-
result, err := parseCollectionLabeledData(items)
1042-
require.NoError(t, err)
1043-
require.Len(t, result.Items, 1)
1044-
require.NotNil(t, result.Items[0].Labels)
1045-
assert.True(t, result.Items[0].Labels.Secrecy.Label.IsEmpty())
1046-
assert.True(t, result.Items[0].Labels.Integrity.Label.IsEmpty())
1047-
})
1048-
1049-
t.Run("non-string tags in labels are skipped", func(t *testing.T) {
1050-
items := []interface{}{
1051-
map[string]interface{}{
1052-
"data": "item",
1053-
"labels": map[string]interface{}{
1054-
"secrecy": []interface{}{"valid-tag", 99, nil},
1055-
"integrity": []interface{}{"ok", true},
1056-
},
1057-
},
1058-
}
1059-
1060-
result, err := parseCollectionLabeledData(items)
1061-
require.NoError(t, err)
1062-
require.Len(t, result.Items, 1)
1063-
assert.Len(t, result.Items[0].Labels.Secrecy.Label.GetTags(), 1)
1064-
assert.Len(t, result.Items[0].Labels.Integrity.Label.GetTags(), 1)
1065-
})
1066-
}
802+
// TestParseResourceResponse and TestParseCollectionLabeledData are in wasm_response_parse_test.go

internal/guard/wasm_response_parse_test.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,9 @@ func TestParseResourceResponse(t *testing.T) {
140140
"secrecy": []interface{}{"private"},
141141
},
142142
},
143-
wantDesc: "",
144-
wantSecrecy: []difc.Tag{"private"},
143+
wantDesc: "",
144+
wantSecrecy: []difc.Tag{"private"},
145+
wantOperation: difc.OperationWrite,
145146
},
146147
{
147148
name: "non-string tags in secrecy array are skipped",
@@ -150,7 +151,8 @@ func TestParseResourceResponse(t *testing.T) {
150151
"secrecy": []interface{}{"valid-tag", 42, true, nil, "another-tag"},
151152
},
152153
},
153-
wantSecrecy: []difc.Tag{"another-tag", "valid-tag"},
154+
wantSecrecy: []difc.Tag{"another-tag", "valid-tag"},
155+
wantOperation: difc.OperationWrite,
154156
},
155157
{
156158
name: "non-string tags in integrity array are skipped",
@@ -160,6 +162,7 @@ func TestParseResourceResponse(t *testing.T) {
160162
},
161163
},
162164
wantIntegrity: []difc.Tag{"also-good", "good-tag"},
165+
wantOperation: difc.OperationWrite,
163166
},
164167

165168
// ── Error cases ────────────────────────────────────────────────────────
@@ -220,7 +223,8 @@ func TestParseResourceResponse(t *testing.T) {
220223
"secrecy": "not-an-array",
221224
},
222225
},
223-
wantSecrecy: []difc.Tag{},
226+
wantSecrecy: []difc.Tag{},
227+
wantOperation: difc.OperationWrite,
224228
},
225229
{
226230
name: "integrity value is not an array (map) defaults to empty label",
@@ -230,6 +234,7 @@ func TestParseResourceResponse(t *testing.T) {
230234
},
231235
},
232236
wantIntegrity: []difc.Tag{},
237+
wantOperation: difc.OperationWrite,
233238
},
234239
{
235240
name: "description field is not a string - ignored",
@@ -238,7 +243,8 @@ func TestParseResourceResponse(t *testing.T) {
238243
"description": 999,
239244
},
240245
},
241-
wantDesc: "",
246+
wantDesc: "",
247+
wantOperation: difc.OperationWrite,
242248
},
243249
}
244250

@@ -305,11 +311,11 @@ func TestParseResourceResponse_OperationCoverage(t *testing.T) {
305311

306312
func TestParseCollectionLabeledData(t *testing.T) {
307313
tests := []struct {
308-
name string
309-
items []interface{}
310-
wantItemCount int
311-
wantErr bool
312-
checkItems func(t *testing.T, collection *difc.CollectionLabeledData)
314+
name string
315+
items []interface{}
316+
wantItemCount int
317+
wantErr bool
318+
checkItems func(t *testing.T, collection *difc.CollectionLabeledData)
313319
}{
314320
// ── Empty / nil inputs ─────────────────────────────────────────────────
315321

@@ -605,7 +611,7 @@ func TestParseCollectionLabeledData(t *testing.T) {
605611
},
606612
"not-a-map",
607613
map[string]interface{}{
608-
"data": "item-2-empty-labels",
614+
"data": "item-2-empty-labels",
609615
"labels": map[string]interface{}{},
610616
},
611617
},

0 commit comments

Comments
 (0)