webRemoteControl.test.ts 20 KB

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