Files
yellowjacket/backend/events/recorder.go
T
logan 5ca6cad45a
Build & publish Arch package / arch-package (push) Successful in 2m8s
CI / check (push) Failing after 1m56s
CI / e2e (push) Skipped
Search index maintenance / maintain-index (push) Successful in 13s
feat(harness): agent-drivable dev harness and CI that gates
A coding agent could develop this repo's Go packages and could not
develop the application: every path to running YellowJacket ended in a
blocking GTK window, so 265 bound methods, 46 events, 33 component
directories and 13 stores had exactly one form of verification
available — `tsc --noEmit`.

The unlock is that `wails dev`'s dev server on :34115 serves the real
frontend with the real generated bindings against the same Go backend a
desktop window attaches to, so a plain Chromium under Xvfb gets a fully
functional app. Four test tiers now exist, cheapest first:

- `make ui-test` — 313 Vitest tests in a real browser in ~2 s, no app,
  no backend, no display. Works because `frontend/wailsjs/` is a pure
  passthrough to `window.go`/`window.runtime`, so faking just those two
  globals runs the real bindings and the real store code.
- `make test` — services in-process, asserting on the payload the
  frontend would receive, via a new `events.Emit` wrapper.
- `make dev-headless` + `playwright-cli` — the real app, driven
  interactively, with an event bridge on `window.__yjEvents` and a
  dev-only control surface at `/__test/`.
- `make e2e` — 19 of those flows frozen as Playwright specs.

`events.Emit(ctx, …)` replaces all 35 direct `runtime.EventsEmit` call
sites: wails' `getEvents` `log.Fatalf`s on any context without its
runtime, so those paths could not run under test and a background
worker could take the app down. Four packages had each hand-rolled the
same guard; nine more guarded on `ctx != nil`, which does not help.
`TestNoDirectRuntimeEmits` fails the build on a new one.

Fixtures are generated, not committed (`make testdata`), and seeds are
built by *running the app* — never by hand-writing config and DB rows,
which would be a second description of a valid YJ_HOME.

`.gitea/workflows/ci.yml` is the first workflow here that tests
anything; the other three only package, so `gitea_ci` reported only
packaging jobs and misled anyone asking whether a push was healthy.
Both jobs were prototyped to green in a bare ubuntu:24.04 container
before the YAML was written, which immediately caught `make lint`
linting three configurations that nothing builds: all three passes
omitted `webkit2_41`, so wails resolved webkit2gtk-4.0 — which Arch
still ships and Ubuntu 24.04 dropped.

Operational instructions live in `.pi/skills/yellowjacket-dev/`,
measured discoveries in `.planning/NOTES.md`, and architecture in
`CLAUDE.md` — split by tense, not by topic, because a topical split
gives every new fact two plausible homes. `make skill-check` fails a
commit if the skill cites a make target that does not exist.
2026-08-10 23:20:42 -04:00

154 lines
3.1 KiB
Go

package events
import (
"sync"
"time"
)
// Event is one recorded emission.
type Event struct {
Name string
Data []any
}
// Payload returns the single data argument almost every event carries,
// or nil for the handful emitted with none.
func (e Event) Payload() any {
if len(e.Data) == 0 {
return nil
}
return e.Data[0]
}
// Recorder is a Sink that buffers events for later assertion.
//
// It is safe for concurrent use: several services emit from background
// goroutines, and Wait exists so a test can block on one of those
// rather than sleep.
type Recorder struct {
mu sync.Mutex
events []Event
// notify is closed and replaced on every emit, so waiters wake
// without the Recorder having to track them individually.
notify chan struct{}
}
// NewRecorder returns an empty Recorder.
func NewRecorder() *Recorder {
return &Recorder{notify: make(chan struct{})}
}
// Emit implements Sink.
func (r *Recorder) Emit(name string, data ...any) {
r.mu.Lock()
defer r.mu.Unlock()
r.events = append(r.events, Event{Name: name, Data: data})
close(r.notify)
r.notify = make(chan struct{})
}
// Events returns every event recorded so far, in order.
func (r *Recorder) Events() []Event {
r.mu.Lock()
defer r.mu.Unlock()
return append([]Event(nil), r.events...)
}
// Named returns every recorded event with the given name, in order.
func (r *Recorder) Named(name string) []Event {
r.mu.Lock()
defer r.mu.Unlock()
var out []Event
for _, ev := range r.events {
if ev.Name == name {
out = append(out, ev)
}
}
return out
}
// Names returns the name of every recorded event, in order.
//
// Assertions read better against this than against Events when what
// matters is which events fired and in what order.
func (r *Recorder) Names() []string {
r.mu.Lock()
defer r.mu.Unlock()
out := make([]string, 0, len(r.events))
for _, ev := range r.events {
out = append(out, ev.Name)
}
return out
}
// Count returns how many times the named event was recorded.
func (r *Recorder) Count(name string) int {
return len(r.Named(name))
}
// Last returns the most recent event with the given name.
func (r *Recorder) Last(name string) (Event, bool) {
r.mu.Lock()
defer r.mu.Unlock()
for i := len(r.events) - 1; i >= 0; i-- {
if r.events[i].Name == name {
return r.events[i], true
}
}
return Event{}, false
}
// Reset discards everything recorded so far.
func (r *Recorder) Reset() {
r.mu.Lock()
defer r.mu.Unlock()
r.events = nil
}
// Wait blocks until an event with the given name is recorded, and
// returns it. It returns false if timeout elapses first.
//
// Events already recorded count, so a test cannot lose a race by
// calling Wait after the emit it is waiting for.
func (r *Recorder) Wait(name string, timeout time.Duration) (Event, bool) {
deadline := time.After(timeout)
from := 0
for {
r.mu.Lock()
for i := from; i < len(r.events); i++ {
if r.events[i].Name == name {
ev := r.events[i]
r.mu.Unlock()
return ev, true
}
}
from = len(r.events)
notify := r.notify
r.mu.Unlock()
select {
case <-notify:
case <-deadline:
return Event{}, false
}
}
}