feat(experiments): 独立注册实验类型并分离用户分组查询

- 实验类型:自定义标识、数据契约、默认值和真实调用后的接入验收

- 测试方案:按类型校验 A/B 值,保留历史分组与预设快照,未验收禁止发布

- 用户管理:真实账号分页搜索、只读预览与有效账号人工指定校验

- 文档与验证:同步迁移014及消费者接口说明,15项核心回归通过
This commit is contained in:
湛兮
2026-10-27 11:40:02 +08:00
parent 7ba4241995
commit 094d0c17e8
10 changed files with 590 additions and 82 deletions
+88 -1
View File
@@ -2,7 +2,13 @@
import "./digital-env";
import test from "node:test";
import assert from "node:assert/strict";
import { bucket, eligible } from "../src/modules/digital/digital.experiments";
import {
bucket,
eligible,
planValue,
activeExperiment,
type Experiment,
} from "../src/modules/digital/digital.experiments";
import {
assemblePrompt,
selectLore,
@@ -14,6 +20,11 @@ import {
} from "../src/modules/digital/digital.schema";
import { completionDeltas } from "../src/modules/digital/digital.chat";
import { createApp } from "../src/app";
import {
validateTypeValue,
normalizePlanValues,
type ExperimentType,
} from "../src/modules/digital/digital.experiment-types";
const config = experimentSchema.parse({
name: "test",
scope: "ui",
@@ -176,9 +187,12 @@ test("数字人及后台接口不接受匿名请求", async () => {
try {
for (const url of [
"/api/digital/bootstrap",
"/api/digital/decisions/ui",
"/api/digital/conversations",
"/api/digital-admin/content",
"/api/digital-admin/experiments",
"/api/digital-admin/experiment-types",
"/api/digital-admin/experiment-users",
]) {
const r = await app.inject({ method: "GET", url });
assert.equal(r.statusCode, 401, url);
@@ -282,6 +296,8 @@ test("员工即使持有旧管理员权限声明,也不能进入数字人运
for (const url of [
"/api/digital-admin/content",
"/api/digital-admin/experiments",
"/api/digital-admin/experiment-types",
"/api/digital-admin/experiment-users",
])
assert.equal(
(
@@ -330,3 +346,74 @@ test("自定义场景按应用接入点生效,停用场景不再参与决策",
undefined,
);
});
test("类型数据契约保留 false、0、null,拒绝错误格式和非 JSON 数据", () => {
for (const [kind, value] of [
["boolean", false],
["number", 0],
["json", null],
["json", { a: false, b: 0 }],
["string", ""],
] as const)
assert.doesNotThrow(() => validateTypeValue(kind, value));
for (const [kind, value] of [
["boolean", "false"],
["number", "0"],
["number", Infinity],
["string", {}],
["json", undefined],
["json", { a: NaN }],
["json", "x".repeat(25000)],
] as const)
assert.throws(() => validateTypeValue(kind, value));
});
test("自定义 A/B 值按注册契约验证,旧 UI 值和预设合同保持兼容", () => {
const t = { valueType: "boolean", contract: {} } as ExperimentType;
assert.deepEqual(
normalizePlanValues(t, { values: { A: false, B: true } }).values,
{ A: false, B: true },
);
assert.throws(() => normalizePlanValues(t, {}));
assert.throws(() =>
normalizePlanValues(t, { values: { A: "false", B: true } }),
);
const ui = {
...t,
valueType: "string",
contract: { fixedValues: { A: "old", B: "new" } },
} as ExperimentType;
assert.deepEqual(
normalizePlanValues(ui, { values: { A: "malicious", B: "other" } }).values,
{ A: "old", B: "new" },
);
const chat = { ...ui, contract: { optionsSource: "presets" } };
assert.equal(
normalizePlanValues(chat, { presetA: "one", presetB: "two" }).presetA,
"one",
);
const plan = {
config: { values: { A: false, B: 0 } },
consumerKey: "custom",
} as unknown as Experiment;
assert.equal(planValue(plan, "A", "fallback"), false);
assert.equal(planValue(plan, "B", "fallback"), 0);
});
test("独立新类型不会共享分流;历史同消费者别名仍互斥选择", () => {
const e = (id: string, consumerKey: string, state: Experiment["state"]) =>
({ id, scope: id, consumerKey, state, sceneEnabled: 1 }) as Experiment;
const list = [
e("draft", "one", "draft"),
e("a", "one", "running"),
e("b", "two", "running"),
];
assert.equal(activeExperiment(list, "one")?.id, "a");
assert.equal(activeExperiment(list, "two")?.id, "b");
assert.equal(activeExperiment(list, "missing"), undefined);
assert.equal(
activeExperiment(
[e("ended", "one", "finished"), e("alias", "one", "paused")],
"one",
)?.id,
"alias",
);
});