feat: v0.3.8–v0.3.14 工作区汇总(依赖升级、整理来源、手动入库、原地入库等)

生产 v0.3.8~v0.3.13 均由 8013196 加工作区修改构建部署,此前未提交;本提交补齐源码,
并含未部署的 v0.3.14 原地入库。中间各版本源码状态未保存,无法逐版拆分,详见
docs/plan/17-full-upgrade.md 与 docs/plan/18-organize-release.md。

- v0.3.8 全面升级:Deno workspace 依赖、Router 8 / Vite 8、camofox 1.18.0 / Node 24、
  PostgreSQL 18.6、备份客户端 18.6、演示 qB 5.2.4
- v0.3.9 媒体整理:分类规则、整理来源与目录快照(0059)、文件管理整理入口
- v0.3.10 手动下载与入库:独立手动下载、详情页资源下载、TMDB / Local 元数据兜底
  (metadata.resolve_organize)、正常多版本 / 一次性洗版(0060)
- v0.3.11 搜索详情与桌面黑边修复
- v0.3.12 列表分页、搜索历史、首页概况
- v0.3.13 统一整理弹窗与队列
- v0.3.14 原地入库(0061,未部署)

同批审计修复:
- 刮削入队:确认原记录存在后才删重放产生的新记录;写回 run 关联失败时保持 queued,
  由处理器补关联(对齐 MP 先登记、只在入队失败时记失败)
- 识别升级链只在结果更确定时采用,不再用 no_match 顶替原有歧义候选
- bluray_dir 资产以原盘目录本身为刮削根(MP 同做法)
- metadata.resolve_organize 登记到入库排障 Skill 与能力地图
- db/src/types.ts 按迁移重新生成;contracts 整理动作枚举顺序与数据库对齐
- 测试:原地入库用例改为直接查记录表;domain 测试模板补 version
- 前端:通知铃铛弹层与用户菜单统一样式;站点卡片共用卡头、底栏贴底、分页间距

依赖升级证据只提交摘要文件,registry-snapshot.json 移至 .local/(SHA-256 记于文档)。

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
rootandClaude Opus 5.5 committed 2026-10-02 15:47:17 +08:00
1 parent 8013196e3f
commit ceb66f8c77
265 files changed
+17751 -2483

No files matched your search

