Files
jellytau/src/lib/services/preload.test.ts
T
dtourolle d01c2aab9f
🏗️ Build and Test JellyTau / Run Tests (pull_request) Successful in 4m39s
Traceability Validation / Check Requirement Traces (pull_request) Failing after 36s
🏗️ Build and Test JellyTau / Build Android APK (pull_request) Failing after 1m57s
Migrate all IPC call sites to typed tauri-specta commands.*
Replace the remaining ~155 untyped invoke() calls across stores, services,
components, and routes with the generated commands.* wrappers from
$lib/api/bindings, so every IPC call is compile-time-checked against the
command signatures.

- Register repository_get_subtitle_url and repository_get_video_download_url
  in specta_builder() and the invoke_handler; regenerate bindings.ts.
- Source duplicated wire types (AutoplaySettings, CacheConfig, Session,
  ConnectivityStatus, audio/video settings, etc.) from bindings.
- Fix two bugs surfaced by the typed wrappers:
  - VideoDownloadButton passed an un-awaited Promise as the stream URL.
  - setAutoplaySettings omitted the required userId argument.
- Update unit tests asserting the old invoke(name, args) shape.
- Remove the five param-naming guard tests; the compiler and codegen now
  enforce what they checked.

svelte-check: 0 errors. vitest: green. cargo test --lib: green.
2026-06-21 08:47:04 +02:00

230 lines
6.9 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,
...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 = {
queuePrecacheEnabled: true,
queuePrecacheCount: 5,
albumAffinityEnabled: false,
albumAffinityThreshold: 0.75,
storageLimit: 2 * 1024 * 1024 * 1024,
wifiOnly: true,
};
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);
});
});
});