Files
kubevela/pkg/definition/defkit/version_test.go
Vishal Kumar 732b49d236 Feat: defkit api completeness (#7064)
* docs: map existing codebase

Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* docs: initialize project

Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* Fix: add Short() and Ignore() methods to FloatParam for API completeness

FloatParam was the only param type missing Short(string) and Ignore()
fluent methods; the underlying baseParam fields already existed. Adds
matching test cases following the existing BoolParam/IntParam pattern.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* feat(defkit): add StatusDetails() to all 4 definition types via baseDefinition

Adds statusDetails string field to baseDefinition with setStatusDetails()
setter and GetStatusDetails() getter. Exposes StatusDetails(string) fluent
method on ComponentDefinition, TraitDefinition, WorkflowStepDefinition, and
PolicyDefinition. Updates writeStatus in cuegen.go and the inline status
render block in trait.go to render statusDetails as a #"""..."""# CUE block
alongside customStatus and healthPolicy when set.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* feat(01-02): add ForceOptional() to IntParam, FloatParam, EnumParam

- IntParam.ForceOptional() *IntParam
- FloatParam.ForceOptional() *FloatParam
- EnumParam.ForceOptional() *EnumParam
- Test cases for all three types following BoolParam/StringParam patterns

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* feat(02-01): add Labels()/GetLabels() to PolicyDefinition and fix hardcoded labels CUE

- Add labels map[string]string field to PolicyDefinition struct
- Add Labels() fluent setter and GetLabels() getter
- Replace hardcoded labels: {} in PolicyCUEGenerator with nil-conditional sorted output
- Add sort import to policy.go

Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* test(02-01): add Ginkgo tests for PolicyDefinition.Labels

- Test store/return labels
- Test sorted key output in CUE
- Test labels block omitted when Labels() never called
- Test empty labels block when Labels called with empty map

Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* docs(02-01): complete Labels on PolicyDefinition plan

- SUMMARY.md: plan 02-01 execution results
- STATE.md: phase 2 in progress, B1 satisfied, key decisions recorded
- ROADMAP.md: phase 2 progress updated (1/3 plans)
- REQUIREMENTS.md: B1 marked complete

Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* feat(02-02): add annotations field to baseDefinition with getter/setter

Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* feat(02-03): wire status block rendering into WorkflowStep and Policy CUE generators

- Add status: { customStatus, healthPolicy, statusDetails } rendering to WorkflowStepCUEGenerator.GenerateTemplate
- Add same status block rendering to PolicyCUEGenerator.GenerateTemplate
- Block only emitted when at least one status field is non-empty, matching trait.go pattern

Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* feat(02-02): add Annotations() fluent method to all 4 definition types

Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* feat(02-02): conditional sorted annotations CUE block for component, trait, policy

Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* test(02-03): add Ginkgo status block CUE render tests for WorkflowStep and Policy

- 5 new tests in WorkflowStepDefinition/Status Block CUE Render context
- 5 new tests in PolicyDefinition/Status Block CUE Render context
- Rule 3 fix: restore sort import in trait.go (used by annotations render, spuriously flagged)

Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* feat(02-02): merge user annotations before category in workflow step CUE block

Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* feat(02-02): add sorted annotations CUE block to WorkflowStepCUEGenerator

- Wire sorted user annotations rendering into WorkflowStep.GenerateFullDefinition
- Matches pattern already added to Component, Trait, and Policy generators

Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* feat(02-02): merge user annotations into metadata.annotations in ToYAML for all 4 types

Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* docs(02-03): complete Wire Status Block Rendering plan

- Add 02-03-SUMMARY.md with full execution record
- Update STATE.md: phase 2 complete, new decision recorded
- Update ROADMAP.md progress for phase 2
- Mark B4 complete in REQUIREMENTS.md

Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* test(02-02): add Annotations Ginkgo tests for all 4 definition types

Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* docs(02-02): complete Annotations plan - SUMMARY, STATE, ROADMAP updated

Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* docs(03): create phase 3 plans for missing CRD spec fields

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* feat(03-01): add Version() fluent method and GetVersion() to all 4 definition types

- version string field added to baseDefinition struct
- setVersion/GetVersion methods on baseDefinition following existing pattern
- Version(string) fluent setter on TraitDefinition, ComponentDefinition, PolicyDefinition, WorkflowStepDefinition
- TDD RED+GREEN: tests for round-trip pass; CUE/YAML render tests added (fail, to be fixed in next task)

Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* feat(03-02): add ManageWorkload/ControlPlaneOnly/RevisionEnabled to TraitDefinition

- Add manageWorkload, controlPlaneOnly, revisionEnabled bool fields to TraitDefinition struct
- Add ManageWorkload(), ControlPlaneOnly(), RevisionEnabled() fluent setters
- Add IsManageWorkload(), IsControlPlaneOnly(), IsRevisionEnabled() getters
- Emit conditionally in ToYAML only when true; ToCue() unaffected
- Add 11 round-trip tests covering defaults, setters, and YAML emission

Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* test(03-03): add Ginkgo tests for ChildResourceKind accumulator on ComponentDefinition

- 7 specs covering nil default, single entry, multi-entry accumulation,
  selector preservation, ToYAML emit/omit, and chaining

Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* feat(03-02): add ManageHealthCheck to PolicyDefinition

- Add manageHealthCheck bool field to PolicyDefinition struct
- Add ManageHealthCheck() fluent setter and IsManageHealthCheck() getter
- Emit conditionally in ToYAML only when true; ToCue() unaffected
- Add 5 round-trip tests covering default, setter, YAML emission, and chaining

Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* test(03-03): add Ginkgo tests for PodSpecPath on ComponentDefinition

- 5 specs covering empty default, set/get round-trip, ToYAML emit/omit,
  and chaining; both fields satisfy C5 (childResourceKinds) and C6 (podSpecPath)

Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* feat(03-01): render version in CUE output and spec.version in ToYAML for all 4 definition types

- cuegen.go: conditional version emit in ComponentDefinition GenerateFullDefinition (after description)
- trait.go: conditional version emit in TraitCUEGenerator.GenerateFullDefinition; spec.version in ToYAML
- policy.go: conditional version emit in PolicyCUEGenerator.GenerateFullDefinition; spec.version in ToYAML
- workflow_step.go: conditional version emit in WorkflowStepCUEGenerator.GenerateFullDefinition; spec.version in ToYAML
- version omitted entirely when not set; TDD GREEN phase complete

Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* docs(03-03): complete ChildResourceKind+PodSpecPath plan - SUMMARY, STATE, ROADMAP updated

- C5 (childResourceKinds accumulator) and C6 (podSpecPath) requirements satisfied
- 12 Ginkgo specs added covering both fields

Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* docs(03-02): complete boolean CRD spec fields plan — manageWorkload/controlPlaneOnly/revisionEnabled/manageHealthCheck

Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* docs(03-01): complete Version() plan — SUMMARY, STATE, ROADMAP updated

- 03-01-SUMMARY.md created documenting Version() on all 4 definition types
- STATE.md: 03-01 session log entry added; phase 3 marked complete; decision recorded
- ROADMAP.md: phase 3 updated to 3/3 plans executed; status Complete
- REQUIREMENTS.md: C1 marked complete

Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* docs(04): add gap closure plan 04-03 for vela-go-definitions call sites

Closes 17 ArrayOf→Of and 1 FilterPred→Filter call sites broken by the phase 04 renames.

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* feat(phase-4): low-risk renames — SetField→Set, ArrayOf→Of, FilterPred→Filter

A1: PolicyTemplate.SetField() renamed to Set()
A3: StructField.ArrayOf() renamed to Of()
A5: HelperBuilder.FilterPred(Predicate) renamed to Filter(Predicate);
    HelperBuilder.Filter(Condition) renamed to FilterCond(Condition)

All callers within defkit updated. Verification: 894 Ginkgo specs pass,
go build exits 0, zero occurrences of old names in pkg/definition/defkit/.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* docs(05-high-impact-renames): create phase plan

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* feat(phase-5): high-impact renames — Values→Enum, Fields→WithFields

A4: EnumParam.Values() renamed to Enum() — aligns with StringParam.Enum()
    and StructField.Enum(); 10 call sites updated in defkit tests

A2: StructParam.Fields() and OneOfVariant.Fields() renamed to WithFields()
    — aligns with ArrayParam and MapParam; ~50 call sites updated in defkit tests

Both repos build clean. go test ./pkg/definition/defkit/... passes.
Non-target .Fields() methods (InCondition, StructBuilder, ArrayElement etc.)
correctly preserved.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* chore: exclude .planning/ and .claude/ from version control

Local development artifacts only — not for upstream.

Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* refactor: adjust formatting for consistency in component and policy definitions

Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* feat: add FilterCond method for filtering items by Condition expression

Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* feat: enhance filter condition handling with AND-composition for multiple filters

Signed-off-by: Vishal Kumar <vishal210893@gmail.com>

* feat: rename Enum method to Values for consistency in parameter definitions

Signed-off-by: Jerrin Francis <jfo@>

* feat: simplify labels handling in policy and trait definitions

Signed-off-by: Jerrin Francis <jerrinfrancis7@gmail.com>

---------

Signed-off-by: Vishal Kumar <vishal210893@gmail.com>
Signed-off-by: Jerrin Francis <jfo@>
Signed-off-by: Jerrin Francis <jerrinfrancis7@gmail.com>
Co-authored-by: Claude <noreply@anthropic.com>
2026-03-10 11:28:23 +00:00

179 lines
6.1 KiB
Go

/*
Copyright 2025 The KubeVela Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package defkit_test
import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"sigs.k8s.io/yaml"
"github.com/oam-dev/kubevela/pkg/definition/defkit"
)
var _ = Describe("Version field on all definition types", func() {
Context("Task 1: GetVersion round-trip", func() {
It("NewTrait Version round-trip", func() {
Expect(defkit.NewTrait("t").Version("1.0").GetVersion()).To(Equal("1.0"))
})
It("NewComponent Version round-trip", func() {
Expect(defkit.NewComponent("c").Version("2.0").GetVersion()).To(Equal("2.0"))
})
It("NewPolicy empty Version", func() {
Expect(defkit.NewPolicy("p").Version("").GetVersion()).To(Equal(""))
})
It("NewTrait default GetVersion is empty string", func() {
Expect(defkit.NewTrait("t").GetVersion()).To(Equal(""))
})
It("Version is chainable on TraitDefinition", func() {
_ = defkit.NewTrait("t").Version("1.0").Description("d")
})
It("Version is chainable on ComponentDefinition", func() {
_ = defkit.NewComponent("c").Version("1.0").Description("d")
})
It("Version is chainable on PolicyDefinition", func() {
_ = defkit.NewPolicy("p").Version("1.0").Description("d")
})
It("Version is chainable on WorkflowStepDefinition", func() {
_ = defkit.NewWorkflowStep("w").Version("1.0").Description("d")
})
})
Context("Task 2: CUE render — version field conditional", func() {
It("NewTrait with Version produces version in ToCue", func() {
cue := defkit.NewTrait("t").Version("1.0").ToCue()
Expect(cue).To(MatchRegexp(`version:\s+"1\.0"`))
})
It("NewTrait without Version omits version in ToCue", func() {
cue := defkit.NewTrait("t").ToCue()
Expect(cue).NotTo(ContainSubstring("version:"))
})
It("NewComponent with Version produces version in ToCue", func() {
cue := defkit.NewComponent("c").Version("2.0").ToCue()
Expect(cue).To(ContainSubstring(`version: "2.0"`))
})
It("NewComponent without Version omits version in ToCue", func() {
cue := defkit.NewComponent("c").ToCue()
Expect(cue).NotTo(ContainSubstring("version:"))
})
It("NewPolicy with Version produces version in ToCue", func() {
cue := defkit.NewPolicy("p").Version("1.0").ToCue()
Expect(cue).To(ContainSubstring(`version: "1.0"`))
})
It("NewWorkflowStep with Version produces version in ToCue", func() {
cue := defkit.NewWorkflowStep("w").Version("1.0").ToCue()
Expect(cue).To(ContainSubstring(`version: "1.0"`))
})
It("NewWorkflowStep without Version omits version in ToCue", func() {
cue := defkit.NewWorkflowStep("w").ToCue()
Expect(cue).NotTo(ContainSubstring("version:"))
})
It("NewPolicy without Version omits version in ToCue", func() {
cue := defkit.NewPolicy("p").ToCue()
Expect(cue).NotTo(ContainSubstring("version:"))
})
})
Context("Task 2: ToYAML spec.version conditional", func() {
It("TraitDefinition with Version sets spec.version in ToYAML", func() {
yamlBytes, err := defkit.NewTrait("t").Version("1.0").ToYAML()
Expect(err).NotTo(HaveOccurred())
var obj map[string]any
Expect(yaml.Unmarshal(yamlBytes, &obj)).To(Succeed())
spec := obj["spec"].(map[string]any)
Expect(spec["version"]).To(Equal("1.0"))
})
It("TraitDefinition without Version omits spec.version in ToYAML", func() {
yamlBytes, err := defkit.NewTrait("t").ToYAML()
Expect(err).NotTo(HaveOccurred())
var obj map[string]any
Expect(yaml.Unmarshal(yamlBytes, &obj)).To(Succeed())
spec := obj["spec"].(map[string]any)
Expect(spec).NotTo(HaveKey("version"))
})
It("ComponentDefinition with Version sets spec.version in ToYAML", func() {
yamlBytes, err := defkit.NewComponent("c").Version("1.0").ToYAML()
Expect(err).NotTo(HaveOccurred())
var obj map[string]any
Expect(yaml.Unmarshal(yamlBytes, &obj)).To(Succeed())
spec := obj["spec"].(map[string]any)
Expect(spec["version"]).To(Equal("1.0"))
})
It("PolicyDefinition with Version sets spec.version in ToYAML", func() {
yamlBytes, err := defkit.NewPolicy("p").Version("1.0").ToYAML()
Expect(err).NotTo(HaveOccurred())
var obj map[string]any
Expect(yaml.Unmarshal(yamlBytes, &obj)).To(Succeed())
spec := obj["spec"].(map[string]any)
Expect(spec["version"]).To(Equal("1.0"))
})
It("WorkflowStepDefinition with Version sets spec.version in ToYAML", func() {
yamlBytes, err := defkit.NewWorkflowStep("w").Version("1.0").ToYAML()
Expect(err).NotTo(HaveOccurred())
var obj map[string]any
Expect(yaml.Unmarshal(yamlBytes, &obj)).To(Succeed())
spec := obj["spec"].(map[string]any)
Expect(spec["version"]).To(Equal("1.0"))
})
It("ComponentDefinition without Version omits spec.version in ToYAML", func() {
yamlBytes, err := defkit.NewComponent("c").ToYAML()
Expect(err).NotTo(HaveOccurred())
var obj map[string]any
Expect(yaml.Unmarshal(yamlBytes, &obj)).To(Succeed())
spec := obj["spec"].(map[string]any)
Expect(spec).NotTo(HaveKey("version"))
})
It("PolicyDefinition without Version omits spec.version in ToYAML", func() {
yamlBytes, err := defkit.NewPolicy("p").ToYAML()
Expect(err).NotTo(HaveOccurred())
var obj map[string]any
Expect(yaml.Unmarshal(yamlBytes, &obj)).To(Succeed())
spec := obj["spec"].(map[string]any)
Expect(spec).NotTo(HaveKey("version"))
})
It("WorkflowStepDefinition without Version omits spec.version in ToYAML", func() {
yamlBytes, err := defkit.NewWorkflowStep("w").ToYAML()
Expect(err).NotTo(HaveOccurred())
var obj map[string]any
Expect(yaml.Unmarshal(yamlBytes, &obj)).To(Succeed())
spec := obj["spec"].(map[string]any)
Expect(spec).NotTo(HaveKey("version"))
})
})
})