client-scenes-mock-server.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import { once } from "node:events";
  2. import { createServer, type ServerResponse } from "node:http";
  3. import type { ClientScenesResponse } from "@zcode/services";
  4. /** 只给需要验证真实模板网格的 case 注入 Client Scenes,避免布局断言依赖远端目录。 */
  5. export const CLIENT_SCENES_AUTOMATIONS_HOME_SPEC = "off-peak-automations-home-layout.test.ts";
  6. export const AUTOMATIONS_HOME_CLIENT_SCENES_FIXTURE = {
  7. code: 0,
  8. msg: "",
  9. data: [
  10. {
  11. namespace: "zcode",
  12. scene: "off-peak-task",
  13. options: {
  14. prompts: {
  15. id: "off-peak-automations-home-prompts",
  16. type: "prompts",
  17. contents: { cn: "闲时任务模板", en: "Idle-time task template" },
  18. items: [
  19. {
  20. id: "item-standupGitSummary",
  21. type: "prompts",
  22. contents: {
  23. cn: "汇总当前项目的 Git 变更并列出需要跟进的事项。",
  24. en: "Summarize the current project's Git changes and follow-ups.",
  25. },
  26. labels: { cn: "Git 变更摘要", en: "Git change summary" },
  27. img: "git-branch",
  28. },
  29. {
  30. id: "item-ciFlakyReport",
  31. type: "prompts",
  32. contents: {
  33. cn: "检查最近的 CI 失败并整理疑似不稳定测试。",
  34. en: "Inspect recent CI failures and list suspected flaky tests.",
  35. },
  36. labels: { cn: "CI 不稳定报告", en: "CI flaky report" },
  37. img: "activity",
  38. },
  39. {
  40. id: "item-documentationSyncCheck",
  41. type: "prompts",
  42. contents: {
  43. cn: "检查项目文档与当前实现是否保持同步。",
  44. en: "Check whether the project documentation matches the implementation.",
  45. },
  46. labels: { cn: "文档同步检查", en: "Documentation sync check" },
  47. img: "file-text",
  48. },
  49. ],
  50. },
  51. },
  52. },
  53. {
  54. namespace: "zcode",
  55. scene: "scheduled-task",
  56. options: {
  57. prompts: {
  58. id: "scheduled-automations-home-prompts",
  59. type: "prompts",
  60. contents: { cn: "定时任务模板", en: "Scheduled task template" },
  61. items: [
  62. {
  63. id: "item-morningDevBrief",
  64. type: "prompts",
  65. contents: {
  66. cn: "汇总今天的代码变更和待跟进事项。",
  67. en: "Summarize today's code changes and follow-ups.",
  68. },
  69. labels: { cn: "晨会动态", en: "Morning dev brief" },
  70. defaults: { cronExpr: ["item-morningDevBrief-cron"] },
  71. img: "target",
  72. },
  73. {
  74. id: "item-noonRiskReview",
  75. type: "prompts",
  76. contents: {
  77. cn: "检查当前项目的风险与阻塞项。",
  78. en: "Review risks and blockers in the current project.",
  79. },
  80. labels: { cn: "午间风险检查", en: "Noon risk review" },
  81. defaults: { cronExpr: ["item-noonRiskReview-cron"] },
  82. img: "activity",
  83. },
  84. {
  85. id: "item-eveningReleaseNotes",
  86. type: "prompts",
  87. contents: {
  88. cn: "整理今天的发布说明草稿。",
  89. en: "Draft release notes for today's changes.",
  90. },
  91. labels: { cn: "晚间发布说明", en: "Evening release notes" },
  92. defaults: { cronExpr: ["item-eveningReleaseNotes-cron"] },
  93. img: "file-text",
  94. },
  95. {
  96. id: "item-weeklyProjectList",
  97. type: "prompts",
  98. contents: {
  99. cn: "汇总本周项目进展与下周计划。",
  100. en: "Summarize this week's project progress and next week's plan.",
  101. },
  102. labels: { cn: "每周项目清单", en: "Weekly project list" },
  103. defaults: { cronExpr: ["item-weeklyProjectList-cron"] },
  104. img: "list",
  105. },
  106. ],
  107. },
  108. cronExpr: {
  109. id: "scheduled-automations-home-cron",
  110. type: "cronExpr",
  111. contents: { cn: "调度规则", en: "Schedule" },
  112. items: [
  113. {
  114. id: "item-morningDevBrief-cron",
  115. type: "cronExpr",
  116. contents: { cn: "0 9 * * 1-5", en: "0 9 * * 1-5" },
  117. labels: { cn: "工作日早上", en: "Weekday morning" },
  118. },
  119. {
  120. id: "item-noonRiskReview-cron",
  121. type: "cronExpr",
  122. contents: { cn: "0 12 * * 1-5", en: "0 12 * * 1-5" },
  123. labels: { cn: "工作日中午", en: "Weekday noon" },
  124. },
  125. {
  126. id: "item-eveningReleaseNotes-cron",
  127. type: "cronExpr",
  128. contents: { cn: "0 18 * * 1-5", en: "0 18 * * 1-5" },
  129. labels: { cn: "工作日傍晚", en: "Weekday evening" },
  130. },
  131. {
  132. id: "item-weeklyProjectList-cron",
  133. type: "cronExpr",
  134. contents: { cn: "0 9 * * 1", en: "0 9 * * 1" },
  135. labels: { cn: "每周一", en: "Monday" },
  136. },
  137. ],
  138. },
  139. },
  140. },
  141. ],
  142. } satisfies ClientScenesResponse;
  143. export interface ClientScenesMockServer {
  144. baseUrl: string;
  145. stop(): Promise<void>;
  146. }
  147. export function applyClientScenesMockEnv(
  148. environment: Record<string, string | undefined>,
  149. baseUrl: string,
  150. ): void {
  151. environment.ZCODE_BASE_URL = baseUrl;
  152. environment.ZCODE_TEST_BASE_URL = baseUrl;
  153. environment.ZCODE_ENDPOINT_ORIGIN = baseUrl;
  154. }
  155. export function clearClientScenesMockEnv(
  156. environment: Record<string, string | undefined>,
  157. baseUrl: string,
  158. ): void {
  159. for (const key of ["ZCODE_BASE_URL", "ZCODE_TEST_BASE_URL", "ZCODE_ENDPOINT_ORIGIN"] as const) {
  160. if (environment[key] === baseUrl) {
  161. delete environment[key];
  162. }
  163. }
  164. }
  165. export function shouldUseClientScenesMock(specs: readonly string[]): boolean {
  166. return specs.some((spec) => spec.includes(CLIENT_SCENES_AUTOMATIONS_HOME_SPEC));
  167. }
  168. export async function startClientScenesMockServer(): Promise<ClientScenesMockServer> {
  169. const server = createServer((request, response) => {
  170. const url = new URL(request.url ?? "/", "http://127.0.0.1");
  171. if (request.method === "GET" && url.pathname === "/api/v1/client/scenes") {
  172. writeJson(response, AUTOMATIONS_HOME_CLIENT_SCENES_FIXTURE);
  173. return;
  174. }
  175. if (request.method === "GET" && url.pathname === "/api/v1/client/configs") {
  176. // 启动同步与 Client Scenes 共用 endpoint origin;无关配置给合法空快照,避免把
  177. // fixture 缺失误报成启动失败。
  178. writeJson(response, { code: 0, data: { configs: {} }, msg: "", success: true });
  179. return;
  180. }
  181. if (request.method === "GET" && url.pathname === "/api/v2/releases/latest") {
  182. writeJson(response, { config_version: "e2e-client-scenes-v1", version: "0.0.0" });
  183. return;
  184. }
  185. if (request.method === "GET" && url.pathname === "/api/v1/zcode-plan/billing/balance") {
  186. writeJson(response, { code: 0, data: { balances: [], plans: [] }, msg: "", success: true });
  187. return;
  188. }
  189. writeJson(
  190. response,
  191. {
  192. code: 404,
  193. data: null,
  194. msg: `Unhandled Client Scenes E2E path: ${request.method} ${url.pathname}`,
  195. },
  196. 404,
  197. );
  198. });
  199. server.listen(0, "127.0.0.1");
  200. await once(server, "listening");
  201. const address = server.address();
  202. if (!address || typeof address === "string") {
  203. throw new Error("Client Scenes E2E mock server did not bind to a TCP port");
  204. }
  205. return {
  206. baseUrl: `http://127.0.0.1:${address.port}`,
  207. stop: async () => {
  208. server.close();
  209. await once(server, "close");
  210. },
  211. };
  212. }
  213. function writeJson(response: ServerResponse, payload: unknown, statusCode = 200): void {
  214. response.writeHead(statusCode, {
  215. "cache-control": "no-store",
  216. "content-type": "application/json; charset=utf-8",
  217. });
  218. response.end(JSON.stringify(payload));
  219. }