webRemoteControl.test.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. import { describe, expect, it } from "vitest";
  2. import { HostMessageTypes } from "../src/channels.js";
  3. import {
  4. WebRemoteControlCloseCodes,
  5. buildWebRemoteControlExternalQrUrl,
  6. matchesWebRemoteControlRoutePath,
  7. parseWebRemoteControlExternalQrParams,
  8. resolveWebRemoteControlFailureReasonFromCloseCode,
  9. resolveWebRemoteControlInitialWorkspaceSelection,
  10. resolveWebRemoteControlRoutePathFromBaseUrl,
  11. resolveWebRemoteControlWorkspaceKey,
  12. } from "../src/web-remote-control.js";
  13. import {
  14. hostIncomingMessageSchema,
  15. parseWebRemoteControlAppPayload,
  16. webRemoteControlWorkspaceTargetSchema,
  17. } from "../src/validation.js";
  18. import { encodeWebRemoteControlRpcTransportMessage } from "../src/web-remote-control-rpc-transport.js";
  19. describe("webRemoteControlWorkspaceTargetSchema", () => {
  20. it("accepts disconnected remote workspace presentation state", () => {
  21. expect(
  22. webRemoteControlWorkspaceTargetSchema.parse({
  23. workspacePath: "/workspace/app",
  24. workspaceIdentity: "remote:ssh:dev:/workspace/app",
  25. label: "app",
  26. kind: "remote",
  27. connectionState: "disconnected",
  28. lastConnectionError: "ssh connection closed",
  29. }),
  30. ).toMatchObject({
  31. connectionState: "disconnected",
  32. lastConnectionError: "ssh connection closed",
  33. });
  34. });
  35. });
  36. describe("resolveWebRemoteControlWorkspaceKey", () => {
  37. it("uses workspaceIdentity before workspacePath for isolation keys", () => {
  38. expect(
  39. resolveWebRemoteControlWorkspaceKey({
  40. workspacePath: "/workspace/app",
  41. workspaceIdentity: "ssh://dev.example.com/workspace/app",
  42. }),
  43. ).toBe("ssh://dev.example.com/workspace/app");
  44. });
  45. it("falls back to workspacePath when workspaceIdentity is empty", () => {
  46. expect(
  47. resolveWebRemoteControlWorkspaceKey({
  48. workspacePath: "/workspace/app",
  49. workspaceIdentity: " ",
  50. }),
  51. ).toBe("/workspace/app");
  52. });
  53. });
  54. describe("external relay QR helpers", () => {
  55. it("validates the optional authoritative desktop version in bootstrap responses", () => {
  56. const response = {
  57. zcode_type: "bootstrap-response",
  58. requestId: "bootstrap-1",
  59. success: true,
  60. result: { windowControlSessionId: "sid-1", workspaces: [] },
  61. };
  62. expect(parseWebRemoteControlAppPayload(response)).not.toBeNull();
  63. const result = { ...response.result, desktopAppVersion: "3.12.2" };
  64. expect(parseWebRemoteControlAppPayload({ ...response, result })).toMatchObject({ result });
  65. for (const desktopAppVersion of ["", " ", 123]) {
  66. expect(
  67. parseWebRemoteControlAppPayload({
  68. ...response,
  69. result: { ...result, desktopAppVersion },
  70. }),
  71. ).toBeNull();
  72. }
  73. });
  74. it("builds an encoded web remote control QR URL without theme params", () => {
  75. expect(
  76. buildWebRemoteControlExternalQrUrl({
  77. baseUrl: "https://zcode.z.ai/remote",
  78. deviceSid: "sid-1",
  79. deviceMid: "mid-1",
  80. deviceName: "MacBook",
  81. appVersion: "1.5.0",
  82. }),
  83. ).toBe("https://zcode.z.ai/remote?sid=sid-1&mid=mid-1&name=MacBook&app_version=1.5.0");
  84. });
  85. it("parses V2 routing hints without carrying theme state", () => {
  86. expect(
  87. parseWebRemoteControlExternalQrParams(new URLSearchParams("sid=sid-1&mid=mid-1&theme=light")),
  88. ).toEqual({ deviceSid: "sid-1", deviceMid: "mid-1" });
  89. });
  90. });
  91. describe("resolveWebRemoteControlRoutePathFromBaseUrl", () => {
  92. it("resolves the v4 QR route from the Vite remote v4 base", () => {
  93. expect(resolveWebRemoteControlRoutePathFromBaseUrl("/remote/v4/")).toBe("/remote/v4");
  94. });
  95. it("keeps the existing dev QR route when Vite uses the root base", () => {
  96. expect(resolveWebRemoteControlRoutePathFromBaseUrl("/")).toBe("/remote");
  97. });
  98. });
  99. describe("matchesWebRemoteControlRoutePath", () => {
  100. it("accepts the remote route with or without a trailing slash", () => {
  101. expect(matchesWebRemoteControlRoutePath("/remote", "/remote")).toBe(true);
  102. expect(matchesWebRemoteControlRoutePath("/remote/", "/remote")).toBe(true);
  103. });
  104. it("does not accept nested static asset paths as the app route", () => {
  105. expect(matchesWebRemoteControlRoutePath("/remote/favicon.ico", "/remote")).toBe(false);
  106. });
  107. });
  108. describe("resolveWebRemoteControlFailureReasonFromCloseCode", () => {
  109. it("maps invalid mobile connection close codes", () => {
  110. expect(
  111. resolveWebRemoteControlFailureReasonFromCloseCode(
  112. WebRemoteControlCloseCodes.InvalidMobileConnection,
  113. ),
  114. ).toBe("invalid-mobile-connection");
  115. });
  116. });
  117. describe("resolveWebRemoteControlInitialWorkspaceSelection", () => {
  118. const workspaces = [
  119. {
  120. workspacePath: "/tmp/first",
  121. label: "first",
  122. kind: "local" as const,
  123. },
  124. {
  125. workspacePath: "/tmp/second",
  126. label: "second",
  127. kind: "local" as const,
  128. },
  129. ];
  130. it("prefers the last mobile view state over the desktop launch task", () => {
  131. expect(
  132. resolveWebRemoteControlInitialWorkspaceSelection({
  133. workspaces,
  134. mobileViewState: {
  135. activeWorkspaceKey: "/tmp/second",
  136. activeTaskId: "mobile-task",
  137. updatedAt: 2,
  138. },
  139. initialViewState: {
  140. activeWorkspaceKey: "/tmp/first",
  141. activeTaskId: "desktop-task",
  142. updatedAt: 1,
  143. },
  144. }),
  145. ).toEqual({
  146. workspaceKey: "/tmp/second",
  147. taskId: "mobile-task",
  148. canBridge: true,
  149. });
  150. });
  151. it("uses the desktop launch task when the phone has no view state yet", () => {
  152. expect(
  153. resolveWebRemoteControlInitialWorkspaceSelection({
  154. workspaces,
  155. initialViewState: {
  156. activeWorkspaceKey: "/tmp/first",
  157. activeTaskId: "desktop-task",
  158. updatedAt: 1,
  159. },
  160. }),
  161. ).toEqual({
  162. workspaceKey: "/tmp/first",
  163. taskId: "desktop-task",
  164. canBridge: true,
  165. });
  166. });
  167. it("falls back to the first workspace when no selected view state is valid", () => {
  168. expect(
  169. resolveWebRemoteControlInitialWorkspaceSelection({
  170. workspaces,
  171. initialViewState: {
  172. activeWorkspaceKey: "/tmp/missing",
  173. activeTaskId: "missing-task",
  174. updatedAt: 1,
  175. },
  176. }),
  177. ).toEqual({
  178. workspaceKey: "/tmp/first",
  179. canBridge: true,
  180. });
  181. });
  182. it("skips disconnected remote workspaces when choosing the initial bridge", () => {
  183. expect(
  184. resolveWebRemoteControlInitialWorkspaceSelection({
  185. workspaces: [
  186. {
  187. workspacePath: "/remote/app",
  188. workspaceIdentity: "ssh://dev/remote/app",
  189. label: "remote",
  190. kind: "remote",
  191. connectionState: "disconnected",
  192. },
  193. {
  194. workspacePath: "/local/app",
  195. label: "local",
  196. kind: "local",
  197. },
  198. ],
  199. mobileViewState: {
  200. activeWorkspaceKey: "ssh://dev/remote/app",
  201. activeTaskId: "remote-task",
  202. updatedAt: 2,
  203. },
  204. }),
  205. ).toEqual({
  206. workspaceKey: "/local/app",
  207. canBridge: true,
  208. });
  209. });
  210. it("returns a disconnected remote workspace for home-only mobile reconnect when no bridge target exists", () => {
  211. expect(
  212. resolveWebRemoteControlInitialWorkspaceSelection({
  213. workspaces: [
  214. {
  215. workspacePath: "/remote/app",
  216. workspaceIdentity: "ssh://dev/remote/app",
  217. label: "remote",
  218. kind: "remote",
  219. connectionState: "disconnected",
  220. },
  221. ],
  222. mobileViewState: {
  223. activeWorkspaceKey: "ssh://dev/remote/app",
  224. activeTaskId: "remote-task",
  225. updatedAt: 2,
  226. },
  227. }),
  228. ).toEqual({
  229. workspaceKey: "ssh://dev/remote/app",
  230. taskId: "remote-task",
  231. canBridge: false,
  232. });
  233. });
  234. });
  235. describe("parseWebRemoteControlAppPayload", () => {
  236. it("accepts pushed workspace list updates", () => {
  237. expect(
  238. parseWebRemoteControlAppPayload({
  239. zcode_type: "workspace-list-updated",
  240. result: {
  241. workspaces: [
  242. {
  243. workspacePath: "/workspace/app",
  244. label: "app",
  245. kind: "local",
  246. },
  247. ],
  248. },
  249. }),
  250. ).toMatchObject({
  251. zcode_type: "workspace-list-updated",
  252. result: {
  253. workspaces: [expect.objectContaining({ workspacePath: "/workspace/app" })],
  254. },
  255. });
  256. });
  257. it("accepts workspace reconnect requests and responses", () => {
  258. expect(
  259. parseWebRemoteControlAppPayload({
  260. zcode_type: "workspace-reconnect-request",
  261. requestId: "reconnect-1",
  262. workspaceKey: "remote:ssh:dev:/workspace/app",
  263. }),
  264. ).toMatchObject({
  265. zcode_type: "workspace-reconnect-request",
  266. workspaceKey: "remote:ssh:dev:/workspace/app",
  267. });
  268. expect(
  269. parseWebRemoteControlAppPayload({
  270. zcode_type: "workspace-reconnect-response",
  271. requestId: "reconnect-1",
  272. workspaceKey: "remote:ssh:dev:/workspace/app",
  273. success: true,
  274. }),
  275. ).toMatchObject({
  276. zcode_type: "workspace-reconnect-response",
  277. success: true,
  278. });
  279. });
  280. it("preserves mobile device info on mobile view state updates", () => {
  281. expect(
  282. parseWebRemoteControlAppPayload({
  283. zcode_type: "mobile-view-state-update",
  284. viewState: {
  285. activeWorkspaceKey: "/workspace/app",
  286. activeTaskId: "task-1",
  287. updatedAt: 1,
  288. },
  289. deviceInfo: {
  290. platform: "web",
  291. version: "1.7.0",
  292. name: "mobile-browser",
  293. userAgent: "Mozilla/5.0 Mobile Safari/604.1",
  294. language: "zh-CN",
  295. languages: ["zh-CN", "en-US"],
  296. browserPlatform: "iPhone",
  297. viewport: {
  298. width: 390,
  299. height: 844,
  300. devicePixelRatio: 3,
  301. },
  302. screen: {
  303. width: 390,
  304. height: 844,
  305. },
  306. timezone: "Asia/Shanghai",
  307. online: true,
  308. updatedAt: 2,
  309. },
  310. }),
  311. ).toMatchObject({
  312. zcode_type: "mobile-view-state-update",
  313. deviceInfo: {
  314. platform: "web",
  315. userAgent: "Mozilla/5.0 Mobile Safari/604.1",
  316. viewport: {
  317. width: 390,
  318. height: 844,
  319. },
  320. },
  321. });
  322. });
  323. it("removes the legacy one-shot rpc-frame branch from the production app parser", () => {
  324. expect(
  325. parseWebRemoteControlAppPayload({
  326. zcode_type: "rpc-frame",
  327. bridgeSessionId: "bridge-1",
  328. bridgeGeneration: 2,
  329. recoveryId: "recovery-1",
  330. seq: 1,
  331. dataBase64: "AQID",
  332. }),
  333. ).toBeNull();
  334. });
  335. it("accepts strict acknowledged rpc fragments and cumulative ACKs in the production app parser", () => {
  336. const [fragment] = encodeWebRemoteControlRpcTransportMessage(new Uint8Array([1, 2, 3]), {
  337. bridgeSessionId: "bridge-1",
  338. bridgeGeneration: 2,
  339. recoveryId: "recovery-1",
  340. firstPhysicalSeq: 1,
  341. messageSeq: 1,
  342. });
  343. expect(parseWebRemoteControlAppPayload(fragment)).toEqual(fragment);
  344. expect(
  345. parseWebRemoteControlAppPayload({
  346. zcode_type: "rpc-frame-ack",
  347. bridgeSessionId: "bridge-1",
  348. bridgeGeneration: 2,
  349. recoveryId: "recovery-1",
  350. ackMessageSeq: 1,
  351. }),
  352. ).toEqual({
  353. zcode_type: "rpc-frame-ack",
  354. bridgeSessionId: "bridge-1",
  355. bridgeGeneration: 2,
  356. recoveryId: "recovery-1",
  357. ackMessageSeq: 1,
  358. });
  359. expect(
  360. parseWebRemoteControlAppPayload({
  361. zcode_type: "rpc-frame",
  362. bridgeSessionId: "bridge-1",
  363. seq: 1,
  364. dataBase64: "AQID",
  365. fragmentCount: 1,
  366. }),
  367. ).toBeNull();
  368. });
  369. it("bounds rpc-frame transport ids so the physical envelope meter is authoritative", () => {
  370. // 生成器使用 prefix + UUID / Date / Math.random,只产生这组 JSON-safe ASCII。
  371. // 所有合法字符均为单字节且无需 JSON escaping;`~` 覆盖字符集边界。
  372. const boundary = "~".repeat(256);
  373. const [boundaryFrame] = encodeWebRemoteControlRpcTransportMessage(new Uint8Array([1, 2, 3]), {
  374. bridgeSessionId: boundary,
  375. recoveryId: boundary,
  376. bridgeGeneration: Number.MAX_SAFE_INTEGER,
  377. firstPhysicalSeq: Number.MAX_SAFE_INTEGER,
  378. messageSeq: Number.MAX_SAFE_INTEGER,
  379. });
  380. expect(parseWebRemoteControlAppPayload(boundaryFrame)).toEqual(boundaryFrame);
  381. expect(
  382. parseWebRemoteControlAppPayload({
  383. ...boundaryFrame,
  384. bridgeSessionId: `${boundary}x`,
  385. }),
  386. ).toBeNull();
  387. expect(
  388. parseWebRemoteControlAppPayload({
  389. ...boundaryFrame,
  390. recoveryId: `${boundary}x`,
  391. }),
  392. ).toBeNull();
  393. for (const invalid of ['"', "\\", "\n", "中", "🙂"]) {
  394. expect(
  395. parseWebRemoteControlAppPayload({
  396. ...boundaryFrame,
  397. bridgeSessionId: `bridge-${invalid}-id`,
  398. }),
  399. ).toBeNull();
  400. expect(
  401. parseWebRemoteControlAppPayload({
  402. ...boundaryFrame,
  403. recoveryId: `recovery-${invalid}-id`,
  404. }),
  405. ).toBeNull();
  406. }
  407. for (const key of ["bridgeGeneration", "seq", "messageSeq"] as const) {
  408. expect(
  409. parseWebRemoteControlAppPayload({
  410. ...boundaryFrame,
  411. [key]: Number.MAX_SAFE_INTEGER + 1,
  412. }),
  413. ).toBeNull();
  414. }
  415. });
  416. it("accepts bridge degraded recovery payloads", () => {
  417. expect(
  418. parseWebRemoteControlAppPayload({
  419. zcode_type: "bridge-degraded",
  420. bridgeSessionId: "bridge-1",
  421. reason: "buffer-timeout",
  422. droppedCount: 1,
  423. }),
  424. ).toMatchObject({
  425. zcode_type: "bridge-degraded",
  426. bridgeSessionId: "bridge-1",
  427. reason: "buffer-timeout",
  428. droppedCount: 1,
  429. });
  430. expect(
  431. parseWebRemoteControlAppPayload({
  432. zcode_type: "bridge-degraded",
  433. bridgeSessionId: "bridge-1",
  434. bridgeGeneration: 2,
  435. recoveryId: "recovery-1",
  436. reason: "rpc-transport-fault",
  437. }),
  438. ).toMatchObject({
  439. zcode_type: "bridge-degraded",
  440. bridgeSessionId: "bridge-1",
  441. bridgeGeneration: 2,
  442. recoveryId: "recovery-1",
  443. reason: "rpc-transport-fault",
  444. });
  445. // 兼容旧 desktop 在原子升级窗口内仍可能发送的细分枚举。
  446. expect(
  447. parseWebRemoteControlAppPayload({
  448. zcode_type: "bridge-degraded",
  449. bridgeSessionId: "bridge-legacy",
  450. reason: "rpc-frame-gap",
  451. }),
  452. ).not.toBeNull();
  453. });
  454. it("preserves bridge metadata on workspace bridge payloads", () => {
  455. expect(
  456. parseWebRemoteControlAppPayload({
  457. zcode_type: "workspace-bridge-open",
  458. requestId: "bridge-open-1",
  459. bridgeSessionId: "bridge-1",
  460. bridgeGeneration: 2,
  461. recoveryId: "recovery-1",
  462. workspaceKey: "/workspace/app",
  463. taskId: "task-1",
  464. }),
  465. ).toMatchObject({
  466. zcode_type: "workspace-bridge-open",
  467. bridgeGeneration: 2,
  468. recoveryId: "recovery-1",
  469. });
  470. expect(
  471. parseWebRemoteControlAppPayload({
  472. zcode_type: "workspace-bridge-ready",
  473. requestId: "bridge-open-1",
  474. bridgeSessionId: "bridge-1",
  475. bridgeGeneration: 2,
  476. recoveryId: "recovery-1",
  477. bridge: {
  478. bridgeSessionId: "bridge-1",
  479. bridgeGeneration: 2,
  480. recoveryId: "recovery-1",
  481. kind: "local",
  482. workspaceKey: "/workspace/app",
  483. workspacePath: "/workspace/app",
  484. },
  485. }),
  486. ).toMatchObject({
  487. zcode_type: "workspace-bridge-ready",
  488. bridgeGeneration: 2,
  489. recoveryId: "recovery-1",
  490. bridge: {
  491. bridgeGeneration: 2,
  492. recoveryId: "recovery-1",
  493. },
  494. });
  495. });
  496. it("accepts low-frequency mobile diagnostic payloads", () => {
  497. expect(
  498. parseWebRemoteControlAppPayload({
  499. zcode_type: "mobile-diagnostic",
  500. event: "pair-status",
  501. timestamp: 1710000000000,
  502. state: "paired",
  503. pairStatus: "waiting",
  504. visibilityState: "visible",
  505. online: true,
  506. }),
  507. ).toMatchObject({
  508. zcode_type: "mobile-diagnostic",
  509. event: "pair-status",
  510. pairStatus: "waiting",
  511. });
  512. });
  513. it("rejects pair status values that are not emitted by the external relay", () => {
  514. expect(
  515. parseWebRemoteControlAppPayload({
  516. zcode_type: "mobile-diagnostic",
  517. event: "pair-status",
  518. timestamp: 1710000000000,
  519. state: "paired",
  520. pairStatus: "rejected",
  521. }),
  522. ).toBeNull();
  523. });
  524. });
  525. describe("hostIncomingMessageSchema", () => {
  526. it("accepts attach service port host messages", () => {
  527. expect(
  528. hostIncomingMessageSchema.parse({
  529. type: HostMessageTypes.AttachServicePort,
  530. requestId: "request-1",
  531. attachmentId: "attachment-1",
  532. clientMode: "web-remote-replayable",
  533. scope: { kind: "local" },
  534. }),
  535. ).toEqual({
  536. type: HostMessageTypes.AttachServicePort,
  537. requestId: "request-1",
  538. attachmentId: "attachment-1",
  539. clientMode: "web-remote-replayable",
  540. scope: { kind: "local" },
  541. });
  542. });
  543. it("accepts explicit service port detach messages", () => {
  544. expect(
  545. hostIncomingMessageSchema.parse({
  546. type: HostMessageTypes.DetachServicePort,
  547. attachmentId: "attachment-1",
  548. }),
  549. ).toEqual({
  550. type: HostMessageTypes.DetachServicePort,
  551. attachmentId: "attachment-1",
  552. });
  553. });
  554. });
  555. describe("project bridge intent validation", () => {
  556. const request = {
  557. zcode_type: "workspace-bridge-open",
  558. requestId: "request",
  559. bridgeSessionId: "bridge",
  560. workspaceKey: "/current",
  561. project: { projectId: null, hostId: "local" },
  562. };
  563. it("preserves the project intent and rejects arbitrary directory injection", () => {
  564. expect(parseWebRemoteControlAppPayload(request)).toMatchObject({ project: request.project });
  565. expect(
  566. parseWebRemoteControlAppPayload({
  567. ...request,
  568. project: { ...request.project, workspacePath: "/injected" },
  569. }),
  570. ).toBeNull();
  571. expect(parseWebRemoteControlAppPayload({ ...request, taskId: "task" })).toBeNull();
  572. });
  573. });
  574. it("accepts a task address for Host admission and rejects mixed draft/task intent", () => {
  575. const request = {
  576. type: HostMessageTypes.PrepareProjectWorkspace,
  577. requestId: "task-navigation",
  578. clientMode: "web-remote-replayable",
  579. hostId: "local",
  580. task: { taskId: "task", workspaceKey: "/original" },
  581. };
  582. expect(hostIncomingMessageSchema.safeParse(request).success).toBe(true);
  583. expect(
  584. hostIncomingMessageSchema.safeParse({ ...request, projectId: "different-project" }).success,
  585. ).toBe(false);
  586. expect(
  587. hostIncomingMessageSchema.safeParse({
  588. ...request,
  589. task: { ...request.task, workspacePath: "/injected" },
  590. }).success,
  591. ).toBe(false);
  592. });
  593. it("MRO02:远端 bridge 保留可选窗口目录能力,旧 bridge 不虚构能力", () => {
  594. const payload = {
  595. zcode_type: "workspace-bridge-ready",
  596. requestId: "request-1",
  597. bridgeSessionId: "bridge-1",
  598. bridge: {
  599. kind: "remote",
  600. bridgeSessionId: "bridge-1",
  601. workspaceKey: "remote:server:source:/repo",
  602. workspacePath: "/repo",
  603. workspaceIdentity: "remote:server:source:/repo",
  604. remoteSessionId: "remote-1",
  605. capabilities: { windowProjectDirectory: true },
  606. },
  607. };
  608. expect(parseWebRemoteControlAppPayload(payload)).toMatchObject({
  609. bridge: { capabilities: { windowProjectDirectory: true } },
  610. });
  611. expect(
  612. parseWebRemoteControlAppPayload({
  613. ...payload,
  614. bridge: { ...payload.bridge, capabilities: undefined },
  615. }),
  616. ).toMatchObject({ bridge: { kind: "remote" } });
  617. expect(
  618. parseWebRemoteControlAppPayload({
  619. ...payload,
  620. bridge: { ...payload.bridge, capabilities: { windowProjectDirectory: "true" } },
  621. }),
  622. ).toBeNull();
  623. });
  624. it("MRO03:可见任务报告包含完整地址和传输代际,离开必须显式 null", () => {
  625. const report = {
  626. zcode_type: "mobile-task-view-update",
  627. epoch: "pair-1",
  628. seq: 1,
  629. bridgeSessionId: "bridge-1",
  630. bridgeGeneration: 2,
  631. view: {
  632. workspacePath: "/repo",
  633. workspaceIdentity: "remote:server:source:/repo",
  634. taskId: "task",
  635. },
  636. };
  637. expect(parseWebRemoteControlAppPayload(report)).toEqual(report);
  638. expect(parseWebRemoteControlAppPayload({ ...report, view: null })).toEqual({
  639. ...report,
  640. view: null,
  641. });
  642. expect(parseWebRemoteControlAppPayload({ ...report, view: { taskId: "task" } })).toBeNull();
  643. for (const seq of [-1, 1.5, Number.MAX_SAFE_INTEGER + 1])
  644. expect(parseWebRemoteControlAppPayload({ ...report, seq })).toBeNull();
  645. expect(parseWebRemoteControlAppPayload({ ...report, epoch: "" })).toBeNull();
  646. expect(
  647. parseWebRemoteControlAppPayload({ zcode_type: "mobile-task-view-request", epoch: "pair-1" }),
  648. ).toEqual({ zcode_type: "mobile-task-view-request", epoch: "pair-1" });
  649. expect(
  650. parseWebRemoteControlAppPayload({
  651. zcode_type: "bootstrap-response",
  652. requestId: "b",
  653. success: true,
  654. result: { windowControlSessionId: "session", workspaces: [], mobileTaskViewEpoch: "pair-1" },
  655. }),
  656. ).toMatchObject({ result: { mobileTaskViewEpoch: "pair-1" } });
  657. });
  658. it("MRO03:客户端不能在可见任务报告中伪造远端 session", () => {
  659. expect(
  660. parseWebRemoteControlAppPayload({
  661. zcode_type: "mobile-task-view-update",
  662. epoch: "epoch",
  663. seq: 1,
  664. bridgeSessionId: "bridge",
  665. bridgeGeneration: 1,
  666. view: {
  667. workspacePath: "/repo",
  668. workspaceIdentity: "remote:ssh:host:/repo",
  669. taskId: "task",
  670. remoteSessionId: "forged-session",
  671. },
  672. }),
  673. ).toBeNull();
  674. });