93 lines
2.5 KiB
Go
93 lines
2.5 KiB
Go
package audit
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestCursorRoundTrip(t *testing.T) {
|
|
t.Parallel()
|
|
at := time.Date(2026, 10, 25, 12, 30, 45, 123456789, time.UTC)
|
|
cursor := Cursor{At: at, Rank: 3, ID: 9007199254740991, FilterHash: "0123456789abcdef"}
|
|
parsed, ok := ParseCursor(cursor.Encode())
|
|
if !ok {
|
|
t.Fatalf("ParseCursor(%q) = !ok", cursor.Encode())
|
|
}
|
|
if !parsed.At.Equal(at) || parsed.Rank != cursor.Rank || parsed.ID != cursor.ID || parsed.FilterHash != cursor.FilterHash {
|
|
t.Fatalf("round trip = %+v, want %+v", parsed, cursor)
|
|
}
|
|
if parsed.Encode() != cursor.Encode() {
|
|
t.Fatalf("re-encoded cursor = %q, want %q", parsed.Encode(), cursor.Encode())
|
|
}
|
|
}
|
|
|
|
func TestCursorOrder(t *testing.T) {
|
|
t.Parallel()
|
|
at := time.Date(2026, 10, 25, 12, 0, 0, 0, time.UTC)
|
|
tests := []struct {
|
|
name string
|
|
newer Cursor
|
|
older Cursor
|
|
}{
|
|
{
|
|
name: "later time is newer regardless of rank",
|
|
newer: Cursor{At: at.Add(time.Second), Rank: 1, ID: 1},
|
|
older: Cursor{At: at, Rank: 4, ID: 99},
|
|
},
|
|
{
|
|
name: "equal time, higher rank is newer",
|
|
newer: Cursor{At: at, Rank: 2, ID: 1},
|
|
older: Cursor{At: at, Rank: 1, ID: 99},
|
|
},
|
|
{
|
|
name: "equal time and rank, higher id is newer",
|
|
newer: Cursor{At: at, Rank: 3, ID: 42},
|
|
older: Cursor{At: at, Rank: 3, ID: 41},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
if !tt.newer.After(tt.older) {
|
|
t.Fatalf("%+v is not after %+v", tt.newer, tt.older)
|
|
}
|
|
if tt.older.After(tt.newer) {
|
|
t.Fatalf("%+v is after %+v", tt.older, tt.newer)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestCursorMalformedMeansNewest(t *testing.T) {
|
|
t.Parallel()
|
|
for _, raw := range []string{
|
|
"",
|
|
"not-a-cursor",
|
|
"2026-10-25T12:00:00.000000000Z|01|0000000000000000001",
|
|
"2026-10-25T12:00:00.000000000Z|xx|0000000000000000001|0123456789abcdef",
|
|
"2026-10-25T12:00:00.000000000Z|01|xx|0123456789abcdef",
|
|
"2026-10-25T12:00:00.000000000Z|01|0000000000000000001|",
|
|
} {
|
|
if _, ok := ParseCursor(raw); ok {
|
|
t.Fatalf("ParseCursor(%q) = ok, want malformed", raw)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCursorRefusesAnotherFilter(t *testing.T) {
|
|
t.Parallel()
|
|
filter := Filter{Categories: []string{"settings"}}
|
|
if err := filter.Normalize(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cursor := Cursor{At: time.Now().UTC(), Rank: 1, ID: 7}
|
|
cursor.FilterHash = filter.Hash()
|
|
if !cursor.MatchesFilter(filter) {
|
|
t.Fatal("cursor does not match the filter it was issued for")
|
|
}
|
|
other := Filter{Categories: []string{"access"}}
|
|
if cursor.MatchesFilter(other) {
|
|
t.Fatal("cursor matched a different filter")
|
|
}
|
|
}
|