remote-device-switch.helpers.mjs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 = {}, exposeChannels },
  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. const channels = new Map();
  33. for (const value of Object.values(contracts)) {
  34. if (!value || typeof value !== "object" || typeof value.channelName !== "string") continue;
  35. const channel = value.channelName;
  36. channels.set(channel, {
  37. listen: (_ctx, event) => events[`${channel}:${event}`] ?? Event.None,
  38. call: async (_ctx, command, args) => {
  39. const key = `${channel}:${command}`;
  40. if (Object.hasOwn(responses, key)) {
  41. const response = responses[key];
  42. return typeof response === "function" ? response(...(args ?? [])) : response;
  43. }
  44. if (Object.hasOwn(replies, key)) return replies[key];
  45. if (["initialize", "initializeWorkspace"].includes(command))
  46. return { available: false, workspaceKey: bridge.workspaceKey };
  47. if (
  48. [
  49. "listTasks",
  50. "listPinnedTaskIds",
  51. "listPinnedTasks",
  52. "listArchivedTasks",
  53. "list",
  54. "getProviders",
  55. "getMcpServers",
  56. "getCustomAgents",
  57. "getCommands",
  58. "getSkills",
  59. "getBuiltinCommands",
  60. ].includes(command)
  61. )
  62. return [];
  63. if (command === "listTaskList") return { items: [], total: 0, hasMore: false };
  64. // 项目侧栏(手机 home 与宽屏共用)读取远端项目目录;本用例只验证设备切换,返回空目录。
  65. if (command === "readDirectory") return { hosts: [], entries: [] };
  66. if (command === "readTree") return { projects: [], tasks: [] };
  67. if (
  68. [
  69. "getTaskMeta",
  70. "getTaskSnapshot",
  71. "load",
  72. "getActiveProvider",
  73. "getSelection",
  74. "shouldOnboard",
  75. ].includes(command)
  76. )
  77. return null;
  78. if (command === "getView" || command === "refresh") return { revision: 0, providers: [] };
  79. if (command === "getWorkspaceSummary")
  80. return {
  81. workspacePath: bridge.workspacePath,
  82. isRepository: false,
  83. isGitAvailable: false,
  84. autoRefreshWatchPaths: [],
  85. };
  86. if (command === "getWorkspacePath") return bridge.workspacePath;
  87. if (command === "getDeviceId") return "e2e-desktop";
  88. if (command === "ensureDefaultProject")
  89. return { path: bridge.workspacePath, created: false };
  90. if (
  91. [
  92. "update",
  93. "send",
  94. "track",
  95. "report",
  96. "releaseWorkspacePreparation",
  97. "watch",
  98. "disposeAll",
  99. ].includes(command)
  100. )
  101. return undefined;
  102. // 非导航相关初始化仍显式拒绝,避免把缺少桌面响应伪装为测试成功。
  103. throw new Error(`Device switch fixture does not implement ${key}`);
  104. },
  105. });
  106. }
  107. // 服务边界用例可复用同一业务替身,再经过真实 Host attachment 装配;其他用例保持原行为。
  108. if (exposeChannels) exposeChannels(server, channels);
  109. else for (const [name, channel] of channels) server.registerChannel(name, channel);
  110. return {
  111. acceptPayload: (payload) => adapter.acceptPayload(payload),
  112. onDegraded: adapter.onDegraded,
  113. dispose: () => {
  114. server.dispose();
  115. adapter.dispose();
  116. },
  117. };
  118. }