Files
archie/internal/events/events_test.go
T
sam 387c033b29 feat: observability — event bus, SQLite event log, live web dashboard
Adapted from tau's eventbus/metrics designs (per Sam), slimmed for a
small daemon:

- internal/events: single-type bus with mutex fan-out and bounded
  per-subscriber buffers that DROP on overflow (counted) — a stalled
  dashboard can never backpressure the task engine (tau's blocking
  delivery deliberately not copied)
- store: events table + queries powering the dashboards: task
  timelines, per-workflow outcomes/spend, stage durations and error
  counts (from stage_finish events), token spend by day
- emit points: task_queued, stage_start/finish(duration, error),
  agent_finish(status, tokens, steps, model), parked, outcome,
  pr_merged/pr_rejected
- internal/webui: read-only dashboard (embedded single page, no build
  step) — status cards, task board with click-through timelines, live
  SSE event tail with catch-up via ?since=<event id>, workflow/stage
  metrics tables. [web] listen config, default 127.0.0.1:8484, no auth
  by design (bind localhost/tailnet); "off" disables
- sink pipeline: bus -> SQLite insert (stamps row id) -> SSE broadcast

Verified live: dashboard serves real workflow stats from the existing
task history; bus overflow and event-log round-trips unit-tested.
2026-07-21 04:20:45 +10:00

47 lines
1.0 KiB
Go

package events
import "testing"
func TestBusFanOutAndDrop(t *testing.T) {
b := NewBus()
defer b.Close()
fast := b.Subscribe(8)
slow := b.Subscribe(2) // will overflow
for i := range 5 {
b.Publish(Event{Kind: KindLog, Issue: i})
}
if got := len(fast.C); got != 5 {
t.Fatalf("fast subscriber buffered %d, want 5", got)
}
if got := len(slow.C); got != 2 {
t.Fatalf("slow subscriber buffered %d, want 2", got)
}
if d := slow.Dropped(); d != 3 {
t.Fatalf("slow.Dropped() = %d, want 3", d)
}
e := <-fast.C
if e.At.IsZero() {
t.Fatal("Publish must stamp At")
}
fast.Close()
b.Publish(Event{Kind: KindLog}) // must not panic on closed subscriber
if got := len(slow.C); got != 2 {
t.Fatalf("closed subscriber removal broke fan-out: slow has %d", got)
}
}
func TestBusCloseStopsDelivery(t *testing.T) {
b := NewBus()
s := b.Subscribe(4)
b.Close()
b.Publish(Event{Kind: KindLog}) // no-op, no panic
if _, open := <-s.C; open {
t.Fatal("subscriber channel must be closed after bus Close")
}
}