Files
yellowjacket/frontend/test/stores/favorites-store.test.ts
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

195 lines
5.6 KiB
TypeScript

/**
* Favourites is the one store that updates optimistically: the heart
* fills before Go has agreed. So the interesting cases are the reverts,
* and the set of playlist events that force a reload — a default
* playlist edited elsewhere has to show up here.
*/
import { describe, expect, it, beforeEach } from 'vitest';
import { favoritesStore } from '@store/favorites-store';
import { Events } from '../../src/events';
import {
emit,
calls,
stub,
stubFailure,
flush,
lastArgs,
resetHarness,
} from '@test/support/harness';
const PATHS = ['/music/a.mp3', '/music/b.mp3'];
function stubReads(paths: string[] = PATHS): void {
stub('playlist.Service.GetDefaultPlaylistTrackPaths', paths);
stub('playlist.Service.GetDefaultPlaylistInfo', { Name: 'Loved' });
stub('config.Config.GetFavoritesPlaylistID', 3);
stub('config.Config.GetFavoritesIconStyle', 'star');
stub('config.Config.GetPinDefaultPlaylist', false);
}
/** Push a config change and let the reloads it triggers settle. */
async function reload(paths: string[] = PATHS): Promise<void> {
stubReads(paths);
emit(Events.FavoritesConfigChanged, {
PlaylistID: 3,
IconStyle: 'star',
PinDefault: false,
});
await flush();
resetHarness();
stubReads(paths);
}
describe('favorites store: cached membership', () => {
beforeEach(async () => {
await reload();
});
it('reports membership by file path', () => {
expect([
favoritesStore.isFavorited('/music/a.mp3'),
favoritesStore.isFavorited('/music/z.mp3'),
]).toEqual([true, false]);
});
it('requires every path for a multi-selection to count as favourited', () => {
expect([
favoritesStore.allFavorited(PATHS),
favoritesStore.allFavorited([...PATHS, '/music/z.mp3']),
]).toEqual([true, false]);
});
it('treats an empty selection as not favourited, so the button is not lit for nothing', () => {
expect(favoritesStore.allFavorited([])).toBe(false);
});
it('adopts the config the backend pushed', () => {
expect([
favoritesStore.getPlaylistId(),
favoritesStore.getIconStyle(),
favoritesStore.getPinDefault(),
]).toEqual([3, 'star', false]);
});
it('resolves the playlist name from the backend', () => {
expect(favoritesStore.getPlaylistName()).toBe('Loved');
});
});
describe('favorites store: optimistic writes', () => {
beforeEach(async () => {
await reload();
});
it('fills the heart before the backend answers', () => {
void favoritesStore.toggleFavorite('/music/z.mp3');
expect(favoritesStore.isFavorited('/music/z.mp3')).toBe(true);
});
it('reverts an add the backend rejected', async () => {
stubFailure('playlist.Service.ToggleDefaultPlaylistTrack');
await favoritesStore.toggleFavorite('/music/z.mp3');
expect(favoritesStore.isFavorited('/music/z.mp3')).toBe(false);
});
it('reverts a removal the backend rejected', async () => {
stubFailure('playlist.Service.ToggleDefaultPlaylistTrack');
await favoritesStore.toggleFavorite('/music/a.mp3');
expect(favoritesStore.isFavorited('/music/a.mp3')).toBe(true);
});
it('adds a batch optimistically and forwards the whole list', async () => {
await favoritesStore.addToFavorites(['/music/y.mp3', '/music/z.mp3']);
expect([
favoritesStore.allFavorited(['/music/y.mp3', '/music/z.mp3']),
lastArgs('playlist.Service.AddToDefaultPlaylist'),
]).toEqual([true, [['/music/y.mp3', '/music/z.mp3']]]);
});
it('removes a batch optimistically', async () => {
await favoritesStore.removeFromFavorites(['/music/a.mp3']);
expect(favoritesStore.isFavorited('/music/a.mp3')).toBe(false);
});
it('resyncs from the backend when a batch write fails, rather than guessing', async () => {
stubFailure('playlist.Service.AddToDefaultPlaylist');
await favoritesStore.addToFavorites(['/music/z.mp3']);
await flush();
expect(
calls('playlist.Service.GetDefaultPlaylistTrackPaths'),
).toHaveLength(1);
});
});
describe('favorites store: reacting to playlist changes', () => {
beforeEach(async () => {
await reload();
});
it('reloads when the default playlist itself changed', async () => {
emit(Events.PlaylistTracksChanged, 3);
await flush();
expect(
calls('playlist.Service.GetDefaultPlaylistTrackPaths'),
).toHaveLength(1);
});
it('ignores changes to some other playlist', async () => {
emit(Events.PlaylistTracksChanged, 99);
await flush();
expect(
calls('playlist.Service.GetDefaultPlaylistTrackPaths'),
).toHaveLength(0);
});
it('reloads after a restore, which rewrites every playlist', async () => {
emit(Events.PlaylistsRestored);
await flush();
expect(
calls('playlist.Service.GetDefaultPlaylistTrackPaths'),
).toHaveLength(1);
});
it('re-reads the name when a playlist is renamed', async () => {
stub('playlist.Service.GetDefaultPlaylistInfo', { Name: 'Renamed' });
emit(Events.PlaylistRenamed, 3);
await flush();
expect(favoritesStore.getPlaylistName()).toBe('Renamed');
});
it('falls back to "Favorites" when no default playlist is configured', async () => {
await favoritesStore.setDefaultPlaylist(0);
expect(favoritesStore.getPlaylistName()).toBe('Favorites');
});
it('persists a changed icon style', async () => {
await favoritesStore.setIconStyle('heart');
expect([
favoritesStore.getIconStyle(),
lastArgs('config.Config.SetFavoritesIconStyle'),
]).toEqual(['heart', ['heart']]);
});
it('persists the pin setting', async () => {
await favoritesStore.setPinDefault(true);
expect(lastArgs('config.Config.SetPinDefaultPlaylist')).toEqual([true]);
});
});