@@ -1046,4 +1046,110 @@ var _ = Describe("CUEGenerator", func() {
10461046 Expect (cue ).To (ContainSubstring (`protocol: "TCP"` ))
10471047 })
10481048 })
1049+
1050+ Describe ("Struct field enum generation in helper definitions" , func () {
1051+ It ("should generate enum with default on a helper struct field" , func () {
1052+ rule := defkit .Struct ("rule" ).Fields (
1053+ defkit .Field ("strategy" , defkit .ParamTypeString ).
1054+ Default ("onAppUpdate" ).
1055+ Enum ("onAppUpdate" , "onAppDelete" , "never" ),
1056+ )
1057+
1058+ p := defkit .NewPolicy ("test-enum-default" ).
1059+ Description ("Test" ).
1060+ Helper ("Rule" , rule )
1061+
1062+ cue := p .ToCue ()
1063+
1064+ Expect (cue ).To (ContainSubstring (`strategy: *"onAppUpdate" | "onAppDelete" | "never"` ))
1065+ Expect (cue ).NotTo (ContainSubstring (`strategy: *"onAppUpdate" | string` ))
1066+ })
1067+
1068+ It ("should generate enum without default on a helper struct field" , func () {
1069+ rule := defkit .Struct ("rule" ).Fields (
1070+ defkit .Field ("propagation" , defkit .ParamTypeString ).
1071+ Enum ("orphan" , "cascading" ).
1072+ Optional (),
1073+ )
1074+
1075+ p := defkit .NewPolicy ("test-enum-no-default" ).
1076+ Description ("Test" ).
1077+ Helper ("Rule" , rule )
1078+
1079+ cue := p .ToCue ()
1080+
1081+ Expect (cue ).To (ContainSubstring (`propagation?: "orphan" | "cascading"` ))
1082+ Expect (cue ).NotTo (ContainSubstring ("propagation?: string" ))
1083+ })
1084+
1085+ It ("should generate required enum without default on a helper struct field" , func () {
1086+ rule := defkit .Struct ("rule" ).Fields (
1087+ defkit .Field ("mode" , defkit .ParamTypeString ).
1088+ Enum ("strict" , "permissive" ).
1089+ Required (),
1090+ )
1091+
1092+ p := defkit .NewPolicy ("test-enum-required" ).
1093+ Description ("Test" ).
1094+ Helper ("Rule" , rule )
1095+
1096+ cue := p .ToCue ()
1097+
1098+ Expect (cue ).To (ContainSubstring (`mode: "strict" | "permissive"` ))
1099+ Expect (cue ).NotTo (ContainSubstring ("mode?: " ))
1100+ Expect (cue ).NotTo (ContainSubstring ("mode: string" ))
1101+ })
1102+
1103+ It ("should generate enum without default on a parameter struct field" , func () {
1104+ comp := defkit .NewComponent ("test-param-enum" ).
1105+ Workload ("v1" , "Pod" ).
1106+ Params (
1107+ defkit .Struct ("config" ).Fields (
1108+ defkit .Field ("level" , defkit .ParamTypeString ).
1109+ Enum ("low" , "medium" , "high" ).
1110+ Optional (),
1111+ defkit .Field ("mode" , defkit .ParamTypeString ).
1112+ Enum ("fast" , "safe" ).
1113+ Required (),
1114+ ),
1115+ ).
1116+ Template (func (tpl * defkit.Template ) {
1117+ tpl .Output (defkit .NewResource ("v1" , "Pod" ))
1118+ })
1119+
1120+ cue := defkit .NewCUEGenerator ().GenerateFullDefinition (comp )
1121+
1122+ Expect (cue ).To (ContainSubstring (`level?: "low" | "medium" | "high"` ))
1123+ Expect (cue ).To (ContainSubstring (`mode: "fast" | "safe"` ))
1124+ Expect (cue ).NotTo (ContainSubstring ("level?: string" ))
1125+ Expect (cue ).NotTo (ContainSubstring ("mode: string" ))
1126+ Expect (cue ).NotTo (ContainSubstring ("mode?: " ))
1127+ })
1128+
1129+ It ("should handle mixed enum fields: with default, without default, and plain string" , func () {
1130+ rule := defkit .Struct ("rule" ).Fields (
1131+ defkit .Field ("strategy" , defkit .ParamTypeString ).
1132+ Default ("always" ).
1133+ Enum ("always" , "never" , "on-failure" ),
1134+ defkit .Field ("propagation" , defkit .ParamTypeString ).
1135+ Enum ("orphan" , "cascading" ).
1136+ Optional (),
1137+ defkit .Field ("name" , defkit .ParamTypeString ).
1138+ Optional (),
1139+ )
1140+
1141+ p := defkit .NewPolicy ("test-mixed" ).
1142+ Description ("Test" ).
1143+ Helper ("Rule" , rule )
1144+
1145+ cue := p .ToCue ()
1146+
1147+ // Enum with default
1148+ Expect (cue ).To (ContainSubstring (`strategy: *"always" | "never" | "on-failure"` ))
1149+ // Enum without default (optional)
1150+ Expect (cue ).To (ContainSubstring (`propagation?: "orphan" | "cascading"` ))
1151+ // Plain string (optional)
1152+ Expect (cue ).To (ContainSubstring ("name?: string" ))
1153+ })
1154+ })
10491155})
0 commit comments