package task import "testing" func TestHappyPath(t *testing.T) { path := []struct { action Action want string }{ {ActionSubmit, PendingReview}, {ActionApprove, Queued}, {ActionDispatch, Dispatched}, {ActionStart, Running}, {ActionSuccess, Success}, } cur := Draft for _, step := range path { next, ok := Next(cur, step.action) if !ok || next != step.want { t.Fatalf("step %s from %s: got %s ok=%v, want %s", step.action, cur, next, ok, step.want) } cur = next } } func TestFailureRetry(t *testing.T) { cur, ok := Next(Draft, ActionSubmit) if !ok { t.Fatal("submit failed") } cur, ok = Next(cur, ActionApprove) if !ok { t.Fatal("approve failed") } cur, ok = Next(cur, ActionDispatch) if !ok { t.Fatal("dispatch failed") } cur, ok = Next(cur, ActionStart) if !ok { t.Fatal("start failed") } cur, ok = Next(cur, ActionFail) if !ok || cur != Failed { t.Fatalf("fail: got %s ok=%v", cur, ok) } cur, ok = Next(cur, ActionRetry) if !ok || cur != Queued { t.Fatalf("retry: got %s ok=%v", cur, ok) } } func TestSuspendResume(t *testing.T) { cur, _ := Next(Running, ActionSuspend) if cur != Suspended { t.Fatalf("suspend: got %s", cur) } cur, ok := Next(cur, ActionResume) if !ok || cur != Queued { t.Fatalf("resume: got %s ok=%v", cur, ok) } } func TestIllegalTransitions(t *testing.T) { cases := []struct { cur string action Action }{ {Draft, ActionApprove}, {PendingReview, ActionStart}, {Queued, ActionSuccess}, {Success, ActionRetry}, {Cancelled, ActionResubmit}, {Running, ActionApprove}, {Dispatched, ActionSuspend}, } for _, c := range cases { if _, ok := Next(c.cur, c.action); ok { t.Fatalf("illegal transition should fail: %s -> %s", c.cur, c.action) } } }