test(web): api client + screen tests for new troubleshooting screens

Client param-mapping tests for playoutHistory + validateSequentialSchedule,
and screen-level flow tests (Block playout filtering + playout->block->
history->decode drilldown; YAML validate valid/invalid/import-toggle).

Refs #145 #158

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-07 22:11:04 +02:00
co-authored by Claude Fable 5
parent ec7531d821
commit 822f3692dc
4 changed files with 274 additions and 1 deletions
+81
View File
@@ -0,0 +1,81 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { getPlayoutBlockHistory, getPlayoutBlocks, getPlayoutHistoryDetails } from './playoutHistory';
function mockJson(body: unknown, status = 200) {
return vi.spyOn(window, 'fetch').mockResolvedValue(
new Response(JSON.stringify(body), {
headers: { 'Content-Type': 'application/json' },
status
})
);
}
describe('getPlayoutBlocks', () => {
beforeEach(() => {
window.localStorage.clear();
vi.restoreAllMocks();
});
it('GETs /api/playouts/{id}/blocks', async () => {
const fetchSpy = mockJson([{ id: 1, name: 'Morning' }]);
await expect(getPlayoutBlocks(7)).resolves.toHaveLength(1);
const [url, init] = fetchSpy.mock.calls[0];
expect(url).toBe('/api/playouts/7/blocks');
expect((init?.method ?? 'GET').toUpperCase()).toBe('GET');
});
});
describe('getPlayoutBlockHistory', () => {
beforeEach(() => {
window.localStorage.clear();
vi.restoreAllMocks();
});
it('GETs history without a query string when no params are given', async () => {
const fetchSpy = mockJson({ totalCount: 0, page: [] });
await getPlayoutBlockHistory(7, 3);
const [url] = fetchSpy.mock.calls[0];
expect(url).toBe('/api/playouts/7/blocks/3/history');
});
it('builds the query string from pageNum and pageSize', async () => {
const fetchSpy = mockJson({ totalCount: 0, page: [] });
await getPlayoutBlockHistory(7, 3, { pageNum: 2, pageSize: 25 });
const [url] = fetchSpy.mock.calls[0];
expect(url).toBe('/api/playouts/7/blocks/3/history?pageNum=2&pageSize=25');
});
});
describe('getPlayoutHistoryDetails', () => {
beforeEach(() => {
window.localStorage.clear();
vi.restoreAllMocks();
});
it('GETs /api/playouts/history/{id}', async () => {
const fetchSpy = mockJson({
playbackOrder: 'Shuffle',
collectionType: 'Collection',
name: 'Cartoons',
mediaItemType: 'Episode',
mediaItemTitle: 'S1E1'
});
await expect(getPlayoutHistoryDetails(42)).resolves.toMatchObject({ name: 'Cartoons' });
const [url] = fetchSpy.mock.calls[0];
expect(url).toBe('/api/playouts/history/42');
});
it('rejects with the ApiError status on failure', async () => {
mockJson({ status: 422, title: 'Validation failed' }, 422);
await expect(getPlayoutHistoryDetails(42)).rejects.toMatchObject({ status: 422 });
});
});
+37 -1
View File
@@ -1,5 +1,5 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { getTroubleshootingInfo } from './troubleshoot';
import { getTroubleshootingInfo, validateSequentialSchedule } from './troubleshoot';
const sampleInfo = {
generalJson: '{"Version":"1.2.3"}',
@@ -41,3 +41,39 @@ describe('getTroubleshootingInfo', () => {
await expect(getTroubleshootingInfo()).rejects.toMatchObject({ status: 500 });
});
});
describe('validateSequentialSchedule', () => {
const sampleResult = { isValid: true, messages: [], json: '{}' };
beforeEach(() => {
window.localStorage.clear();
vi.restoreAllMocks();
});
it('POSTs /api/troubleshoot/validate-schedule with the yaml and isImport body', async () => {
const fetchSpy = vi.spyOn(window, 'fetch').mockResolvedValue(
new Response(JSON.stringify(sampleResult), {
headers: { 'Content-Type': 'application/json' },
status: 200
})
);
await expect(validateSequentialSchedule('content: []', true)).resolves.toMatchObject({ isValid: true });
const [url, init] = fetchSpy.mock.calls[0];
expect(url).toBe('/api/troubleshoot/validate-schedule');
expect((init?.method ?? 'GET').toUpperCase()).toBe('POST');
expect(JSON.parse(String(init?.body))).toEqual({ yaml: 'content: []', isImport: true });
});
it('rejects with the ApiError status on failure', async () => {
vi.spyOn(window, 'fetch').mockResolvedValue(
new Response(JSON.stringify({ status: 400, title: 'Validation failed' }), {
headers: { 'Content-Type': 'application/json' },
status: 400
})
);
await expect(validateSequentialSchedule('', false)).rejects.toMatchObject({ status: 400 });
});
});
@@ -0,0 +1,90 @@
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { BlockPlayoutTroubleshootingScreen } from './BlockPlayoutTroubleshootingScreen';
function jsonResponse(body: unknown, status = 200): Response {
return new Response(JSON.stringify(body), { headers: { 'Content-Type': 'application/json' }, status });
}
const playoutsPage = {
totalCount: 2,
page: [
{ id: 5, channelNumber: '1', channelName: 'Cartoons', scheduleKind: 'Block', scheduleName: '', dailyRebuildTime: null, buildStatus: null },
{ id: 6, channelNumber: '2', channelName: 'Movies', scheduleKind: 'Classic', scheduleName: '', dailyRebuildTime: null, buildStatus: null }
]
};
const blocks = [
{ id: 10, groupId: 1, groupName: 'Morning', name: 'Toons', minutes: 60, stopScheduling: 'AfterDurationEnd' }
];
const historyPage = {
totalCount: 1,
page: [{ id: 99, when: '2026-07-07T00:00:00Z', finish: '2026-07-07T01:00:00Z', key: '{"BlockId":10}', details: '{"MediaItemId":3}' }]
};
const details = {
playbackOrder: 'Shuffle',
collectionType: 'Collection',
name: 'Cartoons Collection',
mediaItemType: 'Episode',
mediaItemTitle: 'S1E1 - Pilot'
};
function mockApi() {
return vi.spyOn(window, 'fetch').mockImplementation((input: RequestInfo | URL, init?: RequestInit) => {
const url = input.toString();
const method = (init?.method ?? 'GET').toUpperCase();
if (url === '/api/playouts' && method === 'GET') {
return Promise.resolve(jsonResponse(playoutsPage));
}
if (url === '/api/playouts/5/blocks' && method === 'GET') {
return Promise.resolve(jsonResponse(blocks));
}
if (url.startsWith('/api/playouts/5/blocks/10/history') && method === 'GET') {
return Promise.resolve(jsonResponse(historyPage));
}
if (url === '/api/playouts/history/99' && method === 'GET') {
return Promise.resolve(jsonResponse(details));
}
return Promise.resolve(new Response(null, { status: 204 }));
});
}
describe('BlockPlayoutTroubleshootingScreen', () => {
beforeEach(() => {
window.localStorage.clear();
vi.restoreAllMocks();
});
afterEach(() => {
cleanup();
});
it('only lists Block playouts in the selector', async () => {
mockApi();
render(<BlockPlayoutTroubleshootingScreen />);
await waitFor(() => expect(screen.getByRole('option', { name: '1 - Cartoons' })).toBeTruthy());
expect(screen.queryByRole('option', { name: '2 - Movies' })).toBeNull();
});
it('drills from playout to block to history to decoded details', async () => {
mockApi();
render(<BlockPlayoutTroubleshootingScreen />);
await screen.findByRole('option', { name: '1 - Cartoons' });
fireEvent.change(screen.getByRole('combobox'), { target: { value: '5' } });
expect(await screen.findByText('Toons')).toBeTruthy();
fireEvent.click(screen.getByRole('button', { name: 'History' }));
expect(await screen.findByText('{"BlockId":10}')).toBeTruthy();
fireEvent.click(screen.getByText('{"BlockId":10}'));
expect(await screen.findByText('Cartoons Collection')).toBeTruthy();
expect(screen.getByText('S1E1 - Pilot')).toBeTruthy();
});
});
@@ -0,0 +1,66 @@
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { YamlValidatorScreen } from './YamlValidatorScreen';
function jsonResponse(body: unknown, status = 200): Response {
return new Response(JSON.stringify(body), { headers: { 'Content-Type': 'application/json' }, status });
}
describe('YamlValidatorScreen', () => {
beforeEach(() => {
window.localStorage.clear();
vi.restoreAllMocks();
});
afterEach(() => {
cleanup();
});
it('validates the entered YAML and shows the JSON output when valid', async () => {
const fetchSpy = vi.spyOn(window, 'fetch').mockResolvedValue(
jsonResponse({ isValid: true, messages: [], json: '{\n "content": []\n}' })
);
render(<YamlValidatorScreen />);
fireEvent.change(screen.getByRole('textbox'), { target: { value: 'content: []' } });
fireEvent.click(screen.getByRole('button', { name: 'Validate' }));
expect(await screen.findByText('Valid')).toBeTruthy();
expect(screen.getByText(/"content": \[\]/)).toBeTruthy();
const [url, init] = fetchSpy.mock.calls[0];
expect(url).toBe('/api/troubleshoot/validate-schedule');
expect(JSON.parse(String(init?.body))).toEqual({ yaml: 'content: []', isImport: false });
});
it('shows validation messages when the schedule is invalid', async () => {
vi.spyOn(window, 'fetch').mockResolvedValue(
jsonResponse({ isValid: false, messages: ['content is required'], json: '{}' })
);
render(<YamlValidatorScreen />);
fireEvent.change(screen.getByRole('textbox'), { target: { value: 'nope: true' } });
fireEvent.click(screen.getByRole('button', { name: 'Validate' }));
expect(await screen.findByText('content is required')).toBeTruthy();
expect(screen.getByText(/1 validation message/)).toBeTruthy();
});
it('sends isImport=true when the import schema switch is enabled', async () => {
const fetchSpy = vi.spyOn(window, 'fetch').mockResolvedValue(
jsonResponse({ isValid: true, messages: [], json: '{}' })
);
render(<YamlValidatorScreen />);
fireEvent.change(screen.getByRole('textbox'), { target: { value: 'content: []' } });
fireEvent.click(screen.getByRole('switch', { name: 'Import schema' }));
fireEvent.click(screen.getByRole('button', { name: 'Validate' }));
await waitFor(() => expect(fetchSpy).toHaveBeenCalled());
const [, init] = fetchSpy.mock.calls[0];
expect(JSON.parse(String(init?.body))).toEqual({ yaml: 'content: []', isImport: true });
});
});