feat(experiments): 场景管理与百分比配置 #4
@@ -808,3 +808,5 @@ docker compose ps
|
|||||||
- 新老用户以账号表 `created_at` 和实验显式分界时间判定;实验启动后规则冻结,结束时选定长期方案。
|
- 新老用户以账号表 `created_at` 和实验显式分界时间判定;实验启动后规则冻结,结束时选定长期方案。
|
||||||
|
|
||||||
新增 `data/digital-seed.json` 保存原创初始设定;迁移 `010_seed_digital_humans.sql` 自动初始化,`docs/DIGITAL_HUMANS.md` 记录参考、交互与资源约束。`pnpm test:digital` 运行规则回归;`pnpm digital:seed` / `pnpm digital:seed:prod` 从源码目录幂等补充内容。
|
新增 `data/digital-seed.json` 保存原创初始设定;迁移 `010_seed_digital_humans.sql` 自动初始化,`docs/DIGITAL_HUMANS.md` 记录参考、交互与资源约束。`pnpm test:digital` 运行规则回归;`pnpm digital:seed` / `pnpm digital:seed:prod` 从源码目录幂等补充内容。
|
||||||
|
|
||||||
|
实验场景通过后台「账号实验中心 → 管理实验场景」创建和启停,系统预置项标注来源;新增场景复用已实现的整站 UI 或对话接入点,同一接入点的运行/暂停实验互斥。比例统一按百分比输入,50代表50%,保留0.01%精度;接口内部整数精度不变,既有分组不重算。
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
-- 场景作为后台资源,已有 ui/chat 实验保持原标识与分组。
|
||||||
|
CREATE TABLE IF NOT EXISTS dh_scenes (
|
||||||
|
id VARCHAR(64) NOT NULL PRIMARY KEY,name VARCHAR(120) NOT NULL,
|
||||||
|
runtime ENUM('ui','chat') NOT NULL,description VARCHAR(2000) NOT NULL DEFAULT '',
|
||||||
|
enabled BOOLEAN NOT NULL DEFAULT TRUE,builtin BOOLEAN NOT NULL DEFAULT FALSE,
|
||||||
|
revision INT NOT NULL DEFAULT 1,created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3)
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||||
|
INSERT IGNORE INTO dh_scenes(id,name,runtime,description,builtin) VALUES
|
||||||
|
('ui','新老 UI','ui','系统预置:A 经典深色金 / B 现代深色粉。',TRUE),
|
||||||
|
('chat','对话风格','chat','系统预置:A/B 使用不同叙事预设。',TRUE);
|
||||||
|
ALTER TABLE dh_experiments MODIFY scope VARCHAR(64) NOT NULL;
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
/** 数字人 API:前台私有会话与后台内容/实验严格分权。 */
|
/** 数字人 API:前台私有会话与后台内容/实验严格分权。 */
|
||||||
import { env } from "../../config/env";
|
import { env } from "../../config/env";
|
||||||
|
import { scenes, saveScene, sceneSchema } from "./digital.scenes";
|
||||||
import { randomUUID } from "node:crypto";
|
import { randomUUID } from "node:crypto";
|
||||||
import type { FastifyInstance, FastifyRequest } from "fastify";
|
import type { FastifyInstance, FastifyRequest } from "fastify";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
@@ -128,7 +129,15 @@ export async function digitalRoutes(app: FastifyInstance) {
|
|||||||
front.get("/conversations/:id", async (r) => {
|
front.get("/conversations/:id", async (r) => {
|
||||||
const { id } = params.parse(r.params);
|
const { id } = params.parse(r.params);
|
||||||
const c = await ownConversation(id, r.user.sub);
|
const c = await ownConversation(id, r.user.sub);
|
||||||
const messages = await rows<{id:string;role:string;content:string;status:string;pinned:number;sequenceId:number;createdAt:Date}>(
|
const messages = await rows<{
|
||||||
|
id: string;
|
||||||
|
role: string;
|
||||||
|
content: string;
|
||||||
|
status: string;
|
||||||
|
pinned: number;
|
||||||
|
sequenceId: number;
|
||||||
|
createdAt: Date;
|
||||||
|
}>(
|
||||||
"SELECT id,role,content,status,pinned,sequence_id AS sequenceId,created_at AS createdAt FROM dh_messages WHERE conversation_id=? ORDER BY sequence_id DESC LIMIT 300",
|
"SELECT id,role,content,status,pinned,sequence_id AS sequenceId,created_at AS createdAt FROM dh_messages WHERE conversation_id=? ORDER BY sequence_id DESC LIMIT 300",
|
||||||
[id],
|
[id],
|
||||||
);
|
);
|
||||||
@@ -141,7 +150,13 @@ export async function digitalRoutes(app: FastifyInstance) {
|
|||||||
title: c.title,
|
title: c.title,
|
||||||
settings: c.settings,
|
settings: c.settings,
|
||||||
busy,
|
busy,
|
||||||
messages: messages.reverse().map(m=>!busy&&m.status==='generating'?{...m,status:'stopped'}:m),
|
messages: messages
|
||||||
|
.reverse()
|
||||||
|
.map((m) =>
|
||||||
|
!busy && m.status === "generating"
|
||||||
|
? { ...m, status: "stopped" }
|
||||||
|
: m,
|
||||||
|
),
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
front.patch("/conversations/:id", async (r) => {
|
front.patch("/conversations/:id", async (r) => {
|
||||||
@@ -340,6 +355,12 @@ export async function digitalRoutes(app: FastifyInstance) {
|
|||||||
});
|
});
|
||||||
return ok({ saved: true });
|
return ok({ saved: true });
|
||||||
});
|
});
|
||||||
|
admin.get("/scenes", async () => ok(await scenes()));
|
||||||
|
admin.put("/scenes/:id", async (r) => {
|
||||||
|
const { id } = params.parse(r.params);
|
||||||
|
await saveScene(r.user.sub, id, sceneSchema.parse(r.body));
|
||||||
|
return ok({ saved: true });
|
||||||
|
});
|
||||||
admin.get("/experiments", async () => ok(await experiments()));
|
admin.get("/experiments", async () => ok(await experiments()));
|
||||||
admin.put("/experiments/:id", async (r) => {
|
admin.put("/experiments/:id", async (r) => {
|
||||||
const { id } = params.parse(r.params);
|
const { id } = params.parse(r.params);
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
/** 后台实验生命周期与账号稳定分配;前台只消费短时决策并回报曝光。 */
|
/** 后台实验生命周期与账号稳定分配;前台只消费短时决策并回报曝光。 */
|
||||||
|
import { scene } from "./digital.scenes";
|
||||||
import { createHash, randomUUID } from "node:crypto";
|
import { createHash, randomUUID } from "node:crypto";
|
||||||
import type { PoolConnection } from "mysql2/promise";
|
import type { PoolConnection } from "mysql2/promise";
|
||||||
import type { z } from "zod";
|
import type { z } from "zod";
|
||||||
@@ -16,7 +17,9 @@ type Config = z.infer<typeof experimentSchema>["config"] & {
|
|||||||
export type Experiment = {
|
export type Experiment = {
|
||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
scope: "ui" | "chat";
|
scope: string;
|
||||||
|
runtime: "ui" | "chat";
|
||||||
|
sceneEnabled: number;
|
||||||
state: "draft" | "running" | "paused" | "finished";
|
state: "draft" | "running" | "paused" | "finished";
|
||||||
config: Config;
|
config: Config;
|
||||||
revision: number;
|
revision: number;
|
||||||
@@ -45,7 +48,7 @@ export function eligible(config: Config, account: string, createdAt: string) {
|
|||||||
export async function experiments() {
|
export async function experiments() {
|
||||||
return (
|
return (
|
||||||
await rows<Experiment>(
|
await rows<Experiment>(
|
||||||
"SELECT * FROM dh_experiments ORDER BY created_at DESC LIMIT 200",
|
"SELECT e.*,s.runtime,s.name AS sceneName,s.enabled AS sceneEnabled FROM dh_experiments e JOIN dh_scenes s ON s.id=e.scope ORDER BY e.created_at DESC LIMIT 200",
|
||||||
)
|
)
|
||||||
).map((e) => ({ ...e, config: json<Config>(e.config) }));
|
).map((e) => ({ ...e, config: json<Config>(e.config) }));
|
||||||
}
|
}
|
||||||
@@ -73,6 +76,13 @@ export async function saveExperiment(
|
|||||||
)
|
)
|
||||||
throw badRequest("新老用户实验必须设置注册时间分界");
|
throw badRequest("新老用户实验必须设置注册时间分界");
|
||||||
return transaction(async (c) => {
|
return transaction(async (c) => {
|
||||||
|
await rows(
|
||||||
|
"SELECT version FROM schema_migrations ORDER BY version LIMIT 1 FOR UPDATE",
|
||||||
|
[],
|
||||||
|
c,
|
||||||
|
);
|
||||||
|
const registered = await scene(input.scope, c);
|
||||||
|
if (!registered.enabled) throw badRequest("场景已停用,不能创建或编辑实验");
|
||||||
const [old] = await rows<Experiment>(
|
const [old] = await rows<Experiment>(
|
||||||
"SELECT * FROM dh_experiments WHERE id=? FOR UPDATE",
|
"SELECT * FROM dh_experiments WHERE id=? FOR UPDATE",
|
||||||
[id],
|
[id],
|
||||||
@@ -136,15 +146,20 @@ export async function transition(
|
|||||||
...json<Config>(e.config),
|
...json<Config>(e.config),
|
||||||
...(winner ? { winner } : {}),
|
...(winner ? { winner } : {}),
|
||||||
};
|
};
|
||||||
|
const registered = await scene(e.scope, c);
|
||||||
if (state === "running") {
|
if (state === "running") {
|
||||||
|
if (!registered.enabled) throw badRequest("场景已停用,不能启动实验");
|
||||||
const others = await rows(
|
const others = await rows(
|
||||||
"SELECT id FROM dh_experiments WHERE scope=? AND id<>? AND state IN ('running','paused')",
|
"SELECT e.id FROM dh_experiments e JOIN dh_scenes s ON s.id=e.scope WHERE s.runtime=? AND e.id<>? AND e.state IN ('running','paused')",
|
||||||
[e.scope, id],
|
[registered.runtime, id],
|
||||||
c,
|
c,
|
||||||
);
|
);
|
||||||
if (others.length) throw conflict("此场景已有未结束的实验");
|
if (others.length)
|
||||||
|
throw conflict(
|
||||||
|
"该应用接入点已有未结束的实验,请先结束,避免多个场景同时控制同一功能",
|
||||||
|
);
|
||||||
const cfg = config;
|
const cfg = config;
|
||||||
if (e.scope === "chat" && !cfg.presetSnapshots) {
|
if (registered.runtime === "chat" && !cfg.presetSnapshots) {
|
||||||
if (!cfg.presetA || !cfg.presetB)
|
if (!cfg.presetA || !cfg.presetB)
|
||||||
throw badRequest("对话实验须分别选择两个已发布预设");
|
throw badRequest("对话实验须分别选择两个已发布预设");
|
||||||
const available = await rows<{ id: string; body: unknown }>(
|
const available = await rows<{ id: string; body: unknown }>(
|
||||||
@@ -246,13 +261,19 @@ export async function resolveExperiment(
|
|||||||
presetSnapshot: cfg.presetSnapshots?.[variant],
|
presetSnapshot: cfg.presetSnapshots?.[variant],
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
/** 从已按创建时间排序的场景实验中选择当前接入点的有效配置。 */
|
||||||
|
export function activeExperiment(list: Experiment[], runtime: "ui" | "chat") {
|
||||||
|
const candidates = list.filter(
|
||||||
|
(e) => e.runtime === runtime && !!e.sceneEnabled && e.state !== "draft",
|
||||||
|
);
|
||||||
|
return (
|
||||||
|
candidates.find((e) => e.state === "running" || e.state === "paused") ??
|
||||||
|
candidates[0]
|
||||||
|
);
|
||||||
|
}
|
||||||
/** 返回前台所需最小决策,规则、指定名单和提示词均不下发。 */
|
/** 返回前台所需最小决策,规则、指定名单和提示词均不下发。 */
|
||||||
export async function decide(account: string, scope: "ui" | "chat") {
|
export async function decide(account: string, scope: "ui" | "chat") {
|
||||||
const list = (await experiments()).filter(
|
const e = activeExperiment(await experiments(), scope);
|
||||||
(e) => e.scope === scope && e.state !== "draft",
|
|
||||||
);
|
|
||||||
const e =
|
|
||||||
list.find((e) => e.state === "running" || e.state === "paused") ?? list[0];
|
|
||||||
if (!e)
|
if (!e)
|
||||||
return {
|
return {
|
||||||
variant: "A" as const,
|
variant: "A" as const,
|
||||||
@@ -307,8 +328,8 @@ export async function trackUiEvent(
|
|||||||
await write(
|
await write(
|
||||||
`INSERT IGNORE INTO dh_events(experiment_id,decision_id,account,variant,cohort,event_name,conversation_id,request_id)
|
`INSERT IGNORE INTO dh_events(experiment_id,decision_id,account,variant,cohort,event_name,conversation_id,request_id)
|
||||||
SELECT d.experiment_id,d.id,d.account,d.variant,IF(d.reason='override','manual','random'),?,?,?
|
SELECT d.experiment_id,d.id,d.account,d.variant,IF(d.reason='override','manual','random'),?,?,?
|
||||||
FROM dh_decisions d JOIN dh_experiments e ON e.id=d.experiment_id
|
FROM dh_decisions d JOIN dh_experiments e ON e.id=d.experiment_id JOIN dh_scenes s ON s.id=e.scope
|
||||||
WHERE d.id=? AND d.account=? AND e.scope='ui' AND d.reason IN ('allocated','sticky','override') AND ${validity}`,
|
WHERE d.id=? AND d.account=? AND s.runtime='ui' AND d.reason IN ('allocated','sticky','override') AND ${validity}`,
|
||||||
[
|
[
|
||||||
event,
|
event,
|
||||||
conversationId,
|
conversationId,
|
||||||
|
|||||||
@@ -0,0 +1,85 @@
|
|||||||
|
/** 场景注册与启停;场景映射到现有应用接入点,不执行任意代码。 */
|
||||||
|
import { z } from "zod";
|
||||||
|
import type { PoolConnection } from "mysql2/promise";
|
||||||
|
import { rows, write, audit, transaction } from "./digital.repository";
|
||||||
|
import { badRequest, conflict, notFound } from "../../shared/http-error";
|
||||||
|
/** 场景编辑契约,版本号保护并发编辑。 */
|
||||||
|
export const sceneSchema = z.object({
|
||||||
|
name: z.string().trim().min(1).max(120),
|
||||||
|
runtime: z.enum(["ui", "chat"]),
|
||||||
|
description: z.string().max(2000).default(""),
|
||||||
|
enabled: z.boolean().default(true),
|
||||||
|
revision: z.number().int().positive().optional(),
|
||||||
|
});
|
||||||
|
/** 后台场景记录。 */
|
||||||
|
export type Scene = {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
runtime: "ui" | "chat";
|
||||||
|
description: string;
|
||||||
|
enabled: number;
|
||||||
|
builtin: number;
|
||||||
|
revision: number;
|
||||||
|
};
|
||||||
|
/** 已注册场景,包括停用场景以便解释历史实验。 */
|
||||||
|
export async function scenes() {
|
||||||
|
return rows<Scene>(
|
||||||
|
"SELECT * FROM dh_scenes ORDER BY builtin DESC,created_at,id",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
/** 查询场景并在配置事务内锁定映射,避免修改执行类型与发布竞争。 */
|
||||||
|
export async function scene(id: string, c?: PoolConnection) {
|
||||||
|
const [s] = await rows<Scene>(
|
||||||
|
`SELECT * FROM dh_scenes WHERE id=?${c ? " FOR UPDATE" : ""}`,
|
||||||
|
[id],
|
||||||
|
c,
|
||||||
|
);
|
||||||
|
if (!s) throw notFound("实验场景不存在,请先创建场景");
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
/** 已使用场景不能切换接入点;运行/暂停中的场景不可停用。 */
|
||||||
|
export async function saveScene(
|
||||||
|
actor: string,
|
||||||
|
id: string,
|
||||||
|
input: z.infer<typeof sceneSchema>,
|
||||||
|
) {
|
||||||
|
await transaction(async (c) => {
|
||||||
|
await rows(
|
||||||
|
"SELECT version FROM schema_migrations ORDER BY version LIMIT 1 FOR UPDATE",
|
||||||
|
[],
|
||||||
|
c,
|
||||||
|
);
|
||||||
|
const [old] = await rows<Scene>(
|
||||||
|
"SELECT * FROM dh_scenes WHERE id=? FOR UPDATE",
|
||||||
|
[id],
|
||||||
|
c,
|
||||||
|
);
|
||||||
|
if (old && old.revision !== input.revision)
|
||||||
|
throw conflict("场景已更新,请刷新后重试");
|
||||||
|
const usage = await rows<{ state: string }>(
|
||||||
|
"SELECT state FROM dh_experiments WHERE scope=?",
|
||||||
|
[id],
|
||||||
|
c,
|
||||||
|
);
|
||||||
|
if (old && old.runtime !== input.runtime && (old.builtin || usage.length))
|
||||||
|
throw badRequest("系统预置或已用于实验的场景不能更换接入点");
|
||||||
|
if (
|
||||||
|
!input.enabled &&
|
||||||
|
usage.some((e) => e.state === "running" || e.state === "paused")
|
||||||
|
)
|
||||||
|
throw conflict("请先结束该场景的运行/暂停实验,再停用场景");
|
||||||
|
if (old)
|
||||||
|
await write(
|
||||||
|
"UPDATE dh_scenes SET name=?,runtime=?,description=?,enabled=?,revision=revision+1 WHERE id=?",
|
||||||
|
[input.name, input.runtime, input.description, input.enabled, id],
|
||||||
|
c,
|
||||||
|
);
|
||||||
|
else
|
||||||
|
await write(
|
||||||
|
"INSERT INTO dh_scenes(id,name,runtime,description,enabled) VALUES (?,?,?,?,?)",
|
||||||
|
[id, input.name, input.runtime, input.description, input.enabled],
|
||||||
|
c,
|
||||||
|
);
|
||||||
|
await audit(actor, old ? "scene.update" : "scene.create", id, input, c);
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -55,7 +55,7 @@ export type ContentInput = z.infer<typeof contentSchema>;
|
|||||||
/** 实验配置仅通过后台写入,分组语义在同一实验生命周期中固定。 */
|
/** 实验配置仅通过后台写入,分组语义在同一实验生命周期中固定。 */
|
||||||
export const experimentSchema = z.object({
|
export const experimentSchema = z.object({
|
||||||
name: z.string().min(1).max(120),
|
name: z.string().min(1).max(120),
|
||||||
scope: z.enum(["ui", "chat"]),
|
scope: idSchema,
|
||||||
config: z.object({
|
config: z.object({
|
||||||
audience: z.enum(["all", "new", "existing", "specified"]).default("all"),
|
audience: z.enum(["all", "new", "existing", "specified"]).default("all"),
|
||||||
cutoff: z.iso.datetime().optional(),
|
cutoff: z.iso.datetime().optional(),
|
||||||
|
|||||||
+100
-15
@@ -226,22 +226,107 @@ test("角色输入拒绝未成年和前台外部执行资源", () => {
|
|||||||
assert.equal("experimentVariant" in parsed.body, false);
|
assert.equal("experimentVariant" in parsed.body, false);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('用户消息中的占位符保持原样,避免内容意外膨胀或被改写',()=>{
|
test("用户消息中的占位符保持原样,避免内容意外膨胀或被改写", () => {
|
||||||
const body=contentSchema.shape.body.parse({cover:''});
|
const body = contentSchema.shape.body.parse({ cover: "" });
|
||||||
const latest='{{user}}'.repeat(500);
|
const latest = "{{user}}".repeat(500);
|
||||||
const result=assemblePrompt({...body,name:'角色'},body,[],[{role:'user',content:latest}],{persona:'',userName:'名'.repeat(60),memory:''},[]);
|
const result = assemblePrompt(
|
||||||
assert.equal(result.messages.at(-1)?.content,latest);assert.ok(result.characters<=body.contextBudget);
|
{ ...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()=>{
|
test("空正文增量不触发前台打字", async () => {
|
||||||
const raw=new TextEncoder().encode('data: {"choices":[{"delta":{"content":""}}]}\n\ndata: [DONE]\n\n');
|
const raw = new TextEncoder().encode(
|
||||||
const stream=new ReadableStream<Uint8Array>({start(c){c.enqueue(raw);c.close();}});
|
'data: {"choices":[{"delta":{"content":""}}]}\n\ndata: [DONE]\n\n',
|
||||||
const chunks=[];for await(const text of completionDeltas(stream))chunks.push(text);assert.deepEqual(chunks,[]);
|
);
|
||||||
|
const stream = new ReadableStream<Uint8Array>({
|
||||||
|
start(c) {
|
||||||
|
c.enqueue(raw);
|
||||||
|
c.close();
|
||||||
|
},
|
||||||
});
|
});
|
||||||
test('员工即使持有旧管理员权限声明,也不能进入数字人运营接口',async()=>{
|
const chunks = [];
|
||||||
const {authService}=await import('../src/modules/auth/auth.service');
|
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;
|
const original = authService.getCurrentUser;
|
||||||
authService.getCurrentUser=async()=>({id:99,username:'unit',displayName:'unit',accountType:'EMPLOYEE',roles:[],permissions:['*'],canManage:true});
|
authService.getCurrentUser = async () => ({
|
||||||
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});
|
id: 99,
|
||||||
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);
|
username: "unit",
|
||||||
}finally{authService.getCurrentUser=original;await app.close();}
|
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,
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user