items_integration_test.go 4.9 KB

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