gitoriaLog in with ident

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitdcc5eecadcc5eecaMerge branch 't14-search'mredcc5eeca/tests/faketmdb.mjs

6.9 KB

  1. // tests/faketmdb.mjs — A FAKE TMDB for the gate (tracker.worldapi.org#9): a tiny local HTTP server that answers
  2. // the three calls tmdbsync.hl makes, from fixed JSON — no real TMDB call ever happens in the gate. The app is
  3. // pointed at it with TMDB_BASE_URL=<base>/3 and TMDB_IMAGE_URL=<base>/t/p.
  4. //
  5. // GET /3/tv/<id> details (shows[id] minus the episodes)
  6. // GET /3/tv/<id>?append_to_response=season/1,… the same + "season/<n>" = { name, overview, air_date, episodes }
  7. // GET /t/p/w342/<file>.png a 1×1 png
  8. // …?append_to_response=…,external_ids + "external_ids" = shows[id].external_ids (tracker.worldapi.org#12)
  9. // GET /3/movie/<id>[?append_to_response=external_ids] movies[id]: { id, title, poster_path } (+ external_ids)
  10. // GET /3/search/multi?query=… tracker.worldapi.org#14: { results = search[query in lower case] }
  11. // — recorded in `fake.searchRequests`, NOT in `fake.requests`
  12. // GET /t/p/w92/<file>.png the search page's small posters → `fake.thumbRequests`
  13. // and A FAKE TVMAZE under /tvmaze (the app gets TVMAZE_BASE_URL=<base>/tvmaze):
  14. // GET /tvmaze/lookup/shows?imdb=tt…|thetvdb=<id> 301 → /tvmaze/shows/<id> (like the real one), 404 when unknown
  15. // GET /tvmaze/shows/<id> { id, name }
  16. // GET /tvmaze/shows/<id>/episodes tvmaze.episodes[id] (tracker.worldapi.org#15): [{ id, season, number, name, airdate }]
  17. //
  18. // `fake.shows` is mutable (the gate adds an episode before the in-app run), `fake.delayMs` slows every answer
  19. // down, `fake.requests` records every TMDB/image request (path, authorization) so the gate can prove what was asked;
  20. // TVmaze requests go to `fake.tvmazeRequests` (paths) instead.
  21. import http from 'node:http';
  22. export const PNG = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==', 'base64');
  23. export async function startFakeTmdb({ port, shows, movies = {}, tvmaze = { imdb: {}, thetvdb: {} }, search = {} }) {
  24. const fake = { shows, movies, tvmaze, search, delayMs: 0, requests: [], tvmazeRequests: [], searchRequests: [], thumbRequests: [], base: `http://127.0.0.1:${port}` };
  25. const details = (s) => ({
  26. id: s.id, name: s.name, overview: s.overview, status: s.status, poster_path: s.poster_path,
  27. first_air_date: s.first_air_date, genres: s.genres, original_language: s.original_language,
  28. number_of_seasons: s.seasons.filter(x => x.season_number > 0).length,
  29. number_of_episodes: s.seasons.filter(x => x.season_number > 0).reduce((a, x) => a + x.episodes.length, 0),
  30. seasons: s.seasons.map(x => ({ id: x.id, season_number: x.season_number, name: x.name, overview: x.overview || '', air_date: x.air_date, episode_count: x.episodes.length })),
  31. });
  32. const server = http.createServer((req, res) => {
  33. const url = new URL(req.url, fake.base);
  34. if (url.pathname.startsWith('/tvmaze/')) {
  35. fake.tvmazeRequests.push(url.pathname + url.search);
  36. const json = (code, body, headers = {}) => { res.writeHead(code, { 'content-type': 'application/json', ...headers }); res.end(JSON.stringify(body)); };
  37. if (url.pathname === '/tvmaze/lookup/shows') {
  38. const [param, value] = [...url.searchParams.entries()][0] || [];
  39. const id = param && (fake.tvmaze[param] || {})[value];
  40. return id ? json(301, {}, { location: `${fake.base}/tvmaze/shows/${id}` }) : json(404, { name: 'Not Found', status: 404 });
  41. }
  42. const em = url.pathname.match(/^\/tvmaze\/shows\/(\d+)\/episodes$/);
  43. if (em) return (fake.tvmaze.episodes || {})[em[1]] ? json(200, fake.tvmaze.episodes[em[1]].map(e => ({ type: 'regular', summary: '<p>tvmaze</p>', ...e }))) : json(404, { name: 'Not Found', status: 404 });
  44. const sm = url.pathname.match(/^\/tvmaze\/shows\/(\d+)$/);
  45. return sm ? json(200, { id: Number(sm[1]), name: 'fake TVmaze show ' + sm[1] }) : json(404, { name: 'Not Found', status: 404 });
  46. }
  47. if (url.pathname === '/3/search/multi') {
  48. fake.searchRequests.push({ path: url.pathname + url.search, authorization: req.headers['authorization'] || null });
  49. const results = fake.search[(url.searchParams.get('query') || '').toLowerCase()] || [];
  50. res.writeHead(200, { 'content-type': 'application/json' });
  51. return res.end(JSON.stringify({ page: 1, results, total_pages: 1, total_results: results.length }));
  52. }
  53. // the search page's small posters (w92, loaded by the BROWSER from TMDB's image host) — kept out of `requests` too
  54. if (/^\/t\/p\/w92\/[a-z0-9]+\.png$/.test(url.pathname)) { fake.thumbRequests.push(url.pathname); res.writeHead(200, { 'content-type': 'image/png' }); return res.end(PNG); }
  55. fake.requests.push({ path: url.pathname + url.search, authorization: req.headers['authorization'] || null });
  56. const answer = () => {
  57. const appended = (url.searchParams.get('append_to_response') || '').split(',').filter(Boolean);
  58. const mv = url.pathname.match(/^\/3\/movie\/(\d+)$/);
  59. if (mv && fake.movies[mv[1]]) {
  60. const m = fake.movies[mv[1]];
  61. const out = { id: m.id, title: m.title, poster_path: m.poster_path, release_date: m.release_date, overview: m.overview, genres: m.genres };
  62. if (appended.includes('external_ids')) out.external_ids = m.external_ids || {};
  63. res.writeHead(200, { 'content-type': 'application/json' });
  64. return res.end(JSON.stringify(out));
  65. }
  66. let m = url.pathname.match(/^\/3\/tv\/(\d+)$/);
  67. if (m && fake.shows[m[1]]) {
  68. const s = fake.shows[m[1]];
  69. const out = details(s);
  70. if (appended.includes('external_ids')) out.external_ids = s.external_ids || {};
  71. for (const part of appended) {
  72. const sm = part.match(/^season\/(\d+)$/);
  73. const season = sm && s.seasons.find(x => x.season_number === Number(sm[1]));
  74. if (season) out[part] = { id: season.id, season_number: season.season_number, name: season.name, overview: season.overview || '', air_date: season.air_date,
  75. episodes: season.episodes.map(e => ({ id: e.id, season_number: season.season_number, episode_number: e.episode_number, name: e.name, overview: e.overview || '', air_date: e.air_date })) };
  76. }
  77. res.writeHead(200, { 'content-type': 'application/json' });
  78. return res.end(JSON.stringify(out));
  79. }
  80. if (/^\/t\/p\/w342\/[a-z0-9]+\.png$/.test(url.pathname)) { res.writeHead(200, { 'content-type': 'image/png' }); return res.end(PNG); }
  81. res.writeHead(404, { 'content-type': 'application/json' });
  82. res.end('{"status_code":34,"status_message":"The resource you requested could not be found."}');
  83. };
  84. if (fake.delayMs > 0) setTimeout(answer, fake.delayMs); else answer();
  85. });
  86. await new Promise((ok, fail) => { server.once('error', fail); server.listen(port, '127.0.0.1', ok); });
  87. fake.stop = () => new Promise(r => server.close(() => r()));
  88. return fake;
  89. }

