|
| 1 | +package vmstatus |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + etypes "github.com/chaitin/MonkeyCode/backend/ent/types" |
| 9 | + "github.com/chaitin/MonkeyCode/backend/pkg/taskflow" |
| 10 | +) |
| 11 | + |
| 12 | +func TestInputDoesNotExposeReportedStatus(t *testing.T) { |
| 13 | + if _, ok := reflect.TypeOf(Input{}).FieldByName("ReportedStatus"); ok { |
| 14 | + t.Fatal("Input should not expose ReportedStatus") |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +func TestResolve(t *testing.T) { |
| 19 | + now := time.Date(2026, 4, 15, 12, 0, 0, 0, time.UTC) |
| 20 | + |
| 21 | + tests := []struct { |
| 22 | + name string |
| 23 | + input Input |
| 24 | + want taskflow.VirtualMachineStatus |
| 25 | + }{ |
| 26 | + { |
| 27 | + name: "is recycled overrides everything", |
| 28 | + input: Input{ |
| 29 | + Online: true, |
| 30 | + IsRecycled: true, |
| 31 | + CreatedAt: now.Add(-10 * time.Minute), |
| 32 | + Now: now, |
| 33 | + }, |
| 34 | + want: taskflow.VirtualMachineStatusOffline, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "online returns online before conditions", |
| 38 | + input: Input{ |
| 39 | + Online: true, |
| 40 | + Conditions: []*etypes.Condition{ |
| 41 | + {Type: etypes.ConditionTypeFailed}, |
| 42 | + }, |
| 43 | + CreatedAt: now.Add(-10 * time.Minute), |
| 44 | + Now: now, |
| 45 | + }, |
| 46 | + want: taskflow.VirtualMachineStatusOnline, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "online returns online", |
| 50 | + input: Input{ |
| 51 | + Online: true, |
| 52 | + CreatedAt: now.Add(-10 * time.Minute), |
| 53 | + Now: now, |
| 54 | + }, |
| 55 | + want: taskflow.VirtualMachineStatusOnline, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "failed condition returns offline", |
| 59 | + input: Input{ |
| 60 | + Conditions: []*etypes.Condition{ |
| 61 | + {Type: etypes.ConditionTypeFailed}, |
| 62 | + }, |
| 63 | + CreatedAt: now.Add(-10 * time.Minute), |
| 64 | + Now: now, |
| 65 | + }, |
| 66 | + want: taskflow.VirtualMachineStatusOffline, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "hibernated condition returns hibernated", |
| 70 | + input: Input{ |
| 71 | + Conditions: []*etypes.Condition{ |
| 72 | + {Type: etypes.ConditionTypeHibernated}, |
| 73 | + }, |
| 74 | + CreatedAt: now.Add(-10 * time.Minute), |
| 75 | + Now: now, |
| 76 | + }, |
| 77 | + want: taskflow.VirtualMachineStatusHibernated, |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "ready older than three minutes returns offline", |
| 81 | + input: Input{ |
| 82 | + Conditions: []*etypes.Condition{ |
| 83 | + {Type: etypes.ConditionTypeReady}, |
| 84 | + }, |
| 85 | + CreatedAt: now.Add(-3*time.Minute - time.Second), |
| 86 | + Now: now, |
| 87 | + }, |
| 88 | + want: taskflow.VirtualMachineStatusOffline, |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "ready within three minutes stays pending", |
| 92 | + input: Input{ |
| 93 | + Conditions: []*etypes.Condition{ |
| 94 | + {Type: etypes.ConditionTypeReady}, |
| 95 | + }, |
| 96 | + CreatedAt: now.Add(-2 * time.Minute), |
| 97 | + Now: now, |
| 98 | + }, |
| 99 | + want: taskflow.VirtualMachineStatusPending, |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "defaults to pending", |
| 103 | + input: Input{ |
| 104 | + CreatedAt: now, |
| 105 | + Now: now, |
| 106 | + }, |
| 107 | + want: taskflow.VirtualMachineStatusPending, |
| 108 | + }, |
| 109 | + } |
| 110 | + |
| 111 | + for _, tt := range tests { |
| 112 | + t.Run(tt.name, func(t *testing.T) { |
| 113 | + got := Resolve(tt.input) |
| 114 | + if got != tt.want { |
| 115 | + t.Fatalf("Resolve() = %q, want %q", got, tt.want) |
| 116 | + } |
| 117 | + }) |
| 118 | + } |
| 119 | +} |
0 commit comments