items_integration_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package fever_test
  4. import (
  5. "cmp"
  6. "maps"
  7. "net/url"
  8. "slices"
  9. "strconv"
  10. "strings"
  11. "testing"
  12. "time"
  13. miniflux "miniflux.app/v2/client"
  14. "miniflux.app/v2/internal/http/server/servertest"
  15. )
  16. func TestItems(t *testing.T) {
  17. t.Parallel()
  18. user := newTestUser(t)
  19. feedID, entries := subscribeToFixture(t, user)
  20. if err := user.rest.UpdateEntries([]int64{entries[0].ID}, miniflux.EntryStatusRead); err != nil {
  21. t.Fatal(err)
  22. }
  23. if err := user.rest.ToggleStarred(entries[1].ID); err != nil {
  24. t.Fatal(err)
  25. }
  26. var result itemsResult
  27. user.fever.getJSON(t, selector("items"), &result)
  28. if result.TotalItems != servertest.EntryCount {
  29. t.Errorf(`Expected total_items %d, got %d`, servertest.EntryCount, result.TotalItems)
  30. }
  31. // Items are sorted by ID. Entry 1 is read and entry 2 is saved.
  32. expected := make([]itemResult, 0, len(entries))
  33. for index, entry := range entries {
  34. expected = append(expected, itemResult{
  35. ID: entry.ID,
  36. FeedID: feedID,
  37. Title: entry.Title,
  38. Author: entry.Author,
  39. HTML: entry.Content,
  40. URL: entry.URL,
  41. IsSaved: boolInt(index == 1),
  42. IsRead: boolInt(index == 0),
  43. CreatedOnTime: entry.Date.Unix(),
  44. })
  45. }
  46. slices.SortFunc(expected, func(left, right itemResult) int { return cmp.Compare(left.ID, right.ID) })
  47. if !slices.Equal(result.Items, expected) {
  48. t.Fatalf(`Unexpected items, got %+v instead of %+v`, result.Items, expected)
  49. }
  50. // The fields come from the fixture feed, not only from the REST API.
  51. item := result.Items[slices.IndexFunc(result.Items, func(item itemResult) bool { return item.ID == entries[1].ID })]
  52. if item.Title != "Entry 2" || !strings.Contains(item.HTML, servertest.SearchTerm) {
  53. t.Errorf(`Expected the title and content of entry 2, got %+v`, item)
  54. }
  55. if expectedTime := time.Date(2025, time.January, 2, 10, 0, 0, 0, time.UTC).Unix(); item.CreatedOnTime != expectedTime {
  56. t.Errorf(`Expected created_on_time %d, got %d`, expectedTime, item.CreatedOnTime)
  57. }
  58. }
  59. func TestItemsSelection(t *testing.T) {
  60. t.Parallel()
  61. user := newTestUser(t)
  62. _, entries := subscribeToFixture(t, user)
  63. ids := sortedIDs(entryIDs(entries)...)
  64. oldest, middle, newest := ids[0], ids[1], ids[2]
  65. scenarios := []struct {
  66. name string
  67. params url.Values
  68. expected []int64
  69. }{
  70. {"no argument returns the oldest items first", nil, []int64{oldest, middle, newest}},
  71. {"since_id returns the newer items, oldest first", url.Values{"since_id": {formatID(oldest)}}, []int64{middle, newest}},
  72. {"since_id=0 returns the oldest items first", url.Values{"since_id": {"0"}}, []int64{oldest, middle, newest}},
  73. {"since_id of the newest item returns nothing", url.Values{"since_id": {formatID(newest)}}, []int64{}},
  74. {"max_id returns the older items, newest first", url.Values{"max_id": {formatID(newest)}}, []int64{middle, oldest}},
  75. {"max_id=0 returns the newest items first", url.Values{"max_id": {"0"}}, []int64{newest, middle, oldest}},
  76. {"max_id of the oldest item returns nothing", url.Values{"max_id": {formatID(oldest)}}, []int64{}},
  77. {"with_ids returns the items, oldest first", url.Values{"with_ids": {formatID(newest) + "," + formatID(oldest)}}, []int64{oldest, newest}},
  78. {"with_ids ignores spaces", url.Values{"with_ids": {formatID(newest) + ", " + formatID(oldest)}}, []int64{oldest, newest}},
  79. {"with_ids ignores invalid IDs", url.Values{"with_ids": {formatID(middle) + ",invalid"}}, []int64{middle}},
  80. {"with_ids without IDs returns nothing", url.Values{"with_ids": {""}}, []int64{}},
  81. {"with_ids without valid IDs returns nothing", url.Values{"with_ids": {"invalid,0,-1"}}, []int64{}},
  82. {
  83. "since_id takes precedence over max_id",
  84. url.Values{"since_id": {formatID(oldest)}, "max_id": {formatID(middle)}},
  85. []int64{middle, newest},
  86. },
  87. {
  88. "max_id takes precedence over with_ids",
  89. url.Values{"max_id": {formatID(newest)}, "with_ids": {formatID(newest)}},
  90. []int64{middle, oldest},
  91. },
  92. }
  93. for _, scenario := range scenarios {
  94. t.Run(scenario.name, func(t *testing.T) {
  95. query := selector("items")
  96. maps.Copy(query, scenario.params)
  97. var result itemsResult
  98. user.fever.getJSON(t, query, &result)
  99. if result.Items == nil {
  100. t.Fatalf(`Expected an items array, got null`)
  101. }
  102. if !slices.Equal(result.ids(), scenario.expected) {
  103. t.Errorf(`Expected items %v, got %v`, scenario.expected, result.ids())
  104. }
  105. // total_items counts every item of the user, whatever the selection.
  106. if result.TotalItems != servertest.EntryCount {
  107. t.Errorf(`Expected total_items %d, got %d`, servertest.EntryCount, result.TotalItems)
  108. }
  109. })
  110. }
  111. }
  112. func formatID(id int64) string {
  113. return strconv.FormatInt(id, 10)
  114. }
  115. // boolInt returns the Fever boolean integer: 1 for true, 0 for false.
  116. func boolInt(value bool) int {
  117. if value {
  118. return 1
  119. }
  120. return 0
  121. }