entry_pagination_integration_test.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package api_test
  4. import (
  5. "fmt"
  6. "net/http"
  7. "net/http/httptest"
  8. "slices"
  9. "strings"
  10. "testing"
  11. miniflux "miniflux.app/v2/client"
  12. )
  13. func TestGetEntriesStablePaginationWithIdenticalPublishedDates(t *testing.T) {
  14. t.Parallel()
  15. testConfig := requireIntegrationTestConfig(t)
  16. const (
  17. entryCount = 120
  18. pageSize = 25
  19. )
  20. var feed strings.Builder
  21. feed.WriteString(`<?xml version="1.0"?><rss version="2.0"><channel><title>Pagination</title><link>https://example.org</link><description>Pagination test</description>`)
  22. for i := range entryCount {
  23. fmt.Fprintf(&feed, `<item><guid>pagination-%d</guid><title>Shared pagination title</title><link>https://example.org/%d</link><pubDate>Wed, 01 Jan 2025 00:00:00 GMT</pubDate><description>Shared pagination content</description></item>`, i, i)
  24. }
  25. feed.WriteString(`</channel></rss>`)
  26. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  27. w.Header().Set("Content-Type", "application/rss+xml")
  28. fmt.Fprint(w, feed.String())
  29. }))
  30. defer server.Close()
  31. client, _ := testConfig.newRegularUser(t)
  32. if _, err := client.CreateFeed(&miniflux.FeedCreationRequest{FeedURL: server.URL}); err != nil {
  33. t.Fatal(err)
  34. }
  35. all, err := client.EntryIDs(nil)
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. if all.Total != entryCount || len(all.EntryIDs) != entryCount {
  40. t.Fatalf("Got %d IDs and total %d, want %d", len(all.EntryIDs), all.Total, entryCount)
  41. }
  42. for _, direction := range []string{"asc", "desc"} {
  43. want := slices.Clone(all.EntryIDs)
  44. slices.Sort(want)
  45. if direction == "desc" {
  46. slices.Reverse(want)
  47. }
  48. for _, search := range []string{"", "pagination"} {
  49. t.Run(direction+"/search="+search, func(t *testing.T) {
  50. var got []int64
  51. for offset := 0; offset < len(want); offset += pageSize {
  52. page, err := client.Entries(&miniflux.Filter{
  53. Order: "published_at",
  54. Direction: direction,
  55. Search: search,
  56. Limit: pageSize,
  57. Offset: offset,
  58. })
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. if page.Total != len(want) {
  63. t.Fatalf("Got total %d, want %d", page.Total, len(want))
  64. }
  65. for _, entry := range page.Entries {
  66. got = append(got, entry.ID)
  67. }
  68. }
  69. if !slices.Equal(got, want) {
  70. t.Fatalf("Got IDs %v, want %v", got, want)
  71. }
  72. })
  73. }
  74. }
  75. }