Add quota receipt aggregation and approved standup advisories

This commit is contained in:
kami
2026-07-26 20:39:21 +04:00
parent 3dfe97f4fd
commit 3e143cb703
5 changed files with 141 additions and 13 deletions
+43
View File
@@ -3,6 +3,7 @@ package operations
import (
"encoding/json"
"orchestra/internal/domain"
"orchestra/internal/store"
"testing"
"time"
)
@@ -16,3 +17,45 @@ func TestBuildBrief(t *testing.T) {
t.Fatalf("unexpected brief: %+v", b)
}
}
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}
}
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}); 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}
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}); 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)
}
}