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:
1 parent
8013196e3f
commit
ceb66f8c77
265 files changed
+17751
-2483
No files matched your search
@@ -37,6 +37,24 @@ describe.skipIf(!hasPg)("POST /engine/runs/{id}/retry", () => {
|
||||
await h?.close();
|
||||
});
|
||||
|
||||
it("工作流多值筛选保留排队核验与独立刮削,单值筛选兼容", async () => {
|
||||
const table = sql.raw(`"${h.engineSchema}".workflow_runs`);
|
||||
await sql`insert into ${table} (namespace_id, id, workflow_name, version, status, config, context, input, attempts, created_at, updated_at)
|
||||
values ('default', 'scrape-queue', 'scrapeMedia', 'v2', 'pending', '{}'::jsonb, null, '{"input":{}}'::jsonb, 0, now(), now())`.execute(
|
||||
h.db,
|
||||
);
|
||||
try {
|
||||
const both = await json(
|
||||
await admin("GET", "/engine/runs?workflowName=verifyAndImport,scrapeMedia&status=pending"),
|
||||
);
|
||||
expect(both.data.items.map((run: { id: string }) => run.id).sort()).toEqual(["p1", "scrape-queue"]);
|
||||
const single = await json(await admin("GET", "/engine/runs?workflowName=verifyAndImport&status=pending"));
|
||||
expect(single.data.items.map((run: { id: string }) => run.id)).toEqual(["p1"]);
|
||||
} finally {
|
||||
await sql`delete from ${table} where id = 'scrape-queue'`.execute(h.db);
|
||||
}
|
||||
});
|
||||
|
||||
it("失败的 run:按原任务名与原始输入重新入队,序号递增,写审计", async () => {
|
||||
const r1 = await json(await admin("POST", "/engine/runs/f1/retry"));
|
||||
expect(r1.data).toEqual({ runId: "new-1", retryOf: "f1", attempt: 1 });
|
||||
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
waitFor,
|
||||
} from "./harness.ts";
|
||||
|
||||
// E10(原编号与 e6-auto-recover 重复,W6-B 改名) W4-A 前后端集成端点:手动提交下载(与自动同流程)、候选评估去重、库存叠加补字段、
|
||||
// E10(原编号与 e6-auto-recover 重复,W6-B 改名) W4-A 前后端集成端点:独立手动下载与自动订阅下载分别验证、候选评估去重、库存叠加补字段、
|
||||
// 整理记录 downloadTaskId、只读资产/产物端点、偏好标签修订历史、列表带作品标题、事件 JSON 补读。
|
||||
|
||||
const hasPg = await pgAvailable();
|
||||
@@ -29,13 +29,17 @@ describe.skipIf(!hasPg)("E10 W4-A 集成端点", () => {
|
||||
await e?.close();
|
||||
}, 60_000);
|
||||
|
||||
it("POST /acquisition-plans:硬条件不满足 → 不提交并给出原因;重复评估只留一条候选记录", async () => {
|
||||
it("download.plan:自动订阅硬条件不满足 → 无计划并给出原因;重复评估只留一条候选记录", async () => {
|
||||
for (let i = 0; i < 2; i++) {
|
||||
const r = await e.api("POST", "/acquisition-plans", { sourceReleaseId: target.r720, slotId: target.slotId });
|
||||
const r = await e.api("POST", "/capabilities/download.plan/invoke", {
|
||||
input: { sourceReleaseId: target.r720, slotId: target.slotId },
|
||||
});
|
||||
expect(r.status).toBe(200);
|
||||
expect(r.body.data).toMatchObject({ submitted: false, plan: null, taskId: null });
|
||||
expect(r.body.data.rejected.length).toBeGreaterThan(0);
|
||||
expect(r.body.data.rejected[0].slotId).toBe(target.slotId);
|
||||
const out = r.body.data.output ?? r.body.data;
|
||||
expect(out.plan).toBeNull();
|
||||
expect(out.slot.id).toBe(target.slotId);
|
||||
expect(out.rejected.length).toBeGreaterThan(0);
|
||||
expect(await e.db.selectFrom("download_task").select("id").execute()).toHaveLength(0);
|
||||
}
|
||||
const n = await sql<{ n: number }>`select count(*)::int as n from artifact
|
||||
where title = 'candidate_evaluation' and subject_id = ${target.slotId}
|
||||
@@ -53,23 +57,30 @@ describe.skipIf(!hasPg)("E10 W4-A 集成端点", () => {
|
||||
expect(c.reasons.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("POST /acquisition-plans:按作品选 Slot,走 download.plan → download.submit,qB 收到种子;整理记录带 downloadTaskId", async () => {
|
||||
const r = await e.api("POST", "/acquisition-plans", {
|
||||
sourceReleaseId: target.r1080,
|
||||
entityId: target.entityId,
|
||||
note: "工作台手动",
|
||||
it("自动订阅 download.plan → download.submit 推进 Slot,qB 收到种子;整理记录带 downloadTaskId", async () => {
|
||||
const planned = await e.api("POST", "/capabilities/download.plan/invoke", {
|
||||
input: { sourceReleaseId: target.r1080, slotId: target.slotId },
|
||||
});
|
||||
expect(r.status).toBe(200);
|
||||
expect(r.body.data).toMatchObject({ submitted: true, slotId: target.slotId });
|
||||
expect(r.body.data.plan.status).toMatch(/reserved|submitted/);
|
||||
expect(r.body.data.taskId).toBeTruthy();
|
||||
expect(planned.status).toBe(200);
|
||||
const out = planned.body.data.output ?? planned.body.data;
|
||||
expect(out.plan).toMatchObject({ slotId: target.slotId, sourceReleaseId: target.r1080 });
|
||||
const grant = await e.api("POST", "/automation-grants", {
|
||||
action: "download.submit",
|
||||
scope: { mediaEntityIds: [target.entityId] },
|
||||
reason: "e10 自动订阅收录验证",
|
||||
});
|
||||
expect(grant.status).toBe(201);
|
||||
const submitted = await e.api("POST", "/capabilities/download.submit/invoke", { input: { planId: out.plan.id } });
|
||||
expect(submitted.status).toBe(200);
|
||||
const accepted = submitted.body.data.output ?? submitted.body.data;
|
||||
expect(accepted.planId).toBe(out.plan.id);
|
||||
expect(accepted.taskId).toBeTruthy();
|
||||
const audits = await e.db
|
||||
.selectFrom("audit_event")
|
||||
.select(["action", "target_id"])
|
||||
.where("action", "in", ["capability.invoke", "acquisition_plan.manual_submit"])
|
||||
.where("action", "=", "capability.invoke")
|
||||
.execute();
|
||||
expect(audits.filter((a) => a.target_id === "download.submit")).toHaveLength(1);
|
||||
expect(audits.some((a) => a.action === "acquisition_plan.manual_submit")).toBe(true);
|
||||
|
||||
await completeDownload(e, "Dune.Part.Two.2024.1080p.BluRay.x264-GRP");
|
||||
const rec = await waitFor(
|
||||
@@ -104,6 +115,80 @@ describe.skipIf(!hasPg)("E10 W4-A 集成端点", () => {
|
||||
expect(slots.body.data.items[0].currentAsset).toMatchObject({ resolution: "1080p" });
|
||||
});
|
||||
|
||||
it("POST /acquisition-plans:没有订阅和 Slot 的作品也能独立手动下载", async () => {
|
||||
const standalone = await e.db
|
||||
.insertInto("media_entity")
|
||||
.values({ kind: "movie", title: "独立手动电影", year: 2025, status: "active" })
|
||||
.returning("id")
|
||||
.executeTakeFirstOrThrow();
|
||||
const site = await e.db
|
||||
.selectFrom("source_release")
|
||||
.select("site_id")
|
||||
.where("id", "=", target.r720)
|
||||
.executeTakeFirstOrThrow();
|
||||
const release = await e.db
|
||||
.insertInto("source_release")
|
||||
.values({
|
||||
site_id: site.site_id,
|
||||
site_release_id: "880003",
|
||||
title: "Manual.Standalone.2025.720p.WEB-DL.x264-GRP",
|
||||
size_bytes: 300000,
|
||||
media_entity_id: standalone.id,
|
||||
resolution_status: "resolved",
|
||||
raw: JSON.stringify({ _site: { hr: false } }),
|
||||
})
|
||||
.returning("id")
|
||||
.executeTakeFirstOrThrow();
|
||||
const before = await e.db.selectFrom("subscription").select("id").execute();
|
||||
const slotBefore = await e.db
|
||||
.selectFrom("acquisition_slot")
|
||||
.select(["current_asset_id", "revision"])
|
||||
.where("id", "=", target.slotId)
|
||||
.executeTakeFirstOrThrow();
|
||||
const r = await e.api("POST", "/acquisition-plans", {
|
||||
sourceReleaseId: release.id,
|
||||
entityId: standalone.id,
|
||||
note: "工作台独立手动",
|
||||
});
|
||||
expect(r.status).toBe(200);
|
||||
expect(r.body.data).toMatchObject({
|
||||
submitted: true,
|
||||
slotId: null,
|
||||
rejected: [],
|
||||
plan: { slotId: null, evaluation: { manual: true, mediaEntityId: standalone.id } },
|
||||
});
|
||||
expect(r.body.data.taskId).toBeTruthy();
|
||||
await waitFor(
|
||||
async () =>
|
||||
e.db
|
||||
.selectFrom("download_task")
|
||||
.selectAll()
|
||||
.where("import_plan_id", "=", r.body.data.plan.id)
|
||||
.executeTakeFirst(),
|
||||
60000,
|
||||
"独立手动下载已提交",
|
||||
);
|
||||
expect(await e.db.selectFrom("subscription").select("id").execute()).toEqual(before);
|
||||
expect(
|
||||
await e.db.selectFrom("acquisition_slot").select("id").where("media_entity_id", "=", standalone.id).execute(),
|
||||
).toHaveLength(0);
|
||||
expect(
|
||||
await e.db
|
||||
.selectFrom("acquisition_slot")
|
||||
.select(["current_asset_id", "revision"])
|
||||
.where("id", "=", target.slotId)
|
||||
.executeTakeFirstOrThrow(),
|
||||
).toEqual(slotBefore);
|
||||
expect(
|
||||
await e.db
|
||||
.selectFrom("audit_event")
|
||||
.select("id")
|
||||
.where("action", "=", "acquisition_plan.manual_submit")
|
||||
.where("target_id", "=", r.body.data.plan.id)
|
||||
.executeTakeFirst(),
|
||||
).toBeTruthy();
|
||||
}, 90000);
|
||||
|
||||
it("GET /artifacts:列表与详情", async () => {
|
||||
const list = await e.api("GET", "/artifacts?kind=evidence");
|
||||
expect(list.status).toBe(200);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { CAPABILITY_IDS } from "@umt/contracts";
|
||||
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
||||
import { delegateScript } from "./fixtures/scripts.ts";
|
||||
import {
|
||||
@@ -45,8 +46,16 @@ describe.skipIf(!hasPg)("E2 Agent 委托闭环(fake 模型 + 真实能力/授权
|
||||
return r.body.data.id as string;
|
||||
};
|
||||
|
||||
it("启动断言:41 个能力全部注册,并在能力目录里可见", async () => {
|
||||
expect(e.booted.ctx.capabilities.list()).toHaveLength(41);
|
||||
it("启动断言:合同中的能力全部注册,整理兜底不可重放,并在能力目录里可见", async () => {
|
||||
expect(
|
||||
e.booted.ctx.capabilities
|
||||
.list()
|
||||
.map((c) => c.descriptor.id)
|
||||
.sort(),
|
||||
).toEqual([...CAPABILITY_IDS].sort());
|
||||
expect(
|
||||
e.booted.ctx.capabilities.list().find((c) => c.descriptor.id === "metadata.resolve_organize")?.descriptor,
|
||||
).toMatchObject({ kind: "execute", replay: "never" });
|
||||
const r = await e.api("GET", "/capabilities");
|
||||
expect(r.body.data.items.map((c: { id: string }) => c.id).sort()).toEqual(
|
||||
e.booted.ctx.capabilities
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { createQbClient } from "@umt/adapters";
|
||||
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
||||
import type { createAcquisitionOrganizeModule } from "../../src/modules/acquisition-organize/index.ts";
|
||||
import { evaluateAcquisition } from "../../src/modules/acquisition-organize/services/acquisition.ts";
|
||||
import { syncDownloads } from "../../src/modules/acquisition-organize/services/downloads.ts";
|
||||
import { torrentFor } from "./fixtures/modules.ts";
|
||||
import { createE2E, type E2E, pgAvailable, seedBase, waitFor } from "./harness.ts";
|
||||
@@ -81,9 +82,11 @@ describe.skipIf(!hasPg)("E8 AT24 标签移除 / 任务消失", () => {
|
||||
}, 60_000);
|
||||
|
||||
const submit = async (k: K) => {
|
||||
const r = await e.api("POST", "/acquisition-plans", { sourceReleaseId: ids[k].releaseId });
|
||||
expect(r.status, JSON.stringify(r.body)).toBe(200);
|
||||
expect(r.body.data.submitted).toBe(true);
|
||||
const planned = await evaluateAcquisition(deps(), {
|
||||
sourceReleaseId: ids[k].releaseId,
|
||||
acquisitionSlotId: ids[k].slotId,
|
||||
});
|
||||
expect(planned.plans, JSON.stringify(planned)).toHaveLength(1);
|
||||
await waitFor(async () => e.qb.get(ids[k].hash), 60_000, `qB 收到 ${k}`);
|
||||
const row = await waitFor(
|
||||
async () =>
|
||||
|
||||
@@ -254,7 +254,7 @@ describe.skipIf(!hasPg)("E9 连续更新剧集:Slot 忙时到达的集全部入
|
||||
30_000,
|
||||
"订阅完成",
|
||||
);
|
||||
expect(sub.status_reason).toBe("slot_satisfied");
|
||||
expect(sub.status_reason).toBe("download_accepted");
|
||||
const slot = await e.db
|
||||
.selectFrom("acquisition_slot")
|
||||
.selectAll()
|
||||
@@ -263,7 +263,7 @@ describe.skipIf(!hasPg)("E9 连续更新剧集:Slot 忙时到达的集全部入
|
||||
expect(slot.status).toBe("satisfied");
|
||||
const hist = await e.api("GET", `/subscription-history?subscriptionId=${subId}`);
|
||||
expect(hist.body.data.items).toHaveLength(1);
|
||||
expect(hist.body.data.items[0]).toMatchObject({ reason: "slot_satisfied", mediaEntityId: entityId });
|
||||
expect(hist.body.data.items[0]).toMatchObject({ reason: "download_accepted", mediaEntityId: entityId });
|
||||
// 进度 API:三集已覆盖,总集数已知
|
||||
const prog = await e.api("GET", `/subscriptions/${subId}/progress`);
|
||||
expect(prog.body.data.seasons[0]).toMatchObject({ episodeTotal: 3, totalKnown: true, complete: true });
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
@@ -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"));
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
// 不挂载 HTTP 路由;隔离 Deno 静态文件适配器,以便本单元测试在 Node 下执行。
|
||||
vi.mock("hono/deno", () => ({ serveStatic: vi.fn() }));
|
||||
|
||||
import { verifyInPlace } from "../src/modules/acquisition-organize/services/organize.ts";
|
||||
|
||||
const path = "/media/unchanged.mkv";
|
||||
const frozen = {
|
||||
rootPath: "/media",
|
||||
sentinel: ".uhd-root",
|
||||
libraryRootId: "root",
|
||||
entityId: "work",
|
||||
sourceFacts: { [path]: { size: 100, mtimeMs: 200 } },
|
||||
} as unknown as Parameters<typeof verifyInPlace>[2];
|
||||
async function verify(observed: unknown, owner: string | null = null) {
|
||||
const executeTakeFirst = vi.fn().mockResolvedValue(owner ? { media_entity_id: owner } : undefined);
|
||||
const query = { innerJoin: () => query, select: () => query, where: () => query, executeTakeFirst };
|
||||
const d = { ctx: { db: { selectFrom: () => query } } } as unknown as Parameters<typeof verifyInPlace>[0];
|
||||
const statSafe = vi.fn().mockResolvedValue(observed);
|
||||
// 仅提供 statSafe;若意外调用复制、移动、清理或写入,本测试立即失败。
|
||||
const fsr = { proxy: { statSafe } } as unknown as Parameters<typeof verifyInPlace>[1];
|
||||
return { result: await verifyInPlace(d, fsr, frozen, path, 100), statSafe, executeTakeFirst };
|
||||
}
|
||||
const observed = (extra = {}) => ({
|
||||
kind: "ok",
|
||||
facts: { isFile: true, isSymbolicLink: false, size: 100, mtimeMs: 200, ...extra },
|
||||
});
|
||||
describe("原地登记执行核验", () => {
|
||||
it("只读核验,不复制或移动文件;同作品重复登记可继续", async () => {
|
||||
const r = await verify(observed(), "work");
|
||||
expect(r.result).toEqual({ status: "succeeded", bytes: 0 });
|
||||
expect(r.statSafe).toHaveBeenCalledWith(path, { root: "/media", sentinel: ".uhd-root" });
|
||||
});
|
||||
it("文件消失、链接、变为目录或计划后的大小/时间变化均不登记", async () => {
|
||||
for (const value of [
|
||||
{ kind: "missing" },
|
||||
observed({ isSymbolicLink: true }),
|
||||
observed({ isFile: false }),
|
||||
observed({ size: 101 }),
|
||||
observed({ mtimeMs: 201 }),
|
||||
]) {
|
||||
const r = await verify(value);
|
||||
expect(r.result.status).toBe("failed");
|
||||
expect(r.executeTakeFirst).not.toHaveBeenCalled();
|
||||
}
|
||||
});
|
||||
it("不会把已有视频悄悄登记为另一个作品", async () => {
|
||||
expect((await verify(observed(), "other")).result).toMatchObject({
|
||||
status: "failed",
|
||||
error: { code: "ASSET_ENTITY_CONFLICT" },
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -102,7 +102,7 @@ describe.skipIf(!hasPg)("Pi 上游(真实 PG)", () => {
|
||||
.plaintextKey as string;
|
||||
expect((await bearer(h.app, readKey)("GET", "/pi/upstreams")).status).toBe(403);
|
||||
const cat = await json(await key("GET", "/pi/providers?models=false"));
|
||||
expect(cat.data.providers).toHaveLength(43);
|
||||
expect(cat.data.providers).toHaveLength(44);
|
||||
const anthropic = cat.data.providers.find((p: { id: string }) => p.id === "anthropic");
|
||||
expect(anthropic.termsNote).toMatch(/封禁/);
|
||||
});
|
||||
|
||||
@@ -15,6 +15,7 @@ import { createOutboxPort } from "../../src/context.ts";
|
||||
import { createApp } from "../../src/http/app.ts";
|
||||
import { EventBus } from "../../src/http/sse.ts";
|
||||
import { createAcquisitionOrganizeModule } from "../../src/modules/acquisition-organize/index.ts";
|
||||
import { createCapabilityRegistry } from "../../src/modules/agent/capability-registry.ts";
|
||||
import { createAgentModule } from "../../src/modules/agent/index.ts";
|
||||
import { createSearchModule } from "../../src/modules/search/index.ts";
|
||||
import { type BaseRelease, filterReleases, quickFilter } from "../../src/modules/search/releases.ts";
|
||||
@@ -149,7 +150,7 @@ describe.skipIf(!up)("全局搜索", () => {
|
||||
outbox: createOutboxPort(),
|
||||
secrets,
|
||||
settings: plainSettings(() => db),
|
||||
capabilities: {} as never,
|
||||
capabilities: createCapabilityRegistry({ logger }),
|
||||
} as ServerContext;
|
||||
// 挂起的站点:请求一直不返回,直到被中止(单站超时)
|
||||
const fetchImpl: typeof fetch = (input, init) => {
|
||||
@@ -208,6 +209,28 @@ describe.skipIf(!up)("全局搜索", () => {
|
||||
await addAccount(sites.mteam, "api_key", mteam.apiKey);
|
||||
sites.slow = await addSite("hdsky", "天空", "nexusphp", `http://${SLOW_HOST}/`);
|
||||
await addAccount(sites.slow, "cookie", "uid=1; pass=x");
|
||||
const downloader = await db
|
||||
.insertInto("downloader_instance")
|
||||
.values({
|
||||
name: "search-download",
|
||||
base_url: "http://downloader.invalid",
|
||||
cache_volume_key: "search-cache",
|
||||
settings: JSON.stringify({ savePath: "/downloads" }),
|
||||
})
|
||||
.returning("id")
|
||||
.executeTakeFirstOrThrow();
|
||||
await db
|
||||
.insertInto("path_mapping")
|
||||
.values({
|
||||
downloader_instance_id: downloader.id,
|
||||
view_kind: "downloader",
|
||||
remote_prefix: "/downloads",
|
||||
local_prefix: "/tmp/umt-search-cache",
|
||||
media_server_instance_id: null,
|
||||
library_root_id: null,
|
||||
verified_at: null,
|
||||
})
|
||||
.execute();
|
||||
// 已关闭的站点不参与搜索
|
||||
await db
|
||||
.insertInto("site")
|
||||
@@ -561,12 +584,16 @@ describe.skipIf(!up)("全局搜索", () => {
|
||||
});
|
||||
|
||||
it("从搜索结果下载:转调 /acquisition-plans(同一鉴权、同一拒绝原因)", async () => {
|
||||
// 未识别的资源 → 与 POST /acquisition-plans 相同的 412
|
||||
// 未识别资源先返回一次结构化确认,不阻断后续手动下载
|
||||
const unresolved = await req(`/search/releases/${sites.nexus}/900001/plan`, { body: {} });
|
||||
expect(unresolved.status).toBe(412);
|
||||
expect(unresolved.body.error.code).toBe("precondition_failed");
|
||||
expect(unresolved.status, JSON.stringify(unresolved.body)).toBe(200);
|
||||
expect(unresolved.body.data).toMatchObject({
|
||||
submitted: false,
|
||||
requiresConfirmation: true,
|
||||
rejected: [{ code: "unrecognized" }],
|
||||
});
|
||||
|
||||
// 已识别但作品没有收录版本 → submitted=false + no_slot
|
||||
// 已识别资源无需订阅或收录版本,独立计划提交
|
||||
const other = (
|
||||
await db
|
||||
.insertInto("media_entity")
|
||||
@@ -582,7 +609,13 @@ describe.skipIf(!up)("全局搜索", () => {
|
||||
.execute();
|
||||
const ok = await req(`/search/releases/${sites.mteam}/912234/plan`, { body: { note: "from search" } });
|
||||
expect(ok.status).toBe(200);
|
||||
expect(ok.body.data).toMatchObject({ submitted: false, plan: null, rejected: [{ code: "no_slot" }] });
|
||||
expect(ok.body.data).toMatchObject({ submitted: true, slotId: null, plan: { slotId: null }, rejected: [] });
|
||||
expect(await db.selectFrom("subscription").selectAll().where("media_entity_id", "=", other).execute()).toHaveLength(
|
||||
0,
|
||||
);
|
||||
expect(
|
||||
await db.selectFrom("acquisition_slot").selectAll().where("media_entity_id", "=", other).execute(),
|
||||
).toHaveLength(0);
|
||||
|
||||
// 不在搜索结果里
|
||||
const missing = await req(`/search/releases/${sites.mteam}/nope/plan`, { body: {} });
|
||||
|
||||
@@ -316,6 +316,28 @@ describe.skipIf(!pg)("ID-3 识别升级链:详情页 ID", () => {
|
||||
expect(siteRequests.some((x) => x.includes("douban"))).toBe(false);
|
||||
});
|
||||
|
||||
it("升级链没认出(豆瓣召回无匹配)时保留原有歧义:候选不丢,照常开有歧义的事项", async () => {
|
||||
await saveDoubanSubject(h.ctx, {
|
||||
id: "36000097",
|
||||
title: "查无此片",
|
||||
aliases: [],
|
||||
// 年份对不上:按豆瓣条目召回 TMDB 拿不到候选
|
||||
year: 1990,
|
||||
source: "web_snippet",
|
||||
ref: "x",
|
||||
at: new Date().toISOString(),
|
||||
});
|
||||
const r = await release(audiences, "725859", "Rose 2026 2160p WEB-DL H265-ADWeb", {
|
||||
_detail: { doubanId: "36000097" },
|
||||
});
|
||||
await h.db.updateTable("source_release").set({ detail_fetched_at: new Date() }).where("id", "=", r.id).execute();
|
||||
const res = await resolveRelease(h.rt, { sourceReleaseId: r.id, trigger: "manual" });
|
||||
expect(res.outcome).toBe("ambiguous");
|
||||
expect(res.decisionId).not.toBeNull();
|
||||
const d = await h.db.selectFrom("decision").select("kind").where("id", "=", res.decisionId!).executeTakeFirst();
|
||||
expect(d?.kind).toBe("identity_ambiguous");
|
||||
});
|
||||
|
||||
it("池条目补全前先走升级链:详情页给了 ID 就直接认出,返回 409,不做网页补全", async () => {
|
||||
const r = await release(hhan, "91007", "Example Show S01 2026 2160p YK WEB-DL H.265 DV HQ AAC2.0-HHWEB");
|
||||
const first = await resolveRelease(h.rt, { sourceReleaseId: r.id, trigger: "manual", escalate: false });
|
||||
|
||||
@@ -0,0 +1,223 @@
|
||||
import { workNfo } from "@umt/domain";
|
||||
import { createMockTmdb } from "@umt/testkit";
|
||||
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
||||
import { imagePathsOf, nfoWork } from "../../src/modules/acquisition-organize/services/scrape-input.ts";
|
||||
import { saveDetailPage } from "../../src/modules/sources-identity/service/detail-page.ts";
|
||||
import {
|
||||
ptDetailFacts,
|
||||
resolveOrganizeMetadata,
|
||||
} from "../../src/modules/sources-identity/service/organize-metadata.ts";
|
||||
import { createHarness, type Harness, pgAvailable, seedSite } from "./harness.ts";
|
||||
|
||||
const ptText =
|
||||
"◎译 名 未收录的故事\n◎原 名 Unlisted Story\n◎年 代 2025\n◎产 地 中国大陆\n◎语 言 汉语普通话\n◎类 别 剧情\n◎导 演 张三\n◎主 演 李四 / 王五\n◎简 介 一位主人公在旅途中发现了一段不为人知的故事,并开始寻找失踪的朋友。";
|
||||
const up = await pgAvailable();
|
||||
describe("PT detail extraction", () => {
|
||||
it("reads evidenced labels and leaves missing fields empty", () => {
|
||||
const facts = ptDetailFacts(ptText, "https://pt.example/details.php?id=1");
|
||||
expect(facts.names).toEqual(["未收录的故事", "Unlisted Story"]);
|
||||
expect(facts.year).toBe(2025);
|
||||
expect(facts.extras).toContainEqual({ field: "originCountry", value: ["CN"] });
|
||||
expect(facts.extras).toContainEqual({ field: "originalLanguage", value: "zh" });
|
||||
expect(ptDetailFacts("Unknown.2025.1080p", "https://pt.example/").candidates).toEqual([]);
|
||||
});
|
||||
it("local final fields reuse NFO input and external poster", () => {
|
||||
const metadata = {
|
||||
overview: { value: "简介" },
|
||||
posterUrl: { value: "https://img.example/cover.jpg" },
|
||||
originCountry: { value: ["CN"] },
|
||||
cast: { value: [{ id: -1, name: "本地主演", role: null, profilePath: null }] },
|
||||
crew: { value: [{ id: -2, name: "本地导演", role: "Director", profilePath: null }] },
|
||||
};
|
||||
expect(imagePathsOf(metadata).posterPath).toBe("https://img.example/cover.jpg");
|
||||
const nfo = nfoWork({ kind: "movie", title: "本地作品", year: 2025, original_title: null, metadata }, {});
|
||||
expect(nfo.ids.tmdb).toBeNull();
|
||||
expect(nfo.overview).toBe("简介");
|
||||
expect(nfo.countries).toEqual(["中国"]);
|
||||
expect(nfo.actors?.[0]?.tmdbId).toBeNull();
|
||||
expect(nfo.directors?.[0]?.tmdbId).toBeNull();
|
||||
const xml = workNfo(nfo);
|
||||
expect(xml).toContain("本地主演");
|
||||
expect(xml).toContain("本地导演");
|
||||
expect(xml).not.toContain("-1");
|
||||
expect(xml).not.toContain("-2");
|
||||
});
|
||||
});
|
||||
describe.skipIf(!up)("organize metadata local recovery (isolated PG)", () => {
|
||||
let h: Harness;
|
||||
let siteId: string;
|
||||
beforeAll(async () => {
|
||||
h = await createHarness("local_metadata", { tmdb: () => null });
|
||||
siteId = (await seedSite(h, { baseUrl: "https://pt.example/" })).site.id;
|
||||
});
|
||||
afterAll(async () => {
|
||||
await h?.close();
|
||||
});
|
||||
const add = async (n: string, text: string, title?: string) => {
|
||||
const release = await h.db
|
||||
.insertInto("source_release")
|
||||
.values({
|
||||
site_id: siteId,
|
||||
site_release_id: n,
|
||||
title: title ?? `Unlisted Story ${n} 2025 1080p`,
|
||||
detail_fetched_at: new Date(),
|
||||
})
|
||||
.returning("id")
|
||||
.executeTakeFirstOrThrow();
|
||||
await saveDetailPage(
|
||||
h.rt,
|
||||
h.db,
|
||||
release.id,
|
||||
{
|
||||
descriptionText: text,
|
||||
mediaInfo: null,
|
||||
screenshots: ["https://img.example/cover.jpg"],
|
||||
imdbId: null,
|
||||
doubanId: null,
|
||||
tmdb: null,
|
||||
files: [],
|
||||
tags: [],
|
||||
uploader: null,
|
||||
probe: { labels: [], hasDescription: true, imageCount: 1 },
|
||||
},
|
||||
3600,
|
||||
);
|
||||
return release.id;
|
||||
};
|
||||
const deps = {
|
||||
search: async () => [],
|
||||
fetch: async () => {
|
||||
throw new Error("unexpected fetch");
|
||||
},
|
||||
complete: async () => null,
|
||||
};
|
||||
it("creates Local with sourced fields, remains revisitable and reuses identity", async () => {
|
||||
const id = await add("1", ptText);
|
||||
const out = await resolveOrganizeMetadata(h.rt, id, deps, { actor: { type: "system", id: "system:organize" } });
|
||||
expect(out.mode).toBe("local");
|
||||
expect(out.mediaEntityId).toBeTruthy();
|
||||
const entity = await h.db
|
||||
.selectFrom("media_entity")
|
||||
.selectAll()
|
||||
.where("id", "=", out.mediaEntityId!)
|
||||
.executeTakeFirstOrThrow();
|
||||
expect(entity.status).toBe("provisional");
|
||||
expect(entity.title).toBe("未收录的故事");
|
||||
expect((entity.metadata as Record<string, { value: unknown }>).originCountry?.value).toEqual(["CN"]);
|
||||
expect((entity.metadata as Record<string, { value: unknown }>).posterUrl?.value).toBe(
|
||||
"https://img.example/cover.jpg",
|
||||
);
|
||||
const release = await h.db.selectFrom("source_release").selectAll().where("id", "=", id).executeTakeFirstOrThrow();
|
||||
expect(release.resolution_locked).toBe(false);
|
||||
const again = await resolveOrganizeMetadata(h.rt, id, deps, { actor: { type: "system", id: "system:organize" } });
|
||||
expect(again.mediaEntityId).toBe(out.mediaEntityId);
|
||||
});
|
||||
it("series fallback preserves actual season and episode instead of fabricating season one", async () => {
|
||||
const id = await add(
|
||||
"season",
|
||||
"◎译 名 独立剧集\n◎年 代 2025\n◎集 数 12\n◎类 别 剧情",
|
||||
"Distinct Local Series S02E03 2025 1080p",
|
||||
);
|
||||
const out = await resolveOrganizeMetadata(h.rt, id, deps, { actor: { type: "system", id: "system:organize" } });
|
||||
expect(out.mode).toBe("local");
|
||||
const units = await h.db
|
||||
.selectFrom("content_unit")
|
||||
.select(["season_number", "episode_number"])
|
||||
.where("media_entity_id", "=", out.mediaEntityId!)
|
||||
.execute();
|
||||
expect(units.some((u) => u.season_number === 1)).toBe(false);
|
||||
expect(units.some((u) => u.season_number === 2 && u.episode_number === 3)).toBe(true);
|
||||
});
|
||||
it("does not create a filename placeholder when facts are insufficient", async () => {
|
||||
const id = await add("2", "no metadata available");
|
||||
const out = await resolveOrganizeMetadata(h.rt, id, deps, { actor: { type: "system", id: "system:organize" } });
|
||||
expect(out.mediaEntityId).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe.skipIf(!up)("organize metadata translated name recovery", () => {
|
||||
it("retries TMDB using evidenced PT aliases after normal title lookup fails", async () => {
|
||||
const tmdb = await createMockTmdb({
|
||||
fixtures: [
|
||||
{
|
||||
type: "movie",
|
||||
id: 990087,
|
||||
original_title: "True Original",
|
||||
original_language: "en",
|
||||
origin_country: ["US"],
|
||||
date: "2025-01-01",
|
||||
titles: { "zh-CN": "正确译名", "en-US": "True Original" },
|
||||
popularity: 10,
|
||||
alternative_titles: [],
|
||||
external_ids: { imdb_id: null },
|
||||
},
|
||||
],
|
||||
});
|
||||
const h = await createHarness("local_alias", { tmdbUrl: tmdb.url });
|
||||
try {
|
||||
const siteId = (await seedSite(h, { baseUrl: "https://pt.example/" })).site.id;
|
||||
const release = await h.db
|
||||
.insertInto("source_release")
|
||||
.values({
|
||||
site_id: siteId,
|
||||
site_release_id: "alias",
|
||||
title: "Unknown Translation 2025 1080p",
|
||||
detail_fetched_at: new Date(),
|
||||
})
|
||||
.returning("id")
|
||||
.executeTakeFirstOrThrow();
|
||||
await saveDetailPage(
|
||||
h.rt,
|
||||
h.db,
|
||||
release.id,
|
||||
{
|
||||
descriptionText: "◎译 名 另类译名\n◎原 名 True Original\n◎年 代 2025\n◎类 别 剧情",
|
||||
mediaInfo: null,
|
||||
screenshots: [],
|
||||
imdbId: null,
|
||||
doubanId: null,
|
||||
tmdb: null,
|
||||
files: [],
|
||||
tags: [],
|
||||
uploader: null,
|
||||
probe: { labels: [], hasDescription: true, imageCount: 0 },
|
||||
},
|
||||
3600,
|
||||
);
|
||||
let completions = 0;
|
||||
const out = await resolveOrganizeMetadata(
|
||||
h.rt,
|
||||
release.id,
|
||||
{
|
||||
search: async () => [],
|
||||
fetch: async () => {
|
||||
throw new Error("unexpected fetch");
|
||||
},
|
||||
complete: async () => {
|
||||
completions++;
|
||||
return { text: "{}", model: "fixture-agent", inputTokens: 100, outputTokens: 5 };
|
||||
},
|
||||
},
|
||||
{ actor: { type: "system", id: "system:organize" } },
|
||||
);
|
||||
expect(completions).toBe(1);
|
||||
expect(out.mode).toBe("tmdb");
|
||||
const identity = await h.db
|
||||
.selectFrom("external_identity")
|
||||
.selectAll()
|
||||
.where("media_entity_id", "=", out.mediaEntityId!)
|
||||
.where("provider", "=", "tmdb")
|
||||
.executeTakeFirstOrThrow();
|
||||
expect(identity.external_id).toBe("990087");
|
||||
const enrichment = await h.db
|
||||
.selectFrom("metadata_enrichment")
|
||||
.selectAll()
|
||||
.where("id", "=", out.enrichmentId!)
|
||||
.executeTakeFirstOrThrow();
|
||||
expect((enrichment.usage as { inputTokens: number }).inputTokens).toBe(100);
|
||||
} finally {
|
||||
await h.close();
|
||||
await tmdb.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -15,8 +15,9 @@ const TiB = 1024 ** 4;
|
||||
const SITE_IDS = ["site.list", "site.health_check", "site.refresh_login", "site.user_data"] as const;
|
||||
|
||||
describe("站点能力规格", () => {
|
||||
it("进入首批能力目录(17 + 5 只读摘要 + 4 站点 + 10 浏览器 + 2 会话交互 + 2 网页搜索 + 1 元数据补全 = 41);类别 / 风险 / 重放按约定", () => {
|
||||
expect(CAPABILITY_IDS).toHaveLength(41);
|
||||
it("进入能力目录(41 个已有能力 + 整理元数据补全);类别 / 风险 / 重放按约定", () => {
|
||||
expect(CAPABILITY_IDS).toHaveLength(42);
|
||||
expect(capabilitySpec("metadata.resolve_organize")).toMatchObject({ kind: "execute", replay: "never" });
|
||||
for (const id of SITE_IDS) expect(CAPABILITY_IDS).toContain(id);
|
||||
expect(capabilitySpec("site.list")).toMatchObject({ kind: "read", riskLevel: "low", replay: "safe" });
|
||||
expect(capabilitySpec("site.user_data")).toMatchObject({ kind: "read", riskLevel: "low", replay: "safe" });
|
||||
|
||||
Reference in new issue
Block a user