Files
UHD_Media_Tool/apps/server/test/file-manager/organize-in-place.test.ts
T
rootandClaude Opus 5.5 f5bc6e9c40 fix(scrape): 整理后刮削照 MP 对作品目录刮,集只刮本次整理的文件
原地入库逐个文件排刮削,单文件刮削只写该集,剧集根目录(tvshow.nfo、海报、季海报)与季目录
(season.nfo)没有元数据。照 MP ScrapingChain.scrape_metadata_event:按作品目录分组,目录元数据照常
初始化,集只刮本次文件;复制 / 移动同样改为只刮本次文件(此前刮整个作品目录)。

- 迁移 0062:scrape_record.videos(null = 目录下全部)
- queueScrape 支持 videos;同目录排队中还没开始的刮削合成一批(有一方刮全部则刮全部)
- 作品详情「重新刮削」与文件管理刮削不变(目录下全部)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-02 18:20:52 +08:00

211 lines
11 KiB
TypeScript

import { randomUUID } from "node:crypto";
import { mkdir, readdir, readFile, stat, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { executeRecord } from "../../src/modules/acquisition-organize/services/organize.ts";
import { runScrapeRecord } from "../../src/modules/acquisition-organize/services/scrape.ts";
import { createFmHarness, type FmHarness, key, pgAvailable, seedEntity } from "./harness.ts";
const skip = !(await pgAvailable());
describe.skipIf(skip)("file manager in-place import", () => {
let h: FmHarness;
beforeAll(async () => {
h = await createFmHarness("in_place", {}, { runTransfers: false });
});
afterAll(async () => {
await h?.close();
});
async function source(label: string, kind: "movie" | "series" = "movie") {
const title = `${label}-${randomUUID().slice(0, 8)}`;
const entity = await seedEntity(h.db, { title, kind, year: 2024 });
await mkdir(join(h.lib, title), { recursive: true });
const path = `${title}/${kind === "movie" ? "original.name.mkv" : "original.name.S03E04.mkv"}`;
await writeFile(join(h.lib, path), "untouched video");
return { entity, path, items: [{ root: h.libRoot, path, mediaEntityId: entity }] };
}
async function preview(items: unknown[], extra: Record<string, unknown> = {}) {
const payload = { items, action: "in_place", minSizeBytes: 0, scrapeAfterImport: false, ...extra };
const r = await h.api("POST", "/files/organize", { ...payload, idempotencyKey: key(), dryRun: true });
expect(r.status).toBe(200);
return { payload, data: r.body.data };
}
async function queue(items: unknown[], extra: Record<string, unknown> = {}) {
const r = await preview(items, extra);
expect(r.data.groups[0].error).toBeNull();
const accepted = await h.api("POST", "/files/organize", {
...r.payload,
idempotencyKey: key(),
dryRun: false,
planIds: r.data.groups.map((g: { planId: string }) => g.planId),
});
expect(accepted.status).toBe(200);
expect(accepted.body.data.groups[0].error).toBeNull();
return { ...r, recordId: accepted.body.data.recordIds[0] as string };
}
it("preserves movie and matched subtitle bytes, paths and mtimes; registers once and never transfers", async () => {
const s = await source("Movie");
const subtitle = s.path.replace(/\.mkv$/, ".chs.srt");
await writeFile(join(h.lib, subtitle), "untouched subtitle");
const before = await Promise.all([s.path, subtitle].map((p) => stat(join(h.lib, p))));
const r = await queue([{ root: h.libRoot, path: s.path.split("/")[0]!, mediaEntityId: s.entity }], {
naming: "template",
onExisting: "replace_with_verify",
});
expect(r.data.groups[0].files).toHaveLength(2);
expect(r.data.groups[0].files.every((f: { source: string; target: string }) => f.source === f.target)).toBe(true);
expect((await executeRecord(h.module.deps().ao, r.recordId)).status).toBe("succeeded");
expect((await executeRecord(h.module.deps().ao, r.recordId)).status).toBe("succeeded");
expect(await readFile(join(h.lib, s.path), "utf8")).toBe("untouched video");
expect(await readFile(join(h.lib, subtitle), "utf8")).toBe("untouched subtitle");
const after = await Promise.all([s.path, subtitle].map((p) => stat(join(h.lib, p))));
expect(after.map((f) => [f.size, f.mtimeMs, f.ino])).toEqual(before.map((f) => [f.size, f.mtimeMs, f.ino]));
expect(await readdir(join(h.lib, s.path.split("/")[0]!))).toEqual(["original.name.chs.srt", "original.name.mkv"]);
const assets = await h.db.selectFrom("media_asset").selectAll().where("media_entity_id", "=", s.entity).execute();
expect(assets).toHaveLength(2);
expect(assets.find((a) => a.kind === "subtitle")?.parent_asset_id).toBe(assets.find((a) => a.kind === "video")?.id);
const ops = await h.db
.selectFrom("asset_operation")
.select(["kind", "phase"])
.where("organization_record_id", "=", r.recordId)
.orderBy("ordinal")
.execute();
expect(ops).toEqual([
{ kind: "register", phase: "verify" },
{ kind: "register", phase: "verify" },
]);
expect(
await h.db.selectFrom("path_binding").select("id").where("media_entity_id", "=", s.entity).execute(),
).toEqual([]);
const again = await queue(s.items);
await executeRecord(h.module.deps().ao, again.recordId);
expect(
await h.db.selectFrom("media_asset").select("id").where("media_entity_id", "=", s.entity).execute(),
).toHaveLength(2);
// 文件管理装配不挂整理记录路由:直接查记录表
const history = await h.db
.selectFrom("organization_record")
.select("action")
.where("media_entity_id", "=", s.entity)
.execute();
expect(history.some((item) => item.action === "in_place")).toBe(true);
});
it("registers TV episodes in their existing directory and honors manual season/episode", async () => {
const s = await source("TV", "series");
const r = await queue(s.items.map((i) => ({ ...i, season: 2, episodes: [7] })));
expect(r.data.groups[0].files[0].target).toBe(join(h.lib, s.path));
expect((await executeRecord(h.module.deps().ao, r.recordId)).status).toBe("succeeded");
const units = await h.db
.selectFrom("content_unit")
.select(["season_number", "episode_number"])
.where("media_entity_id", "=", s.entity)
.execute();
expect(units.some((u) => u.season_number === 2 && u.episode_number === 7)).toBe(true);
});
it("剧集原地入库后照 MP 刮削:剧集根目录与季目录写元数据,集只刮本次登记的", async () => {
const title = `Show-${randomUUID().slice(0, 8)}`;
const entity = await seedEntity(h.db, { title, kind: "series", year: 2022 });
const at = new Date().toISOString();
await h.db
.updateTable("media_entity")
.set({
metadata: JSON.stringify({ title: { value: title, source: "tmdb", language: "zh-CN", at, locked: false } }),
})
.where("id", "=", entity)
.execute();
await mkdir(join(h.lib, title, "Season 1"), { recursive: true });
const ep1 = `${title}/Season 1/original.name.S01E01.mkv`;
const ep2 = `${title}/Season 1/original.name.S01E02.mkv`;
for (const p of [ep1, ep2]) await writeFile(join(h.lib, p), "video");
const r = await queue([{ root: h.libRoot, path: ep1, mediaEntityId: entity }], { scrapeAfterImport: true });
expect((await executeRecord(h.module.deps().ao, r.recordId)).status).toBe("succeeded");
const recs = await h.db
.selectFrom("scrape_record")
.select(["id", "path", "videos"])
.where("organization_record_id", "=", r.recordId)
.execute();
expect(recs).toMatchObject([{ path: title, videos: [ep1] }]);
await runScrapeRecord(h.module.deps().ao, recs[0]!.id);
const show = await readdir(join(h.lib, title));
const season = await readdir(join(h.lib, title, "Season 1"));
expect(show).toContain("tvshow.nfo");
expect(season).toContain("season.nfo");
expect(season).toContain("original.name.S01E01.nfo");
expect(season).not.toContain("original.name.S01E02.nfo");
});
it("scrapes only the newly registered file, retaining its organization association on resume", async () => {
const s = await source("Scrape");
await h.db
.insertInto("external_identity")
.values({
media_entity_id: s.entity,
provider: "tmdb",
entity_type: "movie",
external_id: "123456",
status: "accepted",
})
.execute();
await writeFile(join(h.lib, s.path.split("/")[0]!, "Unrelated.mkv"), "unrelated video");
const r = await queue(s.items, { scrapeAfterImport: true });
await executeRecord(h.module.deps().ao, r.recordId);
await executeRecord(h.module.deps().ao, r.recordId);
const scrapes = await h.db
.selectFrom("scrape_record")
.select(["path", "organization_record_id", "videos"])
.where("organization_record_id", "=", r.recordId)
.execute();
// 照 MP:对作品目录刮(目录海报 / 背景照常),视频只刮本次登记的这一个
expect(scrapes).toEqual([
{ path: s.path.split("/").slice(0, -1).join("/"), organization_record_id: r.recordId, videos: [s.path] },
]);
expect(await readFile(join(h.lib, s.path.split("/")[0]!, "Unrelated.mkv"), "utf8")).toBe("unrelated video");
});
it("rejects an unregistered download directory without submitting a task", async () => {
const entity = await seedEntity(h.db, { kind: "movie", title: "Outside", year: 2024 });
await writeFile(join(h.dl, "outside.mkv"), "video");
const r = await h.api("POST", "/files/organize", {
items: [{ root: h.dlRoot, path: "outside.mkv", mediaEntityId: entity }],
action: "in_place",
minSizeBytes: 0,
idempotencyKey: key(),
dryRun: true,
});
expect(r.status).toBe(400);
expect(await h.db.selectFrom("media_asset").select("id").where("media_entity_id", "=", entity).execute()).toEqual(
[],
);
});
it("does not bind a file to another work or import changed files after queue acceptance", async () => {
const s = await source("Changed");
const r = await queue(s.items);
await writeFile(join(h.lib, s.path), "changed video longer");
expect((await executeRecord(h.module.deps().ao, r.recordId)).status).toBe("failed");
expect(await h.db.selectFrom("media_asset").select("id").where("media_entity_id", "=", s.entity).execute()).toEqual(
[],
);
const original = await source("Owned");
const a = await queue(original.items);
await executeRecord(h.module.deps().ao, a.recordId);
const other = await seedEntity(h.db, { kind: "movie", title: "Other", year: 2024 });
const conflict = await preview([{ root: h.libRoot, path: original.path, mediaEntityId: other }]);
expect(conflict.data.groups[0].error).not.toBeNull();
expect(await h.db.selectFrom("media_asset").select("id").where("media_entity_id", "=", other).execute()).toEqual(
[],
);
});
it("registers read-only library roots without writing metadata", async () => {
const s = await source("ReadOnly");
const rootId = h.libRoot.slice(4);
await h.db.updateTable("library_root").set({ management_mode: "register_only" }).where("id", "=", rootId).execute();
try {
const r = await queue(s.items, { scrapeAfterImport: true });
expect((await executeRecord(h.module.deps().ao, r.recordId)).status).toBe("succeeded");
expect(
await h.db.selectFrom("scrape_record").select("id").where("organization_record_id", "=", r.recordId).execute(),
).toEqual([]);
expect(await readdir(join(h.lib, s.path.split("/")[0]!))).toEqual(["original.name.mkv"]);
} finally {
await h.db.updateTable("library_root").set({ management_mode: "managed" }).where("id", "=", rootId).execute();
}
});
});