@@ -0,0 +1,175 @@
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 { 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("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"])
.where("organization_record_id", "=", r.recordId)
.execute();
expect(scrapes).toEqual([{ path: s.path, organization_record_id: r.recordId }]);
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();
}
});
});
@@ -0,0 +1,277 @@
import { randomUUID } from "node:crypto";
import { access, readFile, 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 { createFmHarness, type FmHarness, key, pgAvailable, seedEntity } from "./harness.ts";
const skip = !(await pgAvailable());
describe.skipIf(skip)("manual organize history and per-import scraping", () => {
let h: FmHarness;
beforeAll(async () => {
h = await createFmHarness("manual_options", {}, { runTransfers: false });
});
afterAll(async () => {
await h?.close();
});
async function source(label: string, local = false) {
const title = `${label} ${randomUUID().slice(0, 8)}`;
const entity = await seedEntity(h.db, { kind: "movie", title, year: 2024 });
if (local) {
await h.db
.updateTable("media_entity")
.set({
metadata: JSON.stringify({
overview: { value: "Local synopsis", source: "web", sources: [], locked: false },
}),
})
.where("id", "=", entity)
.execute();
} else {
await h.db
.insertInto("external_identity")
.values({
media_entity_id: entity,
provider: "tmdb",
entity_type: "movie",
external_id: String(Math.floor(Math.random() * 1_000_000)),
status: "accepted",
})
.execute();
}
const path = `${title.replaceAll(" ", ".")}.2024.1080p.mkv`;
await writeFile(join(h.dl, path), `video-${title}`);
return { entity, path, items: [{ root: h.dlRoot, path, mediaEntityId: entity }] };
}
async function organize(items: unknown[], extra: Record<string, unknown> = {}) {
const payload = { action: "copy", items, minSizeBytes: 0, ...extra };
const dry = await h.api("POST", "/files/organize", { ...payload, idempotencyKey: key(), dryRun: true });
expect(dry.status).toBe(200);
expect(dry.body.data.groups[0].error).toBeNull();
const exec = await h.api("POST", "/files/organize", {
...payload,
idempotencyKey: key(),
dryRun: false,
planIds: dry.body.data.groups.map((g: { planId: string }) => g.planId),
});
expect(exec.status).toBe(200);
expect(exec.body.data.groups[0].error).toBeNull();
return { dry: dry.body.data, exec: exec.body.data };
}
it("successful move reorganizes the target; failed move and copy retain their source", async () => {
const s = await source("Moved");
const r = await organize(s.items, { action: "move", scrapeAfterImport: false });
const rid = r.exec.recordIds[0];
expect((await executeRecord(h.module.deps().ao, rid)).status).toBe("succeeded");
await expect(access(join(h.dl, s.path))).rejects.toThrow();
const refs = await h.api("GET", `/files/record-sources?recordId=${rid}`);
expect(refs.status).toBe(200);
expect(refs.body.data.unresolved).toEqual([]);
expect(refs.body.data.items).toEqual([
{ root: h.libRoot, path: r.dry.groups[0].files[0].target.slice(h.lib.length + 1) },
]);
expect(await readFile(r.dry.groups[0].files[0].target, "utf8")).toContain("video-Moved");
const c = await source("Copied");
const copy = await organize(c.items, { scrapeAfterImport: false });
const crid = copy.exec.recordIds[0];
await executeRecord(h.module.deps().ao, crid);
expect((await h.api("GET", `/files/record-sources?recordId=${crid}`)).body.data.items).toEqual([
{ root: h.dlRoot, path: c.path },
]);
await h.db
.updateTable("organization_record")
.set({ action: "move", status: "failed" })
.where("id", "=", crid)
.execute();
expect((await h.api("GET", `/files/record-sources?recordId=${crid}`)).body.data.items).toEqual([
{ root: h.dlRoot, path: c.path },
]);
});
it("successful-history skip includes matched sidecars and does not enqueue work; latest failure does not hide success", async () => {
const s = await source("History");
const sidecar = s.path.replace(/\.mkv$/, ".srt");
await writeFile(join(h.dl, sidecar), "subtitle");
const first = await organize(s.items, { scrapeAfterImport: false });
await executeRecord(h.module.deps().ao, first.exec.recordIds[0]);
const old = await h.db
.selectFrom("organization_record")
.selectAll()
.where("id", "=", first.exec.recordIds[0])
.executeTakeFirstOrThrow();
const laterId = randomUUID();
await h.db
.insertInto("organization_record")
.values({
...old,
id: laterId,
change_seq: undefined,
status: "failed",
reason: "later_failure",
created_at: new Date(Date.now() + 1000),
})
.execute();
const main = await h.db
.selectFrom("organization_file_result")
.selectAll()
.where("organization_record_id", "=", old.id)
.where("role", "=", "main")
.executeTakeFirstOrThrow();
await h.db
.insertInto("organization_file_result")
.values({ ...main, id: randomUUID(), organization_record_id: laterId, status: "failed" })
.execute();
const identify = await h.api("POST", "/files/identify", {
items: [{ root: h.dlRoot, path: s.path }],
minSizeBytes: 0,
});
expect(identify.body.data.items[0].records).toMatchObject({ successfulCount: 1, lastStatus: "failed" });
const before = h.engine.enqueued.filter((x) => x.task === "verifyAndImport").length;
const second = await organize(s.items, { skipSuccessfulRecords: true, scrapeAfterImport: false });
expect(second.dry.groups[0].files).toHaveLength(2);
expect(
second.dry.groups[0].files.every(
(f: { action: string; skipReason: string }) => f.action === "skip" && f.skipReason === "successful_record",
),
).toBe(true);
expect(second.exec.recordIds).toEqual([]);
expect(h.engine.enqueued.filter((x) => x.task === "verifyAndImport")).toHaveLength(before);
const normal = await h.api("POST", "/files/organize", {
idempotencyKey: key(),
action: "copy",
items: s.items,
minSizeBytes: 0,
dryRun: true,
});
expect(
normal.body.data.groups[0].files.some((f: { skipReason: string }) => f.skipReason === "successful_record"),
).toBe(false);
});
it.each(["failed", "partial"])("%s records do not count as successful histories", async (status) => {
const s = await source(status);
const first = await organize(s.items, { scrapeAfterImport: false });
await executeRecord(h.module.deps().ao, first.exec.recordIds[0]);
await h.db
.updateTable("organization_record")
.set({ status: status as "failed" | "partial" })
.where("id", "=", first.exec.recordIds[0])
.execute();
const dry = await h.api("POST", "/files/organize", {
idempotencyKey: key(),
action: "copy",
items: s.items,
minSizeBytes: 0,
dryRun: true,
skipSuccessfulRecords: true,
});
expect(
dry.body.data.groups[0].files.some((f: { skipReason: string }) => f.skipReason === "successful_record"),
).toBe(false);
});
it("mixed selection skips successful files while organizing the remaining video", async () => {
const s = await source("Mixed");
const first = await organize(s.items, { scrapeAfterImport: false });
await executeRecord(h.module.deps().ao, first.exec.recordIds[0]);
const newPath = s.path.replace("1080p", "2160p");
await writeFile(join(h.dl, newPath), "different new video version");
const result = await organize([...s.items, { root: h.dlRoot, path: newPath, mediaEntityId: s.entity }], {
skipSuccessfulRecords: true,
scrapeAfterImport: false,
});
expect(
result.dry.groups[0].files.filter((f: { skipReason: string }) => f.skipReason === "successful_record"),
).toHaveLength(1);
expect(result.exec.recordIds).toHaveLength(1);
expect((await executeRecord(h.module.deps().ao, result.exec.recordIds[0])).status).toBe("succeeded");
const files = await h.db
.selectFrom("organization_file_result")
.select("source_path")
.where("organization_record_id", "=", result.exec.recordIds[0])
.execute();
expect(files).toEqual([{ source_path: join(h.dl, newPath) }]);
});
it("a succeeded record with an unsuccessful main file does not count as successful", async () => {
const s = await source("MainFailed");
const first = await organize(s.items, { scrapeAfterImport: false });
await executeRecord(h.module.deps().ao, first.exec.recordIds[0]);
await h.db
.updateTable("organization_file_result")
.set({ status: "failed" })
.where("organization_record_id", "=", first.exec.recordIds[0])
.where("role", "=", "main")
.execute();
const dry = await h.api("POST", "/files/organize", {
idempotencyKey: key(),
action: "copy",
items: s.items,
minSizeBytes: 0,
dryRun: true,
skipSuccessfulRecords: true,
});
expect(
dry.body.data.groups[0].files.some((f: { skipReason: string }) => f.skipReason === "successful_record"),
).toBe(false);
});
it.each([false, true])(
"TMDB per-import scrape=%s overrides the opposite global setting and survives replay",
async (enabled) => {
await h.db
.insertInto("settings")
.values({ key: "organize", value: JSON.stringify({ scrapeAfterImport: !enabled }) })
.onConflict((oc) => oc.column("key").doUpdateSet({ value: JSON.stringify({ scrapeAfterImport: !enabled }) }))
.execute();
const s = await source("TMDB");
const r = await organize(s.items, { scrapeAfterImport: enabled });
const rid = r.exec.recordIds[0];
expect((await executeRecord(h.module.deps().ao, rid)).status).toBe("succeeded");
const records = () =>
h.db.selectFrom("scrape_record").selectAll().where("media_entity_id", "=", s.entity).execute();
const scraped = await records();
expect(scraped).toHaveLength(enabled ? 1 : 0);
const byJob = await h.api("GET", `/files/scrape-records?organizationRecordId=${rid}`);
expect(byJob.status).toBe(200);
expect(byJob.body.data.records.map((r: { id: string }) => r.id)).toEqual(scraped.map((r) => r.id));
if (enabled) {
const id = scraped[0]!.id;
const oldQuery = await h.api("GET", `/files/scrape-records?ids=${id}`);
expect(oldQuery.body.data.records[0].id).toBe(id);
const both = await h.api("GET", `/files/scrape-records?ids=${id}&organizationRecordId=${rid}`);
expect(both.body.data.records[0].id).toBe(id);
const differentJob = await h.api("GET", `/files/scrape-records?ids=${id}&organizationRecordId=${randomUUID()}`);
expect(differentJob.body.data.records).toEqual([]);
}
await executeRecord(h.module.deps().ao, rid);
expect(await records()).toHaveLength(enabled ? 1 : 0);
},
);
it.each([false, true])("Local per-import scrape=%s is respected", async (enabled) => {
await h.db
.insertInto("settings")
.values({ key: "organize", value: JSON.stringify({ scrapeAfterImport: false }) })
.onConflict((oc) => oc.column("key").doUpdateSet({ value: JSON.stringify({ scrapeAfterImport: false }) }))
.execute();
const s = await source("Local", true);
const r = await organize(s.items, { scrapeAfterImport: enabled });
const rid = r.exec.recordIds[0];
const record = await h.db
.selectFrom("organization_record")
.select("plan")
.where("id", "=", rid)
.executeTakeFirstOrThrow();
expect(record.plan).toMatchObject({ metadataMode: "local", scrapeAfterImport: enabled });
await executeRecord(h.module.deps().ao, rid);
expect(
await h.db.selectFrom("scrape_record").select("id").where("media_entity_id", "=", s.entity).execute(),
).toHaveLength(enabled ? 1 : 0);
});
it("scrape-records requires an ID list or an organization record", async () => {
expect((await h.api("GET", "/files/scrape-records")).status).toBe(422);
});
});
@@ -0,0 +1,121 @@
import { mkdir, readdir, readFile, 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 { createFmHarness, type FmHarness, key, pgAvailable, seedEntity } from "./harness.ts";
const skip = !(await pgAvailable());
describe.skipIf(skip)("files 整理人工集号校验", () => {
let h: FmHarness;
let show: string;
let film: string;
const path = "Some.Show.S01E02.mkv";
const bytes = "x".repeat(64);
beforeAll(async () => {
h = await createFmHarness("overrides");
show = await seedEntity(h.db, { kind: "series", title: "Some Show", year: 2022 });
film = await seedEntity(h.db, { kind: "movie", title: "Film", year: 2021 });
await writeFile(join(h.dl, path), bytes);
await mkdir(join(h.dl, "Season"));
await writeFile(join(h.dl, "Season", path), bytes);
});
afterAll(async () => h?.close());
const facts = async () => ({
entities: await h.db.selectFrom("media_entity").select("id").orderBy("id").execute(),
identities: await h.db.selectFrom("external_identity").select("id").orderBy("id").execute(),
plans: await h.db.selectFrom("artifact").select("id").where("title", "=", "files_plan").orderBy("id").execute(),
records: await h.db.selectFrom("organization_record").select("id").orderBy("id").execute(),
tasks: await h.db.selectFrom("task").select("id").orderBy("id").execute(),
jobs: h.engine.enqueued.length,
library: await readdir(h.lib, { recursive: true }),
});
it.each([true, false])("非连续集号在 dryRun=%s 时整批拒绝,不创建作品、计划、记录或文件", async (dryRun) => {
const before = await facts();
const r = await h.api("POST", "/files/organize", {
idempotencyKey: key(),
action: "copy",
dryRun,
items: [
// 排在坏输入前的远端候选也不能创建作品或发起 TMDB 请求。
{ root: h.dlRoot, path, external: { provider: "tmdb", externalId: "136315", kind: "tv" } },
{ root: h.dlRoot, path, mediaEntityId: show, season: 2, episodes: [5, 7] },
],
});
expect(r.status).toBe(400);
expect(r.body.error.message).toMatch(/bad_request:.*集号 5,7 不连续/);
expect(await facts()).toEqual(before);
expect(await readFile(join(h.dl, path), "utf8")).toBe(bytes);
});
it("远端剧集候选的不连续集号也在建档前拒绝", async () => {
const before = await facts();
const r = await h.api("POST", "/files/organize", {
idempotencyKey: key(),
action: "copy",
dryRun: true,
items: [
{ root: h.dlRoot, path, external: { provider: "tmdb", externalId: "136315", kind: "tv" }, episodes: [2, 4] },
],
});
expect(r.status).toBe(400);
expect(r.body.error.message).toContain("不连续");
expect(await facts()).toEqual(before);
});
it.each(["local", "external"])("%s 剧集目录指定集号时明确拒绝,不保存计划", async (source) => {
const before = await facts();
const entity =
source === "local"
? { mediaEntityId: show }
: { external: { provider: "tmdb", externalId: "136315", kind: "tv" } };
const r = await h.api("POST", "/files/organize", {
idempotencyKey: key(),
action: "copy",
dryRun: true,
items: [{ root: h.dlRoot, path: "Season", ...entity, season: 1, episodes: [2] }],
});
expect(r.status).toBe(400);
expect(r.body.error.message).toContain("目录项不能指定集号");
expect(await facts()).toEqual(before);
});
it("连续人工季集覆盖原解析,预览与执行路径一致", async () => {
const items = [{ root: h.dlRoot, path, mediaEntityId: show, season: 2, episodes: [4, 3, 4] }];
const dry = await h.api("POST", "/files/organize", { idempotencyKey: key(), action: "copy", dryRun: true, items });
expect(dry.status).toBe(200);
const group = dry.body.data.groups[0];
expect(group.error).toBeNull();
expect(group.warnings).toEqual([]);
expect(group.files).toHaveLength(1);
expect(group.files[0].target).toMatch(/Season 2\/Some Show - S02E03-E04 - 第 3-4 集\.mkv$/);
const exec = await h.api("POST", "/files/organize", {
idempotencyKey: key(),
action: "copy",
dryRun: false,
items,
planIds: [group.planId],
});
expect(exec.status).toBe(200);
expect(exec.body.data.groups[0].error).toBeNull();
expect(exec.body.data.groups[0].files).toEqual(group.files);
expect(exec.body.data.recordIds).toHaveLength(1);
const result = await executeRecord(h.module.deps().ao, exec.body.data.recordIds[0]);
expect(result.status).toBe("succeeded");
expect(await readFile(group.files[0].target, "utf8")).toBe(bytes);
});
it("电影仍忽略季集覆写并提示", async () => {
const r = await h.api("POST", "/files/organize", {
idempotencyKey: key(),
action: "copy",
dryRun: true,
items: [{ root: h.dlRoot, path, mediaEntityId: film, season: 1, episodes: [2, 4] }],
});
expect(r.status).toBe(200);
expect(r.body.data.groups[0].warnings).toEqual([expect.stringMatching(/电影不适用季\/集覆写/)]);
});
});
@@ -0,0 +1,321 @@
import { randomUUID } from "node:crypto";
import { mkdir, rename, stat, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { createQbClient } from "@umt/adapters";
import type { OrganizeSourceConfig } from "@umt/contracts";
import { createMockQb, type MockQb } from "@umt/testkit";
import { afterEach, describe, expect, it } from "vitest";
import {
deleteOrganizeSource,
listOrganizeSources,
saveOrganizeSource,
} from "../../src/modules/file-manager/organize-sources.ts";
import { advanceSnapshot, scanOrganizeSource, scanStock } from "../../src/modules/file-manager/source-monitor.ts";
import { createFmHarness, type FmHarness, pgAvailable, seedDownload, seedEntity } from "./harness.ts";
const fact = { size: 100, mtimeMs: 1000 };
describe("source snapshot observations", () => {
it("baselines historical files and requires two stable observations for new and changed files", () => {
const baseline = advanceSnapshot(null, { "old.mkv": fact });
expect(baseline.eligible).toEqual([]);
const added = advanceSnapshot(baseline.snapshot, { "old.mkv": fact, "new.mkv": fact });
expect(added.eligible).toEqual([]);
const stable = advanceSnapshot(added.snapshot, { "old.mkv": fact, "new.mkv": fact });
expect(stable.eligible).toEqual(["new.mkv"]);
const changed = advanceSnapshot(stable.snapshot, { "old.mkv": { ...fact, size: 200 }, "new.mkv": fact });
expect(changed.eligible).not.toContain("old.mkv");
expect(advanceSnapshot(changed.snapshot, { "old.mkv": { ...fact, size: 200 } }).eligible).toEqual(["old.mkv"]);
});
});
const skip = !(await pgAvailable());
describe.skipIf(skip)("persistent source monitoring", () => {
let h: FmHarness | undefined;
let qb: MockQb | undefined;
afterEach(async () => {
await qb?.close();
qb = undefined;
await h?.close();
h = undefined;
});
const setup = async () => {
qb = await createMockQb();
h = await createFmHarness("source_monitor", {
ao: {
downloader: async (inst) =>
createQbClient({ instanceId: inst.id, baseUrl: qb!.url, username: "admin", password: "adminadmin" }),
},
});
const config: Omit<OrganizeSourceConfig, "id"> = {
name: "来源",
enabled: true,
source: { root: h.dlRoot, path: "" },
target: { root: h.libRoot, path: "" },
mediaType: "all",
category: null,
action: "copy",
trigger: "snapshot",
downloaderInstanceId: null,
tag: null,
intervalSec: 60,
};
const id = randomUUID();
await saveOrganizeSource(h.module.deps(), id, config);
return { h, id, config, qb };
};
it("first scan persists stock baseline without records; stock never changes baseline", async () => {
const { h, id } = await setup();
await writeFile(join(h.dl, "old.mkv"), "old video content");
const result = await scanOrganizeSource(h.module.deps(), { sourceId: id });
expect(result).toMatchObject({ baseline: true, queued: 0 });
const before = await h.db.selectFrom("organize_source_snapshot").selectAll().executeTakeFirstOrThrow();
expect(before.baseline_at).not.toBeNull();
await writeFile(join(h.dl, "new.mkv"), "new video content");
const stock = await scanStock(h.module.deps(), id);
expect(stock.items.map((f) => f.path).sort()).toEqual(["new.mkv", "old.mkv"]);
const after = await h.db.selectFrom("organize_source_snapshot").selectAll().executeTakeFirstOrThrow();
expect(after).toEqual(before);
expect(await h.db.selectFrom("organization_record").select("id").execute()).toEqual([]);
expect((await listOrganizeSources(h.module.deps())).items[0]?.fileCount).toBe(1);
});
it("unavailable source preserves baseline and retry does not import historical files", async () => {
const { h, id } = await setup();
await writeFile(join(h.dl, "old.mkv"), "old video content");
await scanOrganizeSource(h.module.deps(), { sourceId: id });
const before = await h.db.selectFrom("organize_source_snapshot").selectAll().executeTakeFirstOrThrow();
await rename(h.dl, `${h.dl}-offline`);
await scanOrganizeSource(h.module.deps(), { sourceId: id });
const failed = await h.db.selectFrom("organize_source_snapshot").selectAll().executeTakeFirstOrThrow();
expect(failed.snapshot).toEqual(before.snapshot);
expect(failed.baseline_at).toEqual(before.baseline_at);
expect(failed.last_error).not.toBeNull();
await rename(`${h.dl}-offline`, h.dl);
expect(await scanOrganizeSource(h.module.deps(), { sourceId: id })).toMatchObject({
baseline: false,
eligible: 0,
queued: 0,
});
});
it("an identified stable new file plans and queues import once", async () => {
const { h, id, config } = await setup();
await saveOrganizeSource(h.module.deps(), id, { ...config, category: "欧美电影" });
const entityId = await seedEntity(h.db, { kind: "movie", title: "Snapshot Film", year: 2024 });
await h.db
.insertInto("external_identity")
.values({
media_entity_id: entityId,
provider: "tmdb",
external_id: "123456",
status: "accepted",
entity_type: "movie",
})
.execute();
await scanOrganizeSource(h.module.deps(), { sourceId: id });
await writeFile(join(h.dl, "Snapshot.Film.2024.tmdb-123456.1080p.BluRay.mkv"), "video content for snapshot import");
expect(await scanOrganizeSource(h.module.deps(), { sourceId: id })).toMatchObject({ eligible: 0, queued: 0 });
const stableResult = await scanOrganizeSource(h.module.deps(), { sourceId: id });
expect(stableResult).toMatchObject({ eligible: 1, queued: 1 });
const imports = h.engine.enqueued.filter((e) => e.task === "verifyAndImport");
expect(imports).toHaveLength(1);
expect(imports[0]?.input).toMatchObject({ recordId: expect.any(String) });
await scanOrganizeSource(h.module.deps(), { sourceId: id });
expect(h.engine.enqueued.filter((e) => e.task === "verifyAndImport")).toHaveLength(1);
});
it("new stable files create one scoped decision and never reprocess baseline stock", async () => {
const { h, id } = await setup();
await writeFile(join(h.dl, "historical.mkv"), "historical video content");
await scanOrganizeSource(h.module.deps(), { sourceId: id });
await writeFile(join(h.dl, "unidentified.mkv"), "unidentified video content");
expect(await scanOrganizeSource(h.module.deps(), { sourceId: id })).toMatchObject({ eligible: 0, queued: 0 });
expect(await scanOrganizeSource(h.module.deps(), { sourceId: id })).toMatchObject({ eligible: 1, queued: 0 });
const decisions = await h.db.selectFrom("decision").selectAll().execute();
expect(decisions).toHaveLength(1);
expect(decisions[0]?.evidence).toMatchObject({ sourceConfigId: id, root: h.dlRoot, path: "unidentified.mkv" });
await scanOrganizeSource(h.module.deps(), { sourceId: id });
expect(await h.db.selectFrom("decision").select("id").execute()).toHaveLength(1);
});
it("active and completed QB tasks remain under the downloader tag policy", async () => {
const { h, id } = await setup();
await scanOrganizeSource(h.module.deps(), { sourceId: id });
await writeFile(join(h.dl, "active.mkv"), "active video content");
await writeFile(join(h.dl, "completed.mkv"), "completed video content");
await seedDownload(h.db, h, { name: "active.mkv", status: "downloading" });
await seedDownload(h.db, h, { name: "completed.mkv", status: "completed" });
await scanOrganizeSource(h.module.deps(), { sourceId: id });
expect(await scanOrganizeSource(h.module.deps(), { sourceId: id })).toMatchObject({ eligible: 2, queued: 0 });
expect(await h.db.selectFrom("organization_record").select("id").execute()).toEqual([]);
expect(await scanStock(h.module.deps(), id)).toMatchObject({
unavailable: 1,
items: [{ root: h.dlRoot, path: "completed.mkv" }],
});
});
it("untagged paused QB downloads without DB rows wait for completion before snapshot import", async () => {
const { h, id, qb, config } = await setup();
await saveOrganizeSource(h.module.deps(), id, { ...config, category: "欧美电影" });
const entityId = await seedEntity(h.db, { kind: "movie", title: "Snapshot Film", year: 2024 });
await h.db
.insertInto("external_identity")
.values({
media_entity_id: entityId,
provider: "tmdb",
external_id: "123456",
status: "accepted",
entity_type: "movie",
})
.execute();
await scanOrganizeSource(h.module.deps(), { sourceId: id });
const name = "Snapshot.Film.2024.tmdb-123456.1080p.BluRay.mkv";
await writeFile(join(h.dl, name), "stable incomplete video content");
qb.seed({
hash: "a".repeat(40),
name,
tags: [],
state: "pausedDL",
progress: 0.5,
amount_left: 50,
save_path: h.dl,
content_path: join(h.dl, name),
});
expect(await h.db.selectFrom("download_task").select("id").execute()).toEqual([]);
await scanOrganizeSource(h.module.deps(), { sourceId: id });
expect(await scanOrganizeSource(h.module.deps(), { sourceId: id })).toMatchObject({ eligible: 1, queued: 0 });
expect(await scanStock(h.module.deps(), id)).toMatchObject({ items: [], unavailable: 1 });
expect(await h.db.selectFrom("decision").select("id").execute()).toEqual([]);
expect(h.engine.enqueued.filter((e) => e.task === "verifyAndImport")).toEqual([]);
await qb.complete("a".repeat(40));
expect(await scanOrganizeSource(h.module.deps(), { sourceId: id })).toMatchObject({ eligible: 1, queued: 1 });
expect(await h.db.selectFrom("download_task").select("id").execute()).toEqual([]);
});
it("unreachable QB preserves the previous baseline and rejects stock", async () => {
const { h, id, qb } = await setup();
await writeFile(join(h.dl, "baseline.mkv"), "baseline video content");
await scanOrganizeSource(h.module.deps(), { sourceId: id });
const before = await h.db.selectFrom("organize_source_snapshot").selectAll().executeTakeFirstOrThrow();
qb.fault("torrents/info", { status: 503, times: 10 });
await writeFile(join(h.dl, "later.mkv"), "later video content");
expect(await scanOrganizeSource(h.module.deps(), { sourceId: id })).toMatchObject({ queued: 0 });
const after = await h.db.selectFrom("organize_source_snapshot").selectAll().executeTakeFirstOrThrow();
expect(after.snapshot).toEqual(before.snapshot);
expect(after.baseline_at).toEqual(before.baseline_at);
expect(after.last_error).toContain("无法查询来源下载器");
await expect(scanStock(h.module.deps(), id)).rejects.toThrow("无法查询来源下载器");
});
it("partial records exclude successful files while leaving failed files available", async () => {
const { h, id } = await setup();
await writeFile(join(h.dl, "success.mkv"), "successful video content");
await writeFile(join(h.dl, "failure.mkv"), "failed video content");
const record = await h.db
.insertInto("organization_record")
.values({ trigger: "manual", actor_type: "system", action: "copy", status: "partial" })
.returning("id")
.executeTakeFirstOrThrow();
await h.db
.insertInto("organization_file_result")
.values([
{
organization_record_id: record.id,
ordinal: 0,
mode: "copy",
source_path: join(h.dl, "success.mkv"),
status: "succeeded",
},
{
organization_record_id: record.id,
ordinal: 1,
mode: "copy",
source_path: join(h.dl, "failure.mkv"),
status: "failed",
},
])
.execute();
expect(await scanStock(h.module.deps(), id)).toMatchObject({
items: [{ root: h.dlRoot, path: "failure.mkv" }],
skipped: 1,
});
});
it("historical success excludes only matching source facts, so replacement files remain candidates", async () => {
const { h, id } = await setup();
const names = ["same.mkv", "size-changed.mkv", "mtime-changed.mkv"];
await Promise.all(names.map((n) => writeFile(join(h.dl, n), "video content")));
const facts = Object.fromEntries(
await Promise.all(
names.map(async (n) => {
const st = await stat(join(h.dl, n));
return [join(h.dl, n), { size: st.size, mtimeMs: st.mtimeMs }];
}),
),
);
facts[join(h.dl, "size-changed.mkv")]!.size -= 1;
facts[join(h.dl, "mtime-changed.mkv")]!.mtimeMs -= 1;
const record = await h.db
.insertInto("organization_record")
.values({
trigger: "manual",
actor_type: "system",
action: "copy",
status: "succeeded",
plan: JSON.stringify({ sourceFacts: facts }),
})
.returning("id")
.executeTakeFirstOrThrow();
await h.db
.insertInto("organization_file_result")
.values(
names.map((n, ordinal) => ({
organization_record_id: record.id,
ordinal,
mode: "copy" as const,
source_path: join(h.dl, n),
source_size_bytes: facts[join(h.dl, n)]!.size,
status: "succeeded" as const,
})),
)
.execute();
const stock = await scanStock(h.module.deps(), id);
expect(stock.skipped).toBe(1);
expect(stock.items.map((f) => f.path).sort()).toEqual(["mtime-changed.mkv", "size-changed.mkv"]);
});
it("stock batches stay within the identify wizard limit", async () => {
const { h, id } = await setup();
await Promise.all(Array.from({ length: 201 }, (_, n) => writeFile(join(h.dl, `stock-${n}.mkv`), "video content")));
const stock = await scanStock(h.module.deps(), id);
expect(stock.items).toHaveLength(200);
expect(stock.truncated).toBe(true);
expect(await h.db.selectFrom("organize_source_snapshot").selectAll().execute()).toEqual([]);
});
it("stock omits successfully organized files", async () => {
const { h, id } = await setup();
const path = join(h.dl, "organized.mkv");
await writeFile(path, "organized video content");
const record = await h.db
.insertInto("organization_record")
.values({ trigger: "manual", actor_type: "system", action: "copy", status: "succeeded" })
.returning("id")
.executeTakeFirstOrThrow();
await h.db
.insertInto("organization_file_result")
.values({ organization_record_id: record.id, mode: "copy", source_path: path, status: "succeeded" })
.execute();
expect(await scanStock(h.module.deps(), id)).toMatchObject({ items: [], skipped: 1 });
});
it("source edits reset baseline; identical categories coexist; deleting removes state", async () => {
const { h, id, config } = await setup();
await mkdir(join(h.dl, "other"));
await scanOrganizeSource(h.module.deps(), { sourceId: id });
await saveOrganizeSource(h.module.deps(), randomUUID(), { ...config, category: "电影" });
await saveOrganizeSource(h.module.deps(), id, {
...config,
category: "电影",
source: { ...config.source, path: "other" },
});
expect((await listOrganizeSources(h.module.deps())).items).toHaveLength(2);
expect(
await h.db.selectFrom("organize_source_snapshot").selectAll().where("source_id", "=", id).executeTakeFirst(),
).toBeUndefined();
await scanOrganizeSource(h.module.deps(), { sourceId: id });
await deleteOrganizeSource(h.module.deps(), id);
expect(
await h.db.selectFrom("organize_source_snapshot").selectAll().where("source_id", "=", id).executeTakeFirst(),
).toBeUndefined();
});
});
+3 -3
View File
@@ -224,7 +224,7 @@ describe.skipIf(skip)("files W5-A2", () => {
dryRun: true,
items: [
{ root: h.dlRoot, path: "weird-a.mkv", mediaEntityId: series, season: 2, episodes: [3, 4] },
{ root: h.dlRoot, path: "weird-b.mkv", mediaEntityId: series, season: 2, episodes: [5, 7] },
{ root: h.dlRoot, path: "weird-b.mkv", mediaEntityId: series, season: 2, episodes: [5, 6] },
{
root: h.dlRoot,
path: "Film.2019.1080p.mkv",
@@ -243,8 +243,8 @@ describe.skipIf(skip)("files W5-A2", () => {
const mv = groups.find((g: { mediaEntityId: string }) => g.mediaEntityId === film);
const tvTargets = tv.files.map((f: { target: string | null }) => f.target);
expect(tvTargets.some((t: string | null) => t?.endsWith("Season 2/剧 - S02E03-E04 - 第 3-4 集.mkv"))).toBe(true);
// 非连续集号不应用(只按季覆写,文件名无集号 → 计划排除),其余覆写不进 warnings
expect(tv.warnings).toEqual([expect.stringMatching(/weird-b\.mkv:集号 5,7 不连续/)]);
expect(tvTargets.some((t: string | null) => t?.endsWith("Season 2/剧 - S02E05-E06 - 第 5-6 集.mkv"))).toBe(true);
expect(tv.warnings).toEqual([]);
expect(mv.warnings).toEqual([expect.stringMatching(/电影不适用季\/集覆写/)]);
expect(mv).toMatchObject({ action: "copy", slotId: slot.id, targetDir: join(h.lib, "picked") });
expect(mv.files[0].target).toBe(join(h.lib, "picked", "Film (2019) - 1080p.mkv"));