Move daily quota exceeded problem response logic to a dedicated utility in `lib/server/rate-limit/problem-response`. Refactor API routes to use the new builder and remove duplicated formatting code. Update all relevant imports for consistency. This improves maintainability and ensures uniform error responses across TTS endpoints.
64 lines
2.6 KiB
TypeScript
64 lines
2.6 KiB
TypeScript
import { expect, test } from '@playwright/test';
|
|
|
|
import type { CanonicalTtsSourceUnit } from '../../src/lib/shared/tts-segment-plan';
|
|
import {
|
|
buildWalkerPlanningSourceUnits,
|
|
selectUpcomingWalkerItems,
|
|
} from '../../src/lib/client/epub/tts-epub-preload';
|
|
|
|
test.describe('EPUB walker preload helpers', () => {
|
|
test('selectUpcomingWalkerItems honors depth as current + (depth-1) upcoming', () => {
|
|
const items = [
|
|
{ cfi: 'epubcfi(/6/2[;s=a]!/4/2)' }, // same as current after normalization
|
|
{ cfi: 'epubcfi(/6/4!/4/2)' },
|
|
{ cfi: 'epubcfi(/6/6!/4/2)' },
|
|
{ cfi: 'epubcfi(/6/8!/4/2)' },
|
|
];
|
|
|
|
const selected = selectUpcomingWalkerItems(items, 'epubcfi(/6/2!/4/2)', 2);
|
|
expect(selected.map((item) => item.cfi)).toEqual([
|
|
'epubcfi(/6/4!/4/2)',
|
|
]);
|
|
});
|
|
|
|
test('selectUpcomingWalkerItems returns empty list when depth <= 1', () => {
|
|
const items = [
|
|
{ cfi: 'epubcfi(/6/4!/4/2)' },
|
|
{ cfi: 'epubcfi(/6/6!/4/2)' },
|
|
];
|
|
expect(selectUpcomingWalkerItems(items, 'epubcfi(/6/2!/4/2)', 1)).toEqual([]);
|
|
expect(selectUpcomingWalkerItems(items, 'epubcfi(/6/2!/4/2)', 0)).toEqual([]);
|
|
});
|
|
|
|
test('buildWalkerPlanningSourceUnits includes live context when smart splitting is enabled', () => {
|
|
const contextUnits: CanonicalTtsSourceUnit[] = [
|
|
{ sourceKey: 'previous:page-a', text: 'prev sentence', locator: null },
|
|
{ sourceKey: 'page-a', text: 'current sentence', locator: { readerType: 'epub', location: 'page-a' } },
|
|
];
|
|
const upcomingUnits: CanonicalTtsSourceUnit[] = [
|
|
{ sourceKey: 'page-b', text: 'upcoming one', locator: { readerType: 'epub', location: 'page-b' } },
|
|
{ sourceKey: 'page-c', text: 'upcoming two', locator: { readerType: 'epub', location: 'page-c' } },
|
|
];
|
|
|
|
const planned = buildWalkerPlanningSourceUnits(true, contextUnits, upcomingUnits);
|
|
expect(planned.map((item) => item.sourceKey)).toEqual([
|
|
'previous:page-a',
|
|
'page-a',
|
|
'page-b',
|
|
'page-c',
|
|
]);
|
|
});
|
|
|
|
test('buildWalkerPlanningSourceUnits uses only upcoming units when smart splitting is disabled', () => {
|
|
const contextUnits: CanonicalTtsSourceUnit[] = [
|
|
{ sourceKey: 'previous:page-a', text: 'prev sentence', locator: null },
|
|
{ sourceKey: 'page-a', text: 'current sentence', locator: { readerType: 'epub', location: 'page-a' } },
|
|
];
|
|
const upcomingUnits: CanonicalTtsSourceUnit[] = [
|
|
{ sourceKey: 'page-b', text: 'upcoming one', locator: { readerType: 'epub', location: 'page-b' } },
|
|
];
|
|
|
|
const planned = buildWalkerPlanningSourceUnits(false, contextUnits, upcomingUnits);
|
|
expect(planned.map((item) => item.sourceKey)).toEqual(['page-b']);
|
|
});
|
|
});
|