Merge pull request 'chore: 同步新老 UI 接入与登录修复到测试分支' (#13) from master into develop
This commit was merged in pull request #13.
This commit is contained in:
@@ -839,3 +839,9 @@ docker compose ps
|
|||||||
不再配置默认组:草稿、暂停、不符合人群或未进入实验流量的账号均返回类型默认值,决策 `variant=null`,不会记录实验曝光。0%组不参与自动分流,但允许人工指定;结束时可保留任意已配置组,不能选择不存在的组。启动后组别、比例和返回值冻结,恢复保持已保存分组。
|
不再配置默认组:草稿、暂停、不符合人群或未进入实验流量的账号均返回类型默认值,决策 `variant=null`,不会记录实验曝光。0%组不参与自动分流,但允许人工指定;结束时可保留任意已配置组,不能选择不存在的组。启动后组别、比例和返回值冻结,恢复保持已保存分组。
|
||||||
|
|
||||||
旧JSON配置只在读取时转换,保留原B组低位哈希区间和稳定分组,无需数据库DDL或批量迁移。用户端使用决策的 `value` 消费功能,不能以 `variant === 'B'` 作为通用新版判断。当前UI消费者识别 `new` / `true` 为新版,其他值为旧版;这不等于自动接入任意新建类型。
|
旧JSON配置只在读取时转换,保留原B组低位哈希区间和稳定分组,无需数据库DDL或批量迁移。用户端使用决策的 `value` 消费功能,不能以 `variant === 'B'` 作为通用新版判断。当前UI消费者识别 `new` / `true` 为新版,其他值为旧版;这不等于自动接入任意新建类型。
|
||||||
|
|
||||||
|
### 新老 UI 的实际应用接入
|
||||||
|
|
||||||
|
数字人 `bootstrap` 明确消费 `DIGITAL_UI_TYPE_KEY` 配置的类型,当前默认引用运营创建的 `ui-new-old`。`false` 显示旧 UI,`true` 显示新 UI;前台不按 A/B 字母猜测界面。此配置只绑定实际业务消费者,不会创建系统预置类型或方案。更换类型标识时设置同名服务端环境变量,并保持布尔契约。UI 漏斗归因使用同一消费标识,避免新类型有曝光却没有行为统计。未创建该类型时保留应用默认旧 UI。
|
||||||
|
|
||||||
|
接入顺序:创建类型 → 发布真实消费代码 → 前台读取默认值并验证 → 在后台完成接入验收 → 对指定测试账号启动方案 → 核对两账号返回值、渲染界面及曝光。草稿和暂停使用类型默认值,提前保存人工指定不会启动实验。
|
||||||
|
|||||||
@@ -21,6 +21,11 @@ const envSchema = z.object({
|
|||||||
DB_NAME: z.string().min(1),
|
DB_NAME: z.string().min(1),
|
||||||
DB_CONNECTION_LIMIT: z.coerce.number().int().positive().default(10),
|
DB_CONNECTION_LIMIT: z.coerce.number().int().positive().default(10),
|
||||||
|
|
||||||
|
// 显式业务接入:引用运营创建的类型,不创建或预置任何实验记录。
|
||||||
|
DIGITAL_UI_TYPE_KEY: z
|
||||||
|
.string()
|
||||||
|
.regex(/^[a-z][a-z0-9_-]{1,63}$/)
|
||||||
|
.default("ui-new-old"),
|
||||||
DEEPSEEK_API_KEY: z.string().min(1).optional(),
|
DEEPSEEK_API_KEY: z.string().min(1).optional(),
|
||||||
JWT_SECRET: z.string().min(32),
|
JWT_SECRET: z.string().min(32),
|
||||||
JWT_EXPIRES_IN: z.string().min(1).default("2h"),
|
JWT_EXPIRES_IN: z.string().min(1).default("2h"),
|
||||||
|
|||||||
@@ -94,9 +94,11 @@ export async function digitalRoutes(app: FastifyInstance) {
|
|||||||
"SELECT body FROM dh_profiles WHERE account=?",
|
"SELECT body FROM dh_profiles WHERE account=?",
|
||||||
[r.user.sub],
|
[r.user.sub],
|
||||||
),
|
),
|
||||||
decide(r.user.sub, "ui", { value: "old" }),
|
decide(r.user.sub, env.DIGITAL_UI_TYPE_KEY, { value: "old" }),
|
||||||
]);
|
]);
|
||||||
const publicDecision = {
|
const publicDecision = {
|
||||||
|
typeKey: decision.typeKey,
|
||||||
|
reason: decision.reason,
|
||||||
variant: decision.variant,
|
variant: decision.variant,
|
||||||
value: decision.value,
|
value: decision.value,
|
||||||
fallbackValue: decision.fallbackValue,
|
fallbackValue: decision.fallbackValue,
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import {
|
|||||||
hasExperimentGroup,
|
hasExperimentGroup,
|
||||||
type ExperimentConfig,
|
type ExperimentConfig,
|
||||||
} from "./digital.experiment-groups";
|
} from "./digital.experiment-groups";
|
||||||
|
import { env } from "../../config/env";
|
||||||
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";
|
||||||
@@ -400,13 +401,14 @@ export async function trackUiEvent(
|
|||||||
`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 JOIN dh_scenes s ON s.id=e.scope
|
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 s.consumer_key='ui' AND d.reason IN ('allocated','sticky','override') AND ${validity}`,
|
WHERE d.id=? AND d.account=? AND s.consumer_key=? AND d.reason IN ('allocated','sticky','override') AND ${validity}`,
|
||||||
[
|
[
|
||||||
event,
|
event,
|
||||||
conversationId,
|
conversationId,
|
||||||
requestId,
|
requestId,
|
||||||
decisionId,
|
decisionId,
|
||||||
account,
|
account,
|
||||||
|
env.DIGITAL_UI_TYPE_KEY,
|
||||||
...(completion ? [requestId] : []),
|
...(completion ? [requestId] : []),
|
||||||
],
|
],
|
||||||
c,
|
c,
|
||||||
|
|||||||
@@ -826,3 +826,128 @@ test("结束实验拒绝不存在的组,允许保留零比例C组", async (t)
|
|||||||
assert.equal(commit, 1);
|
assert.equal(commit, 1);
|
||||||
assert.equal(JSON.parse(String(updated?.[1])).winner, "C");
|
assert.equal(JSON.parse(String(updated?.[1])).winner, "C");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("真实 bootstrap 消费自建 UI 类型,两账号按人工指定返回不同布尔值并更新调用证据", async (t) => {
|
||||||
|
const { pool } = await import("../src/db/pool");
|
||||||
|
const { env } = await import("../src/config/env");
|
||||||
|
const { authService } = await import("../src/modules/auth/auth.service");
|
||||||
|
t.mock.method(authService, "getCurrentUser", async () => ({
|
||||||
|
accountType: "EMPLOYEE",
|
||||||
|
}));
|
||||||
|
let state = "running",
|
||||||
|
receipts = 0,
|
||||||
|
decisions = 0;
|
||||||
|
const type = {
|
||||||
|
id: env.DIGITAL_UI_TYPE_KEY,
|
||||||
|
consumerKey: env.DIGITAL_UI_TYPE_KEY,
|
||||||
|
enabled: 1,
|
||||||
|
valueType: "boolean",
|
||||||
|
defaultValue: false,
|
||||||
|
contract: {},
|
||||||
|
contractRevision: 1,
|
||||||
|
};
|
||||||
|
const plan = {
|
||||||
|
id: "custom-ui-plan",
|
||||||
|
scope: type.id,
|
||||||
|
consumerKey: type.id,
|
||||||
|
sceneEnabled: 1,
|
||||||
|
revision: 1,
|
||||||
|
config: {
|
||||||
|
...config,
|
||||||
|
audience: "specified",
|
||||||
|
accounts: ["employee:1", "super_admin:1"],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
t.mock.method(pool, "query", async (q: { sql: string }, args: unknown[]) => {
|
||||||
|
if (q.sql.includes("FROM dh_scenes WHERE id")) {
|
||||||
|
assert.equal(args[0], "ui-new-old");
|
||||||
|
return [[type], []];
|
||||||
|
}
|
||||||
|
if (q.sql.startsWith("UPDATE dh_scenes SET last_seen")) {
|
||||||
|
receipts++;
|
||||||
|
return [{ affectedRows: 1 }, []];
|
||||||
|
}
|
||||||
|
if (q.sql.includes("FROM dh_experiments e JOIN"))
|
||||||
|
return [[{ ...plan, state }], []];
|
||||||
|
if (q.sql.includes("FROM dh_content") || q.sql.includes("FROM dh_profiles"))
|
||||||
|
return [[], []];
|
||||||
|
if (q.sql.includes("created_at FROM"))
|
||||||
|
return [[{ created_at: new Date("2026-01-01T00:00:00Z") }], []];
|
||||||
|
if (q.sql.includes("FROM dh_overrides"))
|
||||||
|
return [
|
||||||
|
[
|
||||||
|
{
|
||||||
|
variant: args[1] === "employee:1" ? "A" : "B",
|
||||||
|
bypass_audience: false,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
[],
|
||||||
|
];
|
||||||
|
if (q.sql.startsWith("INSERT INTO dh_decisions")) {
|
||||||
|
decisions++;
|
||||||
|
return [{ affectedRows: 1 }, []];
|
||||||
|
}
|
||||||
|
throw new Error("意外 SQL:" + q.sql);
|
||||||
|
});
|
||||||
|
const app = createApp();
|
||||||
|
try {
|
||||||
|
await app.ready();
|
||||||
|
for (const [account, value, variant] of [
|
||||||
|
["employee:1", false, "A"],
|
||||||
|
["super_admin:1", true, "B"],
|
||||||
|
] as const) {
|
||||||
|
const token = app.jwt.sign({ sub: account, accountType: "EMPLOYEE" });
|
||||||
|
for (let i = 0; i < 2; i++) {
|
||||||
|
const response = await app.inject({
|
||||||
|
method: "GET",
|
||||||
|
url: "/api/digital/bootstrap",
|
||||||
|
headers: { authorization: `Bearer ${token}` },
|
||||||
|
});
|
||||||
|
assert.equal(response.statusCode, 200);
|
||||||
|
const d = response.json().data.decision;
|
||||||
|
assert.equal(d.typeKey, type.id);
|
||||||
|
assert.equal(d.variant, variant);
|
||||||
|
assert.equal(d.value, value);
|
||||||
|
assert.equal(d.reason, "override");
|
||||||
|
assert.ok(d.decisionId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert.equal(receipts, 4);
|
||||||
|
assert.equal(decisions, 4);
|
||||||
|
state = "paused";
|
||||||
|
const token = app.jwt.sign({
|
||||||
|
sub: "super_admin:1",
|
||||||
|
accountType: "EMPLOYEE",
|
||||||
|
});
|
||||||
|
const response = await app.inject({
|
||||||
|
method: "GET",
|
||||||
|
url: "/api/digital/bootstrap",
|
||||||
|
headers: { authorization: `Bearer ${token}` },
|
||||||
|
});
|
||||||
|
const d = response.json().data.decision;
|
||||||
|
assert.equal(d.variant, null);
|
||||||
|
assert.equal(d.value, false);
|
||||||
|
assert.equal(d.decisionId, null);
|
||||||
|
} finally {
|
||||||
|
await app.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test("UI 业务事件按实际接入的自建类型归因,不再写死 ui", async (t) => {
|
||||||
|
const { pool } = await import("../src/db/pool");
|
||||||
|
const { trackUiEvent } = await import(
|
||||||
|
"../src/modules/digital/digital.experiments"
|
||||||
|
);
|
||||||
|
t.mock.method(pool, "query", async (q: { sql: string }, args: unknown[]) => {
|
||||||
|
assert.match(q.sql, /s.consumer_key=\?/);
|
||||||
|
assert.equal(args[5], "ui-new-old");
|
||||||
|
return [{ affectedRows: 1 }, []];
|
||||||
|
});
|
||||||
|
await trackUiEvent(
|
||||||
|
"employee:1",
|
||||||
|
"decision",
|
||||||
|
"conversation_started",
|
||||||
|
"conversation",
|
||||||
|
"request",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user