Files
yellowjacket/backend/config/emit_test.go
T
yonluandClaude Opus 5 4471db3aef feat(wails): move the Go side to v3
Phases 2 and 3 of plan 009, plus the parts of phase 1 that could not
land before them. Nothing in the tree imports wails/v2 any more; all
three lint and test configurations are green and `go build .` produces
a running binary.

The point of the migration is one file. backend/events/emit.go probed
ctx.Value("events") — a v2-*private* context key — to decide whether
emitting was safe, because runtime.EventsEmit called log.Fatalf on a
context without the runtime and took the process down with it. v3's
emit takes no context, so that is now application.Get() == nil. D1
held: events.Emit keeps its ctx as the WithSink test seam, and all 45
call sites and 7 test files are untouched.

The bootstrap splits into application.New + Window.NewWithOptions +
Run. Ten bound services implement ServiceStartup instead of being
handed a context by hand from OnStartup, which also stops ten
SetContext methods being exported as bindings. jobs.Registry and
explore.SearchIndex keep theirs — neither is bound, so converting them
would be churn for no binding removed.

Four things differed from the plan and are written up in it: GPU policy
moved to the per-window LinuxWindow options rather than surviving on
LinuxOptions; there is no OnStartup/OnDomReady option, so app-level
wiring hangs off ApplicationStarted; application.NewService is generic,
so FEBindings []any could not survive (the binding generator is a
static analyser and would have seen nothing); and the quit veto had to
be restructured, because v3's dialog answers on a callback rather than
returning the button, so ShouldQuit vetoes, asks, and quits again from
the callback.

Window state saving moves to a WindowClosing hook — the size has to be
read while the window still exists, and v3's OnShutdown has neither
context nor window. backend/logging is deleted rather than ported:
v3 takes a *slog.Logger directly, so the v2 logger.Logger adapter had
no caller left.

Phase 1's tail rides along, now that it can: the Makefile's wails
invocations, all 50 webkit2_41 sites, lefthook, both packaging recipes
and ci.yml's apt lists. v3 builds against GTK4 + WebKitGTK 6.0, which
Arch and ubuntu:24.04 both ship, so the tag is a deletion rather than
a translation.

Phase 4 is next and the branch is not usable until it lands: the app
builds, but frontend/wailsjs/ is v2's tree and nothing regenerates it,
so the frontend cannot reach the backend yet.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UDCbcCZQepnpSQYJ6SxxZm
2026-08-14 14:01:02 -04:00

191 lines
4.9 KiB
Go

package config
import (
"context"
"log/slog"
"path/filepath"
"testing"
"github.com/wailsapp/wails/v3/pkg/application"
"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.ServiceStartup(
events.WithSink(context.Background(), rec),
application.ServiceOptions{},
)
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)
}
}
}