- Sites / accounts / feeds: registry view (/site-definitions), site CRUD with
enable/disable (history stays queryable, marked site_disabled), accounts and
feed URLs stored as secrets (only secret_ref / redacted urlDisplay exposed),
feed enable/disable/reset/fetch; one shared rate limiter + adapter per site.
- Tasks: ingestFeed (FeedFetchState/FeedCursor on feed_source, observation
feed+guid upsert with digest-change updates, source_release site+id
aggregation across feeds, auth failure -> account invalid + decision, no
retry, gap -> feed.gap_suspected -> budgeted backfill snapshot),
resolveObservation (parseRelease -> local recall via title_alias pg_trgm +
accepted identities -> TMDB -> resolveIdentity; auto binds entity /
provisional units / identities / aliases, otherwise identity_candidate pool
with evidence version and bounded review backoff; manual lock respected),
refreshMetadata (metadata_field + field_override, user locks win, paths
untouched), snapshotSite (site_snapshot coverage records).
- Events: feed.due, feed.gap_suspected, release.observed (self-consumed),
release.resolved, decision.created/updated; scheduler in worker start().
- Identity API: entity detail with redirects, manual mapping correction with
identity_revision, merge with identity_redirect, pool list/assign/dismiss/
retry, content unit numbering lock, aliases, external identities.
- Decisions: list/summary (by entity and kind)/detail/resolve; identity
options executed directly.
- Workbench: /sites/{id}/releases (snapshot first, coalesced live search with
backoff, origin + freshness) and /sites/{id}/releases/{rid} (volatile field
recheck times, refresh keeps data on failure).
- Capabilities: catalog.lookup, release.search, release.inspect,
identity.resolve.
- Migrations 0018-0020: site.adapter api, source_release.resolution_locked,
site_snapshot, identity_candidate, resolution_attempt, identity_redirect,
metadata_field, field_override.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
98 lines
3.7 KiB
TypeScript
98 lines
3.7 KiB
TypeScript
import { TaskRegistry } from "@umt/tasks";
|
|
import { createMockMteamApi, type MockMteamApi } from "@umt/testkit";
|
|
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
|
import { snapshotSite } from "../../src/modules/sources-identity/service/snapshot.ts";
|
|
import { createHarness, type Harness, pgAvailable } from "./harness.ts";
|
|
|
|
const up = await pgAvailable();
|
|
|
|
describe.skipIf(!up)("M-Team 快照与任务注册", () => {
|
|
let h: Harness;
|
|
let api: MockMteamApi;
|
|
let siteId: string;
|
|
|
|
beforeAll(async () => {
|
|
api = await createMockMteamApi();
|
|
h = await createHarness("mteam");
|
|
const site = await h.db
|
|
.insertInto("site")
|
|
.values({
|
|
key: "mteam",
|
|
name: "馒头",
|
|
adapter: "mteam_api",
|
|
base_url: "https://kp.m-team.cc/",
|
|
rate_limit: JSON.stringify({ minIntervalMs: 0 }),
|
|
config: JSON.stringify({ apiBaseUrl: api.baseUrl }),
|
|
})
|
|
.returningAll()
|
|
.executeTakeFirstOrThrow();
|
|
siteId = site.id;
|
|
const ref = await h.secrets.create({ purpose: "site_api_key", plaintext: api.apiKey });
|
|
await h.db
|
|
.insertInto("site_account")
|
|
.values({ site_id: siteId, label: "api", auth_kind: "api_key", secret_ref: ref })
|
|
.execute();
|
|
});
|
|
afterAll(async () => {
|
|
await h?.close();
|
|
await api?.close();
|
|
});
|
|
|
|
it("API Key 站点快照:原始分类保留,促销带观察/过期时间;key 不入库", async () => {
|
|
const r = await snapshotSite(h.rt, { siteId, kind: "periodic" });
|
|
expect(r.status).toBe("ok");
|
|
expect(r.itemCount).toBeGreaterThan(0);
|
|
const rows = await h.db.selectFrom("source_release").selectAll().where("site_id", "=", siteId).execute();
|
|
const sports = rows.find((x) => x.site_release_id === "912237");
|
|
expect(sports?.category_raw).toBe("407");
|
|
const promo = rows.find((x) => x.site_release_id === "912233")!;
|
|
expect((promo.promotion as { observedAt?: string }).observedAt).toBeTruthy();
|
|
expect(JSON.stringify(rows)).not.toContain(api.apiKey);
|
|
});
|
|
|
|
it("key 失效:账号标失效 + 待处理,快照结果 failed", async () => {
|
|
api.setFailMode("banned_key");
|
|
const r = await snapshotSite(h.rt, { siteId, kind: "manual" });
|
|
expect(r.status).toBe("failed");
|
|
const acc = await h.db
|
|
.selectFrom("site_account")
|
|
.select(["status"])
|
|
.where("site_id", "=", siteId)
|
|
.executeTakeFirstOrThrow();
|
|
expect(["invalid", "expired"]).toContain(acc.status);
|
|
const d = await h.db
|
|
.selectFrom("decision")
|
|
.selectAll()
|
|
.where("dedupe_key", "=", `site-auth:${siteId}`)
|
|
.executeTakeFirstOrThrow();
|
|
expect(d.status).toBe("open");
|
|
api.setFailMode(null);
|
|
});
|
|
|
|
it("模块注册 4 个任务与 3 条 outbox 路由;任务处理器经 step.run 调服务", async () => {
|
|
const reg = new TaskRegistry();
|
|
h.module.registerTasks(reg, h.ctx);
|
|
expect(reg.names()).toEqual(["ingestFeed", "refreshMetadata", "resolveObservation", "snapshotSite"]);
|
|
expect(Object.keys(h.module.outboxRoutes!(h.ctx)).sort()).toEqual([
|
|
"feed.due",
|
|
"feed.gap_suspected",
|
|
"release.observed",
|
|
]);
|
|
const def = reg.latest("resolveObservation");
|
|
expect(def.inputSchema.safeParse({}).success).toBe(false);
|
|
expect(def.inputSchema.safeParse({ observationId: "00000000-0000-4000-8000-000000000000" }).success).toBe(true);
|
|
const steps: string[] = [];
|
|
const out = await reg.latest("snapshotSite").handler({
|
|
input: { siteId, kind: "periodic" },
|
|
step: {
|
|
run: async (name: string, fn: () => unknown) => {
|
|
steps.push(name);
|
|
return fn();
|
|
},
|
|
},
|
|
} as never);
|
|
expect(steps).toEqual(["snapshot"]);
|
|
expect(out).toMatchObject({ status: "skipped", reason: "auth_invalid" });
|
|
});
|
|
});
|