Files
access-manage/tests/digital-suggestions.test.ts
T
湛兮 51ec4598df feat(nsfw): 持久化账号偏好并接入生成控制
- digital.profile.ts: 未保存偏好时默认开启,原子合并 JSON 保留其他资料
- digital.controller.ts、digital.users.ts: 接入本人和管理员偏好读写、事务及审计
- digital.chat.ts、digital.prompt.ts、digital.suggestions.ts: 各生成入口读取服务端账号偏好,保留游客鉴权
- tests/digital-*.test.ts: 覆盖默认值、关闭保留、旧客户端兼容与越权拦截
- README.md: 同步模块职责和发布要求
2026-10-27 20:18:16 +08:00

107 lines
3.6 KiB
TypeScript

/** 回复灵感的格式、上下文预算与匿名鉴权回归,不调用真实模型。 */
import "./digital-env";
import test from "node:test";
import assert from "node:assert/strict";
import {
parseReplyIdeas,
replyIdeasMessages,
} from "../src/modules/digital/digital.suggestions";
import { HttpError } from "../src/shared/http-error";
import { createApp } from "../src/app";
test("灵感只接受三个非空、互不重复的短回复,允许模型返回JSON围栏", () => {
const replies = [
"我想听听你的故事。",
"我们一起去看看吧。",
"你刚才说的那个地方在哪里?",
];
assert.deepEqual(
parseReplyIdeas(
"```json\n" + JSON.stringify({ suggestions: replies }) + "\n```",
),
replies,
);
for (const value of [
undefined,
"invalid-secret-do-not-echo",
JSON.stringify({ suggestions: [] }),
JSON.stringify({ suggestions: ["相同", "相同 ", "另一个"] }),
JSON.stringify({ suggestions: ["x".repeat(241), "二", "三"] }),
JSON.stringify({ suggestions: ["one", null, "three"] }),
])
assert.throws(
() => parseReplyIdeas(value),
(e) =>
e instanceof HttpError &&
e.statusCode === 502 &&
!e.message.includes("secret-do-not-echo"),
);
});
test("灵感上下文保留最新对话、用户身份与草稿,并限制历史和资料预算", () => {
const history = Array.from({ length: 40 }, (_, i) => ({
role: (i % 2 ? "assistant" : "user") as "user" | "assistant",
content: ("旧消息" + i).repeat(1000),
}));
history.push({ role: "user", content: "最后一句:蓝色灯笼该叫什么名字?" });
const messages = replyIdeasMessages({
name: "陆沉舟",
description: "设定".repeat(10000),
scenario: "场景".repeat(10000),
userName: "小雨",
persona: "旅人",
memory: "我们约好看灯塔",
draft: "我想叫它",
lore: ["已触发的世界书"],
history,
});
assert.ok(messages[0].content.includes("用户视角"));
assert.equal(messages[0].role, "system");
const context = JSON.parse(messages[1].content);
assert.equal(context.history.length, 12);
assert.equal(context.history.at(-1).speaker, "用户");
assert.ok(context.history.at(-1).content.includes("蓝色灯笼"));
assert.equal(context.draft, "我想叫它");
assert.equal(context.userName, "小雨");
assert.ok(context.description.length <= 1200);
assert.ok(context.scenario.length <= 800);
assert.ok(messages[1].content.length < 20000);
});
test("灵感接口不接受匿名用户,即使请求体伪造账号也会先拒绝", async () => {
const app = createApp();
try {
const result = await app.inject({
method: "POST",
url: "/api/digital/conversations/unit-conversation/suggestions",
payload: { account: "super_admin:1", draft: "测试" },
});
assert.equal(result.statusCode, 401);
} finally {
await app.close();
}
});
test("回复灵感遵循账号开关,关闭时保留日常内容限制", () => {
for (const nsfwEnabled of [true, false]) {
const messages = replyIdeasMessages({
name: "角色",
description: "",
scenario: "",
userName: "旅人",
persona: "",
memory: "",
draft: "",
lore: [],
history: [],
nsfwEnabled,
});
assert.ok(
messages[0].content.includes(nsfwEnabled ? "NSFW 已开启" : "NSFW 已关闭"),
);
assert.match(messages[0].content, /禁止涉及未成年人的性内容/);
assert.equal(
messages[0].content.includes("不生成露骨性描写"),
!nsfwEnabled,
);
}
});