package resume

import (
	"testing"

	"haixun-backend/internal/model/job/domain/entity"
	"haixun-backend/internal/model/job/domain/enum"
)

func TestPrepareStepsForRetry_PreservesSucceeded(t *testing.T) {
	started := int64(100)
	ended := int64(200)
	steps := []entity.StepProgress{
		{ID: "prepare", Status: enum.StepStatusSucceeded, StartedAt: &started, EndedAt: &ended, Message: "done"},
		{ID: "execute", Status: enum.StepStatusFailed, Message: "boom"},
	}
	out := PrepareStepsForRetry(steps)
	if out[0].Status != enum.StepStatusSucceeded {
		t.Fatalf("prepare status = %s", out[0].Status)
	}
	if out[1].Status != enum.StepStatusPending {
		t.Fatalf("execute status = %s", out[1].Status)
	}
	if out[1].Message != "" {
		t.Fatalf("execute message = %q", out[1].Message)
	}
}

func TestFirstResumablePhase(t *testing.T) {
	steps := []entity.StepProgress{
		{ID: "prepare", Status: enum.StepStatusSucceeded},
		{ID: "execute", Status: enum.StepStatusPending},
	}
	if got := FirstResumablePhase(steps); got != "execute" {
		t.Fatalf("phase = %s, want execute", got)
	}
}

func TestShouldSkipStep(t *testing.T) {
	if !ShouldSkipStep(enum.StepStatusSucceeded) {
		t.Fatal("succeeded should be skipped")
	}
	if ShouldSkipStep(enum.StepStatusPending) {
		t.Fatal("pending should not be skipped")
	}
}
