Files
NewTube/server/tests/youtube-innertube.test.mjs
T
bruno 4bdcd393fe feat(youtube): InnerTube-first search, transcripts and watch-next related (Steps 15-18)
- InnerTube layer via pinned youtubei.js 18.1.0 (no quota, no key):
  search with merged continuations (unlimited pages), watch-next
  related with LockupView mapping, caption-track discovery
- 3-layer dispatcher (YT_SEARCH_MODE, default innertube-first):
  innertube -> yt-dlp scrape -> official API, graceful errors.yt
- Robust yt-dlp binary resolution (YT_DLP_PATH > PATH > bundled)
  with systematic API fallback (fixes spawn ENOENT in UI)
- Transcript: InnerTube caption discovery (YT_TRANSCRIPT_SOURCE),
  reusing pickTrack/orderedTracks/parseTrackText; yt-dlp fallback kept
- Watch: sidebar uses real watch-next related[] (/api/details),
  title-search fallback for other providers
- Cache: memory LRU + SQLite (youtube_search_cache, youtube_metrics),
  never persist empty pages; /healthz observability; /api/trending
- Includes pending Step 15/16 leftovers in same files (suggest,
  test scripts); unrelated provider adapters left uncommitted
2026-09-25 19:38:46 -04:00

132 lines
5.7 KiB
JavaScript

