@@ -740,3 +740,81 @@ func TestBuildConfig_Filter(t *testing.T) {
740740 assert .Empty (t , config .Filter .Tags .Exclude )
741741 })
742742}
743+
744+ // TestToConfigMapWithContentChecksum tests that ToConfigMapWithContentChecksum creates
745+ // a ConfigMap with the correct checksum annotation based on the YAML content.
746+ func TestToConfigMapWithContentChecksum (t * testing.T ) {
747+ t .Parallel ()
748+
749+ // Create a populated Config object
750+ config := & Config {
751+ RegistryName : "checksum-test-registry" ,
752+ Source : & SourceConfig {
753+ Format : "toolhive" ,
754+ Git : & GitConfig {
755+ Repository : "https://github.com/example/mcp-servers.git" ,
756+ Branch : "main" ,
757+ },
758+ },
759+ SyncPolicy : & SyncPolicyConfig {
760+ Interval : "15m" ,
761+ },
762+ Filter : & FilterConfig {
763+ Names : & NameFilterConfig {
764+ Include : []string {"mcp-*" },
765+ Exclude : []string {"*-dev" },
766+ },
767+ },
768+ }
769+
770+ mcpRegistry := & mcpv1alpha1.MCPRegistry {
771+ ObjectMeta : metav1.ObjectMeta {
772+ Name : "test-registry" ,
773+ Namespace : "test-namespace" ,
774+ },
775+ }
776+
777+ // Call ToConfigMapWithContentChecksum
778+ configMap , err := config .ToConfigMapWithContentChecksum (mcpRegistry )
779+
780+ // Verify no error occurred
781+ require .NoError (t , err )
782+ require .NotNil (t , configMap )
783+
784+ // Verify basic ConfigMap properties
785+ assert .Equal (t , "checksum-test-registry-configmap" , configMap .Name )
786+ assert .Equal (t , "test-namespace" , configMap .Namespace )
787+
788+ // Verify the checksum annotation exists
789+ require .NotNil (t , configMap .Annotations )
790+ checksum , exists := configMap .Annotations ["toolhive.dev/content-checksum" ]
791+ require .True (t , exists , "Expected checksum annotation to exist" )
792+ require .NotEmpty (t , checksum , "Checksum should not be empty" )
793+
794+ // Verify the checksum format (should be a hex string)
795+ // The actual checksum is calculated by ctrlutil.CalculateConfigHash
796+ assert .Regexp (t , "^[a-f0-9]+$" , checksum , "Checksum should be a hex string" )
797+
798+ // Verify the Data contains config.yaml
799+ require .Contains (t , configMap .Data , "config.yaml" )
800+ yamlData := configMap .Data ["config.yaml" ]
801+ require .NotEmpty (t , yamlData )
802+
803+ // Verify YAML content includes expected fields
804+ assert .Contains (t , yamlData , "registryName: checksum-test-registry" )
805+ assert .Contains (t , yamlData , "repository: https://github.com/example/mcp-servers.git" )
806+ assert .Contains (t , yamlData , "interval: 15m" )
807+
808+ // Test that the same config produces the same checksum
809+ configMap2 , err := config .ToConfigMapWithContentChecksum (mcpRegistry )
810+ require .NoError (t , err )
811+ checksum2 := configMap2 .Annotations ["toolhive.dev/content-checksum" ]
812+ assert .Equal (t , checksum , checksum2 , "Same config should produce same checksum" )
813+
814+ // Test that different config produces different checksum
815+ config .SyncPolicy .Interval = "30m"
816+ configMap3 , err := config .ToConfigMapWithContentChecksum (mcpRegistry )
817+ require .NoError (t , err )
818+ checksum3 := configMap3 .Annotations ["toolhive.dev/content-checksum" ]
819+ assert .NotEqual (t , checksum , checksum3 , "Different config should produce different checksum" )
820+ }
0 commit comments