| 1 | package schedule |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/carlosframework/platform/internal/release" |
| 9 | ) |
| 10 | |
| 11 | // The wire shape is additive: an object written before this field existed |
| 12 | // must still parse, and read back as false. |
| 13 | func TestStatusWithoutHoldsAwakeStillParses(t *testing.T) { |
| 14 | raw := []byte(`{"schema":1,"host":"h","box":"b","reported_at":"2026-08-30T21:40:05Z","timers":[{"name":"sweep","kind":"schedule","spec":"every 5m","path":"/jobs/sweep","next_at":1788126300}]}`) |
| 15 | var st Status |
| 16 | if err := json.Unmarshal(raw, &st); err != nil { |
| 17 | t.Fatalf("unmarshal a pre-holds_awake status: %v", err) |
| 18 | } |
| 19 | if len(st.Timers) != 1 || st.Timers[0].HoldsAwake { |
| 20 | t.Errorf("HoldsAwake = %v, want false for an object written before the field existed", st.Timers[0].HoldsAwake) |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | func TestWriteAndListStatus(t *testing.T) { |
| 25 | ctx := context.Background() |
| 26 | st := release.NewDirStore(t.TempDir()) |
| 27 | b := Status{Host: "b.bab.oncarlos.com", Box: "box1", ReportedAt: "2026-08-23T22:00:00Z", Timers: []TimerStatus{{Name: "sync", Kind: KindSchedule, Spec: "every 6h0m0s", Path: "/jobs/sync", NextAt: 100}}} |
| 28 | a := Status{Host: "a.bab.oncarlos.com", Box: "box1", ReportedAt: "2026-08-23T22:00:00Z", Unsupported: "unit-backed instance"} |
| 29 | for _, s := range []Status{b, a, b} { // the rewrite must not conflict |
| 30 | if err := WriteStatus(ctx, st, "bab", "app", s); err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | } |
| 34 | got, err := ListStatus(ctx, st, "bab", "app") |
| 35 | if err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | if len(got) != 2 || got[0].Host != a.Host || got[1].Timers[0].Name != "sync" || got[0].Schema != 1 { |
| 39 | t.Fatalf("list = %+v", got) |
| 40 | } |
| 41 | if got[0].Timers == nil { |
| 42 | t.Fatal("timers must marshal as [] not null — the CLI ranges over it") |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | func TestListStatusEmptyPrefix(t *testing.T) { |
| 47 | got, err := ListStatus(context.Background(), release.NewDirStore(t.TempDir()), "bab", "app") |
| 48 | if err != nil || len(got) != 0 { |
| 49 | t.Fatalf("%v %v", got, err) |
| 50 | } |
| 51 | } |
| 52 | |