package outbound_test import ( "errors" "fmt" "log/slog" "strings" "testing" "time" "github.com/google/uuid" "lincodex/backend/internal/port/outbound" ) func validLabRequest() outbound.LabRequest { var seed outbound.LabSeed for index := range seed { seed[index] = byte(0xa0 + index%16) } return outbound.LabRequest{ Key: outbound.LabKey{AttemptItemID: uuid.New()}, Template: outbound.LabTemplateRef{ID: "mikrotik-ospf-pair", Version: 1}, Seed: seed, Lifetime: 45 * time.Minute, } } func TestLabRequestValidateAcceptsAContractRequest(t *testing.T) { if err := validLabRequest().Validate(); err != nil { t.Fatalf("Validate() error = %v", err) } } func TestLabRequestValidateRejectsWhatTheProvisionerRejects(t *testing.T) { tests := []struct { name string change func(*outbound.LabRequest) }{ {"no attempt item", func(r *outbound.LabRequest) { r.Key.AttemptItemID = uuid.Nil }}, {"empty template", func(r *outbound.LabRequest) { r.Template.ID = "" }}, {"template with capitals", func(r *outbound.LabRequest) { r.Template.ID = "MikroTik" }}, {"template too long", func(r *outbound.LabRequest) { r.Template.ID = strings.Repeat("a", 49) }}, {"template version zero", func(r *outbound.LabRequest) { r.Template.Version = 0 }}, {"template version above range", func(r *outbound.LabRequest) { r.Template.Version = 10001 }}, {"zero seed", func(r *outbound.LabRequest) { r.Seed = outbound.LabSeed{} }}, {"lifetime below range", func(r *outbound.LabRequest) { r.Lifetime = 4 * time.Minute }}, {"lifetime above range", func(r *outbound.LabRequest) { r.Lifetime = 4*time.Hour + time.Second }}, {"lifetime with a fraction of a second", func(r *outbound.LabRequest) { r.Lifetime = 10*time.Minute + time.Millisecond }}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { request := validLabRequest() test.change(&request) err := request.Validate() if !errors.Is(err, outbound.ErrInvalidLabRequest) { t.Fatalf("Validate() error = %v, want ErrInvalidLabRequest", err) } }) } } func TestLabRequestAndSeedNeverPrintTheSeed(t *testing.T) { request := validLabRequest() seedHex := request.Seed.Hex() if len(seedHex) != 2*outbound.LabSeedBytes { t.Fatalf("Hex() length = %d", len(seedHex)) } var logged strings.Builder logger := slog.New(slog.NewJSONHandler(&logged, nil)) logger.Info("lab", slog.Any("request", request), slog.Any("seed", request.Seed)) printed := []string{ fmt.Sprint(request), fmt.Sprintf("%+v", request), fmt.Sprintf("%#v", request), fmt.Sprintf("%x", request.Seed), fmt.Sprintf("%v", request.Seed), logged.String(), } for _, text := range printed { if strings.Contains(text, seedHex) || strings.Contains(text, "mikrotik-ospf-pair") { t.Fatalf("printed form leaks the request: %q", text) } } } func TestLabConsoleTokenIsRedacted(t *testing.T) { token := outbound.LabConsoleToken{Token: "console-token-marker", Node: "r1", AttachPath: "/v1/labs/x/console"} var logged strings.Builder slog.New(slog.NewJSONHandler(&logged, nil)).Info("token", slog.Any("token", token)) for _, text := range []string{fmt.Sprint(token), fmt.Sprintf("%+v", token), fmt.Sprintf("%#v", token), logged.String()} { if strings.Contains(text, "console-token-marker") { t.Fatalf("printed form leaks the token: %q", text) } } } func TestBusyIsACapacityStateAndNotAnInfrastructureError(t *testing.T) { busy := outbound.LabStatus{State: outbound.LabBusy, RetryAfter: 30 * time.Second} if busy.Active() { t.Fatal("a busy answer holds no lab") } for _, err := range []error{ outbound.ErrLabProvisionerDisabled, outbound.ErrLabProvisionerUnauthorized, outbound.ErrLabProvisionerBadResponse, } { if !errors.Is(err, outbound.ErrLabProvisionerUnavailable) { t.Fatalf("%v must be an unavailable provisioner", err) } } for _, err := range []error{ outbound.ErrInvalidLabRequest, outbound.ErrLabNotFound, outbound.ErrLabKeyReused, outbound.ErrLabNotReady, } { if errors.Is(err, outbound.ErrLabProvisionerUnavailable) { t.Fatalf("%v must not be an unavailable provisioner", err) } } } func TestLabStatusActive(t *testing.T) { for state, active := range map[outbound.LabState]bool{ outbound.LabProvisioning: true, outbound.LabReady: true, outbound.LabDeleting: false, outbound.LabDeleted: false, outbound.LabFailed: false, outbound.LabBusy: false, } { if got := (outbound.LabStatus{State: state}).Active(); got != active { t.Fatalf("Active() for %s = %v, want %v", state, got, active) } } } func TestValidateLabNodeName(t *testing.T) { if err := outbound.ValidateLabNodeName("r1"); err != nil { t.Fatalf("ValidateLabNodeName(r1) error = %v", err) } for _, name := range []string{"", "R1", "1r", "r_1", strings.Repeat("a", 16)} { if err := outbound.ValidateLabNodeName(name); !errors.Is(err, outbound.ErrInvalidLabRequest) { t.Fatalf("ValidateLabNodeName(%q) error = %v", name, err) } } } func TestLabCheckResultValidate(t *testing.T) { valid := outbound.LabCheckResult{ CheckedAt: time.Date(2026, 10, 11, 9, 0, 0, 0, time.UTC), Score: 1, MaxScore: 2, Criteria: []outbound.LabCriterionResult{{ID: "ospf", Passed: true}, {ID: "ping", Passed: false}}, } if err := valid.Validate(); err != nil { t.Fatalf("Validate() error = %v", err) } tests := []struct { name string change func(*outbound.LabCheckResult) }{ {"no criteria", func(r *outbound.LabCheckResult) { r.Criteria, r.Score, r.MaxScore = nil, 0, 0 }}, {"score above passed", func(r *outbound.LabCheckResult) { r.Score = 2 }}, {"max score not the count", func(r *outbound.LabCheckResult) { r.MaxScore = 3 }}, {"duplicate criterion", func(r *outbound.LabCheckResult) { r.Criteria[1].ID = "ospf" }}, {"no check time", func(r *outbound.LabCheckResult) { r.CheckedAt = time.Time{} }}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { result := valid result.Criteria = append([]outbound.LabCriterionResult(nil), valid.Criteria...) test.change(&result) if err := result.Validate(); err == nil { t.Fatal("Validate() accepted a contradictory result") } }) } if text := fmt.Sprint(valid); strings.Contains(text, "ospf") { t.Fatalf("printed result leaks a criterion: %q", text) } }