fix(subtitle-prefetch): reuse startup runs and prefetch overlap lines

- Warm combined text for overlapping subtitle cues.
- Preserve valid in-flight tokenization across startup restarts while rejecting invalidated cache results.
This commit is contained in:
sudacode committed 2026-09-28 01:17:59 -07:00
1 parent 4648c5e5a1
commit a4325740b7
9 files changed
+274 -6

No files matched your search

@@ -300,3 +300,65 @@ test('prefetch service deduplicates repeated cue text within a run', async () =>
);
assert.ok(tokenizedTexts.includes('other'));
});
test('prefetch service keeps an in-flight result from a superseded run', async () => {
const cues = makeCues(5);
const cachedTexts: string[] = [];
let releaseFirst!: () => void;
const firstGate = new Promise<void>((resolve) => {
releaseFirst = resolve;
});
const service = createSubtitlePrefetchService({
cues,
tokenizeSubtitle: async (text) => {
if (text === 'line-0') await firstGate;
return { text, tokens: [] };
},
preCacheTokenization: (text) => {
cachedTexts.push(text);
},
getCacheGeneration: () => 0,
priorityWindowSize: 3,
});
service.start(0);
await flushMicrotasks();
service.stop();
releaseFirst();
await flushMicrotasks();
assert.deepEqual(cachedTexts, ['line-0']);
});
test('prefetch service drops an in-flight result after cache invalidation', async () => {
const cues = makeCues(5);
const cachedTexts: string[] = [];
let generation = 0;
let releaseFirst!: () => void;
const firstGate = new Promise<void>((resolve) => {
releaseFirst = resolve;
});
const service = createSubtitlePrefetchService({
cues,
tokenizeSubtitle: async (text) => {
if (text === 'line-0') await firstGate;
return { text, tokens: [] };
},
preCacheTokenization: (text) => {
cachedTexts.push(text);
},
getCacheGeneration: () => generation,
priorityWindowSize: 3,
});
service.start(0);
await flushMicrotasks();
service.stop();
generation += 1;
releaseFirst();
await flushMicrotasks();
assert.deepEqual(cachedTexts, []);
});