This repository has been archived on 2026-03-09. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
homelab-deploy/internal/cli/deploy_test.go
Torjus Håkestad 713d1e7584 chore: migrate module path from git.t-juice.club to code.t-juice.club
Gitea to Forgejo host migration — update Go module path and all
import references.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-09 19:37:47 +01:00

144 lines
3.4 KiB
Go

package cli
import (
"testing"
"code.t-juice.club/torjus/homelab-deploy/internal/messages"
)
func TestDeployResult_AllSucceeded(t *testing.T) {
tests := []struct {
name string
responses []*messages.DeployResponse
errors []error
want bool
}{
{
name: "all completed",
responses: []*messages.DeployResponse{
{Hostname: "host1", Status: messages.StatusCompleted},
{Hostname: "host2", Status: messages.StatusCompleted},
},
want: true,
},
{
name: "one failed",
responses: []*messages.DeployResponse{
{Hostname: "host1", Status: messages.StatusCompleted},
{Hostname: "host2", Status: messages.StatusFailed},
},
want: false,
},
{
name: "one rejected",
responses: []*messages.DeployResponse{
{Hostname: "host1", Status: messages.StatusRejected},
},
want: false,
},
{
name: "no responses",
responses: []*messages.DeployResponse{},
want: false,
},
{
name: "has errors",
responses: []*messages.DeployResponse{
{Hostname: "host1", Status: messages.StatusCompleted},
},
errors: []error{nil}, // placeholder error
want: false,
},
{
name: "with intermediate responses - success",
responses: []*messages.DeployResponse{
{Hostname: "host1", Status: messages.StatusStarted},
{Hostname: "host1", Status: messages.StatusCompleted},
},
want: true,
},
{
name: "with intermediate responses - failure",
responses: []*messages.DeployResponse{
{Hostname: "host1", Status: messages.StatusStarted},
{Hostname: "host1", Status: messages.StatusFailed},
},
want: false,
},
{
name: "multiple hosts with intermediate responses",
responses: []*messages.DeployResponse{
{Hostname: "host1", Status: messages.StatusStarted},
{Hostname: "host2", Status: messages.StatusStarted},
{Hostname: "host1", Status: messages.StatusCompleted},
{Hostname: "host2", Status: messages.StatusCompleted},
},
want: true,
},
{
name: "only intermediate responses - no final",
responses: []*messages.DeployResponse{
{Hostname: "host1", Status: messages.StatusStarted},
{Hostname: "host1", Status: messages.StatusAccepted},
},
want: false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
r := &DeployResult{
Responses: tc.responses,
Errors: tc.errors,
}
got := r.AllSucceeded()
if got != tc.want {
t.Errorf("AllSucceeded() = %v, want %v", got, tc.want)
}
})
}
}
func TestDeployResult_HostCount(t *testing.T) {
tests := []struct {
name string
responses []*messages.DeployResponse
want int
}{
{
name: "no responses",
responses: []*messages.DeployResponse{},
want: 0,
},
{
name: "unique hosts",
responses: []*messages.DeployResponse{
{Hostname: "host1"},
{Hostname: "host2"},
{Hostname: "host3"},
},
want: 3,
},
{
name: "duplicate hosts",
responses: []*messages.DeployResponse{
{Hostname: "host1", Status: messages.StatusStarted},
{Hostname: "host1", Status: messages.StatusCompleted},
{Hostname: "host2", Status: messages.StatusStarted},
{Hostname: "host2", Status: messages.StatusCompleted},
},
want: 2,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
r := &DeployResult{Responses: tc.responses}
got := r.HostCount()
if got != tc.want {
t.Errorf("HostCount() = %d, want %d", got, tc.want)
}
})
}
}