Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2305,6 +2305,66 @@ func TestTruncateReasons(t *testing.T) {
}
}

func TestAggregateMachineMessages(t *testing.T) {
shortMsg := "machine is unhealthy\n"
// Build a message that is exactly (1000 - len(shortMsg)) chars so the total is exactly 1000.
padLen := maxMessageLength - len(shortMsg)
paddedMsg := strings.Repeat("x", padLen-1) + "\n"

for _, tc := range []struct {
name string
msgs []string
expect string
}{
{
name: "When input is nil it should return empty string",
msgs: nil,
expect: "",
},
{
name: "When input is empty it should return empty string",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Consider adding a nil input case alongside the empty slice. Go distinguishes nil from []string{} and aggregateMachineMessages handles both, but documenting that contract explicitly makes the test more robust as a spec.

{
    name:   "When input is nil it should return empty string",
    msgs:   nil,
    expect: "",
},

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Added a nil input test case before the empty slice case.


AI-assisted response via Claude Code

msgs: []string{},
expect: "",
},
{
name: "When a single short message is given it should return it verbatim",
msgs: []string{shortMsg},
expect: shortMsg,
},
{
name: "When multiple messages fit within limit it should return them all concatenated",
msgs: []string{"error one\n", "error two\n", "error three\n"},
expect: "error one\nerror two\nerror three\n",
},
{
name: "When messages exactly hit the 1000-char boundary it should include all without truncation",
msgs: []string{paddedMsg, shortMsg},
expect: paddedMsg + shortMsg,
},
{
name: "When messages exceed the 1000-char limit it should truncate and append suffix",
msgs: []string{paddedMsg, shortMsg, "this overflows\n"},
expect: paddedMsg + shortMsg + endOfMessage,
},
{
name: "When a single message is exactly maxMessageLength it should be included",
msgs: []string{strings.Repeat("z", maxMessageLength)},
expect: strings.Repeat("z", maxMessageLength),
},
{
name: "When a single message exceeds the limit it should return only the truncation suffix",
msgs: []string{strings.Repeat("a", maxMessageLength+1)},
expect: endOfMessage,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The boundary between "fits" and "truncated" for a single message is not covered. The existing "exact boundary" case uses two messages summing to 1000, and this "single oversized" case uses 1001. But a single message of exactly 1000 chars is missing — this is the tightest boundary test for the > condition on the first loop iteration (0 + 1000 = 1000, NOT > 1000, so it should pass through).

Add this test case to the table (e.g. before this one):

{
	name:   "When a single message is exactly maxMessageLength it should be included",
	msgs:   []string{strings.Repeat("z", maxMessageLength)},
	expect: strings.Repeat("z", maxMessageLength),
},

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Added test case for a single message of exactly maxMessageLength chars to cover the tight boundary of the > condition.


AI-assisted response via Claude Code

},
} {
t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)
result := aggregateMachineMessages(tc.msgs)
g.Expect(result).To(Equal(tc.expect))
})
}
}

func TestAggregateMachineReasonsAndMessages(t *testing.T) {
g := NewWithT(t)

Expand Down