156 lines
4.6 KiB
TypeScript
156 lines
4.6 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
|
|
import { requestCrudService } from "@/service/request-axios-service";
|
|
import {
|
|
buildTaskOpeningText,
|
|
ensureNarrativeSummary,
|
|
fetchTaskActionStructure,
|
|
} from "./task-deeplink";
|
|
|
|
const CONSUMER = "https://ch1.b.uat.4.cn/consumer";
|
|
|
|
const memory = new Map<string, string>();
|
|
Object.assign(globalThis, {
|
|
window: {
|
|
__CM_RUNTIME_CONFIG__: { VITE_AIPI_CONSUMER_URL: CONSUMER },
|
|
},
|
|
sessionStorage: {
|
|
getItem: (key: string) => memory.get(key) ?? null,
|
|
setItem: (key: string, value: string) => {
|
|
memory.set(key, String(value));
|
|
},
|
|
removeItem: (key: string) => {
|
|
memory.delete(key);
|
|
},
|
|
clear: () => {
|
|
memory.clear();
|
|
},
|
|
key: (index: number) => [...memory.keys()][index] ?? null,
|
|
get length() {
|
|
return memory.size;
|
|
},
|
|
},
|
|
});
|
|
|
|
interface RecordedCall {
|
|
method: "GET";
|
|
endpoint: string;
|
|
params: Record<string, unknown>;
|
|
}
|
|
|
|
const calls: RecordedCall[] = [];
|
|
|
|
const actionDetails: Record<string, unknown> = {
|
|
"9001": {
|
|
state: 200,
|
|
msg: "操作成功",
|
|
data: {
|
|
actionId: "9001",
|
|
purpose: null,
|
|
actor: null,
|
|
expectedBehavior: null,
|
|
drivingFactor: null,
|
|
narrationList: null,
|
|
},
|
|
},
|
|
"9002": {
|
|
state: 200,
|
|
msg: "操作成功",
|
|
data: {
|
|
actionId: "9002",
|
|
purpose: "扩大影响",
|
|
actor: "媒体账号",
|
|
expectedBehavior: "转发评论",
|
|
drivingFactor: "认同感",
|
|
narrationList: ["叙事03"],
|
|
},
|
|
},
|
|
};
|
|
|
|
requestCrudService.get = async (endpoint: string, params: Record<string, unknown> = {}) => {
|
|
calls.push({ method: "GET", endpoint, params: { ...params } });
|
|
if (endpoint.endsWith("/task-action/actionDetail")) {
|
|
const actionId = String(params.actionId ?? "");
|
|
const body = actionDetails[actionId];
|
|
if (!body) throw new Error(`no mock for actionId=${actionId}`);
|
|
return body;
|
|
}
|
|
if (endpoint.endsWith("/mainnets/narratives-by-name")) {
|
|
return {
|
|
state: 200,
|
|
msg: "操作成功",
|
|
data: { summary: "这条传播网主要讨论暑期文旅消费。" },
|
|
};
|
|
}
|
|
throw new Error(`unexpected GET ${endpoint}`);
|
|
};
|
|
|
|
function queryString(params: Record<string, unknown>): string {
|
|
const search = new URLSearchParams();
|
|
for (const [key, value] of Object.entries(params)) {
|
|
search.set(key, String(value));
|
|
}
|
|
const text = search.toString();
|
|
return text ? `?${text}` : "";
|
|
}
|
|
|
|
test("模拟 xdfx 行动结构与传播网简介的 consumer 调用", async () => {
|
|
const task = {
|
|
id: "2055",
|
|
overview: "",
|
|
taskDirection: "",
|
|
taskName: "任务01:暑期文旅舆情综合研判",
|
|
taskContent: "围绕暑期文旅消费高峰做综合研判。",
|
|
taskType: "1",
|
|
cbwName: "暑期文旅传播网",
|
|
};
|
|
|
|
await fetchTaskActionStructure("9001");
|
|
await ensureNarrativeSummary("9001", task.cbwName);
|
|
const missingPurpose = buildTaskOpeningText(task, "2055", {
|
|
actionId: "9001",
|
|
actionName: "暑期引导行动",
|
|
narration: "引导公众讨论文旅消费。",
|
|
cbwId: "88",
|
|
cbwName: task.cbwName,
|
|
});
|
|
|
|
await fetchTaskActionStructure("9002");
|
|
await ensureNarrativeSummary("9002", task.cbwName);
|
|
const complete = buildTaskOpeningText(task, "2055", {
|
|
actionId: "9002",
|
|
actionName: "暑期引导行动",
|
|
});
|
|
|
|
console.log("\n--- 模拟调用 ---");
|
|
for (const call of calls) {
|
|
console.log(`${call.method} ${call.endpoint}${queryString(call.params)}`);
|
|
console.log(`params ${JSON.stringify(call.params)}`);
|
|
}
|
|
console.log("\n--- purpose 为 null 时的用户消息 ---");
|
|
console.log(missingPurpose);
|
|
console.log("\n--- purpose 和 actor 都有值时的用户消息 ---");
|
|
console.log(complete);
|
|
console.log("---");
|
|
|
|
assert.deepEqual(
|
|
calls.map((call) => ({ endpoint: call.endpoint, params: call.params })),
|
|
[
|
|
{ endpoint: `${CONSUMER}/task-action/actionDetail`, params: { actionId: "9001" } },
|
|
{
|
|
endpoint: `${CONSUMER}/mainnets/narratives-by-name`,
|
|
params: { cbwName: "暑期文旅传播网" },
|
|
},
|
|
{ endpoint: `${CONSUMER}/task-action/actionDetail`, params: { actionId: "9002" } },
|
|
],
|
|
);
|
|
for (const label of ["目的:", "行为体:", "预期行为:", "驱动因素:", "叙事:"]) {
|
|
assert.equal(missingPurpose.includes(label), false, label);
|
|
}
|
|
assert.match(missingPurpose, /传播网简介:这条传播网主要讨论暑期文旅消费。/);
|
|
assert.equal(complete.includes("传播网简介"), false);
|
|
assert.match(complete, /目的:扩大影响/);
|
|
assert.match(complete, /叙事:叙事03/);
|
|
});
|