remote-device-switch.helpers.mjs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // 设备导航验证用桌面 RPC 替身;复用真实帧协议,不模拟 UI 或修改产品模块。
  2. import { createAcknowledgedWebRemoteControlRelayProtocol } from "@zcode/client";
  3. import * as contracts from "../../../../../services/src/index.ts";
  4. import { ChannelServer, Event } from "../../../../../rpc/src/index.ts";
  5. export function createDeviceSwitchDesktopBridge(
  6. socket,
  7. bridge,
  8. { locale, theme, responses = {}, events = {} },
  9. ) {
  10. const adapter = createAcknowledgedWebRemoteControlRelayProtocol({
  11. bridgeSessionId: bridge.bridgeSessionId,
  12. bridgeGeneration: bridge.bridgeGeneration,
  13. sendFrame: (payload) => {
  14. socket.send(JSON.stringify({ type: "data", payload }));
  15. return true;
  16. },
  17. });
  18. const server = new ChannelServer(adapter.protocol, "device-switch-e2e");
  19. const replies = {
  20. [`${contracts.ISettingService.channelName}:get`]: {
  21. locale,
  22. theme,
  23. recentProjects: [],
  24. onboardingOccupation: "other",
  25. },
  26. [`${contracts.IOAuthService.channelName}:getProviders`]: [],
  27. [`${contracts.IOAuthService.channelName}:getActiveProvider`]: null,
  28. [`${contracts.IOAuthService.channelName}:restoreCachedSession`]: null,
  29. [`${contracts.IOAuthService.channelName}:restoreSession`]: null,
  30. [`${contracts.IOAuthService.channelName}:restoreCachedSessionState`]: { status: "signed-out" },
  31. };
  32. for (const value of Object.values(contracts)) {
  33. if (!value || typeof value !== "object" || typeof value.channelName !== "string") continue;
  34. const channel = value.channelName;
  35. server.registerChannel(channel, {
  36. listen: (_ctx, event) => events[`${channel}:${event}`] ?? Event.None,
  37. call: async (_ctx, command, args) => {
  38. const key = `${channel}:${command}`;
  39. if (Object.hasOwn(responses, key)) {
  40. const response = responses[key];
  41. return typeof response === "function" ? response(...(args ?? [])) : response;
  42. }
  43. if (Object.hasOwn(replies, key)) return replies[key];
  44. if (["initialize", "initializeWorkspace"].includes(command))
  45. return { available: false, workspaceKey: bridge.workspaceKey };
  46. if (
  47. [
  48. "listTasks",
  49. "listPinnedTaskIds",
  50. "listPinnedTasks",
  51. "listArchivedTasks",
  52. "list",
  53. "getProviders",
  54. "getMcpServers",
  55. "getCustomAgents",
  56. "getCommands",
  57. "getSkills",
  58. "getBuiltinCommands",
  59. ].includes(command)
  60. )
  61. return [];
  62. if (command === "listTaskList") return { items: [], total: 0, hasMore: false };
  63. // 项目侧栏(手机 home 与宽屏共用)读取远端项目目录;本用例只验证设备切换,返回空目录。
  64. if (command === "readDirectory") return { hosts: [], entries: [] };
  65. if (command === "readTree") return { projects: [], tasks: [] };
  66. if (
  67. [
  68. "getTaskMeta",
  69. "getTaskSnapshot",
  70. "load",
  71. "getActiveProvider",
  72. "getSelection",
  73. "shouldOnboard",
  74. ].includes(command)
  75. )
  76. return null;
  77. if (command === "getView" || command === "refresh") return { revision: 0, providers: [] };
  78. if (command === "getWorkspaceSummary")
  79. return {
  80. workspacePath: bridge.workspacePath,
  81. isRepository: false,
  82. isGitAvailable: false,
  83. autoRefreshWatchPaths: [],
  84. };
  85. if (command === "getWorkspacePath") return bridge.workspacePath;
  86. if (command === "getDeviceId") return "e2e-desktop";
  87. if (command === "ensureDefaultProject")
  88. return { path: bridge.workspacePath, created: false };
  89. if (
  90. [
  91. "update",
  92. "send",
  93. "track",
  94. "report",
  95. "releaseWorkspacePreparation",
  96. "watch",
  97. "disposeAll",
  98. ].includes(command)
  99. )
  100. return undefined;
  101. // 非导航相关初始化仍显式拒绝,避免把缺少桌面响应伪装为测试成功。
  102. throw new Error(`Device switch fixture does not implement ${key}`);
  103. },
  104. });
  105. }
  106. return {
  107. acceptPayload: (payload) => adapter.acceptPayload(payload),
  108. dispose: () => {
  109. server.dispose();
  110. adapter.dispose();
  111. },
  112. };
  113. }