package api import ( "slices" "testing" "github.com/google/uuid" ) func TestPaginate(t *testing.T) { // A list of 0..9 shown in order, read the way the queries do: from a // cursor in reading direction, one row more than the page. all := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} key := func(i int) cursor { return cursor{S: string(rune('a' + i)), ID: uuid.Nil} } read := func(p pageReq) []int { var out []int if p.backward() { for i := len(all) - 1; i >= 0; i-- { if key(all[i]).S < p.before.S { out = append(out, all[i]) } } } else { for _, v := range all { if p.after == nil || key(v).S > p.after.S { out = append(out, v) } } } return out[:min(len(out), int(p.fetch()))] } page := func(p pageReq) ([]int, pageInfo) { return paginate(p, read(p), key) } first, info := page(pageReq{limit: 4}) if !slices.Equal(first, []int{0, 1, 2, 3}) || info.Prev != "" || info.Next == "" { t.Fatalf("first = %v %+v", first, info) } next, _ := decodeCursor(info.Next) second, info := page(pageReq{limit: 4, after: next}) if !slices.Equal(second, []int{4, 5, 6, 7}) || info.Prev == "" || info.Next == "" { t.Fatalf("second = %v %+v", second, info) } n3, _ := decodeCursor(info.Next) last, lastInfo := page(pageReq{limit: 4, after: n3}) if !slices.Equal(last, []int{8, 9}) || lastInfo.Next != "" || lastInfo.Prev == "" { t.Fatalf("last = %v %+v", last, lastInfo) } // Back from the last page and from the second to the first. prev, _ := decodeCursor(lastInfo.Prev) back, backInfo := page(pageReq{limit: 4, before: prev}) if !slices.Equal(back, []int{4, 5, 6, 7}) || backInfo.Prev == "" || backInfo.Next == "" { t.Fatalf("back = %v %+v", back, backInfo) } p2, _ := decodeCursor(backInfo.Prev) start, startInfo := page(pageReq{limit: 4, before: p2}) if !slices.Equal(start, []int{0, 1, 2, 3}) || startInfo.Prev != "" || startInfo.Next == "" { t.Fatalf("start = %v %+v", start, startInfo) } if _, ok := decodeCursor("not a cursor!"); ok { t.Error("garbage cursor accepted") } } func TestPageSlice(t *testing.T) { items := []string{"a", "b", "c", "d", "e"} key := func(s string) cursor { return cursor{S: s} } first, info := pageSlice(items, pageReq{limit: 2}, key) next, _ := decodeCursor(info.Next) second, info2 := pageSlice(items, pageReq{limit: 2, after: next}, key) prev, _ := decodeCursor(info2.Prev) back, _ := pageSlice(items, pageReq{limit: 2, before: prev}, key) if !slices.Equal(first, []string{"a", "b"}) || !slices.Equal(second, []string{"c", "d"}) || !slices.Equal(back, first) { t.Errorf("first %v, second %v, back %v", first, second, back) } } func TestEmailKey(t *testing.T) { for in, want := range map[string]string{ "Max.Power+uptime@googlemail.com": "maxpower@gmail.com", "max.power@gmail.com": "maxpower@gmail.com", "a.b+c@example.com": "a.b@example.com", "A.B@Example.com": "a.b@example.com", } { if got := emailKey(in); got != want { t.Errorf("emailKey(%q) = %q, want %q", in, got, want) } } if clientKey("2001:db8:1:2:aaaa::1") != clientKey("2001:db8:1:2:bbbb::9") || clientKey("2001:db8:1:3::1") == clientKey("2001:db8:1:2::1") { t.Error("IPv6 clients are not grouped by /64") } if clientKey("203.0.113.7") != "203.0.113.7" { t.Error("IPv4 clients are grouped") } }