Branches

Latest commits

  • dcc5eecaMerge branch 't14-search'mre
  • 03edc783Merge branch 't15-tvmaze'mre
  • 71b46345tracker#15: numbering check by date or title, placeholder titles in other languages, docs + real-data proofmre
  • 6bb2daf1tracker#13: homepage (tiles, intro, latest movies/shows), /shows, /movies/page/N, /my/movies; lists cached in memorymre
  • b8bd1157tracker#14: README + STATUS (search, real-data numbers, gate, merge notes)mre
  • 65c694a8tracker#14: search — header magnifier, /search/<text> (in-memory word-prefix index over titles + people), Fetch from web (TMDB search/multi, ours left out), Add = import via syncShow; gate +25 checks, real-data scriptmre
  • 34f2c15btracker#15: TVmaze merge in the sync (gaps only: new episodes/seasons, empty titles/air dates; numbering check), fake TVmaze episodes + gatemre
  • cbdc4ea7tracker#12: link icons TMDB/IMDb/TVDB/TVmaze; sync fills missing ids (TVmaze lookup); movies fetched via /movie/mre
  • b105bcd8tracker#11: Hybriel master ff51cf46 (checks no longer vanish), mobile-first styles, carets, follow button, sign-in modal, inverted check, orange castmre
  • 31b758aatracker#10: installable app (manifest, service worker, offline shell), own icon + faviconmre
  • 2fa9d997tracker#9: TMDB sync (followed shows: seasons, episodes, posters), tools/sync-tmdb.hl + daily run 04:00 UTC, fake TMDB in gatemre
  • 49e1f61edeploy.sh: back up live storage/.sessions/.env before every deploy (newest 5 kept)mre
  • 54070a4etracker#8: /my/unwatched + /my/schedule (301 from old), S01E01, title (year), 1 episode, watched-set lookup (unwatched 15s -> 1s)mre
  • 3251488atracker#7: /my/shows (followed shows, newest follow first, poster, title, last watched SxxEyy); gate can take screenshots (TRACKER_GATE_SHOTS)mre
  • 44b7d9f9tracker#6: /schedule — upcoming episodes of followed shows, soonest firstmre
  • 91c9fc8ctracker#5: /unwatched — unwatched released episodes of followed shows, newest firstmre
  • a97c0295tracker#4: show page /shows/:slug (header, seasons, episodes, watch checks) + tools/relink-episode-seasons.hlmre
  • 05f407c5tracker: no border on any button except inverted ones (Log out, ident status and identities too); header brand weight 100mre
  • 17375427tracker#2: tools/migrate.hl + tools/verify.hl — old MongoDB data into mpackdb with new idsmre
  • 31aac936tracker#3: no border on the header and on filled buttons; inverted buttons keep theirsmre