fix(experiments): 移除系统预置并支持空白实验配置

- 迁移清理预置类型及关联分流数据,保留自建类型与清理审计
- 未配置实验时沿用应用默认行为,不自动创建类型或接入记录
- 补充空白系统启动回归与迁移说明
This commit is contained in:
湛兮
2026-10-27 14:41:59 +08:00
parent cdae952c6a
commit 9350aa9632
7 changed files with 145 additions and 25 deletions
+59
View File
@@ -6,6 +6,7 @@ import { resolve } from "node:path";
import assert from "node:assert/strict";
import {
bucket,
decide,
eligible,
planValue,
activeExperiment,
@@ -497,3 +498,61 @@ test("新增预设的数据库迁移与初始化种子保持同一数据契约",
assert.equal(contentSchema.safeParse(record).success, true);
assert.ok(/INSERT IGNORE/.test(sql));
});
test("空白实验注册表保持为空,应用默认决策不创建类型或伪造接入", async (t) => {
const { pool } = await import("../src/db/pool");
const { experimentTypes } = await import("../src/modules/digital/digital.experiment-types");
const queries: string[] = [];
t.mock.method(pool, "query", async (options: { sql: string }) => {
queries.push(options.sql);
assert.match(options.sql, /^SELECT .* FROM dh_scenes /);
return [[], []];
});
assert.deepEqual(await experimentTypes(), []);
for (const [key, value] of [["ui", "old"], ["chat", ""], ["custom", false]] as const) {
const d = await decide("employee:1", key, { value });
assert.equal(d.value, value);
assert.equal(d.typeRevision, 0);
assert.equal(d.reason, "default");
assert.equal(d.decisionId, null);
assert.equal(d.experimentId, null);
assert.equal(d.presetId, undefined);
}
await assert.rejects(decide("employee:1", "missing"), (e: unknown) =>
!!e && typeof e === "object" && "statusCode" in e && e.statusCode === 404);
assert.equal(queries.length, 5);
});
test("应用未配置实验的默认行为不能吞掉数据库故障", async (t) => {
const { pool } = await import("../src/db/pool");
t.mock.method(pool, "query", async () => { throw new Error("database unavailable"); });
await assert.rejects(decide("employee:1", "ui", { value: "old" }), /database unavailable/);
});
test("空白实验系统仍能启动前台,且不为默认界面写入实验记录", async (t) => {
const { pool } = await import("../src/db/pool");
const { authService } = await import("../src/modules/auth/auth.service");
t.mock.method(authService, "getCurrentUser", async () => ({ accountType: "EMPLOYEE" }));
t.mock.method(pool, "query", async (options: { sql: string }) => {
assert.match(options.sql, /^SELECT .* FROM (dh_scenes|dh_content|dh_profiles) /);
return [[], []];
});
const app = createApp();
try {
await app.ready();
const token = app.jwt.sign({ sub: "employee:1", accountType: "EMPLOYEE" });
const result = await app.inject({
method: "GET", url: "/api/digital/bootstrap",
headers: { authorization: `Bearer ${token}` },
});
assert.equal(result.statusCode, 200);
const data = result.json().data;
assert.deepEqual(data.catalog, []);
assert.equal(data.decision.variant, "A");
assert.equal(data.decision.experimentId, null);
assert.equal(data.decision.decisionId, null);
} finally {
await app.close();
}
});