From da60f2a0e074231e38032ac9b3b05d1358d0a169 Mon Sep 17 00:00:00 2001 From: Justin Benge Date: Wed, 3 Jun 2026 19:51:00 +0200 Subject: [PATCH 1/4] Added tests to verify basic GetAllItems functionality --- internal/store/store_test.go | 107 +++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 internal/store/store_test.go diff --git a/internal/store/store_test.go b/internal/store/store_test.go new file mode 100644 index 0000000..ba5c37a --- /dev/null +++ b/internal/store/store_test.go @@ -0,0 +1,107 @@ +package store + +import ( + "testing" + "time" +) + +func TestStore_GetAllItemsOrder(t *testing.T) { + now := time.Now() + testCases := []struct { + name string + ordering string + items []Item + expectedOrder []string + }{ + { + name: "test descending_by_new", + ordering: "desc", + items: []Item{ + { + PublishedAt: now.Add(-2 * time.Hour), + CreatedAt: now.Add(-2 * time.Hour), + Title: "First Item", + Content: "A short string", + FeedURL: "example.com", + Link: "example.com/endpoint", + }, + { + PublishedAt: now.Add(-23 * time.Hour), + CreatedAt: now.Add(-24 * time.Hour), + Title: "Second Item", + Content: "A really long string", + FeedURL: "example.com", + Link: "example.com/api", + }, + { + PublishedAt: now.Add(-20 * time.Hour), + CreatedAt: now.Add(-20 * time.Hour), + Title: "Third Item", + Content: "Some boring string. Use lorem ipsum", + FeedURL: "example.com", + Link: "example.com/anotherExample", + }, + }, + expectedOrder: []string{"First Item", "Second Item", "Third Item"}, + }, + { + name: "test_order_by_content_size", + ordering: "length", + items: []Item{ + { + PublishedAt: now.Add(-2 * time.Hour), + CreatedAt: now.Add(-2 * time.Hour), + Title: "First Item", + Content: "AA", + FeedURL: "example.com", + Link: "example.com/endpoint", + }, + { + PublishedAt: now.Add(-23 * time.Hour), + CreatedAt: now.Add(-24 * time.Hour), + Title: "Second Item", + Content: "AAA", + FeedURL: "example.com", + Link: "example.com/api", + }, + { + PublishedAt: now.Add(-20 * time.Hour), + CreatedAt: now.Add(-20 * time.Hour), + Title: "Third Item", + Content: "A", + FeedURL: "example.com", + Link: "example.com/anotherExample", + }, + }, + expectedOrder: []string{"First Item", "Second Item", "Third Item"}, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + sls, err := NewInMemorySQLiteStore() + if err != nil { + t.Fatalf("Error initailizing db: %v", err) + } + for _, item := range tc.items { + err = sls.UpsertItem(&item) + if item.Title == "Third Item" { + sls.ToggleRead(item.ID) + } + if err != nil { + t.Errorf("Error insert %v: %v", item, err) + } + } + allItems, err := sls.GetAllItems(tc.ordering) + if err != nil { + t.Fatalf("Error fetching all items: %v", err) + } + for i := range allItems { + if allItems[i].Title != tc.expectedOrder[i] { + t.Fatalf("Unexpected item order. Want: %v Got: %v", tc.expectedOrder[i], allItems[i].Title) + } + } + + }) + } +} From d6df2d111b5bd738bb2bae598684666969a869a7 Mon Sep 17 00:00:00 2001 From: Justin Benge Date: Wed, 3 Jun 2026 19:51:37 +0200 Subject: [PATCH 2/4] Added new constant to store: length ordering. To allow ordering by length --- internal/constants/store.go | 1 + internal/store/store.go | 12 +++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/internal/constants/store.go b/internal/constants/store.go index d45c450..835e50d 100644 --- a/internal/constants/store.go +++ b/internal/constants/store.go @@ -4,5 +4,6 @@ package constants const ( AscendingOrdering = "asc" DescendingOrdering = "desc" + LengthOrdering = "length" DefaultOrdering = AscendingOrdering ) diff --git a/internal/store/store.go b/internal/store/store.go index b9da04e..d51b58d 100644 --- a/internal/store/store.go +++ b/internal/store/store.go @@ -257,15 +257,21 @@ func (sls *SQLiteStore) upsertItem(db statementPreparer, item *Item) error { // TODO: pagination func (sls SQLiteStore) GetAllItems(ordering string) ([]Item, error) { itemStmt := ` - select id, feedurl, guid, link, title, content, author, readat, favourite, publishedat, createdat, updatedat from items order by readat is not null asc, coalesce(publishedat, createdat) %s; + select id, feedurl, guid, link, title, content, author, readat, favourite, publishedat, createdat, updatedat from items order by readat is not null asc, %s; ` var stmt string + var timeStmt string + switch ordering { + case constants.LengthOrdering: + stmt = fmt.Sprintf(itemStmt, "length(content) asc") case constants.DescendingOrdering: - stmt = fmt.Sprintf(itemStmt, constants.DescendingOrdering) + timeStmt = fmt.Sprintf("coalesce(publishedat, createdat) %s", constants.DescendingOrdering) + stmt = fmt.Sprintf(itemStmt, timeStmt) default: - stmt = fmt.Sprintf(itemStmt, constants.DefaultOrdering) + timeStmt = fmt.Sprintf("coalesce(publishedat, createdat) %s", constants.DefaultOrdering) + stmt = fmt.Sprintf(itemStmt, timeStmt) } rows, err := sls.db.Query(stmt) From 576516a0ff2f0c63029f33ade56227c9fbf29423 Mon Sep 17 00:00:00 2001 From: Justin Benge Date: Wed, 3 Jun 2026 21:38:45 +0200 Subject: [PATCH 3/4] Fixed keybind that reverses sort order to allow for new ordering flag --- internal/commands/list.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/internal/commands/list.go b/internal/commands/list.go index da29a1d..b4a236e 100644 --- a/internal/commands/list.go +++ b/internal/commands/list.go @@ -95,10 +95,13 @@ func (m *model) UpdateList() tea.Cmd { func sortList(m model) func() tea.Msg { return func() tea.Msg { // reverse sorting order - if m.commands.config.Ordering == constants.AscendingOrdering { + switch m.commands.config.Ordering { + case constants.AscendingOrdering: m.commands.config.Ordering = constants.DescendingOrdering - } else { + case constants.DescendingOrdering: m.commands.config.Ordering = constants.AscendingOrdering + default: + // noop } items, err := m.commands.GetAllFeeds() From 7925f977ed6d1786da1302dbeca78e4681e5dcad Mon Sep 17 00:00:00 2001 From: Justin Benge Date: Wed, 3 Jun 2026 21:39:02 +0200 Subject: [PATCH 4/4] Updated readme to include new config --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 00b1c64..b336761 100644 --- a/README.md +++ b/README.md @@ -100,8 +100,7 @@ autoread: true ``` ### Ordering - -Set the default sort ordering of the list +Set the default sort ordering of the list. Ordering can be one of "asc", "desc" or "length". Length will sort by article content length in ascending order, unread items still show at the top. ```yaml ordering: asc