feat(report): ship daily report v1 experience

This commit is contained in:
phamnazage-jpg
2026-05-13 20:13:02 +08:00
parent 6a2cd3f159
commit 85f37a4d95
13 changed files with 3541 additions and 565 deletions

View File

@@ -6,6 +6,7 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"testing"
)
@@ -38,6 +39,9 @@ func TestSubscriptionPlansHandlerReturnsEnvelope(t *testing.T) {
},
}, nil
},
func(context.Context, *sql.DB) (*latestReportResponse, error) {
return nil, sql.ErrNoRows
},
)
req := httptest.NewRequest(http.MethodGet, "/api/v1/subscription-plans", nil)
@@ -76,3 +80,85 @@ func TestSubscriptionPlansHandlerReturnsEnvelope(t *testing.T) {
t.Fatalf("unexpected model scope length: %d", len(got.ModelScope))
}
}
func TestLatestReportHandlerReturnsEnvelope(t *testing.T) {
mux := newMux(
&sql.DB{},
func(context.Context, *sql.DB) ([]modelResponse, error) {
return nil, nil
},
func(context.Context, *sql.DB) ([]subscriptionPlanResponse, error) {
return nil, nil
},
func(context.Context, *sql.DB) (*latestReportResponse, error) {
return &latestReportResponse{
ReportDate: "2026-05-13",
Status: "generated",
ModelCount: 504,
MarkdownPath: "reports/daily/daily_report_2026-05-13.md",
HTMLPath: "reports/daily/html/daily_report_2026-05-13.html",
MarkdownURL: "/api/v1/reports/latest/markdown",
HTMLURL: "/api/v1/reports/latest/html",
}, nil
},
)
req := httptest.NewRequest(http.MethodGet, "/api/v1/reports/latest", nil)
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("expected status 200, got %d", rec.Code)
}
var payload struct {
Data latestReportResponse `json:"data"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &payload); err != nil {
t.Fatalf("unmarshal response: %v", err)
}
if payload.Data.ReportDate != "2026-05-13" {
t.Fatalf("unexpected report date: %q", payload.Data.ReportDate)
}
if payload.Data.HTMLURL != "/api/v1/reports/latest/html" {
t.Fatalf("unexpected html url: %q", payload.Data.HTMLURL)
}
}
func TestLatestReportHTMLHandlerServesArtifact(t *testing.T) {
tempDir := t.TempDir()
htmlPath := tempDir + "/daily_report_2026-05-13.html"
if err := os.WriteFile(htmlPath, []byte("<html><body>ok</body></html>"), 0644); err != nil {
t.Fatalf("write temp html: %v", err)
}
mux := newMux(
&sql.DB{},
func(context.Context, *sql.DB) ([]modelResponse, error) {
return nil, nil
},
func(context.Context, *sql.DB) ([]subscriptionPlanResponse, error) {
return nil, nil
},
func(context.Context, *sql.DB) (*latestReportResponse, error) {
return &latestReportResponse{
ReportDate: "2026-05-13",
Status: "generated",
MarkdownPath: tempDir + "/daily_report_2026-05-13.md",
HTMLPath: htmlPath,
}, nil
},
)
req := httptest.NewRequest(http.MethodGet, "/api/v1/reports/latest/html", nil)
rec := httptest.NewRecorder()
mux.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("expected status 200, got %d", rec.Code)
}
if body := rec.Body.String(); body != "<html><body>ok</body></html>" {
t.Fatalf("unexpected body: %q", body)
}
}