feat(adapters): qBittorrent, filesystem, TMDB and Emby adapters with testkit mocks (W1-3a)

- qbittorrent: WebUI API v2 client (cookie/API-key auth, add/info/files/filePrio,
  pause/resume with 5.x stop/start fallback, tags, delete with explicit deleteFiles,
  maindata), error classes, idempotent submit with local infohash (own bencode)
  and post-timeout hash/probe-tag lookup; state mapping per MoviePilot
- fs: copyWithVerify (tmp + fsync + size/sha256/sample + no-clobber commit),
  moveSafe (EXDEV surfaced), resumable crossDeviceMove, statSafe/checkRoot with
  root sentinel, budgeted incremental scan with cursor, ffprobe, statfs usage
- tmdb: multi-language search/details/seasons, LRU+TTL cache with coalescing,
  rate-limit backoff, MoviePilot match chain (name normalization, year +-1)
- emby: Emby/Jellyfin items/seasons/episodes/MediaSources, failures -> unknown
- testkit: mock qB / TMDB / Emby servers, torrent builder, temp-tree fixtures
- docs: W1-3a porting table appended to downloader-and-sites ledger

Co-Authored-By: Claude Fable 5.1 <[email protected]>
This commit is contained in:
rootandClaude Fable 5.1 committed 2026-09-27 18:07:48 +08:00
1 parent 9c881e737f
commit ef2e504a94
45 files changed
+5766 -1

No files matched your search

+158
View File
@@ -0,0 +1,158 @@
import { TMDB_FIXTURES, type TmdbFixture } from "./fixtures";
import { type MockHttp, type MockReply, startMockHttp } from "./mock-http";
export interface MockTmdbOptions {
apiKey?: string;
accessToken?: string;
fixtures?: TmdbFixture[];
port?: number;
}
export type MockTmdb = MockHttp & { fixtures: TmdbFixture[] };
const norm = (s: string) => s.toLowerCase().replace(/[\s\p{P}\p{S}]+/gu, "");
const pick = (m: Record<string, string>, lang: string | undefined, fb: string) =>
m[lang ?? "en-US"] ?? m[(lang ?? "").split("-")[0] ?? ""] ?? m["en-US"] ?? fb;
const notFound: MockReply = {
status: 404,
json: { success: false, status_code: 34, status_message: "The resource you requested could not be found." },
};
/** mock TMDB v3:/3/search/{movie,tv}、/3/{movie,tv}/:id(append_to_response)、/3/tv/:id/season/:n */
export async function createMockTmdb(opts: MockTmdbOptions = {}): Promise<MockTmdb> {
const fx = opts.fixtures ?? TMDB_FIXTURES;
const apiKey = opts.apiKey ?? "test-tmdb-key";
const find = (type: string, id: string) => fx.find((f) => f.type === type && String(f.id) === id);
const matchText = (f: TmdbFixture, needle: string) =>
[f.original_title, ...Object.values(f.titles), ...f.alternative_titles.map((a) => a.title)].some((t) =>
norm(t).includes(needle),
);
const brief = (f: TmdbFixture, lang?: string) => {
const title = pick(f.titles, lang, f.original_title);
const overview = f.overviews ? pick(f.overviews, lang, "") : "";
return f.type === "movie"
? {
id: f.id,
title,
original_title: f.original_title,
release_date: f.date,
original_language: f.original_language,
overview,
popularity: f.popularity,
}
: {
id: f.id,
name: title,
original_name: f.original_title,
first_air_date: f.date,
original_language: f.original_language,
origin_country: f.origin_country,
overview,
popularity: f.popularity,
};
};
const http = await startMockHttp(
[
[
/^\/3\/search\/multi$/,
(_m, q) => {
const needle = norm(q.query ?? "");
const results = fx
.filter((f) => needle && matchText(f, needle))
.sort((a, b) => b.popularity - a.popularity)
.map((f) => ({ ...brief(f, q.language), media_type: f.type }));
return { json: { page: 1, results, total_pages: 1, total_results: results.length } };
},
],
[
/^\/3\/search\/(movie|tv)$/,
(m, q) => {
const type = m[1]!;
const needle = norm(q.query ?? "");
const year = q.year ?? q.primary_release_year ?? q.first_air_date_year;
const results = fx
.filter((f) => f.type === type)
.filter((f) => !year || f.date.startsWith(year))
.filter((f) => !!needle && matchText(f, needle))
.sort((a, b) => b.popularity - a.popularity)
.map((f) => brief(f, q.language));
return { json: { page: 1, results, total_pages: 1, total_results: results.length } };
},
],
[
/^\/3\/tv\/(\d+)\/season\/(\d+)$/,
(m, q) => {
const f = find("tv", m[1]!);
const s = f?.seasons?.find((x) => String(x.season_number) === m[2]);
if (!f || !s) return notFound;
return {
json: {
id: f.id * 100 + s.season_number,
season_number: s.season_number,
name: pick(s.names, q.language, `Season ${s.season_number}`),
air_date: s.air_date,
episodes: s.episodes.map((e) => ({
episode_number: e.episode_number,
season_number: s.season_number,
name: pick(e.names, q.language, ""),
air_date: e.air_date,
})),
},
};
},
],
[
/^\/3\/(movie|tv)\/(\d+)$/,
(m, q) => {
const f = find(m[1]!, m[2]!);
if (!f) return notFound;
const append = new Set((q.append_to_response ?? "").split(",").filter(Boolean));
const out: Record<string, unknown> = { ...brief(f, q.language), origin_country: f.origin_country };
if (f.type === "tv") {
out.number_of_seasons = f.seasons?.filter((s) => s.season_number > 0).length ?? 0;
out.seasons = (f.seasons ?? []).map((s) => ({
season_number: s.season_number,
episode_count: s.episodes.length,
name: pick(s.names, q.language, ""),
air_date: s.air_date,
}));
}
if (append.has("alternative_titles")) {
out.alternative_titles =
f.type === "movie" ? { titles: f.alternative_titles } : { results: f.alternative_titles };
}
if (append.has("external_ids")) out.external_ids = f.external_ids;
if (append.has("translations")) {
const key = f.type === "movie" ? "title" : "name";
out.translations = {
translations: [
...Object.entries(f.titles).map(([lang, t]) => {
const [l, r] = lang.split("-");
return { iso_639_1: l, iso_3166_1: r ?? "", data: { [key]: t } };
}),
...(f.extra_translations ?? []).map((x) => ({
iso_639_1: x.iso_639_1,
iso_3166_1: x.iso_3166_1,
data: { [key]: x.title },
})),
],
};
}
return { json: out };
},
],
],
{
...(opts.port !== undefined ? { port: opts.port } : {}),
guard: (req, q) => {
const bearer = /^Bearer (.+)$/.exec(req.headers.authorization ?? "")?.[1];
const ok = q.api_key === apiKey || (opts.accessToken !== undefined && bearer === opts.accessToken);
return ok ? null : { status: 401, json: { success: false, status_code: 7, status_message: "Invalid API key" } };
},
},
);
return Object.assign(http, { fixtures: fx });
}