feat(reports): align v2 report envelopes

This commit is contained in:
2026-06-06 23:01:46 +08:00
parent d48969f07f
commit 2d94ec9b53
7 changed files with 75 additions and 49 deletions
@@ -1,6 +1,7 @@
package v1
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
@@ -39,6 +40,7 @@ func (s *SummaryReportHandlerTestSuite) SetupSuite() {
accountGroup.GET("/summary_reports/team", s.handler.Team)
accountGroup.GET("/summary_reports/inbox", s.handler.Inbox)
accountGroup.GET("/summary_reports/label", s.handler.Label)
accountGroup.GET("/summary_reports/channel", s.handler.Channel)
s.router = r
s.account = &model.Account{Name: "TestAccount"}
@@ -51,7 +53,7 @@ func TestSummaryReportHandlerTestSuite(t *testing.T) {
func (s *SummaryReportHandlerTestSuite) TestAgent_InvalidAccountID() {
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/api/v1/accounts/abc/summary_reports/agent?since=2024-01-01T00:00:00Z&until=2024-12-31T23:59:59Z", nil)
req := httptest.NewRequest(http.MethodGet, "/api/v1/accounts/abc/summary_reports/agent?since=1704067200&until=1735689599", nil)
s.router.ServeHTTP(w, req)
s.Equal(http.StatusBadRequest, w.Code)
}
@@ -65,15 +67,16 @@ func (s *SummaryReportHandlerTestSuite) TestAgent_MissingDateRange() {
func (s *SummaryReportHandlerTestSuite) TestAgent_Success() {
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/api/v1/accounts/%d/summary_reports/agent?since=2024-01-01T00:00:00Z&until=2024-12-31T23:59:59Z", s.account.ID), nil)
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/api/v1/accounts/%d/summary_reports/agent?since=1704067200&until=1735689599", s.account.ID), nil)
s.router.ServeHTTP(w, req)
// PG-specific SQL may fail on SQLite
s.True(w.Code == http.StatusOK || w.Code == http.StatusUnprocessableEntity, "got %d", w.Code)
s.Equal(http.StatusOK, w.Code)
var body []interface{}
s.NoError(json.Unmarshal(w.Body.Bytes(), &body))
}
func (s *SummaryReportHandlerTestSuite) TestTeam_InvalidAccountID() {
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/api/v1/accounts/abc/summary_reports/team?since=2024-01-01T00:00:00Z&until=2024-12-31T23:59:59Z", nil)
req := httptest.NewRequest(http.MethodGet, "/api/v1/accounts/abc/summary_reports/team?since=1704067200&until=1735689599", nil)
s.router.ServeHTTP(w, req)
s.Equal(http.StatusBadRequest, w.Code)
}
@@ -87,14 +90,21 @@ func (s *SummaryReportHandlerTestSuite) TestTeam_MissingDateRange() {
func (s *SummaryReportHandlerTestSuite) TestInbox_InvalidAccountID() {
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/api/v1/accounts/abc/summary_reports/inbox?since=2024-01-01T00:00:00Z&until=2024-12-31T23:59:59Z", nil)
req := httptest.NewRequest(http.MethodGet, "/api/v1/accounts/abc/summary_reports/inbox?since=1704067200&until=1735689599", nil)
s.router.ServeHTTP(w, req)
s.Equal(http.StatusBadRequest, w.Code)
}
func (s *SummaryReportHandlerTestSuite) TestLabel_InvalidAccountID() {
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/api/v1/accounts/abc/summary_reports/label?since=2024-01-01T00:00:00Z&until=2024-12-31T23:59:59Z", nil)
req := httptest.NewRequest(http.MethodGet, "/api/v1/accounts/abc/summary_reports/label?since=1704067200&until=1735689599", nil)
s.router.ServeHTTP(w, req)
s.Equal(http.StatusBadRequest, w.Code)
}
}
func (s *SummaryReportHandlerTestSuite) TestChannel_DateRangeTooLong() {
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/api/v1/accounts/%d/summary_reports/channel?since=1704067200&until=1735689599", s.account.ID), nil)
s.router.ServeHTTP(w, req)
s.Equal(http.StatusBadRequest, w.Code)
}