gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmaind2e6d26bMerge branch 'records-park55' into mainTrackermain/tests/tmdbfix.mjs

12.1 KB

  1. // tests/tmdbfix.mjs — tools/tmdb-fix.hl against a fake TMDB (tracker#36, mission T36). A fresh gate storage (tests/seed-tmdbfix.hl) with a
  2. // title for every group; checks: the dry run plans exactly the right changes and changes NOTHING; the apply fixes years and switches ids
  3. // with the watches / follows / seasons / episodes still on the record; ambiguous, taken, no-year and translated titles stay as they
  4. // were; a second apply and a second dry run change / plan nothing. No real token, no real TMDB.
  5. // STORAGE=/abs/new/dir PORT=8767 node tests/tmdbfix.mjs (STORAGE: a scratch dir the gate fills, never live data)
  6. import http from 'node:http';
  7. import fs from 'node:fs';
  8. import { spawn } from 'node:child_process';
  9. const PORT = Number(process.env.PORT || 8767), STORAGE = process.env.STORAGE, TOOL = process.env.TOOL || 'tools/tmdb-fix.hl';
  10. if (!STORAGE || !STORAGE.startsWith('/')) { console.log('set STORAGE (absolute, a scratch dir)'); process.exit(2); }
  11. fs.rmSync(STORAGE, { recursive: true, force: true }); fs.mkdirSync(STORAGE, { recursive: true });
  12. const movie = (id, title, date, extra = {}) => ({ id, title, original_title: title, release_date: date, ...extra });
  13. const tv = (id, name, date) => ({ id, name, original_name: name, first_air_date: date });
  14. const DB = {
  15. '/3/movie/101': movie(101, 'Year Missing', '2008-05-01'), '/3/movie/102': movie(102, 'Other Year', '2008-01-01'),
  16. '/3/movie/103': movie(103, 'Uebersetzt', '2010-03-03'), '/3/movie/109': movie(109, 'Gone Taken', '2016-06-06'),
  17. '/3/movie/110': movie(110, 'Other Thing', '2020-02-02'), '/3/movie/111': movie(111, 'Fine', '2005-05-05'),
  18. '/3/movie/204': movie(204, 'Gone Unique', '2015-04-04'), '/3/tv/305': tv(305, 'Gone Series', '2011-01-01'),
  19. };
  20. const SEARCH = {
  21. '/3/search/movie|Gone Unique': [movie(204, 'Gone Unique', '2015-04-04'), movie(205, 'Gone Unique', '2001-01-01'), movie(299, 'Gone Unique Returns', '2015-09-09')],
  22. '/3/search/movie|Gone Many': [movie(206, 'Gone Many', '2012-01-01'), movie(207, 'Gone Many', '2012-12-12')],
  23. '/3/search/movie|Gone Other Year': [movie(208, 'Gone Other Year', '2014-01-01')],
  24. '/3/search/movie|Gone Taken': [movie(109, 'Gone Taken', '2016-06-06')],
  25. '/3/search/tv|Gone Series': [tv(305, 'Gone Series', '2011-01-01'), tv(306, 'Gone Series Kids', '2011-02-02')],
  26. };
  27. let seen = [];
  28. const server = http.createServer((req, res) => {
  29. const u = new URL(req.url, 'http://x'); seen.push(u.pathname + (u.pathname.includes('search') ? '?' + u.searchParams.get('query') : ''));
  30. const send = (code, body) => { res.writeHead(code, { 'content-type': 'application/json' }); res.end(JSON.stringify(body)); };
  31. if (u.pathname.includes('/search/')) return send(200, { page: 1, results: SEARCH[u.pathname + '|' + u.searchParams.get('query')] || [] });
  32. return DB[u.pathname] ? send(200, DB[u.pathname]) : send(404, { status_code: 34 });
  33. });
  34. await new Promise(r => server.listen(PORT, '127.0.0.1', r));
  35. const run = (file, args, extraEnv = {}) => new Promise((resolve) => {
  36. seen = []; let out = '';
  37. const p = spawn('./bin/hybriel', [file, ...args], { env: { ...process.env, TMDB_READ_TOKEN: 'fake-token', TMDB_BASE_URL: `http://127.0.0.1:${PORT}/3`, TRACKER_STORAGE: STORAGE, ...extraEnv } });
  38. p.stdout.on('data', d => out += d); p.stderr.on('data', d => out += d);
  39. p.on('close', code => resolve({ code, out, seen: [...seen] }));
  40. });
  41. let pass = 0, fail = 0;
  42. const check = (name, ok, extra = '') => { ok ? pass++ : fail++; console.log((ok ? 'PASS ' : 'FAIL ') + name + (ok ? '' : ' — ' + String(extra).slice(0, 700))); };
  43. const peek = async () => { const r = await run('tests/peek-tmdbfix.hl', []); const shows = {}; const rest = []; for (const l of r.out.split('\n')) { if (l.startsWith('show ')) { const j = JSON.parse(l.slice(5)); shows[j.slug] = j; } else if (l.trim()) rest.push(l); } return { shows, rest: rest.sort() }; };
  44. try {
  45. const seed = await run('tests/seed-tmdbfix.hl', []);
  46. check('seeded', /seeded/.test(seed.out), seed.out);
  47. const before = await peek();
  48. const plan = STORAGE + '.plan.tsv';
  49. const dry = await run(TOOL, ['--dry-run', '--plan-out', plan]);
  50. const has = (s) => dry.out.includes(s);
  51. check('dry run exits 0 and ends with the totals', dry.code == 0 && /done: asked=\d+ ok=\d+ group1=2 \(year fixes=2 nothing-to-take=0\) group2=1 group3=6 \(switch=2 listed=4\) group4=1/.test(dry.out), dry.out);
  52. check('year missing → TMDB year planned', /fix-year .*Year-Missing.*year \? → 2008/.test(dry.out), dry.out);
  53. check('other year, same name → TMDB year planned', /fix-year .*Other-Year.*year 2002 → 2008/.test(dry.out), dry.out);
  54. check('gone id with ONE same-name same-year result → switch planned (the wrong-year and other-name results do not count)', /fix-tmdb .*Gone-Unique.*tmdb 104 → 204/.test(dry.out), dry.out);
  55. check('gone series → switch planned via the TV search', /fix-tmdb tv .*Gone-Series.*tmdb 105 → 305/.test(dry.out), dry.out);
  56. check('two results with the same name and year → listed, not switched', /list-3 .*Gone-Many.*several TMDB results.*206 "Gone Many" \(2012\); 207/.test(dry.out) && !has('106 →'), dry.out);
  57. check('only another year → listed, not switched', /list-3 .*Gone-Other-Year.*no TMDB result with this name and year/.test(dry.out), dry.out);
  58. check('the one result is an id another record of ours already has → listed, not switched', /list-3 .*Gone-Taken .*already ours at \/movies\/Gone-Taken-2/.test(dry.out) || /list-3 .*Gone-Taken tmdb.*already ours/.test(dry.out), dry.out);
  59. check('no year of ours → no search, listed', /list-3 .*Gone-No-Year.*no year to search with/.test(dry.out) && !dry.seen.some(s => s.includes('Gone No Year')), dry.out + JSON.stringify(dry.seen));
  60. check('translation (same year, other name) → left, listed as leave-2', /leave-2 .*Translated/.test(dry.out) && !/fix-.*Translated/.test(dry.out), dry.out);
  61. check('both differ → list-4 only', /list-4 .*Both-Differ/.test(dry.out) && !/fix-.*Both-Differ/.test(dry.out), dry.out);
  62. check('a fine title is not mentioned', !dry.out.includes('/Fine ') && !dry.out.includes('/movies/Fine"'), dry.out);
  63. const afterDry = await peek();
  64. check('the dry run changed NOTHING', JSON.stringify(afterDry) == JSON.stringify(before), JSON.stringify(afterDry.shows));
  65. check('the plan file has the 4 changes', fs.readFileSync(plan, 'utf8').trim().split('\n').length == 4, fs.readFileSync(plan, 'utf8'));
  66. const ap = await run(TOOL, ['--apply', plan]);
  67. check('apply: 4 applied (2 years, 2 ids), nothing skipped or failed', /apply\) done: planned=4 applied=4 \(year=2 tmdb=2\) skipped=0 failed=0/.test(ap.out), ap.out);
  68. const after = await peek();
  69. const S = after.shows, B = before.shows;
  70. check('year missing: year 2008, no migratedYear', S['Year-Missing'].year == 2008 && S['Year-Missing'].migratedYear == null, JSON.stringify(S['Year-Missing']));
  71. check('other year: year 2008, the old 2002 kept in migratedYear', S['Other-Year'].year == 2008 && S['Other-Year'].migratedYear == 2002, JSON.stringify(S['Other-Year']));
  72. check('release follows TMDB: Other Year 2008-01-01 (old 2002-03-04 kept in migratedRelease), Year Missing 2008-05-01 (nothing old to keep)', S['Other-Year'].release == '2008-01-01' && S['Other-Year'].migratedRelease == '2002-03-04' && S['Year-Missing'].release == '2008-05-01' && S['Year-Missing'].migratedRelease == null, JSON.stringify([S['Other-Year'], S['Year-Missing']]));
  73. check('movie id switched, old id kept, the 404 marker cleared', S['Gone-Unique'].tmdbId == 204 && S['Gone-Unique'].tmdbIdWas == 104 && S['Gone-Unique'].adultCheck == null, JSON.stringify(S['Gone-Unique']));
  74. check('series id switched, old id kept', S['Gone-Series'].tmdbId == 305 && S['Gone-Series'].tmdbIdWas == 105, JSON.stringify(S['Gone-Series']));
  75. check('the record ids are unchanged (nothing re-created)', Object.keys(S).every(k => S[k].id == B[k].id) && Object.keys(S).length == Object.keys(B).length, JSON.stringify(Object.keys(S)));
  76. check('watches, follows, seasons and episodes are exactly as before (the switched movie and series keep theirs)', JSON.stringify(after.rest) == JSON.stringify(before.rest) && after.rest.some(l => l.startsWith('watch wEp1')) && after.rest.some(l => l.startsWith('follow fMovie')), JSON.stringify(after.rest));
  77. const untouched = ['Translated', 'Gone-Many', 'Gone-Other-Year', 'Gone-Taken', 'Gone-Taken-2', 'Both-Differ', 'Fine', 'Gone-No-Year'];
  78. check('every other title is exactly as before', untouched.every(k => JSON.stringify(S[k]) == JSON.stringify(B[k])), untouched.filter(k => JSON.stringify(S[k]) != JSON.stringify(B[k])).join());
  79. const ap2 = await run(TOOL, ['--apply', plan]);
  80. check('a second apply changes nothing (idempotent)', /planned=4 applied=0 \(year=0 tmdb=0\) skipped=4 failed=0/.test(ap2.out), ap2.out);
  81. const again = await run(TOOL, ['--dry-run']);
  82. check('a second dry run plans no change and lists only what is left for the creator', !/^fix-/m.test(again.out) && /group1=0 .*group2=1 group3=4 \(switch=0 listed=4\) group4=1/.test(again.out), again.out);
  83. check('and still changes nothing', JSON.stringify(await peek()) == JSON.stringify(after));
  84. // Each case starts from the original records, then changes one precondition.
  85. const resetPlan = async () => {
  86. fs.rmSync(STORAGE, { recursive: true, force: true }); fs.mkdirSync(STORAGE, { recursive: true });
  87. await run('tests/seed-tmdbfix.hl', []);
  88. await run(TOOL, ['--dry-run', '--plan-out', plan]);
  89. };
  90. const edit = async fields => run('tests/seed-tmdbfix.hl', [], { TMDBFIX_EDIT: JSON.stringify(fields) });
  91. const skippedUnchanged = async (name, slug) => {
  92. const b = await peek();
  93. const r = await run(TOOL, ['--apply', plan]);
  94. const a = await peek();
  95. check(name, r.code === 0 && r.out.includes('skip ') && !r.out.includes('applied tmdb ' + slug) && !r.out.includes('applied year ' + slug) && JSON.stringify(a.shows[slug]) === JSON.stringify(b.shows[slug]), r.out);
  96. };
  97. await resetPlan();
  98. DB['/3/movie/204'] = movie(204, 'A Completely Different Movie', '1999-01-01');
  99. await skippedUnchanged('fresh movie name/year mismatch is skipped without writes', 'Gone-Unique');
  100. DB['/3/movie/204'] = movie(204, 'Gone Unique', '2015-04-04');
  101. await resetPlan();
  102. DB['/3/tv/305'] = tv(305, 'Gone Series', '1999-01-01');
  103. await skippedUnchanged('fresh series year mismatch is skipped without writes', 'Gone-Series');
  104. DB['/3/tv/305'] = tv(305, 'Another Series', '2011-01-01');
  105. await skippedUnchanged('fresh series name mismatch is skipped without writes', 'Gone-Series');
  106. DB['/3/tv/305'] = tv(305, 'Gone Series', '2011-01-01');
  107. for (const [name, fields, slug] of [
  108. ['changed planned old year', { id: 'mOtherYear00000002', year: 2003 }, 'Other-Year'],
  109. ['changed planned old release', { id: 'mOtherYear00000002', release: '2002-09-09' }, 'Other-Year'],
  110. ['changed planned old TMDB id', { id: 'mGoneUnique000004', tmdbId: 999 }, 'Gone-Unique'],
  111. ['changed title', { id: 'mGoneUnique000004', title: 'Renamed' }, 'Gone-Unique'],
  112. ]) {
  113. await resetPlan(); await edit(fields); await skippedUnchanged(name + ' is skipped without writes', slug);
  114. }
  115. await resetPlan();
  116. await edit({ id: 'mTakenHolder000009', tmdbId: 204 });
  117. await skippedUnchanged('target id acquired by another record is skipped without writes', 'Gone-Unique');
  118. await resetPlan();
  119. await edit({ id: 'mTakenHolder000009', tmdbId: 204, urlSegment: null });
  120. await skippedUnchanged('target owner without a slug is still protected', 'Gone-Unique');
  121. await resetPlan();
  122. await edit({ id: 'mGoneMany0000006', title: 'Gone Unique', year: 2015 });
  123. const collision = await run(TOOL, ['--dry-run', '--plan-out', plan]);
  124. const collisionLines = fs.readFileSync(plan, 'utf8').trim().split('\n').filter(l => l.startsWith('tmdb ') && l.split(' ')[4] === '204');
  125. check('dry run reserves target id: only one of two records is planned', collisionLines.length === 1 && /list-3 .*already ours/.test(collision.out), collision.out);
  126. await resetPlan();
  127. delete DB['/3/movie/204'];
  128. await skippedUnchanged('failed fresh TMDB check is skipped without writes', 'Gone-Unique');
  129. DB['/3/movie/204'] = movie(204, 'Gone Unique', '2015-04-04');
  130. const refuse = await run(TOOL, []);
  131. check('without --dry-run / --apply the tool refuses', /REFUSED/.test(refuse.out) && refuse.code == 2, refuse.out);
  132. } finally { server.close(); }
  133. console.log(`tmdbfix: ${pass} pass, ${fail} fail`); process.exit(fail ? 1 : 0);

