Files
ersatztv/web/src/api/settings.test.ts
T
timothyandClaude Opus 4.8 ef2bd65c27
Build ErsatzTV Image / decisions.md append-only (pull_request) Successful in 10s
Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 10s
Build ErsatzTV Image / Formatting (changed .cs conform to .editorconfig) (pull_request) Successful in 1m12s
Build ErsatzTV Image / API docs in sync (OpenAPI + endpoint index) (pull_request) Successful in 3m4s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 8m17s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 10m36s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
feat(api): #286 — mount the whole /api surface at /api/v1
Version every /api route to /api/v1 (251 controller routes + ~24 Location
headers + the scanner callback URL + the Startup request-log literal),
uniform across the machine API, auth, scanner and scripted-build surfaces.

Add ApiVersionRewriteMiddleware: a legacy unversioned /api/* request is
rewritten (NOT redirected) to /api/v1/* in-pipeline — method, body, auth
headers and query survive — carrying RFC 8594 Deprecation/Sunset headers,
so curl / the future MCP server / bookmarks keep working. An already-
versioned path passes through; a future /api/v2 is never forced to v1.

Standardize the route convention (leading-slash absolute route per method,
no class-[Route] — except the two Scanner/Scripted controllers whose ~all
actions share a parametrized {id} prefix), enforced by ApiRouteVersioningTests
(^/api/v\d+/ over the whole Controllers.Api surface; browser-nav
/auth/oidc/login is out of scope).

Regenerate v1.json (160 paths, all /api/v1)/endpoint-index/v1.d.ts; sweep 945
SPA request literals + the test mocks (regex + positional URL parsers). /api/v1
is additive-only after freeze; the legacy-rewrite shim sunsets in ~2 releases
(owner decision) with removal tracked as a Phase-3 follow-up.

Docs: decisions.md 2026-07-13, api-conventions §1/§9, rest-api/spa-conventions/
blazor-route-parity/e2e-local/domain-model.

fixes #286
refs #197

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-13 00:30:20 +02:00

189 lines
7.5 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest';
import {
createResolution,
deleteResolution,
getFfmpegSettings,
getHdhrSettings,
getLoggingSettings,
getPlayoutSettings,
getResolutions,
getScannerSettings,
getUiSettings,
getXmltvSettings,
messageFromSettingsError,
updateFfmpegSettings,
updateHdhrSettings,
updateLoggingSettings,
updatePlayoutSettings,
updateScannerSettings,
updateUiSettings,
updateXmltvSettings
} from './settings';
function jsonResponse(body: unknown, status = 200): Response {
return new Response(JSON.stringify(body), {
headers: { 'Content-Type': 'application/json' },
status
});
}
function lastFetchCall(): [RequestInfo | URL, RequestInit | undefined] {
const calls = vi.mocked(window.fetch).mock.calls;
return calls[calls.length - 1] as [RequestInfo | URL, RequestInit | undefined];
}
describe('settings API module', () => {
beforeEach(() => {
window.localStorage.clear();
vi.restoreAllMocks();
});
it('fetches ffmpeg settings from GET /api/v1/settings/ffmpeg', async () => {
const settings = { fFmpegPath: '/usr/bin/ffmpeg' };
vi.spyOn(window, 'fetch').mockImplementation(() => Promise.resolve(jsonResponse(settings)));
await expect(getFfmpegSettings()).resolves.toMatchObject(settings);
const [path] = lastFetchCall();
expect(path.toString()).toBe('/api/v1/settings/ffmpeg');
});
it('PUTs ffmpeg settings to /api/v1/settings/ffmpeg', async () => {
const body = { fFmpegPath: '/usr/bin/ffmpeg', hlsDirectOutputFormat: 'MpegTs' as const };
vi.spyOn(window, 'fetch').mockResolvedValue(jsonResponse(body));
await updateFfmpegSettings(body as never);
const [path, init] = lastFetchCall();
expect(path.toString()).toBe('/api/v1/settings/ffmpeg');
expect(init?.method).toBe('PUT');
expect(JSON.parse(init?.body as string)).toMatchObject({ fFmpegPath: '/usr/bin/ffmpeg' });
});
it('fetches and updates playout settings', async () => {
const settings = { daysToBuild: 2, scriptedScheduleTimeoutSeconds: 30, skipMissingItems: true };
vi.spyOn(window, 'fetch').mockImplementation(() => Promise.resolve(jsonResponse(settings)));
await expect(getPlayoutSettings()).resolves.toMatchObject(settings);
expect(lastFetchCall()[0].toString()).toBe('/api/v1/settings/playout');
await updatePlayoutSettings(settings);
const [path, init] = lastFetchCall();
expect(path.toString()).toBe('/api/v1/settings/playout');
expect(init?.method).toBe('PUT');
});
it('fetches and updates xmltv settings', async () => {
const settings = { blockBehavior: 'SplitTimeEvenly' as const, daysToBuild: 2, timeZone: 'Local' as const };
vi.spyOn(window, 'fetch').mockImplementation(() => Promise.resolve(jsonResponse(settings)));
await expect(getXmltvSettings()).resolves.toMatchObject(settings);
expect(lastFetchCall()[0].toString()).toBe('/api/v1/settings/xmltv');
await updateXmltvSettings(settings);
const [path, init] = lastFetchCall();
expect(path.toString()).toBe('/api/v1/settings/xmltv');
expect(init?.method).toBe('PUT');
});
it('fetches and updates scanner settings', async () => {
const settings = { libraryRefreshInterval: 6 };
vi.spyOn(window, 'fetch').mockImplementation(() => Promise.resolve(jsonResponse(settings)));
await expect(getScannerSettings()).resolves.toMatchObject(settings);
expect(lastFetchCall()[0].toString()).toBe('/api/v1/settings/scanner');
await updateScannerSettings(settings);
const [path, init] = lastFetchCall();
expect(path.toString()).toBe('/api/v1/settings/scanner');
expect(init?.method).toBe('PUT');
});
it('fetches and updates logging settings', async () => {
const settings = {
defaultMinimumLogLevel: 'Information' as const,
httpMinimumLogLevel: 'Warning' as const,
scanningMinimumLogLevel: 'Information' as const,
schedulingMinimumLogLevel: 'Information' as const,
searchingMinimumLogLevel: 'Information' as const,
streamingMinimumLogLevel: 'Information' as const
};
vi.spyOn(window, 'fetch').mockImplementation(() => Promise.resolve(jsonResponse(settings)));
await expect(getLoggingSettings()).resolves.toMatchObject(settings);
expect(lastFetchCall()[0].toString()).toBe('/api/v1/settings/logging');
await updateLoggingSettings(settings);
const [path, init] = lastFetchCall();
expect(path.toString()).toBe('/api/v1/settings/logging');
expect(init?.method).toBe('PUT');
});
it('fetches and updates UI settings', async () => {
const settings = { isDarkMode: true, language: 'en-US' };
vi.spyOn(window, 'fetch').mockImplementation(() => Promise.resolve(jsonResponse(settings)));
await expect(getUiSettings()).resolves.toMatchObject(settings);
expect(lastFetchCall()[0].toString()).toBe('/api/v1/settings/ui');
await updateUiSettings(settings);
const [path, init] = lastFetchCall();
expect(path.toString()).toBe('/api/v1/settings/ui');
expect(init?.method).toBe('PUT');
});
it('fetches and updates HDHR settings', async () => {
const settings = { tunerCount: 2, uuid: 'abc-123' };
vi.spyOn(window, 'fetch').mockImplementation(() => Promise.resolve(jsonResponse(settings)));
await expect(getHdhrSettings()).resolves.toMatchObject(settings);
expect(lastFetchCall()[0].toString()).toBe('/api/v1/settings/hdhr');
await updateHdhrSettings({ tunerCount: 2 });
const [path, init] = lastFetchCall();
expect(path.toString()).toBe('/api/v1/settings/hdhr');
expect(init?.method).toBe('PUT');
expect(JSON.parse(init?.body as string)).toMatchObject({ tunerCount: 2 });
});
it('fetches resolutions from GET /api/v1/settings/resolutions', async () => {
const resolutions = [{ height: 1080, id: 1, isCustom: false, name: '1920x1080', width: 1920 }];
vi.spyOn(window, 'fetch').mockResolvedValue(jsonResponse(resolutions));
await expect(getResolutions()).resolves.toMatchObject(resolutions);
expect(lastFetchCall()[0].toString()).toBe('/api/v1/settings/resolutions');
});
it('creates a custom resolution via POST /api/v1/settings/resolutions', async () => {
const created = { height: 1080, id: 3, isCustom: true, name: '2560x1080', width: 2560 };
vi.spyOn(window, 'fetch').mockResolvedValue(jsonResponse(created, 201));
await expect(createResolution({ height: 1080, width: 2560 })).resolves.toMatchObject(created);
const [path, init] = lastFetchCall();
expect(path.toString()).toBe('/api/v1/settings/resolutions');
expect(init?.method).toBe('POST');
expect(JSON.parse(init?.body as string)).toMatchObject({ height: 1080, width: 2560 });
});
it('deletes a custom resolution via DELETE /api/v1/settings/resolutions/{id}', async () => {
vi.spyOn(window, 'fetch').mockResolvedValue(new Response(null, { status: 204 }));
await deleteResolution(3);
const [path, init] = lastFetchCall();
expect(path.toString()).toBe('/api/v1/settings/resolutions/3');
expect(init?.method).toBe('DELETE');
});
describe('messageFromSettingsError', () => {
it('prefers the ApiError detail over the fallback message', async () => {
vi.spyOn(window, 'fetch').mockResolvedValue(jsonResponse({ detail: 'ffmpeg path does not exist', title: 'Unprocessable' }, 422));
await expect(getFfmpegSettings()).rejects.toMatchObject({ detail: 'ffmpeg path does not exist' });
});
it('falls back to a default message for unknown errors', () => {
expect(messageFromSettingsError('boom', 'Unable to load settings')).toBe('Unable to load settings');
});
});
});