test-actions.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import type { TaskChatMessage as ChatMessage } from "@/lib/taskChatMessageTypes.js";
  2. import { shouldExposeE2EStoreBridge } from "@/lib/e2eStoreBridge.js";
  3. import type { IBotsService, IZCodeAgentService, IZCodeTaskService } from "@zcode/services";
  4. import type { TaskListE2EActions } from "@/lib/taskListE2EActions.js";
  5. import { useEffect } from "react";
  6. import type { Locale } from "@zcode/shared";
  7. // Vite 注入的 import.meta.env 类型声明
  8. declare global {
  9. interface ImportMeta {
  10. env: { PROD: boolean; DEV: boolean; [key: string]: unknown };
  11. }
  12. }
  13. /**
  14. * E2E 测试可通过 window.__testActions 调用的操作集合。
  15. * 仅在非 production 环境下注册,避免泄露到生产。
  16. */
  17. export interface TestActions extends TaskListE2EActions {
  18. /** 受控 E2E 输入走真实 Host Bot 服务与 CLI admission,不伪造队列投影。 */
  19. sendBotMessage: IBotsService["handleInboundMessage"];
  20. /** 受控材料夹具走真实任务服务、上传与 CLI 队列,不注入 UI row。 */
  21. submitBotGroupInput: NonNullable<IZCodeTaskService["submitBotGroupInput"]>;
  22. /** 获取当前主题 */
  23. getTheme: () => string;
  24. /** 设置主题 */
  25. setTheme: (theme: "light" | "dark" | "zai-light" | "zai-dark" | "system") => void;
  26. /** 获取当前语言,仅供跨语言展示 E2E */
  27. getLocale: () => Locale;
  28. /** 设置当前语言,仅供跨语言展示 E2E */
  29. setLocale: (locale: Locale) => void;
  30. /** 注入聊天展示用的 mock 消息 */
  31. setChatMessages: (messages: ChatMessage[]) => void;
  32. /** 获取当前 mock 消息数量 */
  33. getChatMessageCount: () => number;
  34. /** E2E 通过真实 zcodeAgentService 拉取插件 overview */
  35. getPluginsOverview: IZCodeAgentService["getPluginsOverview"];
  36. /** E2E 通过真实 zcodeAgentService 添加 marketplace */
  37. addPluginMarketplace: IZCodeAgentService["addPluginMarketplace"];
  38. /** E2E 通过真实 zcodeAgentService 刷新 marketplace */
  39. updatePluginMarketplace: IZCodeAgentService["updatePluginMarketplace"];
  40. /** E2E 通过真实 zcodeAgentService 安装 marketplace plugin */
  41. installPlugin: IZCodeAgentService["installPlugin"];
  42. /** E2E 通过真实 zcodeAgentService 触发插件 discover */
  43. listPlugins: IZCodeAgentService["listPlugins"];
  44. /** E2E 通过真实 zcodeAgentService 查询 Workspace/Session Plugin catalog */
  45. getPluginReferenceCatalog: IZCodeAgentService["getPluginReferenceCatalog"];
  46. }
  47. declare global {
  48. interface Window {
  49. __testActions?: TestActions;
  50. }
  51. }
  52. /**
  53. * 将 test actions 注册到 window.__testActions。
  54. * 仅在 import.meta.env.PROD 为 true 时跳过注册(Vite 环境)。
  55. */
  56. export function useTestActions(actions: TestActions) {
  57. useEffect(() => {
  58. // E2E 的 production renderer 也需要服务入口;只能由专用 bridge 开关打开,避免普通生产包泄露。
  59. if (import.meta.env.PROD && !shouldExposeE2EStoreBridge()) return;
  60. window.__testActions = actions;
  61. return () => {
  62. actions.releaseTaskMembershipRefreshHold();
  63. delete window.__testActions;
  64. };
  65. }, [actions]);
  66. }