package operations import ( "encoding/json" "orchestra/internal/authz" "orchestra/internal/domain" "orchestra/internal/store" "testing" "time" ) func TestBuildBrief(t *testing.T) { now := time.Now() p, _ := json.Marshal(map[string]any{"harness_id": "cc", "consumed": 12}) completed, _ := json.Marshal(map[string]any{"report_ref": "sha256:abc", "receipt": map[string]any{"numerator": 42.0}}) es := []domain.Event{{TaskID: "t1", Type: "TaskCompleted", At: now, Payload: completed}, {Type: "TaskFailed", At: now}, {Type: "ApprovalRequested", At: now}, {Type: "QuotaReported", At: now, Payload: p}} b := BuildBrief(es, now.Add(-time.Minute), now.Add(time.Minute), map[string]GitSync{"proj": {Status: "clean"}}) if b.Completed != 1 || b.Failed != 1 || len(b.NeedsAttention) != 2 || b.Quota["cc"] != 12 { t.Fatalf("unexpected brief: %+v", b) } if len(b.Receipts) != 1 || b.Receipts[0].TaskID != "t1" || b.Receipts[0].ReportRef != "sha256:abc" || b.Receipts[0].Receipt["numerator"] != 42.0 { t.Fatalf("unexpected receipts: %+v", b.Receipts) } if b.Git["proj"].Status != "clean" { t.Fatalf("unexpected git: %+v", b.Git) } } func TestAggregateQuotaSumsRotationsAndWindows(t *testing.T) { now := time.Now().UTC() enc := func(at time.Time, n float64) domain.Event { p, _ := json.Marshal(map[string]any{"harness_id": "codex", "consumed": n}) return domain.Event{Type: "QuotaReported", At: at, Payload: p, Surface: string(authz.System)} } es := []domain.Event{enc(now.Add(-2*time.Hour), 4), enc(now.Add(-time.Hour), 6), enc(now.Add(-48*time.Hour), 100)} got := AggregateQuota(es, now.Add(-3*time.Hour), now) if got["codex"] != 10 { t.Fatalf("quota=%v, want 10", got) } } func TestApplyAdvisoryRequiresApproval(t *testing.T) { s, err := store.Open(t.TempDir()) if err != nil { t.Fatal(err) } p, _ := json.Marshal(map[string]any{"source": "test", "external_id": "1", "project": "p", "title": "old"}) if err := s.Append(domain.Event{ID: "task", TaskID: "task", Type: "TaskCreated", Version: 1, Payload: p, Surface: string(authz.System)}); err != nil { t.Fatal(err) } ap, _ := json.Marshal(map[string]any{"items": []StandupItem{{TaskID: "task", Title: "new"}}}) adv := domain.Event{ID: "adv", TaskID: "system", Type: "StandupAdvisory", Payload: ap, Surface: string(authz.System)} if err := s.Append(adv); err != nil { t.Fatal(err) } if _, err := ApplyAdvisory(s, "adv"); err == nil { t.Fatal("unapproved advisory applied") } grant, _ := json.Marshal(map[string]any{"subject_ref": "adv"}) if err := s.Append(domain.Event{ID: "grant", TaskID: "system", Type: "ApprovalGranted", Payload: grant, Surface: string(authz.System)}); err != nil { t.Fatal(err) } if _, err := ApplyAdvisory(s, "adv"); err != nil { t.Fatal(err) } if task, _ := s.Task("task"); task.Title != "new" { t.Fatalf("title=%q", task.Title) } }