feat(experiments): 新增后台场景注册与应用接入映射

保留已有ui/chat场景与分组,支持命名、创建和停用;同一应用接入点实验互斥,保护历史场景类型与运行实验;增加自定义场景决策回归。
This commit is contained in:
湛兮
2026-10-27 11:01:52 +08:00
parent fd0b039729
commit 2e3a7c0a7e
7 changed files with 257 additions and 32 deletions
+101 -16
View File
@@ -226,22 +226,107 @@ test("角色输入拒绝未成年和前台外部执行资源", () => {
assert.equal("experimentVariant" in parsed.body, false);
});
test('用户消息中的占位符保持原样,避免内容意外膨胀或被改写',()=>{
const body=contentSchema.shape.body.parse({cover:''});
const latest='{{user}}'.repeat(500);
const result=assemblePrompt({...body,name:'角色'},body,[],[{role:'user',content:latest}],{persona:'',userName:'名'.repeat(60),memory:''},[]);
assert.equal(result.messages.at(-1)?.content,latest);assert.ok(result.characters<=body.contextBudget);
test("用户消息中的占位符保持原样,避免内容意外膨胀或被改写", () => {
const body = contentSchema.shape.body.parse({ cover: "" });
const latest = "{{user}}".repeat(500);
const result = assemblePrompt(
{ ...body, name: "角色" },
body,
[],
[{ role: "user", content: latest }],
{ persona: "", userName: "名".repeat(60), memory: "" },
[],
);
assert.equal(result.messages.at(-1)?.content, latest);
assert.ok(result.characters <= body.contextBudget);
});
test('空正文增量不触发前台打字',async()=>{
const raw=new TextEncoder().encode('data: {"choices":[{"delta":{"content":""}}]}\n\ndata: [DONE]\n\n');
const stream=new ReadableStream<Uint8Array>({start(c){c.enqueue(raw);c.close();}});
const chunks=[];for await(const text of completionDeltas(stream))chunks.push(text);assert.deepEqual(chunks,[]);
test("空正文增量不触发前台打字", async () => {
const raw = new TextEncoder().encode(
'data: {"choices":[{"delta":{"content":""}}]}\n\ndata: [DONE]\n\n',
);
const stream = new ReadableStream<Uint8Array>({
start(c) {
c.enqueue(raw);
c.close();
},
});
const chunks = [];
for await (const text of completionDeltas(stream)) chunks.push(text);
assert.deepEqual(chunks, []);
});
test('员工即使持有旧管理员权限声明,也不能进入数字人运营接口',async()=>{
const {authService}=await import('../src/modules/auth/auth.service');
const original=authService.getCurrentUser;
authService.getCurrentUser=async()=>({id:99,username:'unit',displayName:'unit',accountType:'EMPLOYEE',roles:[],permissions:['*'],canManage:true});
const app=createApp();try{await app.ready();const token=app.jwt.sign({sub:'employee:99',employeeId:99,accountType:'EMPLOYEE',scene:'EMPLOYEE_APP',username:'unit',roleCodes:['admin'],permissions:['*'],canManage:true});
for(const url of ['/api/digital-admin/content','/api/digital-admin/experiments'])assert.equal((await app.inject({method:'GET',url,headers:{authorization:`Bearer ${token}`}})).statusCode,403);
}finally{authService.getCurrentUser=original;await app.close();}
test("员工即使持有旧管理员权限声明,也不能进入数字人运营接口", async () => {
const { authService } = await import("../src/modules/auth/auth.service");
const original = authService.getCurrentUser;
authService.getCurrentUser = async () => ({
id: 99,
username: "unit",
displayName: "unit",
accountType: "EMPLOYEE",
roles: [],
permissions: ["*"],
canManage: true,
});
const app = createApp();
try {
await app.ready();
const token = app.jwt.sign({
sub: "employee:99",
employeeId: 99,
accountType: "EMPLOYEE",
scene: "EMPLOYEE_APP",
username: "unit",
roleCodes: ["admin"],
permissions: ["*"],
canManage: true,
});
for (const url of [
"/api/digital-admin/content",
"/api/digital-admin/experiments",
])
assert.equal(
(
await app.inject({
method: "GET",
url,
headers: { authorization: `Bearer ${token}` },
})
).statusCode,
403,
);
} finally {
authService.getCurrentUser = original;
await app.close();
}
});
test("自定义场景按应用接入点生效,停用场景不再参与决策", async () => {
const { activeExperiment } = await import(
"../src/modules/digital/digital.experiments"
);
const custom = {
id: "e1",
scope: "my-ui-scene",
runtime: "ui",
sceneEnabled: 1,
state: "running",
};
const disabled = {
...custom,
id: "e2",
scope: "disabled-scene",
sceneEnabled: 0,
};
const chat = { ...custom, id: "e3", runtime: "chat" };
const list = [disabled, chat, custom] as Parameters<
typeof activeExperiment
>[0];
assert.equal(activeExperiment(list, "ui")?.id, "e1");
assert.equal(activeExperiment(list, "chat")?.id, "e3");
assert.equal(
activeExperiment(
[disabled] as Parameters<typeof activeExperiment>[0],
"ui",
),
undefined,
);
});