From c105b14f58464239451b55dd2d020bdf6aaf4048 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Mon, 18 Aug 2025 20:00:15 -0700 Subject: [PATCH] test(api): add integration test for fetching categories with counters --- client/client.go | 16 +++++++++++ internal/api/api_integration_test.go | 42 ++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/client/client.go b/client/client.go index 192f3874..bdb81237 100644 --- a/client/client.go +++ b/client/client.go @@ -276,6 +276,22 @@ func (c *Client) Categories() (Categories, error) { return categories, nil } +// CategoriesWithCounters fetches the categories with their respective feed and unread counts. +func (c *Client) CategoriesWithCounters() (Categories, error) { + body, err := c.request.Get("/v1/categories?counts=true") + if err != nil { + return nil, err + } + defer body.Close() + + var categories Categories + if err := json.NewDecoder(body).Decode(&categories); err != nil { + return nil, fmt.Errorf("miniflux: response error (%v)", err) + } + + return categories, nil +} + // CreateCategory creates a new category. func (c *Client) CreateCategory(title string) (*Category, error) { body, err := c.request.Post("/v1/categories", &CategoryCreationRequest{ diff --git a/internal/api/api_integration_test.go b/internal/api/api_integration_test.go index c94916f3..1e0ba2c6 100644 --- a/internal/api/api_integration_test.go +++ b/internal/api/api_integration_test.go @@ -1262,6 +1262,14 @@ func TestGetCategoriesEndpoint(t *testing.T) { t.Fatalf(`Invalid title, got %q instead of %q`, categories[0].Title, "All") } + if categories[0].FeedCount != nil { + t.Errorf(`Expected FeedCount to be nil, got %d`, *categories[0].FeedCount) + } + + if categories[0].TotalUnread != nil { + t.Errorf(`Expected TotalUnread to be nil, got %d`, *categories[0].TotalUnread) + } + if categories[1].ID != category.ID { t.Fatalf(`Invalid categoryID, got %d`, categories[0].ID) } @@ -1273,6 +1281,40 @@ func TestGetCategoriesEndpoint(t *testing.T) { if categories[1].Title != "My category" { t.Fatalf(`Invalid title, got %q instead of %q`, categories[0].Title, "My category") } + + if categories[1].FeedCount != nil { + t.Errorf(`Expected FeedCount to be nil, got %d`, *categories[1].FeedCount) + } + + if categories[1].TotalUnread != nil { + t.Errorf(`Expected TotalUnread to be nil, got %d`, *categories[1].TotalUnread) + } + + categories, err = regularUserClient.CategoriesWithCounters() + if err != nil { + t.Fatal(err) + } + + if len(categories) != 2 { + t.Fatalf(`Invalid number of categories, got %d instead of %d`, len(categories), 1) + } + + if categories[1].FeedCount == nil { + t.Fatalf(`Expected FeedCount to be not nil`) + } + + if categories[1].TotalUnread == nil { + t.Fatalf(`Expected TotalUnread to be not nil`) + } + + expectedCounterValue := 0 + if *categories[1].FeedCount != expectedCounterValue { + t.Errorf(`Expected FeedCount to be %d, got %d`, expectedCounterValue, *categories[1].FeedCount) + } + + if *categories[1].TotalUnread != expectedCounterValue { + t.Errorf(`Expected TotalUnread to be %d, got %d`, expectedCounterValue, *categories[1].TotalUnread) + } } func TestMarkCategoryAsReadEndpoint(t *testing.T) {