webRemoteControlRpcTransport.test.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. WEB_REMOTE_CONTROL_RPC_TRANSPORT_LIMITS,
  4. WebRemoteControlRpcTransportAssembler,
  5. WebRemoteControlRpcTransportEncodingError,
  6. encodeWebRemoteControlRpcTransportMessage,
  7. measureWebRemoteControlRpcRelayEnvelopeBytes,
  8. parseWebRemoteControlRpcRelayEnvelopeJson,
  9. webRemoteControlRpcRelayEnvelopeSchema,
  10. webRemoteControlRpcTransportAckSchema,
  11. webRemoteControlRpcTransportFrameSchema,
  12. webRemoteControlRpcTransportPayloadSchema,
  13. type WebRemoteControlRpcTransportFramePayload,
  14. type WebRemoteControlRpcTransportIdentity,
  15. } from "../src/web-remote-control-rpc-transport.js";
  16. const identity: WebRemoteControlRpcTransportIdentity = {
  17. bridgeSessionId: "bridge-1",
  18. bridgeGeneration: 2,
  19. recoveryId: "recovery-1",
  20. };
  21. function frame(
  22. overrides: Partial<WebRemoteControlRpcTransportFramePayload> = {},
  23. ): WebRemoteControlRpcTransportFramePayload {
  24. return {
  25. zcode_type: "rpc-frame",
  26. ...identity,
  27. seq: 1,
  28. messageSeq: 1,
  29. fragmentIndex: 0,
  30. fragmentCount: 1,
  31. messageBytes: 3,
  32. checksum: { algorithm: "crc32", value: "55bc801d" },
  33. dataBase64: "AQID",
  34. ...overrides,
  35. };
  36. }
  37. function encoderOptions(
  38. overrides: Partial<{
  39. bridgeSessionId: string;
  40. bridgeGeneration: number;
  41. recoveryId: string;
  42. firstPhysicalSeq: number;
  43. messageSeq: number;
  44. maxPhysicalFrameBytes: number;
  45. maxMessageBytes: number;
  46. maxFragments: number;
  47. }> = {},
  48. ) {
  49. return {
  50. ...identity,
  51. firstPhysicalSeq: 1,
  52. messageSeq: 1,
  53. ...overrides,
  54. };
  55. }
  56. describe("mobile raw RPC transport schemas", () => {
  57. it("严格接受合法 frame/ACK,拒绝多余字段、非法联动与非安全整数", () => {
  58. expect(webRemoteControlRpcTransportFrameSchema.safeParse(frame()).success).toBe(true);
  59. expect(
  60. webRemoteControlRpcTransportAckSchema.safeParse({
  61. zcode_type: "rpc-frame-ack",
  62. ...identity,
  63. ackMessageSeq: 1,
  64. }).success,
  65. ).toBe(true);
  66. expect(webRemoteControlRpcTransportPayloadSchema.safeParse(frame()).success).toBe(true);
  67. expect(
  68. webRemoteControlRpcTransportFrameSchema.safeParse({ ...frame(), extra: true }).success,
  69. ).toBe(false);
  70. expect(
  71. webRemoteControlRpcTransportFrameSchema.safeParse({
  72. ...frame(),
  73. checksum: { algorithm: "crc32", value: "55bc801d", extra: true },
  74. }).success,
  75. ).toBe(false);
  76. expect(
  77. webRemoteControlRpcTransportFrameSchema.safeParse({
  78. ...frame(),
  79. fragmentIndex: 1,
  80. fragmentCount: 1,
  81. }).success,
  82. ).toBe(false);
  83. for (const key of ["seq", "messageSeq", "bridgeGeneration"] as const) {
  84. expect(
  85. webRemoteControlRpcTransportFrameSchema.safeParse({
  86. ...frame(),
  87. [key]: Number.MAX_SAFE_INTEGER + 1,
  88. }).success,
  89. ).toBe(false);
  90. }
  91. expect(
  92. webRemoteControlRpcTransportAckSchema.safeParse({
  93. zcode_type: "rpc-frame-ack",
  94. ...identity,
  95. ackMessageSeq: Number.MAX_SAFE_INTEGER + 1,
  96. }).success,
  97. ).toBe(false);
  98. });
  99. it("transport identity 只接受 1..256 个 JSON-safe ASCII 字符", () => {
  100. const maxId = "~".repeat(256);
  101. expect(
  102. webRemoteControlRpcTransportFrameSchema.safeParse({
  103. ...frame(),
  104. bridgeSessionId: maxId,
  105. recoveryId: maxId,
  106. }).success,
  107. ).toBe(true);
  108. for (const invalid of ["", "x".repeat(257), "bad id", 'bad"id', "中文", "🙂"]) {
  109. expect(
  110. webRemoteControlRpcTransportFrameSchema.safeParse({
  111. ...frame(),
  112. bridgeSessionId: invalid,
  113. }).success,
  114. ).toBe(false);
  115. }
  116. });
  117. it("拒绝非 canonical base64、空 chunk、16MiB+1 与第 65 片", () => {
  118. expect(
  119. webRemoteControlRpcTransportFrameSchema.safeParse({ ...frame(), dataBase64: "AB==" }).success,
  120. ).toBe(false);
  121. expect(
  122. webRemoteControlRpcTransportFrameSchema.safeParse({ ...frame(), dataBase64: "" }).success,
  123. ).toBe(false);
  124. expect(
  125. webRemoteControlRpcTransportFrameSchema.safeParse({
  126. ...frame(),
  127. messageBytes: WEB_REMOTE_CONTROL_RPC_TRANSPORT_LIMITS.maxMessageBytes + 1,
  128. }).success,
  129. ).toBe(false);
  130. expect(
  131. webRemoteControlRpcTransportFrameSchema.safeParse({
  132. ...frame(),
  133. fragmentCount: WEB_REMOTE_CONTROL_RPC_TRANSPORT_LIMITS.maxFragments + 1,
  134. messageBytes: WEB_REMOTE_CONTROL_RPC_TRANSPORT_LIMITS.maxFragments + 1,
  135. }).success,
  136. ).toBe(false);
  137. });
  138. it("relay envelope 与 raw JSON parser 都 strict,并精确执行 1MiB-1/1MiB/+1", () => {
  139. const envelope = {
  140. type: "data" as const,
  141. payload: frame(),
  142. client_ts: Number.MAX_SAFE_INTEGER,
  143. server_ts: Number.MAX_SAFE_INTEGER,
  144. };
  145. expect(webRemoteControlRpcRelayEnvelopeSchema.safeParse(envelope).success).toBe(true);
  146. expect(
  147. webRemoteControlRpcRelayEnvelopeSchema.safeParse({ ...envelope, extra: true }).success,
  148. ).toBe(false);
  149. const rawEnvelope = JSON.stringify(envelope);
  150. for (const exactBytes of [
  151. WEB_REMOTE_CONTROL_RPC_TRANSPORT_LIMITS.maxPhysicalFrameBytes - 1,
  152. WEB_REMOTE_CONTROL_RPC_TRANSPORT_LIMITS.maxPhysicalFrameBytes,
  153. ]) {
  154. const bounded = `${rawEnvelope}${" ".repeat(exactBytes - rawEnvelope.length)}`;
  155. expect(parseWebRemoteControlRpcRelayEnvelopeJson(bounded)).toMatchObject({
  156. kind: "accepted",
  157. bytes: exactBytes,
  158. });
  159. }
  160. const oversized = `${rawEnvelope}${" ".repeat(
  161. WEB_REMOTE_CONTROL_RPC_TRANSPORT_LIMITS.maxPhysicalFrameBytes + 1 - rawEnvelope.length,
  162. )}`;
  163. const result = parseWebRemoteControlRpcRelayEnvelopeJson(oversized);
  164. expect(result).toMatchObject({
  165. kind: "fault",
  166. fault: { reasonCode: "remote.rpcFrame.envelopeTooLarge" },
  167. });
  168. });
  169. });
  170. describe("mobile raw RPC exact encoder", () => {
  171. it("小消息单片编码,meter 与最终双 timestamp JSON UTF-8 bytes 完全一致", () => {
  172. const encoded = encodeWebRemoteControlRpcTransportMessage(
  173. new Uint8Array([1, 2, 3]),
  174. encoderOptions(),
  175. );
  176. expect(encoded).toHaveLength(1);
  177. expect(encoded[0]).toMatchObject({
  178. ...identity,
  179. seq: 1,
  180. messageSeq: 1,
  181. fragmentIndex: 0,
  182. fragmentCount: 1,
  183. messageBytes: 3,
  184. dataBase64: "AQID",
  185. });
  186. const actual = new TextEncoder().encode(
  187. JSON.stringify({
  188. type: "data",
  189. payload: encoded[0],
  190. client_ts: Number.MAX_SAFE_INTEGER,
  191. server_ts: Number.MAX_SAFE_INTEGER,
  192. }),
  193. ).byteLength;
  194. expect(measureWebRemoteControlRpcRelayEnvelopeBytes(encoded[0]!)).toBe(actual);
  195. });
  196. it("最大 identity/sequence 下二分分片,每个最终 relay JSON 都 <=1MiB", () => {
  197. const maxId = "x".repeat(256);
  198. const bytes = new Uint8Array(2 * 1024 * 1024 + 7);
  199. bytes[0] = 1;
  200. bytes[bytes.length - 1] = 2;
  201. const encoded = encodeWebRemoteControlRpcTransportMessage(bytes, {
  202. bridgeSessionId: maxId,
  203. bridgeGeneration: Number.MAX_SAFE_INTEGER,
  204. recoveryId: maxId,
  205. firstPhysicalSeq: Number.MAX_SAFE_INTEGER - 10,
  206. messageSeq: Number.MAX_SAFE_INTEGER,
  207. });
  208. expect(encoded.length).toBeGreaterThan(1);
  209. for (const payload of encoded) {
  210. expect(measureWebRemoteControlRpcRelayEnvelopeBytes(payload)).toBeLessThanOrEqual(
  211. WEB_REMOTE_CONTROL_RPC_TRANSPORT_LIMITS.maxPhysicalFrameBytes,
  212. );
  213. }
  214. expect(encoded.at(-1)?.seq).toBe(Number.MAX_SAFE_INTEGER - 10 + encoded.length - 1);
  215. });
  216. it("16MiB 正好可编码;empty、16MiB+1 与 physical seq range overflow 明确拒绝", () => {
  217. expect(
  218. encodeWebRemoteControlRpcTransportMessage(
  219. new Uint8Array(WEB_REMOTE_CONTROL_RPC_TRANSPORT_LIMITS.maxMessageBytes),
  220. encoderOptions(),
  221. ).length,
  222. ).toBeLessThanOrEqual(WEB_REMOTE_CONTROL_RPC_TRANSPORT_LIMITS.maxFragments);
  223. for (const operation of [
  224. () => encodeWebRemoteControlRpcTransportMessage(new Uint8Array(), encoderOptions()),
  225. () =>
  226. encodeWebRemoteControlRpcTransportMessage(
  227. new Uint8Array(WEB_REMOTE_CONTROL_RPC_TRANSPORT_LIMITS.maxMessageBytes + 1),
  228. encoderOptions(),
  229. ),
  230. () =>
  231. encodeWebRemoteControlRpcTransportMessage(
  232. new Uint8Array(1024 * 1024),
  233. encoderOptions({ firstPhysicalSeq: Number.MAX_SAFE_INTEGER }),
  234. ),
  235. ]) {
  236. expect(operation).toThrow(WebRemoteControlRpcTransportEncodingError);
  237. }
  238. });
  239. it("调用方不能放宽 hard limits,极小 outer budget 会触发 fragment limit", () => {
  240. expect(() =>
  241. encodeWebRemoteControlRpcTransportMessage(
  242. new Uint8Array(256 * 1024),
  243. encoderOptions({ maxPhysicalFrameBytes: 1024, maxFragments: 2 }),
  244. ),
  245. ).toThrow(WebRemoteControlRpcTransportEncodingError);
  246. const encoded = encodeWebRemoteControlRpcTransportMessage(
  247. new Uint8Array([1]),
  248. encoderOptions({
  249. maxPhysicalFrameBytes: WEB_REMOTE_CONTROL_RPC_TRANSPORT_LIMITS.maxPhysicalFrameBytes + 1,
  250. maxMessageBytes: WEB_REMOTE_CONTROL_RPC_TRANSPORT_LIMITS.maxMessageBytes + 1,
  251. maxFragments: WEB_REMOTE_CONTROL_RPC_TRANSPORT_LIMITS.maxFragments + 1,
  252. }),
  253. );
  254. expect(encoded).toHaveLength(1);
  255. });
  256. it("exact 64 fragments 可编码并还原,第 65 片需求由 encoder 明确拒绝", () => {
  257. const bytes = new Uint8Array(192);
  258. bytes[0] = 1;
  259. bytes[bytes.length - 1] = 2;
  260. const exactThreeByteBudget = measureWebRemoteControlRpcRelayEnvelopeBytes(
  261. frame({
  262. seq: 64,
  263. messageSeq: 1,
  264. fragmentIndex: 63,
  265. fragmentCount: 64,
  266. messageBytes: bytes.byteLength,
  267. dataBase64: "AQID",
  268. }),
  269. );
  270. const encoded = encodeWebRemoteControlRpcTransportMessage(
  271. bytes,
  272. encoderOptions({
  273. maxPhysicalFrameBytes: exactThreeByteBudget,
  274. maxFragments: WEB_REMOTE_CONTROL_RPC_TRANSPORT_LIMITS.maxFragments,
  275. }),
  276. );
  277. expect(encoded).toHaveLength(64);
  278. expect(() =>
  279. encodeWebRemoteControlRpcTransportMessage(
  280. bytes,
  281. encoderOptions({
  282. maxPhysicalFrameBytes: exactThreeByteBudget,
  283. maxFragments: WEB_REMOTE_CONTROL_RPC_TRANSPORT_LIMITS.maxFragments - 1,
  284. }),
  285. ),
  286. ).toThrowError(
  287. expect.objectContaining({ reasonCode: "remote.rpcFrame.fragmentLimitExceeded" }),
  288. );
  289. expect(() =>
  290. encodeWebRemoteControlRpcTransportMessage(
  291. new Uint8Array(bytes.byteLength + 1),
  292. encoderOptions({
  293. maxPhysicalFrameBytes: exactThreeByteBudget,
  294. maxFragments: WEB_REMOTE_CONTROL_RPC_TRANSPORT_LIMITS.maxFragments,
  295. }),
  296. ),
  297. ).toThrowError(
  298. expect.objectContaining({ reasonCode: "remote.rpcFrame.fragmentLimitExceeded" }),
  299. );
  300. const assembler = new WebRemoteControlRpcTransportAssembler({
  301. identity,
  302. maxPhysicalFrameBytes: exactThreeByteBudget,
  303. });
  304. let result = assembler.accept(encoded[0]!);
  305. for (const payload of encoded.slice(1)) result = assembler.accept(payload);
  306. expect(result).toMatchObject({ kind: "complete", messageSeq: 1 });
  307. if (result.kind !== "complete") throw new Error("expected complete");
  308. expect(result.bytes).toEqual(bytes);
  309. });
  310. });
  311. describe("mobile raw RPC one-message assembler", () => {
  312. it("最后一片前不交付,完整后只原子交付一次,旧 replay 只报 duplicate", () => {
  313. const bytes = new TextEncoder().encode("中🙂".repeat(200_000));
  314. const encoded = encodeWebRemoteControlRpcTransportMessage(bytes, encoderOptions());
  315. const assembler = new WebRemoteControlRpcTransportAssembler({ identity });
  316. for (const payload of encoded.slice(0, -1)) {
  317. expect(assembler.accept(payload)).toMatchObject({ kind: "incomplete" });
  318. }
  319. const completed = assembler.accept(encoded.at(-1)!);
  320. expect(completed).toMatchObject({ kind: "complete", messageSeq: 1 });
  321. if (completed.kind !== "complete") throw new Error("expected complete");
  322. expect(completed.bytes).toEqual(bytes);
  323. for (const replayed of encoded) {
  324. expect(assembler.accept(replayed)).toMatchObject({
  325. kind: "duplicate",
  326. ackMessageSeq: 1,
  327. });
  328. }
  329. expect(assembler.accept({ ...encoded[0]!, dataBase64: "AA==" })).toMatchObject({
  330. kind: "fault",
  331. fault: { reasonCode: "remote.rpcFrame.conflictingDuplicate" },
  332. });
  333. });
  334. it("相同 active duplicate no-op 且不续期;冲突 duplicate typed fault", () => {
  335. const encoded = encodeWebRemoteControlRpcTransportMessage(
  336. new Uint8Array(1024 * 1024),
  337. encoderOptions(),
  338. );
  339. expect(encoded.length).toBeGreaterThan(1);
  340. const assembler = new WebRemoteControlRpcTransportAssembler({ identity, timeoutMs: 10 });
  341. expect(assembler.accept(encoded[0]!, 0).kind).toBe("incomplete");
  342. expect(assembler.accept(encoded[0]!, 9)).toEqual({
  343. kind: "duplicate",
  344. ackMessageSeq: null,
  345. });
  346. expect(assembler.expire(10)).toMatchObject({
  347. kind: "fault",
  348. fault: { reasonCode: "remote.rpcFrame.assemblyTimeout" },
  349. });
  350. const conflicting = new WebRemoteControlRpcTransportAssembler({ identity });
  351. expect(conflicting.accept(encoded[0]!).kind).toBe("incomplete");
  352. expect(conflicting.accept({ ...encoded[0]!, dataBase64: "AA==" })).toMatchObject({
  353. kind: "fault",
  354. fault: { reasonCode: "remote.rpcFrame.conflictingDuplicate" },
  355. });
  356. });
  357. it("physical/message/fragment gap 与 metadata/length/CRC 错误均 typed fault", () => {
  358. const two = encodeWebRemoteControlRpcTransportMessage(
  359. new Uint8Array(1024 * 1024),
  360. encoderOptions(),
  361. );
  362. expect(two.length).toBeGreaterThan(1);
  363. const cases: Array<{
  364. mutate(payloads: WebRemoteControlRpcTransportFramePayload[]): unknown;
  365. reasonCode: string;
  366. }> = [
  367. {
  368. mutate: (payloads) => ({ ...payloads[0], seq: payloads[0]!.seq + 1 }),
  369. reasonCode: "remote.rpcFrame.physicalGap",
  370. },
  371. {
  372. mutate: (payloads) => ({ ...payloads[0], messageSeq: 2 }),
  373. reasonCode: "remote.rpcFrame.messageGap",
  374. },
  375. {
  376. mutate: (payloads) => ({ ...payloads[0], fragmentIndex: 1 }),
  377. reasonCode: "remote.rpcFrame.fragmentGap",
  378. },
  379. {
  380. mutate: (payloads) => ({
  381. ...payloads[0],
  382. checksum: { algorithm: "crc32", value: "00000000" },
  383. }),
  384. reasonCode: "remote.rpcFrame.checksumMismatch",
  385. },
  386. {
  387. mutate: (payloads) => ({
  388. ...payloads[0],
  389. messageBytes: payloads[0]!.fragmentCount,
  390. }),
  391. reasonCode: "remote.rpcFrame.lengthMismatch",
  392. },
  393. ];
  394. for (const testCase of cases) {
  395. const assembler = new WebRemoteControlRpcTransportAssembler({ identity });
  396. const mutated = testCase.mutate([...two]);
  397. let result = assembler.accept(mutated);
  398. if (result.kind === "incomplete") {
  399. for (const payload of two.slice(1)) result = assembler.accept(payload);
  400. }
  401. expect(result).toMatchObject({
  402. kind: "fault",
  403. fault: { reasonCode: testCase.reasonCode },
  404. });
  405. }
  406. });
  407. it("跨片 metadata/checksum 冲突与最终 whole-message CRC 错误可区分", () => {
  408. const encoded = encodeWebRemoteControlRpcTransportMessage(
  409. new Uint8Array(1024 * 1024),
  410. encoderOptions(),
  411. );
  412. expect(encoded.length).toBeGreaterThan(1);
  413. const metadataConflict = new WebRemoteControlRpcTransportAssembler({ identity });
  414. expect(metadataConflict.accept(encoded[0]!).kind).toBe("incomplete");
  415. expect(
  416. metadataConflict.accept({
  417. ...encoded[1]!,
  418. messageBytes: encoded[1]!.messageBytes + 1,
  419. }),
  420. ).toMatchObject({
  421. kind: "fault",
  422. fault: { reasonCode: "remote.rpcFrame.metadataMismatch" },
  423. });
  424. const checksumConflict = new WebRemoteControlRpcTransportAssembler({ identity });
  425. expect(checksumConflict.accept(encoded[0]!).kind).toBe("incomplete");
  426. expect(
  427. checksumConflict.accept({
  428. ...encoded[1]!,
  429. checksum: { algorithm: "crc32", value: "00000000" },
  430. }),
  431. ).toMatchObject({
  432. kind: "fault",
  433. fault: { reasonCode: "remote.rpcFrame.checksumMismatch" },
  434. });
  435. const finalCrcMismatch = new WebRemoteControlRpcTransportAssembler({ identity });
  436. let finalResult = finalCrcMismatch.accept({
  437. ...encoded[0]!,
  438. checksum: { algorithm: "crc32", value: "00000000" },
  439. });
  440. for (const payload of encoded.slice(1)) {
  441. finalResult = finalCrcMismatch.accept({
  442. ...payload,
  443. checksum: { algorithm: "crc32", value: "00000000" },
  444. });
  445. }
  446. expect(finalResult).toMatchObject({
  447. kind: "fault",
  448. fault: { reasonCode: "remote.rpcFrame.checksumMismatch" },
  449. });
  450. });
  451. it("foreign identity/generation/recovery 不推进、解码或清空 owning assembly", () => {
  452. const bytes = new Uint8Array(1024 * 1024);
  453. bytes[0] = 7;
  454. const encoded = encodeWebRemoteControlRpcTransportMessage(bytes, encoderOptions());
  455. const assembler = new WebRemoteControlRpcTransportAssembler({ identity });
  456. expect(assembler.accept(encoded[0]!).kind).toBe("incomplete");
  457. for (const foreignIdentity of [
  458. { bridgeSessionId: "foreign" },
  459. { bridgeGeneration: identity.bridgeGeneration! + 1 },
  460. { recoveryId: "foreign" },
  461. ]) {
  462. expect(assembler.accept({ ...encoded[1]!, ...foreignIdentity })).toMatchObject({
  463. kind: "fault",
  464. fault: { reasonCode: "remote.rpcFrame.identityMismatch", terminal: false },
  465. });
  466. }
  467. let result = assembler.accept(encoded[1]!);
  468. for (const payload of encoded.slice(2)) result = assembler.accept(payload);
  469. expect(result).toMatchObject({ kind: "complete", messageSeq: 1 });
  470. const timeoutAssembler = new WebRemoteControlRpcTransportAssembler({
  471. identity,
  472. timeoutMs: 10,
  473. });
  474. expect(timeoutAssembler.accept(encoded[0]!, 0).kind).toBe("incomplete");
  475. expect(
  476. timeoutAssembler.accept(
  477. { ...encoded[1]!, bridgeSessionId: "foreign", dataBase64: "AB==" },
  478. 10,
  479. ),
  480. ).toMatchObject({
  481. kind: "fault",
  482. fault: { reasonCode: "remote.rpcFrame.identityMismatch", terminal: false },
  483. });
  484. expect(timeoutAssembler.expire(10)).toMatchObject({
  485. kind: "fault",
  486. fault: { reasonCode: "remote.rpcFrame.assemblyTimeout", terminal: true },
  487. });
  488. });
  489. it("连续 message 累计 tombstone 防二次交付,并只回最新 completed ACK", () => {
  490. const first = encodeWebRemoteControlRpcTransportMessage(
  491. new TextEncoder().encode("first"),
  492. encoderOptions(),
  493. );
  494. const second = encodeWebRemoteControlRpcTransportMessage(
  495. new TextEncoder().encode("second"),
  496. encoderOptions({ firstPhysicalSeq: first.length + 1, messageSeq: 2 }),
  497. );
  498. const assembler = new WebRemoteControlRpcTransportAssembler({ identity });
  499. expect(assembler.accept(first[0]!)).toMatchObject({ kind: "complete", messageSeq: 1 });
  500. expect(assembler.accept(second[0]!)).toMatchObject({ kind: "complete", messageSeq: 2 });
  501. for (const replayed of [...first, ...second]) {
  502. expect(assembler.accept(replayed)).toEqual({ kind: "duplicate", ackMessageSeq: 2 });
  503. }
  504. });
  505. it("MAX_SAFE physical/message sequence 收口后要求新 bridge generation", () => {
  506. const physical = new WebRemoteControlRpcTransportAssembler({
  507. identity,
  508. initialPhysicalSeq: Number.MAX_SAFE_INTEGER,
  509. });
  510. const lastPhysical = frame({ seq: Number.MAX_SAFE_INTEGER });
  511. expect(physical.accept(lastPhysical)).toMatchObject({ kind: "complete" });
  512. expect(physical.accept(lastPhysical)).toEqual({ kind: "duplicate", ackMessageSeq: 1 });
  513. expect(physical.accept({ ...lastPhysical, messageSeq: 2, dataBase64: "AA==" })).toMatchObject({
  514. kind: "fault",
  515. fault: { reasonCode: "remote.rpcFrame.physicalSequenceExhausted" },
  516. });
  517. const unfinishedPhysical = new WebRemoteControlRpcTransportAssembler({
  518. identity,
  519. initialPhysicalSeq: Number.MAX_SAFE_INTEGER,
  520. });
  521. expect(
  522. unfinishedPhysical.accept(
  523. frame({
  524. seq: Number.MAX_SAFE_INTEGER,
  525. fragmentCount: 2,
  526. messageBytes: 6,
  527. }),
  528. ),
  529. ).toMatchObject({
  530. kind: "fault",
  531. fault: { reasonCode: "remote.rpcFrame.physicalSequenceExhausted" },
  532. });
  533. const message = new WebRemoteControlRpcTransportAssembler({
  534. identity,
  535. initialMessageSeq: Number.MAX_SAFE_INTEGER,
  536. });
  537. expect(message.accept(frame({ messageSeq: Number.MAX_SAFE_INTEGER }))).toMatchObject({
  538. kind: "complete",
  539. });
  540. expect(message.accept(frame({ seq: 2, messageSeq: Number.MAX_SAFE_INTEGER }))).toMatchObject({
  541. kind: "fault",
  542. fault: { reasonCode: "remote.rpcFrame.messageSequenceExhausted" },
  543. });
  544. });
  545. it("invalid base64 candidate 产生 typed fault,而不是异常或静默丢弃", () => {
  546. const assembler = new WebRemoteControlRpcTransportAssembler({ identity });
  547. expect(assembler.accept({ ...frame(), dataBase64: "AB==" })).toMatchObject({
  548. kind: "fault",
  549. fault: { reasonCode: "remote.rpcFrame.invalidBase64" },
  550. });
  551. const oversizedBase64 = new WebRemoteControlRpcTransportAssembler({ identity });
  552. expect(
  553. oversizedBase64.accept({
  554. ...frame(),
  555. dataBase64: "A".repeat(WEB_REMOTE_CONTROL_RPC_TRANSPORT_LIMITS.maxPhysicalFrameBytes + 4),
  556. }),
  557. ).toMatchObject({
  558. kind: "fault",
  559. fault: { reasonCode: "remote.rpcFrame.invalidBase64" },
  560. });
  561. });
  562. });