Files
UHD_Media_Tool/apps/server/test/file-manager/episode-mapping-identify.test.ts
T
rootandClaude Opus 5.5 63e94b308e feat(identify-words): 识别词——全局识别词(MP 语法)+ 作品识别词(季集换算),规则页「识别词」页签
- 新表 identify_words(0094):全局一块 + 每部作品一块原文,带版本与审计;episode_mapping_rule 转成文本行后删表
- 全局识别词移植 MP WordsMatcher(屏蔽 / 替换 / 集数偏移 / 替换+偏移 / {[tmdbid=…]}),所有按标题 / 文件名识别的地方解析前套用,命中的行记在解析结果上;保存后后台按改动行重新识别
- 作品识别词:S02 >> S01 EP+24 / S01E13 >> S00E01 / S01 @组 >> 忽略,解析器 + 打印器 + 编译;发布组繁简视为同一个
- 去掉规则自动停用:算出来对不上季表只待确认,要停用就注释掉那一行
- 集数待确认的采用 / 忽略与智能体 apply_rule 改为往作品识别词追加一行(带来历注释)
- 编辑器可视化 / 文本两种模式改同一份原文,保存前试算;作品页「集数映射」用同一个编辑器

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Cxwgnn2pbzQ7rXv3rq7fei
2026-10-08 17:03:00 +08:00

132 lines
5.2 KiB
TypeScript

import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { setWordsText } from "../support/identify-words.ts";
import { createFmHarness, type FmHarness, pgAvailable, READ_KEY, seedEntity } from "./harness.ts";
// 文件管理识别 / 推荐名按作品映射集数(docs/plan/episode-resolution.md 批次 3,只读):识别到本地剧集作品后,
// 展示的季集、建议单元与推荐文件名用映射后的规范季集;集数待确认的不给单元与推荐名,也不开待处理。
const skip = !(await pgAvailable());
describe.skipIf(skip)("文件管理识别与推荐名按作品映射集数", () => {
let h: FmHarness;
let show: string;
let s2e13: string;
beforeAll(async () => {
h = await createFmHarness("episode_mapping_identify");
show = await seedEntity(h.db, { kind: "series", title: "Kusuriya no Hitorigoto", year: 2023 });
await h.db
.updateTable("media_entity")
.set({ metadata: JSON.stringify({ airStatus: { value: "Ended", source: "tmdb" } }) })
.where("id", "=", show)
.execute();
for (const [season, total] of [
[1, 12],
[2, 13],
] as const) {
await h.db
.insertInto("content_unit")
.values({
media_entity_id: show,
kind: "season",
season_number: season,
episode_total: total,
status: "confirmed",
source: "tmdb",
})
.execute();
}
s2e13 = (
await h.db
.insertInto("content_unit")
.values({
media_entity_id: show,
kind: "episode",
season_number: 2,
episode_number: 13,
status: "confirmed",
source: "tmdb",
})
.returning("id")
.executeTakeFirstOrThrow()
).id;
await writeFile(join(h.dl, "[ANi] Kusuriya no Hitorigoto - 25 [1080P].mkv"), "v25");
await writeFile(join(h.dl, "[ANi] Kusuriya no Hitorigoto - 40 [1080P].mkv"), "v40");
});
afterAll(async () => h?.close());
it("识别:`- 25` 展示与建议单元为 S02E13;`- 40` 待确认不给单元、带提示,不开待处理", async () => {
const r = await h.api(
"POST",
"/files/identify",
{
items: [
{ root: h.dlRoot, path: "[ANi] Kusuriya no Hitorigoto - 25 [1080P].mkv" },
{ root: h.dlRoot, path: "[ANi] Kusuriya no Hitorigoto - 40 [1080P].mkv" },
],
minSizeBytes: 0,
},
READ_KEY,
);
expect(r.status).toBe(200);
const [ok, bad] = r.body.data.items;
expect(ok.candidates[0]).toMatchObject({ mediaEntityId: show });
expect(ok.parsed).toMatchObject({ season: 2, episodes: [13] });
expect(ok.suggestion).toMatchObject({ mediaEntityId: show, contentUnit: "S02E13", contentUnitId: s2e13 });
expect(bad.suggestion).toMatchObject({ mediaEntityId: show, contentUnit: null, contentUnitId: null });
expect(bad.warnings.some((w: string) => w.includes("集数对不上"))).toBe(true);
const decisions = await h.db.selectFrom("decision").select("id").where("kind", "=", "episode_mapping").execute();
expect(decisions).toHaveLength(0);
});
it("推荐名:`- 25` 按 S02E13 命名;`- 40` 不给推荐名(episode_unresolved)", async () => {
const ok = await h.api(
"POST",
"/files/suggest-name",
{ root: h.dlRoot, path: "[ANi] Kusuriya no Hitorigoto - 25 [1080P].mkv", mediaEntityId: show },
READ_KEY,
);
expect(ok.status).toBe(200);
expect(ok.body.data.name).toMatch(/S02E13/);
const bad = await h.api(
"POST",
"/files/suggest-name",
{ root: h.dlRoot, path: "[ANi] Kusuriya no Hitorigoto - 40 [1080P].mkv", mediaEntityId: show },
READ_KEY,
);
expect(bad.body.data).toMatchObject({ name: null, reason: "episode_unresolved", mediaEntityId: show });
});
it("全局识别词与作品识别词都生效:`- 40` 经全局识别词减 15 后是 25,再按作品季表认作 S02E13", async () => {
await setWordsText(h.db, null, "Kusuriya no Hitorigoto - <> \\[1080P >> EP-15\n");
try {
const r = await h.api(
"POST",
"/files/identify",
{ items: [{ root: h.dlRoot, path: "[ANi] Kusuriya no Hitorigoto - 40 [1080P].mkv" }], minSizeBytes: 0 },
READ_KEY,
);
expect(r.status).toBe(200);
const [it40] = r.body.data.items;
expect(it40.parsed).toMatchObject({ season: 2, episodes: [13] });
expect(it40.suggestion).toMatchObject({ mediaEntityId: show, contentUnit: "S02E13", contentUnitId: s2e13 });
// 再加作品识别词:全局识别词先改文件名(25),作品识别词再换算(25 - 24 → S02E01)
await setWordsText(h.db, show, "S01 @ANi >> S02 EP-24\n");
const mapped = await h.api(
"POST",
"/files/identify",
{ items: [{ root: h.dlRoot, path: "[ANi] Kusuriya no Hitorigoto - 40 [1080P].mkv" }], minSizeBytes: 0 },
READ_KEY,
);
expect(mapped.body.data.items[0].parsed).toMatchObject({ season: 2, episodes: [1] });
expect(mapped.body.data.items[0].suggestion).toMatchObject({ contentUnit: "S02E01" });
} finally {
await setWordsText(h.db, null, "");
await setWordsText(h.db, show, "");
}
});
});