2026-10-07 10:45:47 -07:00

80 lines
3.9 KiB
Go

package productionstats
import (
"testing"
"time"
)
func TestPeriodCountsDeduplicateAndKeepHistoricalSupplier(t *testing.T) {
r := Report{Sources: []Source{{ID: "one", ProjectID: "p", SupplierID: "b", Stage: "annotation"}}, Events: []Event{
{ID: "e1", SourceID: "one", SupplierID: "a", Metric: "submitted", At: "2026-09-27T16:00:00Z"}, {ID: "e2", SourceID: "one", SupplierID: "b", Metric: "submitted", At: "2026-09-28T00:00:00Z"}, {ID: "e3", SourceID: "one", SupplierID: "b", Metric: "submitted", At: "2026-09-28T01:00:00Z"}, {ID: "e4", SourceID: "one", SupplierID: "a", Metric: "submitted", At: "2026-09-28T16:00:00Z"}}}
q, err := ParseQuery("period", "2026-09-28", "2026-09-28", time.Now())
if err != nil {
t.Fatal(err)
}
got := Summarize(r, q)
if got.Totals["submitted"] != 1 || !got.SupplierOverlap {
t.Fatalf("overall dedup: %+v", got)
}
q.SupplierID = "a"
got = Summarize(r, q)
if got.Totals["submitted"] != 1 {
t.Fatalf("event ownership: %+v", got)
}
}
func TestHistoricalDetailsKeepEventReferences(t *testing.T) {
r := Report{Sources: []Source{{ID: "s", ProjectID: "p", SupplierID: "new", RunID: "new-run", Stage: "annotation"}}, Events: []Event{{ID: "e", SourceID: "s", SupplierID: "old", Metric: "quality", At: "2026-09-28T01:00:00Z", RunID: "old-run", WorkflowID: "w", TaskID: "old-task"}}}
q, err := ParseQuery("period", "2026-09-28", "2026-09-28", time.Now())
if err != nil {
t.Fatal(err)
}
q.SupplierID = "old"
rows := Details(r, q, "quality", "")
if len(rows) != 1 || len(rows[0].Evidence) != 1 || rows[0].Evidence[0].RunID != "old-run" || rows[0].Evidence[0].SupplierID != "old" {
t.Fatalf("historical evidence replaced by current state: %+v", rows)
}
}
func TestCompactPipelineMapsFourStagesAndSuppressesFaults(t *testing.T) {
r := Report{Sources: []Source{
{ID: "a", ProjectID: "p", SupplierID: "v", Stage: "annotation", Exception: true, ExceptionCategory: "source_pts"},
{ID: "q", ProjectID: "p", SupplierID: "v", Stage: "quality"},
{ID: "f", ProjectID: "p", SupplierID: "v", Stage: "acceptance"},
{ID: "d", ProjectID: "p", SupplierID: "v", Stage: "delivery"},
{ID: "e", ProjectID: "p", SupplierID: "v", Stage: "delivered"},
}}
got := Summarize(r, Query{View: "current", Group: "project", Compact: true})
if got.Pipeline.Annotation != 1 || got.Pipeline.Quality != 1 || got.Pipeline.Acceptance != 1 || got.Pipeline.Delivery != 2 {
t.Fatalf("four-stage current mapping: %+v", got.Pipeline)
}
if got.Totals["attention"] != 0 || len(got.StageErrors) != 0 || len(got.ExceptionCategories) != 0 {
t.Fatalf("compact response retained fault details: %+v", got)
}
if len(got.PipelineRows) != 1 || got.PipelineRows[0].Counts["delivery"] != 2 {
t.Fatalf("pipeline row mapping: %+v", got.PipelineRows)
}
}
func TestPeriodPipelineMapsMilestonesToFourStages(t *testing.T) {
r := Report{Sources: []Source{{ID: "s", ProjectID: "p", SupplierID: "v", Stage: "delivery"}}, Events: []Event{
{ID: "a", SourceID: "s", SupplierID: "v", Metric: "submitted", At: "2026-09-28T01:00:00Z"},
{ID: "q", SourceID: "s", SupplierID: "v", Metric: "quality", At: "2026-09-28T02:00:00Z"},
{ID: "f", SourceID: "s", SupplierID: "v", Metric: "acceptance", At: "2026-09-28T03:00:00Z"},
{ID: "d", SourceID: "s", SupplierID: "v", Metric: "delivered", At: "2026-09-28T04:00:00Z"},
}}
q, err := ParseQuery("period", "2026-09-28", "2026-09-28", time.Now())
if err != nil {
t.Fatal(err)
}
got := Summarize(r, q)
if got.Pipeline != (PipelineSummary{Annotation: 1, Quality: 1, Acceptance: 1, Delivery: 1}) {
t.Fatalf("four-stage period mapping: %+v", got.Pipeline)
}
if got := Details(r, q, "annotation", ""); len(got) != 1 || len(got[0].Evidence) != 1 || got[0].Evidence[0].Metric != "submitted" {
t.Fatalf("annotation detail mapping: %+v", got)
}
if got := Details(r, q, "delivery", ""); len(got) != 1 || len(got[0].Evidence) != 1 || got[0].Evidence[0].Metric != "delivered" {
t.Fatalf("delivery detail mapping: %+v", got)
}
}