Branches

Latest commits

  • d2e6d26bMerge branch 'records-park55' into mainTracker
  • f567f025|||PR records: park #55, no Claude workers ruleTracker
  • c21aff6bMerge branch 'records-hm55c' into mainTracker
  • 9ba258d3|||PR records: HM55b resultTracker
  • 64f2d300Merge branch 'records-hm55b2' into mainTracker
  • ae3b80dc|||PR records: HM55b briefTracker
  • 7ef3cb3bMerge branch 'records-st55' into mainTracker
  • fec2e080|||PR records: STATUS HM55Tracker
  • 8e174918Merge branch 'records-hm55b' into mainTracker
  • b1ecee57|||PR records: HM55 stateTracker
  • 28eab08cMerge branch 'records-hm55' into mainTracker
  • d3edaa05|||PR records: HM55 start, PROCEDURES.mdTracker
  • 3059f86cMerge branch 'records-design-rule' into mainTracker
  • 421a4809|||PR records: his-design rule (Apollo 2026-11-13)Tracker
  • d1390692Merge branch 'records-dp75b' into mainTracker
  • a4d1a7d2|||PR records: later steps checked, branch auditTracker
  • e070e16fMerge branch 'records-dp75' into mainTracker
  • 642b4fa8|||PR records: DP75 day62 live, AP62 briefTracker
  • bfc4ea4dMerge branch 'ap62' into mainTracker
  • 2c95ac46|||PR AP62: re-vendor Hybriel set day62 (source 70341ad2, bin a54f5f08)Tracker