// Step 18 — tests offline InnerTube (AUCUN réseau : mappers purs + modes).
import assert from 'node:assert/strict';
import { mapVideoNode, mapLockupView, mapNodes, mapCaptionTracks, parseViewsText, parseDurationLabel } from '../providers/youtube-innertube.mjs';
import { pickTrack, orderedTracks } from '../transcript.mjs';
import { getSearchMode, YT_SEARCH_MODES } from '../providers/youtube-common.mjs';
function ok(msg) { console.log(`✓ ${msg}`); }
// 1) parseViewsText FR/EN
{
assert.equal(parseViewsText('81 973 vues'), 81973);
assert.equal(parseViewsText('1,2 M vues'), 1200000);
assert.equal(parseViewsText('57K'), 57000);
assert.equal(parseViewsText('3 k vues'), 3000);
assert.equal(parseViewsText(''), undefined);
assert.equal(parseViewsText(null), undefined);
ok('parseViewsText FR/EN');
}
// 1b) parseDurationLabel (a11y)
{
assert.equal(parseDurationLabel("What's new in Angular 18 2 minutes, 27 seconds"), 147);
assert.equal(parseDurationLabel('Live 1 heure, 5 minutes'), 3900);
assert.equal(parseDurationLabel('nonsense'), undefined);
ok('parseDurationLabel');
}
// 2) mapVideoNode type Video (forme réelle youtubei.js v18)
{
const m = mapVideoNode({
type: 'Video', id: 'abc123',
title: { text: 'Demo' },
duration: { text: '15:22', seconds: 922 },
thumbnails: [{ url: 'http://t/low.jpg' }, { url: 'http://t/high.jpg' }],
author: { id: 'UC999', name: 'Chaine' },
published: { text: 'il y a 2 ans' },
view_count: { text: '81 973 vues' },
badges: [{ label: 'Sous-titres' }],
is_live: false,
});
assert.equal(m.id, 'abc123');
assert.equal(m.url, 'https://www.youtube.com/watch?v=abc123');
assert.equal(m.thumbnail, 'http://t/high.jpg');
assert.equal(m.duration, 922);
assert.equal(m.views, 81973);
assert.equal(m.channelId, 'UC999');
assert.deepEqual(m.badges, ['Sous-titres']);
assert.equal(m.isShort, undefined);
ok('mapVideoNode Video');
}
// 3) Shorts/Reel -> isShort ; live -> isLive ; bruit filtré
{
const s = mapVideoNode({ type: 'ShortsLockupView', id: 'sh1', title: { text: 'Short' }, thumbnails: [{ url: 'u' }] });
assert.equal(s.isShort, true);
const l = mapVideoNode({ type: 'Video', id: 'lv1', title: { text: 'Live' }, thumbnails: [{ url: 'u' }], is_live: true });
assert.equal(l.isLive, true);
assert.equal(mapVideoNode({ type: 'Playlist', id: 'PL1', title: { text: 'PL' } }), null);
assert.equal(mapVideoNode({ type: 'Channel', id: 'UC1', title: { text: 'C' } }), null);
assert.equal(mapVideoNode(null), null);
assert.equal(mapVideoNode({ type: 'Video', title: { text: 'no id' } }), null);
ok('mapVideoNode shorts/live/filtrage');
}
// 4) mapNodes déduplique
{
const mk = (id) => ({ type: 'Video', id, title: { text: id }, thumbnails: [{ url: 'u' }] });
const out = mapNodes([mk('a'), mk('a'), mk('b'), null]);
assert.deepEqual(out.map((x) => x.id), ['a', 'b']);
ok('mapNodes dédup');
}
// 4b) mapLockupView (renderer moderne du watch-next façon SmartTube)
{
const m = mapLockupView({
type: 'LockupView', content_id: '0wIL6d5TxGc', content_type: 'VIDEO',
content_image: { image: [{ url: 'http://t/small.jpg', width: 168 }, { url: 'http://t/big.jpg', width: 336 }] },
metadata: {
title: { text: "What's new" },
metadata: { metadata_rows: [
{ metadata_parts: [{ text: { text: 'Rainer Hahnekamp' } }] },
{ metadata_parts: [{ text: { text: '57K' } }] },
] },
image: { a11y_label: 'Go to channel Rainer Hahnekamp' },
},
renderer_context: { accessibility_context: { label: "What's new 2 minutes, 27 seconds" } },
});
assert.equal(m.id, '0wIL6d5TxGc');
assert.equal(m.url, 'https://www.youtube.com/watch?v=0wIL6d5TxGc');
assert.equal(m.thumbnail, 'http://t/big.jpg');
assert.equal(m.uploaderName, 'Rainer Hahnekamp');
assert.equal(m.views, 57000);
assert.equal(m.duration, 147);
assert.equal(mapVideoNode({ type: 'LockupView', content_id: 'x', content_type: 'VIDEO', metadata: { title: { text: 'T' } } }).id, 'x');
assert.equal(mapLockupView({ type: 'LockupView', content_type: 'PLAYLIST', content_id: 'PL1', metadata: { title: { text: 'PL' } } }), null);
ok('mapLockupView watch-next');
}
// 5) modes innertube connus
{
assert.ok(YT_SEARCH_MODES.includes('innertube-first'));
assert.ok(YT_SEARCH_MODES.includes('innertube-only'));
const prev = process.env.YT_SEARCH_MODE;
process.env.YT_SEARCH_MODE = 'innertube-only';
assert.equal(getSearchMode(), 'innertube-only');
if (prev === undefined) delete process.env.YT_SEARCH_MODE; else process.env.YT_SEARCH_MODE = prev;
ok('modes innertube-first/only');
}
// 6) mapCaptionTracks : forme InnerTube -> pseudo dump yt-dlp, interop pickTrack
{
const cap = mapCaptionTracks([
{ base_url: 'https://x/t?caps=asr&x=1', language_code: 'en', kind: 'asr', name: { text: 'English (auto-generated)' } },
{ base_url: 'https://x/t?caps=m&x=2', language_code: 'fr', name: { text: 'Français' } },
{ base_url: '', language_code: 'de', name: 'Deutsch' },
]);
assert.deepEqual(cap.languages.sort(), ['en', 'fr']);
assert.equal(cap.trackCount, 2);
assert.match(cap.automatic_captions.en[0].url, /fmt=json3/);
assert.equal(cap.automatic_captions.en[0].ext, 'json3');
assert.equal(cap.subtitles.fr[0].name, 'Français');
// Interop : le pseudo dump passe dans pickTrack/orderedTracks sans modification
const picked = pickTrack({ subtitles: cap.subtitles, automatic_captions: cap.automatic_captions }, 'fr');
assert.equal(picked.lang, 'fr');
assert.ok(picked.track.url.includes('fmt=json3'));
const ordered = orderedTracks({ subtitles: cap.subtitles, automatic_captions: cap.automatic_captions }, 'de');
assert.ok(ordered.length >= 2);
ok('mapCaptionTracks + interop pickTrack/orderedTracks');
}
console.log('youtube-innertube tests passed');