Files
NewTube/server/tests/youtube-innertube.test.mjs
T
bruno f1f673040e
CI / build-and-test (push) Successful in 14m10s
feat(search): panneau de filtres unifié, clavier/focus du typeahead
Barre de recherche
- Panneau de filtres unifié (remplace le ProviderPicker) : sources, type
  (vidéos/shorts/direct/chaînes), période (heure/jour/semaine/mois/année),
  durée (<4 / 4-20 / >20 min) et tri. Pastilles + roving tabindex
  (←/→ dans un groupe, ↑/↓ entre groupes), focus trap, Échap, Ctrl/⌘+Maj+F,
  recherches récentes, « mémoriser par défaut », « Réinitialiser ».
- Pastilles des filtres actifs sous la barre (retrait en un clic) + compteur.
- Typeahead : un seul keydown (fin du double Enter), ↑/↓ bouclants,
  Home/End, PageUp/PageDown, Entrée valide la ligne surlignée (sinon
  recherche brute), Tab complète sans chercher, Échap ferme puis vide,
  ligne « Rechercher <q> », défilement de l'option active, aria à jour.
- Focus : le panneau se referme dès que le focus quitte la barre
  (focusout + relatedTarget + activeElement), pointerdown neutralisé sur les
  lignes pour garder le focus dans l'input (plus de scintillement).
- Opérateurs en clair : `linux live:`, `tuto today: long:` deviennent des
  filtres et sont retirés de la requête (autocomplétion après le `:`).

Filtres côté serveur
- Modèle partagé : src/app/search/filters.ts + server/search-filters.mjs.
- /api/search?type=&duration=&period=&sort= : InnerTube (upload_date, type,
  duration, features), Data API v3 (type, videoDuration, publishedAfter),
  yt-dlp --dateafter ; post-filtrage pour les providers sans filtre natif.
  Un champ manquant ne fait jamais disparaître un résultat.
- Filtres dans l'URL (partageables) + adapters + SearchService (clé de cache).
- Correction : la recherche Shorts renvoyait 0 (ShortsLockupView/GridShelfView
  non mappés, id/titre dans l'endpoint) -> mappage + shelf + vignette/vues.

Tests / docs
- 23 tests unitaires (clavier, focus, opérateurs, panneau de filtres),
  server/tests/search-filters.test.mjs (npm run test:filters) + CI,
  scénarios e2e des filtres, README/api/README/todo/MCP à jour.
2026-09-28 17:42:19 -04:00

175 lines
7.8 KiB
JavaScript

// Step 18 — tests offline InnerTube (AUCUN réseau : mappers purs + modes).
import assert from 'node:assert/strict';
import { mapVideoNode, mapLockupView, mapNodes, mapShelfNodes, 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) mapShelfNodes + ShortsLockupView : les Shorts d'une recherche « Shorts »
// n'ont ni `id` ni `title` classiques (endpoint + overlay_metadata), et
// vivent dans un GridShelfView -> sans ce complément la recherche renvoie 0.
{
// Forme réelle observée (youtubei.js v18, ShortsLockupView)
const shortsNode = (id) => ({
type: 'ShortsLockupView',
entity_id: `shorts-shelf-item-${id}`,
on_tap_endpoint: { name: 'reelWatchEndpoint', payload: { videoId: id } },
overlay_metadata: {
primary_text: { text: `Short ${id}` },
secondary_text: { text: '219 M de vues' },
},
thumbnail: [{ url: 'http://t/low.jpg', width: 100 }, { url: 'http://t/high.jpg', width: 1080 }],
});
const mapped = mapVideoNode(shortsNode('s1'));
assert.equal(mapped.id, 's1', 'l\'id du Short est lu dans on_tap_endpoint');
assert.equal(mapped.title, 'Short s1', 'le titre vient de overlay_metadata');
assert.equal(mapped.isShort, true, 'un Short est marqué isShort');
assert.equal(mapped.views, 219000000, 'les vues viennent de la 2e ligne d\'overlay');
assert.equal(mapped.thumbnail, 'http://t/high.jpg', 'la plus grande vignette est retenue');
// Forme « brute » (avant parsing youtubei.js) également gérée
assert.equal(mapVideoNode({
type: 'ShortsLockupView',
on_tap: { innertubeCommand: { reelWatchEndpoint: { videoId: 'raw1' } } },
overlay_metadata: { primary_text: { text: 'Raw' } },
}).id, 'raw1', 'forme brute (on_tap) gérée');
const feed = {
results: [
{ type: 'Video', id: 'regular1', title: { text: 'Vidéo normale' } },
{ type: 'GridShelfView', contents: [shortsNode('s1'), shortsNode('s2'), { type: 'ChipCloud' }] },
],
};
const shelf = mapShelfNodes(feed);
assert.equal(shelf.length, 2, 'les 2 Shorts du shelf sont mappés');
assert.deepEqual(shelf.map((s) => s.id).sort(), ['s1', 's2']);
assert.equal(mapShelfNodes({ results: [{ type: 'Video', id: 'v' }] }).length, 0, 'hors shelf, rien à faire');
assert.deepEqual(mapShelfNodes(null), []);
assert.deepEqual(mapShelfNodes({}), []);
ok('mapShelfNodes + ShortsLockupView (Shorts dans un shelf)');
}
// 6) 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');
}
// 7) 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');