Files
ersatztv/web/src/api/playouts.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

238 lines
7.8 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest';
import {
createPlayout,
deletePlayout,
erasePlayoutItems,
erasePlayoutItemsAndHistory,
getAlternateSchedules,
getPlayoutItemSchedulingContext,
getPlayoutTemplates,
replaceAlternateSchedules,
replacePlayoutTemplates,
resetChannelPlayout,
updatePlayoutDefaultDeco,
updatePlayoutDetails,
type CreatePlayoutRequest
} from './playouts';
function jsonResponse(body: unknown, status = 200): Response {
return new Response(JSON.stringify(body), {
headers: { 'Content-Type': 'application/json' },
status
});
}
function noContentResponse(): Response {
return new Response(null, { status: 204 });
}
describe('playouts api client', () => {
beforeEach(() => {
window.localStorage.clear();
vi.restoreAllMocks();
});
it('createPlayout POSTs a classic playout request', async () => {
const fetchMock = vi.spyOn(window, 'fetch').mockResolvedValue(jsonResponse({ id: 7 }, 201));
const body: CreatePlayoutRequest = {
channelId: 1,
programScheduleId: 2,
scheduleFile: null,
scheduleKind: 'Classic'
};
await expect(createPlayout(body)).resolves.toMatchObject({ id: 7 });
const [url, init] = fetchMock.mock.calls[0];
expect(url).toBe('/api/v1/playouts');
expect(init).toMatchObject({ method: 'POST' });
expect(JSON.parse(String(init?.body))).toMatchObject({
channelId: 1,
programScheduleId: 2,
scheduleKind: 'Classic'
});
});
it('createPlayout POSTs a sequential playout request with a schedule file', async () => {
const fetchMock = vi.spyOn(window, 'fetch').mockResolvedValue(jsonResponse({ id: 9 }, 201));
const body: CreatePlayoutRequest = {
channelId: 3,
programScheduleId: null,
scheduleFile: '/config/schedule.yml',
scheduleKind: 'Sequential'
};
await createPlayout(body);
const [, init] = fetchMock.mock.calls[0];
expect(JSON.parse(String(init?.body))).toMatchObject({
channelId: 3,
scheduleFile: '/config/schedule.yml',
scheduleKind: 'Sequential'
});
});
it('updatePlayoutDetails PUTs to the id route', async () => {
const fetchMock = vi.spyOn(window, 'fetch').mockResolvedValue(jsonResponse({ id: 3 }));
await updatePlayoutDetails(3, { dailyRebuildTime: '04:00:00', scheduleFile: null });
const [url, init] = fetchMock.mock.calls[0];
expect(url).toBe('/api/v1/playouts/3');
expect(init).toMatchObject({ method: 'PUT' });
expect(JSON.parse(String(init?.body))).toMatchObject({ dailyRebuildTime: '04:00:00' });
});
it('updatePlayoutDetails sends null dailyRebuildTime to clear the daily reset', async () => {
const fetchMock = vi.spyOn(window, 'fetch').mockResolvedValue(jsonResponse({ id: 3 }));
await updatePlayoutDetails(3, { dailyRebuildTime: null, scheduleFile: null });
const [, init] = fetchMock.mock.calls[0];
expect(JSON.parse(String(init?.body))).toMatchObject({ dailyRebuildTime: null });
});
it('updatePlayoutDefaultDeco PUTs the deco id to the deco route', async () => {
const fetchMock = vi.spyOn(window, 'fetch').mockResolvedValue(jsonResponse({ id: 3 }));
await updatePlayoutDefaultDeco(3, 7);
const [url, init] = fetchMock.mock.calls[0];
expect(url).toBe('/api/v1/playouts/3/deco');
expect(init).toMatchObject({ method: 'PUT' });
expect(JSON.parse(String(init?.body))).toMatchObject({ decoId: 7 });
});
it('deletePlayout DELETEs the id route', async () => {
const fetchMock = vi.spyOn(window, 'fetch').mockResolvedValue(noContentResponse());
await expect(deletePlayout(4)).resolves.toBeUndefined();
const [url, init] = fetchMock.mock.calls[0];
expect(url).toBe('/api/v1/playouts/4');
expect(init).toMatchObject({ method: 'DELETE' });
});
it('resetChannelPlayout POSTs to the id-keyed channel playout reset route without a mode', async () => {
const fetchMock = vi.spyOn(window, 'fetch').mockResolvedValue(jsonResponse({}));
await resetChannelPlayout(20);
const [url, init] = fetchMock.mock.calls[0];
expect(url).toBe('/api/v1/channels/20/playout/reset');
expect(init).toMatchObject({ method: 'POST' });
});
it('erasePlayoutItems POSTs to the erase-items route', async () => {
const fetchMock = vi.spyOn(window, 'fetch').mockResolvedValue(noContentResponse());
await erasePlayoutItems(6);
const [url, init] = fetchMock.mock.calls[0];
expect(url).toBe('/api/v1/playouts/6/erase-items');
expect(init).toMatchObject({ method: 'POST' });
});
it('erasePlayoutItemsAndHistory POSTs to the erase-items-and-history route', async () => {
const fetchMock = vi.spyOn(window, 'fetch').mockResolvedValue(noContentResponse());
await erasePlayoutItemsAndHistory(6);
const [url, init] = fetchMock.mock.calls[0];
expect(url).toBe('/api/v1/playouts/6/erase-items-and-history');
expect(init).toMatchObject({ method: 'POST' });
});
it('getPlayoutItemSchedulingContext GETs the item scheduling-context route', async () => {
const fetchMock = vi.spyOn(window, 'fetch').mockResolvedValue(jsonResponse({ context: '{ "x": 1 }' }));
await expect(getPlayoutItemSchedulingContext(42)).resolves.toMatchObject({ context: '{ "x": 1 }' });
const [url, init] = fetchMock.mock.calls[0];
expect(url).toBe('/api/v1/playouts/items/42/scheduling-context');
expect(init?.method ?? 'GET').toBe('GET');
});
it('getAlternateSchedules GETs the alternate-schedules route', async () => {
const fetchMock = vi.spyOn(window, 'fetch').mockResolvedValue(jsonResponse([]));
await getAlternateSchedules(5);
const [url, init] = fetchMock.mock.calls[0];
expect(url).toBe('/api/v1/playouts/5/alternate-schedules');
expect(init?.method ?? 'GET').toBe('GET');
});
it('replaceAlternateSchedules PUTs items with day-name strings', async () => {
const fetchMock = vi.spyOn(window, 'fetch').mockResolvedValue(jsonResponse([]));
await replaceAlternateSchedules(5, {
items: [
{
id: 0,
programScheduleId: 2,
daysOfWeek: ['Monday', 'Tuesday'],
daysOfMonth: [1, 2],
monthsOfYear: [1],
limitToDateRange: false,
startMonth: 1,
startDay: 1,
startYear: null,
endMonth: 12,
endDay: 31,
endYear: null
}
]
});
const [url, init] = fetchMock.mock.calls[0];
expect(url).toBe('/api/v1/playouts/5/alternate-schedules');
expect(init).toMatchObject({ method: 'PUT' });
const body = JSON.parse(String(init?.body));
expect(body.items[0].daysOfWeek).toEqual(['Monday', 'Tuesday']);
expect(body.items[0].programScheduleId).toBe(2);
});
it('getPlayoutTemplates GETs the templates route', async () => {
const fetchMock = vi.spyOn(window, 'fetch').mockResolvedValue(jsonResponse([]));
await getPlayoutTemplates(5);
const [url] = fetchMock.mock.calls[0];
expect(url).toBe('/api/v1/playouts/5/templates');
});
it('replacePlayoutTemplates PUTs template items', async () => {
const fetchMock = vi.spyOn(window, 'fetch').mockResolvedValue(jsonResponse([]));
await replacePlayoutTemplates(5, {
items: [
{
id: 0,
templateId: 3,
decoTemplateId: null,
daysOfWeek: ['Sunday'],
daysOfMonth: [1],
monthsOfYear: [1],
limitToDateRange: false,
startMonth: 1,
startDay: 1,
startYear: null,
endMonth: 12,
endDay: 31,
endYear: null
}
]
});
const [url, init] = fetchMock.mock.calls[0];
expect(url).toBe('/api/v1/playouts/5/templates');
expect(init).toMatchObject({ method: 'PUT' });
const body = JSON.parse(String(init?.body));
expect(body.items[0].templateId).toBe(3);
expect(body.items[0].decoTemplateId).toBeNull();
});
});