feat(anime): remember the default Anime Browser source

- Add `anime.defaultSource` with installed-source fallback
- Let the source picker save a source or All sources as default
This commit is contained in:
2026-09-02 23:17:20 -07:00
parent 516fadd530
commit 68dd789fbf
22 changed files with 260 additions and 19 deletions
@@ -52,6 +52,9 @@ export function registerAnimeBrowserIpcHandlers(deps: AnimeBrowserIpcDeps): void
handle(channels.animeBrowserSelectSource, (event, sessionId, sourceId) =>
runtime.selectSource(String(sourceId), registerSession(deps, event, sessionId)),
);
handle(channels.animeBrowserSetDefaultSource, (_event, sourceId) =>
runtime.setDefaultSource(String(sourceId)),
);
handle(channels.animeBrowserSearch, (event, sessionId, query, page) =>
runtime.search(String(query ?? ''), toPage(page), registerSession(deps, event, sessionId)),
);
@@ -0,0 +1,81 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { mkdtemp, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import path from 'node:path';
import type { AnimeBridgeClient } from '../../anime-bridge/bridge-client';
import { ALL_SOURCES_ID } from '../../types/anime-browser';
import { createAnimeBrowserRuntime } from './anime-browser-runtime';
const client = {
listAnimeSources: async () => [{ id: 'shared', name: 'Source', lang: 'en' }],
} as unknown as AnimeBridgeClient;
async function setupRuntime(defaultSource: string) {
const dir = await mkdtemp(path.join(tmpdir(), 'subminer-anime-default-source-'));
await writeFile(path.join(dir, 'pkg.one.apk'), 'one');
await writeFile(path.join(dir, 'pkg.two.apk'), 'two');
let saved = defaultSource;
const runtime = createAnimeBrowserRuntime({
extensionsDir: () => dir,
repos: () => [],
setRepos: () => undefined,
defaultSourceId: () => saved,
setDefaultSourceId: (sourceId) => {
saved = sourceId;
},
preferencesFile: path.join(dir, 'preferences.json'),
ensureBinaries: async () => ({}) as never,
checkBridgeUpdate: async () => null,
stageBridgeUpdate: async () => {
throw new Error('not under test');
},
sendMpvCommand: () => undefined,
ensureMpvConnected: async () => true,
onBridgeState: () => undefined,
log: () => undefined,
startSidecar: async () => ({
client,
baseUrl: 'http://127.0.0.1:12345',
port: 12345,
stop: async () => undefined,
onExit: () => undefined,
}),
startStreamStripProxy: async () => ({
origin: 'http://127.0.0.1:12346',
port: 12346,
close: async () => undefined,
}),
});
await runtime.ensureBridge();
return { runtime, saved: () => saved };
}
test('a new browser session opens on the configured default source', async () => {
const { runtime } = await setupRuntime('pkg.two:shared');
const snapshot = runtime.getSnapshot('fresh');
assert.equal(snapshot.selectedSourceId, 'pkg.two:shared');
assert.equal(snapshot.defaultSourceId, 'pkg.two:shared');
await runtime.dispose();
});
test('"all" as the default selects every source, and an unknown default falls back to the first', async () => {
const all = await setupRuntime(ALL_SOURCES_ID);
assert.equal(all.runtime.getSnapshot('fresh').selectedSourceId, ALL_SOURCES_ID);
await all.runtime.dispose();
const missing = await setupRuntime('pkg.gone:shared');
assert.equal(missing.runtime.getSnapshot('fresh').selectedSourceId, 'pkg.one:shared');
await missing.runtime.dispose();
});
test('setDefaultSource persists an installed source and rejects an unknown one', async () => {
const { runtime, saved } = await setupRuntime('');
runtime.setDefaultSource('pkg.two:shared');
assert.equal(saved(), 'pkg.two:shared');
assert.throws(() => runtime.setDefaultSource('pkg.gone:shared'), /no longer installed/);
assert.equal(saved(), 'pkg.two:shared');
// Sessions that already exist keep their own selection; only new ones follow the default.
assert.equal(runtime.getSnapshot('existing').selectedSourceId, 'pkg.two:shared');
await runtime.dispose();
});
@@ -79,6 +79,13 @@ export interface AnimeBrowserRuntimeDeps {
/** Streams per-source progress while a search invoke is pending. */
onSearchUpdate?: (update: AnimeBrowserSearchUpdate, sessionId: string) => void;
preferredQuality?: () => string | undefined;
/**
* Source id (or `all`) a new browser session starts on. Falls back to the
* first installed source when unset or no longer installed.
*/
defaultSourceId?: () => string | undefined;
/** Persists the default; absent when the host has no config to write to. */
setDefaultSourceId?: (sourceId: string) => void;
log: (message: string) => void;
/** Overrides process startup in focused runtime tests. */
startSidecar?: typeof startSidecar;
+31 -2
View File
@@ -92,10 +92,24 @@ export function createAnimeBrowserRuntime(deps: AnimeBrowserRuntimeDeps) {
return result;
}
/**
* The configured default when it is installed (or `all` with anything
* installed), otherwise the first source the scan found. Null with nothing
* installed.
*/
function initialSourceId(): string | null {
const configured = deps.defaultSourceId?.()?.trim();
if (configured) {
if (configured === ALL_SOURCES_ID && sources.length > 0) return ALL_SOURCES_ID;
if (sources.some((source) => source.id === configured)) return configured;
}
return sources[0]?.id ?? null;
}
function getBrowserSession(sessionId = 'default') {
const existing = browserSessions.get(sessionId);
if (existing) return existing;
const created = { selectedSourceId: sources[0]?.id ?? null, searchToken: 0 };
const created = { selectedSourceId: initialSourceId(), searchToken: 0 };
browserSessions.set(sessionId, created);
return created;
}
@@ -245,7 +259,7 @@ export function createAnimeBrowserRuntime(deps: AnimeBrowserRuntimeDeps) {
for (const session of browserSessions.values()) {
const keptAll = session.selectedSourceId === ALL_SOURCES_ID && sources.length > 0;
if (!keptAll && !sources.some((source) => source.id === session.selectedSourceId)) {
session.selectedSourceId = sources[0]?.id ?? null;
session.selectedSourceId = initialSourceId();
}
}
@@ -526,6 +540,7 @@ export function createAnimeBrowserRuntime(deps: AnimeBrowserRuntimeDeps) {
pkg: source.pkg,
})),
selectedSourceId: session.selectedSourceId,
defaultSourceId: deps.defaultSourceId?.()?.trim() || null,
loadFailures,
installed: toInstalledExtensionViews(extensions, sources, loadFailures),
extensionsDir: deps.extensionsDir(),
@@ -656,6 +671,20 @@ export function createAnimeBrowserRuntime(deps: AnimeBrowserRuntimeDeps) {
if (sources.some((source) => source.id === sourceId)) session.selectedSourceId = sourceId;
},
/**
* Remember `sourceId` as the source every future browser session opens on.
* Only an installed source (or `all`) is accepted, so a typo cannot be
* persisted from the UI; the config file itself is not validated this way.
*/
setDefaultSource(sourceId: string): void {
const valid =
(sourceId === ALL_SOURCES_ID && sources.length > 0) ||
sources.some((source) => source.id === sourceId);
if (!valid) throw new Error('That source is no longer installed. Rescan and try again.');
if (!deps.setDefaultSourceId) throw new Error('Default source cannot be saved here.');
deps.setDefaultSourceId(sourceId);
},
/**
* The extension's settings schema, merged with anything saved locally. The
* extension is the source of truth for structure; saved values only supply