@@ -1002,3 +1002,90 @@ func TestReadAgentControlDefinitions_DropsBrokenContentField(t *testing.T) {
10021002 })
10031003 }
10041004}
1005+
1006+ func TestReadAgentDefinition_FileNotFound (t * testing.T ) {
1007+ tmpDir := t .TempDir ()
1008+ configDir := filepath .Join (tmpDir , config .GetRootFolderForAgentRepo ())
1009+ require .NoError (t , os .MkdirAll (configDir , 0755 ))
1010+
1011+ result , err := ReadAgentDefinition (context .Background (), tmpDir )
1012+ require .NoError (t , err )
1013+ assert .Nil (t , result )
1014+ }
1015+
1016+ func TestReadAgentDefinition_ValidFile (t * testing.T ) {
1017+ tmpDir := t .TempDir ()
1018+ configDir := filepath .Join (tmpDir , config .GetRootFolderForAgentRepo ())
1019+ require .NoError (t , os .MkdirAll (configDir , 0755 ))
1020+
1021+ yamlContent := `bindings:
1022+ - type: REQUIRES
1023+ targetType: AGENT
1024+ target: NrInfra
1025+ versions:
1026+ - ">=1.0.0"
1027+ breakingChange: "2.0.0"`
1028+ defFile := filepath .Join (configDir , "agentDefinition.yml" )
1029+ require .NoError (t , os .WriteFile (defFile , []byte (yamlContent ), 0644 ))
1030+
1031+ result , err := ReadAgentDefinition (context .Background (), tmpDir )
1032+ require .NoError (t , err )
1033+ require .NotNil (t , result )
1034+
1035+ require .NotNil (t , result .BreakingChange )
1036+ assert .Equal (t , "2.0.0" , * result .BreakingChange )
1037+
1038+ require .Len (t , result .Bindings , 1 )
1039+ binding := result .Bindings [0 ].(map [string ]interface {})
1040+ assert .Equal (t , "REQUIRES" , binding ["type" ])
1041+ assert .Equal (t , "AGENT" , binding ["targetType" ])
1042+ assert .Equal (t , "NrInfra" , binding ["target" ])
1043+ }
1044+
1045+ func TestReadAgentDefinition_BreakingChangeOnly (t * testing.T ) {
1046+ tmpDir := t .TempDir ()
1047+ configDir := filepath .Join (tmpDir , config .GetRootFolderForAgentRepo ())
1048+ require .NoError (t , os .MkdirAll (configDir , 0755 ))
1049+
1050+ yamlContent := `breakingChange: "1.5.0"`
1051+ defFile := filepath .Join (configDir , "agentDefinition.yml" )
1052+ require .NoError (t , os .WriteFile (defFile , []byte (yamlContent ), 0644 ))
1053+
1054+ result , err := ReadAgentDefinition (context .Background (), tmpDir )
1055+ require .NoError (t , err )
1056+ require .NotNil (t , result )
1057+ require .NotNil (t , result .BreakingChange )
1058+ assert .Equal (t , "1.5.0" , * result .BreakingChange )
1059+ assert .Nil (t , result .Bindings )
1060+ }
1061+
1062+ func TestReadAgentDefinition_BreakingChangeUnquotedFloat (t * testing.T ) {
1063+ tmpDir := t .TempDir ()
1064+ configDir := filepath .Join (tmpDir , config .GetRootFolderForAgentRepo ())
1065+ require .NoError (t , os .MkdirAll (configDir , 0755 ))
1066+
1067+ // Unquoted float — go-yaml coerces to string representation
1068+ yamlContent := `breakingChange: 2.0`
1069+ defFile := filepath .Join (configDir , "agentDefinition.yml" )
1070+ require .NoError (t , os .WriteFile (defFile , []byte (yamlContent ), 0644 ))
1071+
1072+ result , err := ReadAgentDefinition (context .Background (), tmpDir )
1073+ require .NoError (t , err )
1074+ require .NotNil (t , result )
1075+ require .NotNil (t , result .BreakingChange )
1076+ assert .Equal (t , "2.0" , * result .BreakingChange )
1077+ }
1078+
1079+ func TestReadAgentDefinition_InvalidYAML (t * testing.T ) {
1080+ tmpDir := t .TempDir ()
1081+ configDir := filepath .Join (tmpDir , config .GetRootFolderForAgentRepo ())
1082+ require .NoError (t , os .MkdirAll (configDir , 0755 ))
1083+
1084+ defFile := filepath .Join (configDir , "agentDefinition.yml" )
1085+ require .NoError (t , os .WriteFile (defFile , []byte (":\t invalid: yaml: [" ), 0644 ))
1086+
1087+ result , err := ReadAgentDefinition (context .Background (), tmpDir )
1088+ require .Error (t , err )
1089+ assert .Nil (t , result )
1090+ assert .Contains (t , err .Error (), "failed to parse agentDefinition.yml" )
1091+ }
0 commit comments