Files
jellytau/src/lib/services/preload.test.ts
T
dtourolle 62873cab3d feat(search): answer search from a local index; tier downloads by lifetime
Search's instant leg read only downloaded items, so with no downloads it
returned nothing and every keystroke fell through to a full Recursive=true
server query. It now reads the whole synced catalog through the same
availability CTE get_items uses, gated on the same include_catalog_browse
flag so search and browse cannot diverge. (UR-065, DR-108)

Also fixes three defects found while confirming that:

- items_fts grew by a full duplicate index every catalog pass. INSERT OR
  REPLACE fires no AFTER DELETE trigger without recursive_triggers, so the
  old index row was orphaned, and a TEXT PRIMARY KEY meant the replacement
  took a fresh rowid and inserted a second entry. Now a real upsert, with
  migration 021 rebuilding existing indexes. (DR-110)
- DELETE FROM items existed nowhere, so server-side deletions never
  propagated. Adds a post-crawl mark-and-sweep, scoped to crawled types,
  skipping downloaded items, and refusing to run after a partial crawl
  because items.parent_id cascades. (DR-110)
- The index omitted MusicArtist, Playlist and People, which search groups
  results by. Adds them plus people_fts (migration 022). (DR-111)

Re-indexing moves from a frontend startup call to a Rust background task
with a 6h TTL, so a long session no longer searches a stale catalog and a
restart no longer forces a crawl regardless of freshness. (DR-109, IR-030)

Downloads gain a lifetime tier. Eviction selected every completed row by
age with no download_source filter, so hitting the storage limit deleted
the oldest download -- typically one saved deliberately for offline -- to
make room for a precached track. It now reclaims only 'auto' rows, and
expired ones are reclaimed first, before live cache is evicted.
(DR-126, DR-127)

Downloaded video and audio-only handoffs now play from disk instead of
streaming; the video path had never consulted downloads at all. No
transcode is involved: MPV runs video=no and ExoPlayer has no surface for
an Audio item. (DR-123 in part, DR-128)

FTS queries are built as quoted phrases so apostrophes, hyphens and
slashes are data rather than operator syntax, and the item-type filter is
bound rather than interpolated.

Specs: docs/specs/catalog-index-search.md,
docs/specs/read-through-media-cache.md

Includes concurrently-developed favourites browsing and background-audio
stream-end handling; the two workstreams share offline.rs, lib.rs and
online.rs, so no subset of files builds independently.
2026-08-04 17:35:17 +02:00

224 lines
6.8 KiB
TypeScript

