@@ -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
0 commit comments