Files
jackwener__opencli/clis/youtube/transcript.test.js
Ocean 4a6cfe8060 fix(xhs,youtube): 把合法空数据语义切到 EmptyResultError (#1674)
* fix(xhs,youtube): 把"合法空数据"语义切到 EmptyResultError

把 xiaohongshu/user 和 youtube/transcript 跟 bilibili/subtitle 已有的
structured-error 模式对齐,让下游能区分"用户/视频没内容"和"fetch 真的挂了"。

## xiaohongshu user

返回 `No public notes found for this Xiaohongshu user` 的场景——目标用户
零公开笔记(销号 / 私密 / 全删)——原来抛 plain `Error`,下游无法和
"真的 fetch 失败 / cookie 死"区分。

改抛 `EmptyResultError`,exit code 变 66,stderr 携带 `code: EMPTY_RESULT`,
跟 `bilibili subtitle` empty 同 shape。

## youtube transcript

`No captions available for this video`(作者没开 CC、YouTube 也没自动生成)
是数据条件,不是基础设施失败。原来跟 HTTP/解析错误一样抛
`CommandExecutionError`,造成调用方反复重试。

改这个特定 case 抛 `EmptyResultError`;其他 caption 错误(HTTP / parse /
empty response)继续走 `CommandExecutionError` 触发重试。

## 为什么 downstream 需要这个

调用方(如自动化采集流水线)通常对 "data.length === 0 && exitCode !== 0"
做 soft-rate-limit 启发式:N 次连续 soft fail 触发 24h 平台跳过。当 seed 列表
里有变质条目(XHS 账号销号 / YouTube 视频丢失字幕),"empty" 响应堆积会
误触跳过——cookies 和平台本身都健康。EmptyResultError 让调用方能区分
"这个用户没内容"和"API 挂了"。

## 测试

- `npx vitest run clis/xiaohongshu clis/youtube` —— 全过
- `npx tsc --noEmit` 干净

* fix(xhs): distinguish empty user notes from parser drift

* fix(empty): tighten legal empty evidence

---------

Co-authored-by: jackwener <jakevingoo@gmail.com>
2026-05-19 19:02:32 +08:00

297 lines
14 KiB
JavaScript

import { afterEach, describe, expect, it, vi } from 'vitest';
import { readFileSync } from 'node:fs';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { getRegistry } from '@jackwener/opencli/registry';
import { CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors';
import './transcript.js';
const __dirname = dirname(fileURLToPath(import.meta.url));
const transcriptSource = readFileSync(resolve(__dirname, 'transcript.js'), 'utf8');
function createPageMock(captionUrl) {
const page = {
goto: vi.fn().mockResolvedValue(undefined),
wait: vi.fn().mockResolvedValue(undefined),
evaluate: vi.fn(),
};
page.evaluate
.mockResolvedValueOnce(null)
.mockResolvedValueOnce({
captionUrl,
language: 'en',
kind: 'manual',
available: ['en'],
requestedLang: null,
langMatched: false,
langPrefixMatched: false,
})
.mockResolvedValue([{ start: 1, end: 3, text: 'hello & world' }]);
return page;
}
afterEach(() => {
vi.unstubAllGlobals();
});
describe('youtube transcript source contract', () => {
it('uses the watch player captions module before falling back to watch HTML, not Android InnerTube', () => {
expect(transcriptSource).toContain("player.loadModule?.('captions')");
expect(transcriptSource).toContain("player.setOption('captions', 'track', track)");
expect(transcriptSource).toContain("url.includes('pot=')");
expect(transcriptSource).toContain("fetch('/watch?v='");
expect(transcriptSource).toContain("extractJsonAssignmentFromHtml(html, 'ytInitialPlayerResponse')");
expect(transcriptSource).toContain('playerCaptionsTracklistRenderer');
expect(transcriptSource).not.toContain('/youtubei/v1/player');
expect(transcriptSource).not.toContain("clientName: 'ANDROID'");
});
it('normalizes caption URL to request srv3 XML format', () => {
expect(transcriptSource).toContain('fmt=srv3');
});
it('checks HTTP status before reading caption response body', () => {
expect(transcriptSource).toContain('resp.ok');
});
it('restores page fetch and XHR hooks even when caption probing exits early', () => {
expect(transcriptSource).toContain('} finally {');
expect(transcriptSource).toContain('globalThis.fetch = originalFetch');
expect(transcriptSource).toContain('globalThis.XMLHttpRequest = OriginalXHR');
});
it('scopes timedtext URL matching to the current videoId in both in-page paths', () => {
// YouTube is an SPA — daemon-shared tabs preserve prior videos'
// performance.getEntriesByType('resource') across watch→watch navigations.
// Both findTimedtextUrl (resource buffer) and isJson3TimedtextUrl (fetch/XHR
// hook) must require the URL contain v=<currentVideoId>, otherwise a
// previously-viewed same-language video's captions can be returned.
expect(transcriptSource).toContain('const targetVideoId = ');
expect(transcriptSource).toContain("parsed.searchParams.get('v') === targetVideoId");
expect(transcriptSource).toContain('timedtextUrlMatchesVideo(url)');
});
});
describe('youtube transcript caption fetch', () => {
const command = getRegistry().get('youtube/transcript');
it('requests srv3 when the caption track URL has no explicit format', async () => {
const page = createPageMock('https://www.youtube.com/api/timedtext?v=abc&lang=en');
const rows = await command.func(page, { url: 'abc', mode: 'raw' });
expect(page.evaluate.mock.calls[2][0]).toContain('const primaryUrl = "https://www.youtube.com/api/timedtext?v=abc&lang=en&fmt=srv3"');
expect(page.evaluate.mock.calls[2][0]).toContain('const originalUrl = "https://www.youtube.com/api/timedtext?v=abc&lang=en"');
expect(rows).toEqual([{ index: 1, start: '1.00s', end: '3.00s', text: 'hello & world' }]);
});
it('uses Browser Bridge envelope-wrapped player caption segments without fallback', async () => {
const page = {
goto: vi.fn().mockResolvedValue(undefined),
wait: vi.fn().mockResolvedValue(undefined),
evaluate: vi.fn().mockResolvedValueOnce({
session: 'browser:default',
data: [{ start: 2, end: 4.5, text: 'from player captions' }],
}),
};
const rows = await command.func(page, { url: 'abc', mode: 'raw' });
expect(page.evaluate).toHaveBeenCalledTimes(1);
expect(rows).toEqual([{ index: 1, start: '2.00s', end: '4.50s', text: 'from player captions' }]);
});
it('uses captured timedtext json3 when player selection returns no segments', async () => {
const page = {
goto: vi.fn().mockResolvedValue(undefined),
wait: vi.fn().mockResolvedValue(undefined),
startNetworkCapture: vi.fn().mockResolvedValue(undefined),
readNetworkCapture: vi.fn().mockResolvedValue({
session: 'browser:default',
data: [{
url: 'https://www.youtube.com/api/timedtext?v=abc&lang=en&fmt=json3&pot=token',
responsePreview: JSON.stringify({
events: [
{ tStartMs: 1000, dDurationMs: 1500, segs: [{ utf8: 'hello ' }, { utf8: 'capture' }] },
],
}),
}],
}),
evaluate: vi.fn().mockResolvedValueOnce(null),
};
const rows = await command.func(page, { url: 'abc', mode: 'raw', lang: 'en' });
expect(page.startNetworkCapture).toHaveBeenCalledWith('/api/timedtext');
expect(page.evaluate).toHaveBeenCalledTimes(1);
expect(rows).toEqual([{ index: 1, start: '1.00s', end: '2.50s', text: 'hello capture' }]);
});
it('ignores captured timedtext entries from a prior video and uses only the current videoId', async () => {
// Regression: opencli daemon reuses one Chrome tab across sequential
// youtube transcript calls. YouTube's SPA navigation between watch URLs
// leaves prior videos' timedtext entries in performance.getEntriesByType
// and (rarely) in the CDP capture buffer. Without filtering by videoId,
// the same-language predecessor's captions can leak into the current row.
const page = {
goto: vi.fn().mockResolvedValue(undefined),
wait: vi.fn().mockResolvedValue(undefined),
startNetworkCapture: vi.fn().mockResolvedValue(undefined),
readNetworkCapture: vi.fn().mockResolvedValue({
session: 'browser:default',
data: [
{
// Stale entry from a prior watch on the shared tab — must be ignored.
url: 'https://www.youtube.com/api/timedtext?v=prev&lang=en&fmt=json3&pot=token',
responsePreview: JSON.stringify({
events: [
{ tStartMs: 0, dDurationMs: 1000, segs: [{ utf8: 'WRONG video captions' }] },
],
}),
},
{
// Prefix collision: substring matching for "v=abc" would accept this.
url: 'https://www.youtube.com/api/timedtext?v=abcd&lang=en&fmt=json3&pot=token',
responsePreview: JSON.stringify({
events: [
{ tStartMs: 1000, dDurationMs: 1000, segs: [{ utf8: 'WRONG prefix captions' }] },
],
}),
},
{
// Current video's captions.
url: 'https://www.youtube.com/api/timedtext?v=abc&lang=en&fmt=json3&pot=token',
responsePreview: JSON.stringify({
events: [
{ tStartMs: 2000, dDurationMs: 1000, segs: [{ utf8: 'right captions' }] },
],
}),
},
],
}),
evaluate: vi.fn().mockResolvedValueOnce(null),
};
const rows = await command.func(page, { url: 'abc', mode: 'raw', lang: 'en' });
expect(rows).toEqual([{ index: 1, start: '2.00s', end: '3.00s', text: 'right captions' }]);
});
it('does not override an existing caption format', async () => {
const page = createPageMock('https://www.youtube.com/api/timedtext?v=abc&lang=en&fmt=vtt');
await command.func(page, { url: 'abc', mode: 'raw' });
expect(page.evaluate.mock.calls[2][0]).toContain('const primaryUrl = "https://www.youtube.com/api/timedtext?v=abc&lang=en&fmt=vtt"');
expect(page.evaluate.mock.calls[2][0]).toContain('const originalUrl = "https://www.youtube.com/api/timedtext?v=abc&lang=en&fmt=vtt"');
});
it('falls back to the original URL only after an empty successful srv3 response', async () => {
const page = createPageMock('https://www.youtube.com/api/timedtext?v=abc&lang=en');
await command.func(page, { url: 'abc', mode: 'raw' });
const script = page.evaluate.mock.calls[2][0];
expect(script).toContain('if (!result.xml.length && originalUrl !== primaryUrl)');
expect(script).toContain('result = await fetchCaptionXml(originalUrl)');
expect(script).toContain('if (result.error) {');
});
it('fails typed on caption HTTP errors instead of falling back silently', async () => {
const page = createPageMock('https://www.youtube.com/api/timedtext?v=abc&lang=en');
page.evaluate.mockReset();
page.evaluate
.mockResolvedValueOnce(null)
.mockResolvedValueOnce({
captionUrl: 'https://www.youtube.com/api/timedtext?v=abc&lang=en',
language: 'en',
kind: 'manual',
available: ['en'],
requestedLang: null,
langMatched: false,
langPrefixMatched: false,
})
.mockResolvedValueOnce({ error: 'Caption URL returned HTTP 503' });
await expect(command.func(page, { url: 'abc', mode: 'raw' })).rejects.toMatchObject({
code: 'COMMAND_EXEC',
message: expect.stringContaining('HTTP 503'),
});
});
it('fails typed on malformed browser extraction payloads', async () => {
const page = createPageMock('https://www.youtube.com/api/timedtext?v=abc&lang=en');
page.evaluate.mockReset();
page.evaluate
.mockResolvedValueOnce(null)
.mockResolvedValueOnce({
captionUrl: 'https://www.youtube.com/api/timedtext?v=abc&lang=en',
language: 'en',
kind: 'manual',
available: ['en'],
requestedLang: null,
langMatched: false,
langPrefixMatched: false,
})
.mockResolvedValueOnce({ session: 'browser:default', data: { rows: [] } });
await expect(command.func(page, { url: 'abc', mode: 'raw' })).rejects.toMatchObject({
code: 'COMMAND_EXEC',
message: expect.stringContaining('Malformed caption XML extraction payload'),
});
});
it('fails typed on malformed caption info payloads before URL construction', async () => {
const page = createPageMock('https://www.youtube.com/api/timedtext?v=abc&lang=en');
page.evaluate.mockReset();
page.evaluate
.mockResolvedValueOnce(null)
.mockResolvedValueOnce({ session: 'browser:default', data: { rows: [] } });
await expect(command.func(page, { url: 'abc', mode: 'raw' })).rejects.toMatchObject({
code: 'COMMAND_EXEC',
message: expect.stringContaining('Malformed caption info payload'),
});
});
it('maps explicit no-captions watch metadata to EmptyResultError', async () => {
const page = createPageMock('https://www.youtube.com/api/timedtext?v=abc&lang=en');
page.evaluate.mockReset();
page.evaluate
.mockResolvedValueOnce(null)
.mockResolvedValueOnce({ error: 'No captions available for this video' });
await expect(command.func(page, { url: 'abc', mode: 'raw' })).rejects.toBeInstanceOf(EmptyResultError);
});
it('keeps malformed watch metadata as CommandExecutionError', async () => {
const page = createPageMock('https://www.youtube.com/api/timedtext?v=abc&lang=en');
page.evaluate.mockReset();
page.evaluate
.mockResolvedValueOnce(null)
.mockResolvedValueOnce({ error: 'ytInitialPlayerResponse not found in watch HTML' });
await expect(command.func(page, { url: 'abc', mode: 'raw' })).rejects.toBeInstanceOf(CommandExecutionError);
});
it('fails typed on malformed captured timedtext json3', async () => {
const page = {
goto: vi.fn().mockResolvedValue(undefined),
wait: vi.fn().mockResolvedValue(undefined),
startNetworkCapture: vi.fn().mockResolvedValue(undefined),
readNetworkCapture: vi.fn().mockResolvedValue([
{
url: 'https://www.youtube.com/api/timedtext?v=abc&lang=en&fmt=json3&pot=token',
responsePreview: '{"events":',
},
]),
evaluate: vi.fn().mockResolvedValueOnce(null),
};
await expect(command.func(page, { url: 'abc', mode: 'raw', lang: 'en' })).rejects.toMatchObject({
code: 'COMMAND_EXEC',
message: expect.stringContaining('Malformed json3 timedtext response'),
});
});
});