/**
* Preload service tests
*
* TRACES: UR-004, UR-011 | DR-006, DR-015
*/
import { describe, it, expect, beforeEach, vi } from "vitest";
import { preloadUpcomingTracks, updateCacheConfig, getCacheConfig } from "./preload";
import type { CacheConfig } from "$lib/api/bindings";
// updateCacheConfig now takes a full CacheConfig (matches the backend command)
function makeConfig(overrides: Partial<CacheConfig> = {}): CacheConfig {
return {
queuePrecacheEnabled: true,
queuePrecacheCount: 5,
albumAffinityEnabled: false,
albumAffinityThreshold: 0.75,
storageLimit: 2 * 1024 * 1024 * 1024,
wifiOnly: false,
temporaryTtlHours: 24 * 7,
...overrides,
};
}
vi.mock("@tauri-apps/api/core", () => ({
invoke: vi.fn(async (command: string, args?: any) => {
if (command === "player_preload_upcoming") {
return {
queuedCount: 3,
alreadyDownloaded: 2,
skipped: 1,
};
}
if (command === "player_set_cache_config") {
return undefined;
}
if (command === "player_get_cache_config") {
return {
queuePrecacheEnabled: true,
queuePrecacheCount: 5,
albumAffinityEnabled: true,
albumAffinityThreshold: 0.8,
storageLimit: 1024 * 1024 * 1024,
wifiOnly: false,
};
}
return null;
}),
}));
vi.mock("$lib/stores/auth", () => ({
auth: {
getUserId: vi.fn(() => "user-123"),
},
}));
describe("preload service", () => {
beforeEach(() => {
vi.clearAllMocks();
});
describe("preloadUpcomingTracks", () => {
it("should preload tracks without options", async () => {
await expect(preloadUpcomingTracks()).resolves.toBeUndefined();
});
it("should invoke correct backend command", async () => {
const { invoke } = await import("@tauri-apps/api/core");
const invokeSpy = vi.mocked(invoke);
await preloadUpcomingTracks();
const call = invokeSpy.mock.calls.find(
(c) => c[0] === "player_preload_upcoming"
);
expect(call).toBeDefined();
});
it("should include userId in command", async () => {
const { invoke } = await import("@tauri-apps/api/core");
const invokeSpy = vi.mocked(invoke);
await preloadUpcomingTracks();
const call = invokeSpy.mock.calls.find(
(c) => c[0] === "player_preload_upcoming"
);
expect(call![1]).toHaveProperty("userId", "user-123");
});
it("should use override userId if provided", async () => {
const { invoke } = await import("@tauri-apps/api/core");
const invokeSpy = vi.mocked(invoke);
await preloadUpcomingTracks({ userId: "user-456" });
const call = invokeSpy.mock.calls.find(
(c) => c[0] === "player_preload_upcoming"
);
expect(call![1]).toHaveProperty("userId", "user-456");
});
it("should skip if no active user", async () => {
const { auth } = await import("$lib/stores/auth");
const authModule = vi.mocked(auth);
authModule.getUserId = vi.fn(() => null);
const { invoke } = await import("@tauri-apps/api/core");
const invokeSpy = vi.mocked(invoke);
await preloadUpcomingTracks();
const call = invokeSpy.mock.calls.find(
(c) => c[0] === "player_preload_upcoming"
);
expect(call).toBeUndefined();
});
it("should handle preload result", async () => {
// Should not throw even with result
await expect(preloadUpcomingTracks()).resolves.toBeUndefined();
});
it("should handle errors gracefully", async () => {
const { invoke } = await import("@tauri-apps/api/core");
const invokeSpy = vi.mocked(invoke);
invokeSpy.mockRejectedValueOnce(new Error("Backend error"));
// Should not throw
await expect(preloadUpcomingTracks()).resolves.toBeUndefined();
});
it("should support debug option", async () => {
await expect(preloadUpcomingTracks({ debug: true })).resolves.toBeUndefined();
});
it("should support both debug and userId options", async () => {
const { invoke } = await import("@tauri-apps/api/core");
const invokeSpy = vi.mocked(invoke);
await preloadUpcomingTracks({ debug: true, userId: "user-789" });
const call = invokeSpy.mock.calls.find(
(c) => c[0] === "player_preload_upcoming"
);
expect(call![1]).toHaveProperty("userId", "user-789");
});
});
describe("updateCacheConfig", () => {
it("should update cache config", async () => {
const config = makeConfig({
queuePrecacheEnabled: false,
queuePrecacheCount: 10,
});
await expect(updateCacheConfig(config)).resolves.toBeUndefined();
});
it("should invoke correct backend command", async () => {
const { invoke } = await import("@tauri-apps/api/core");
const invokeSpy = vi.mocked(invoke);
const config = makeConfig({ queuePrecacheEnabled: true });
await updateCacheConfig(config);
const call = invokeSpy.mock.calls.find(
(c) => c[0] === "player_set_cache_config"
);
expect(call).toBeDefined();
expect(call![1]).toHaveProperty("config", config);
});
it("should support overriding individual config options", async () => {
const config = makeConfig({ wifiOnly: true });
await expect(updateCacheConfig(config)).resolves.toBeUndefined();
});
it("should support all config options", async () => {
const config = makeConfig({ wifiOnly: true, albumAffinityEnabled: false });
await expect(updateCacheConfig(config)).resolves.toBeUndefined();
});
});
describe("getCacheConfig", () => {
it("should get cache config", async () => {
const config = await getCacheConfig();
expect(config).toBeDefined();
expect(typeof config.queuePrecacheEnabled).toBe("boolean");
expect(typeof config.queuePrecacheCount).toBe("number");
expect(typeof config.albumAffinityEnabled).toBe("boolean");
expect(typeof config.albumAffinityThreshold).toBe("number");
expect(typeof config.storageLimit).toBe("number");
expect(typeof config.wifiOnly).toBe("boolean");
});
it("should invoke correct backend command", async () => {
const { invoke } = await import("@tauri-apps/api/core");
const invokeSpy = vi.mocked(invoke);
await getCacheConfig();
const call = invokeSpy.mock.calls.find(
(c) => c[0] === "player_get_cache_config"
);
expect(call).toBeDefined();
});
it("should return valid config structure", async () => {
const config = await getCacheConfig();
expect(config.queuePrecacheEnabled).toBe(true);
expect(config.queuePrecacheCount).toBe(5);
expect(config.albumAffinityEnabled).toBe(true);
expect(config.albumAffinityThreshold).toBe(0.8);
expect(config.storageLimit).toBe(1024 * 1024 * 1024);
expect(config.wifiOnly).toBe(false);
});
});
});