- root deno.json workspace (nodeModulesDir auto), npm versions registered once in imports, deno.lock; remove pnpm-lock/pnpm-workspace/root package.json; members keep minimal package.json (name/type/exports) - tasks: dev, typecheck (tsc 7.0.2 per member + deno check), lint/fmt (biome, useImportExtensions), test (vitest via deno), check:tokens, build (web + deno compile), ci, db:migrate/reset, gen:types - tsconfig base moved to config/ (no root tsconfig.json); deno types -> types/deno.d.ts; web drops baseUrl - server: Deno.serve entry with graceful shutdown, --role migrate|preflight|probe, WEB_ROOT static SPA hosting; drop @hono/node-server, tsx, esbuild build.mjs - logger: redact child() bindings and serialize err by message/stack; sync stdout destination; tests - relative imports get explicit extensions (sites/mp-sync paths left for follow-up) - crash-recovery tests spawn `deno run -A`; tasks tests use unique per-file schemas - single-container Dockerfile + entrypoint (migrate -> preflight -> api+worker watchdog), docker-compose.yml - docs: README, runbook, ADR-001 superseded, plan §1 validation row Co-Authored-By: Claude Fable 5.1 <[email protected]>
77 lines
2.9 KiB
TypeScript
77 lines
2.9 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { buildSystemPrompt, REDACTED, redactForModel, redactString, summarizeForLog } from "../src/index.ts";
|
|
|
|
describe("redactForModel", () => {
|
|
it("遮蔽敏感键、含 passkey 的 URL、Bearer、请求头、URL 账号密码", () => {
|
|
const input = {
|
|
title: "Dune",
|
|
cookie: "uid=1; pass=abc",
|
|
apiKey: "sk-ant-1234567890abcd",
|
|
api_key: "k",
|
|
Authorization: "Bearer abcdefghijk",
|
|
accessToken: "t0k3n",
|
|
passkey: "p",
|
|
usage: { totalTokens: 100, inputTokens: 20, maxTokens: 5 },
|
|
links: [
|
|
"https://pt.example/download.php?id=9&passkey=0123456789abcdef",
|
|
"https://mt.example/api/rss?sign=abc&t=1",
|
|
"http://admin:[email protected]:8080/api/v2",
|
|
],
|
|
log: "fetch failed: Authorization: Basic dXNlcjpwYXNz\nnext",
|
|
text: "use token=abcdef and key sk-live-ABCDEFGH1234",
|
|
nested: new Map([["set-cookie", "a=b"]]),
|
|
};
|
|
const out = redactForModel(input);
|
|
const text = JSON.stringify(out);
|
|
for (const secret of [
|
|
"uid=1",
|
|
"sk-ant-",
|
|
"abcdefghijk",
|
|
"t0k3n",
|
|
"0123456789abcdef",
|
|
"sign=abc",
|
|
"hunter2",
|
|
"dXNlcjpwYXNz",
|
|
"=abcdef",
|
|
"sk-live",
|
|
]) {
|
|
expect(text).not.toContain(secret);
|
|
}
|
|
expect(out.cookie).toBe(REDACTED);
|
|
expect(out.passkey).toBe(REDACTED);
|
|
expect(out.usage).toEqual({ totalTokens: 100, inputTokens: 20, maxTokens: 5 });
|
|
expect(out.links[0]).toBe(`https://pt.example/download.php?id=9&passkey=${REDACTED}`);
|
|
expect(out.links[2]).toContain("qb.local:8080");
|
|
expect(out.title).toBe("Dune");
|
|
expect(input.cookie).toBe("uid=1; pass=abc");
|
|
});
|
|
|
|
it("字符串与摘要截断", () => {
|
|
expect(redactString("rss https://a.b/rss?rsskey=XYZ&x=1")).toBe(`rss https://a.b/rss?rsskey=${REDACTED}&x=1`);
|
|
const s = summarizeForLog({ long: "x".repeat(1000) }, 50);
|
|
expect(s.length).toBeLessThan(80);
|
|
expect(s).toContain("more");
|
|
const circular: Record<string, unknown> = { a: 1 };
|
|
circular.self = circular;
|
|
expect(summarizeForLog(circular)).toContain("[Circular]");
|
|
});
|
|
});
|
|
|
|
describe("buildSystemPrompt", () => {
|
|
it("包含证据、授权、已提交≠已入库、不可信外部内容、凭证禁令与约束", () => {
|
|
const p = buildSystemPrompt({
|
|
role: "媒体库自动化助手",
|
|
goal: "把 Dune 1080p 收进来",
|
|
constraints: ["只给方案,不执行"],
|
|
});
|
|
expect(p).toContain("你是媒体库自动化助手");
|
|
expect(p).toContain("把 Dune 1080p 收进来");
|
|
expect(p).toContain("证据");
|
|
expect(p).toContain("不得声称用户已授权");
|
|
expect(p).toContain("“已提交”不等于“已入库”");
|
|
expect(p).toMatch(/网页、RSS.*不可信数据/);
|
|
expect(p).toMatch(/不得输出.*凭证.*passkey/);
|
|
expect(p).toContain("- 只给方案,不执行");
|
|
});
|
|
});
|