Files
yellowjacket/backend/config/emit_test.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

186 lines
4.8 KiB
Go

package config
import (
"context"
"log/slog"
"path/filepath"
"testing"
"yellowjacket/backend/events"
)
// setupRecordedConfig builds a Config that saves to a temp directory
// and records the events it would push to the frontend.
func setupRecordedConfig(t *testing.T) (*Config, *events.Recorder) {
t.Helper()
conf := &Config{
logger: slog.Default(),
filePath: filepath.Join(t.TempDir(), "config.toml"),
}
conf.applyDefaults()
// Load, not just applyDefaults: Save refuses to write a config that
// was never hydrated from disk, so without this only the first
// setter in a test succeeds.
if err := conf.Load(); err != nil {
t.Fatalf("Load: %v", err)
}
rec := events.NewRecorder()
conf.SetContext(events.WithSink(context.Background(), rec))
return conf, rec
}
// payloadMap returns the map payload of the most recent named event.
func payloadMap(
t *testing.T,
rec *events.Recorder,
name string,
) map[string]any {
t.Helper()
ev, ok := rec.Last(name)
if !ok {
t.Fatalf("no %s emitted; got %v", name, rec.Names())
}
data, ok := ev.Payload().(map[string]any)
if !ok {
t.Fatalf("%s payload is %T, want map[string]any", name, ev.Payload())
}
return data
}
// TestEmit_ThemeChangeCarriesBothFields pins that the theme event is a
// snapshot of both fields, not a delta: the frontend applies the whole
// colour ramp from it, so an accent change that omitted the shade would
// re-derive the ramp against a default background.
func TestEmit_ThemeChangeCarriesBothFields(t *testing.T) {
t.Parallel()
conf, rec := setupRecordedConfig(t)
if err := conf.SetThemeBackgroundShade("light"); err != nil {
t.Fatalf("SetThemeBackgroundShade: %v", err)
}
if err := conf.SetThemeAccentColor("#ff0000"); err != nil {
t.Fatalf("SetThemeAccentColor: %v", err)
}
if got := rec.Count(events.ThemeConfigChanged); got != 2 {
t.Errorf("emitted %d ThemeConfigChanged, want 2", got)
}
data := payloadMap(t, rec, events.ThemeConfigChanged)
if data["AccentColor"] != "#ff0000" {
t.Errorf("AccentColor = %v, want #ff0000", data["AccentColor"])
}
if data["BackgroundShade"] != "light" {
t.Errorf("BackgroundShade = %v, want light", data["BackgroundShade"])
}
}
// TestEmit_ThemeChangeIsNotEmittedOnRejectedValue pins that a rejected
// write does not tell the frontend the theme changed.
func TestEmit_ThemeChangeIsNotEmittedOnRejectedValue(t *testing.T) {
t.Parallel()
conf, rec := setupRecordedConfig(t)
if err := conf.SetThemeAccentColor("not-a-colour"); err == nil {
t.Fatal("SetThemeAccentColor accepted an invalid colour")
}
if got := rec.Count(events.ThemeConfigChanged); got != 0 {
t.Errorf("emitted %d ThemeConfigChanged for a rejected write, want 0", got)
}
}
// TestEmit_ShortcutChangeSendsWholeBindingMap covers the surface the
// 357-line frontend shortcut service rebuilds itself from.
func TestEmit_ShortcutChangeSendsWholeBindingMap(t *testing.T) {
t.Parallel()
conf, rec := setupRecordedConfig(t)
if err := conf.SetShortcut("playPause", "k"); err != nil {
t.Fatalf("SetShortcut: %v", err)
}
ev, ok := rec.Last(events.ShortcutsConfigChanged)
if !ok {
t.Fatalf("no ShortcutsConfigChanged; got %v", rec.Names())
}
bindings, ok := ev.Payload().(map[string]string)
if !ok {
t.Fatalf("payload is %T, want map[string]string", ev.Payload())
}
if bindings["playPause"] != "k" {
t.Errorf("playPause = %q, want k", bindings["playPause"])
}
// The whole map, not just the changed key — the frontend replaces
// its binding table wholesale on this event.
if len(bindings) < 2 {
t.Errorf("emitted %d bindings, want the full default set", len(bindings))
}
}
func TestEmit_ResetShortcutsRepublishesDefaults(t *testing.T) {
t.Parallel()
conf, rec := setupRecordedConfig(t)
if err := conf.SetShortcut("playPause", "k"); err != nil {
t.Fatalf("SetShortcut: %v", err)
}
rec.Reset()
if err := conf.ResetShortcuts(); err != nil {
t.Fatalf("ResetShortcuts: %v", err)
}
ev, ok := rec.Last(events.ShortcutsConfigChanged)
if !ok {
t.Fatalf("no ShortcutsConfigChanged after reset; got %v", rec.Names())
}
bindings, ok := ev.Payload().(map[string]string)
if !ok {
t.Fatalf("payload is %T, want map[string]string", ev.Payload())
}
if bindings["playPause"] == "k" {
t.Error("reset emitted the overridden binding, not the default")
}
}
func TestEmit_FavoritesChangeCarriesFullConfig(t *testing.T) {
t.Parallel()
conf, rec := setupRecordedConfig(t)
if err := conf.SetFavoritesPlaylistID(7); err != nil {
t.Fatalf("SetFavoritesPlaylistID: %v", err)
}
data := payloadMap(t, rec, events.FavoritesConfigChanged)
if data["PlaylistID"] != int64(7) {
t.Errorf("PlaylistID = %#v, want int64(7)", data["PlaylistID"])
}
for _, key := range []string{"IconStyle", "PinDefault"} {
if _, ok := data[key]; !ok {
t.Errorf("payload is missing %q; the settings page reads it", key)
}
}
}