| 1 | package applog |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | "time" |
| 6 | ) |
| 7 | |
| 8 | func TestCursorRoundTripAndOrder(t *testing.T) { |
| 9 | r := Record{TS: time.Date(2026, 8, 10, 12, 0, 30, 500, time.UTC), Box: "b1", Seq: 42, Msg: "m"} |
| 10 | c, ok := DecodeCursor(EncodeCursor(r)) |
| 11 | if !ok || !c.TS.Equal(r.TS) || c.Box != "b1" || c.Seq != 42 { |
| 12 | t.Fatalf("round trip: %+v %v", c, ok) |
| 13 | } |
| 14 | // Before mirrors MergeRecords' (ts, box, seq) order exactly. |
| 15 | if c.Before(r) { |
| 16 | t.Fatal("a cursor at r is not before r") |
| 17 | } |
| 18 | for _, after := range []Record{ |
| 19 | {TS: r.TS.Add(time.Nanosecond), Box: "a0", Seq: 1}, |
| 20 | {TS: r.TS, Box: "b2", Seq: 1}, |
| 21 | {TS: r.TS, Box: "b1", Seq: 43}, |
| 22 | } { |
| 23 | if !c.Before(after) { |
| 24 | t.Fatalf("cursor should be before %+v", after) |
| 25 | } |
| 26 | } |
| 27 | if c.Before(Record{TS: r.TS, Box: "b1", Seq: 41}) { |
| 28 | t.Fatal("cursor before an earlier seq") |
| 29 | } |
| 30 | for _, bad := range []string{"", "!!!", "bm90IGpzb24"} { |
| 31 | if _, ok := DecodeCursor(bad); ok { |
| 32 | t.Errorf("DecodeCursor(%q) ok", bad) |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |