webRemoteControlMobileLog.test.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { describe, expect, it } from "vitest";
  2. import { parseWebRemoteControlAppPayload } from "../src/validation.js";
  3. import { WEB_REMOTE_CONTROL_MOBILE_LOG_LIMITS } from "../src/web-remote-relay/index.js";
  4. const entry = { ts: 1_700_000_000_000, level: "info", tag: "rc", message: "hello" };
  5. describe("web remote control mobile log payloads", () => {
  6. it("接受实时 mobile-log 帧,忽略未知字段(前向兼容)", () => {
  7. const parsed = parseWebRemoteControlAppPayload({
  8. zcode_type: "mobile-log",
  9. device: { id: "install-1", platform: "android", appVersion: "1.0.0" },
  10. sessionId: "boot-1",
  11. events: [{ ...entry, extra: true }],
  12. futureField: 1,
  13. });
  14. expect(parsed).toEqual({
  15. zcode_type: "mobile-log",
  16. device: { id: "install-1", platform: "android", appVersion: "1.0.0" },
  17. sessionId: "boot-1",
  18. events: [entry],
  19. });
  20. });
  21. it("拒绝空帧、超条数与非法级别", () => {
  22. expect(parseWebRemoteControlAppPayload({ zcode_type: "mobile-log", events: [] })).toBeNull();
  23. expect(
  24. parseWebRemoteControlAppPayload({
  25. zcode_type: "mobile-log",
  26. events: Array.from(
  27. { length: WEB_REMOTE_CONTROL_MOBILE_LOG_LIMITS.maxEventsPerFrame + 1 },
  28. () => entry,
  29. ),
  30. }),
  31. ).toBeNull();
  32. expect(
  33. parseWebRemoteControlAppPayload({
  34. zcode_type: "mobile-log",
  35. events: [{ ...entry, level: "verbose" }],
  36. }),
  37. ).toBeNull();
  38. });
  39. it("解析拉取请求与三种应答(明文 / gzip / 失败)", () => {
  40. expect(
  41. parseWebRemoteControlAppPayload({
  42. zcode_type: "mobile-log-pull-request",
  43. requestId: "r1",
  44. sinceTs: 1,
  45. minLevel: "warn",
  46. acceptEncodings: ["gzip"],
  47. }),
  48. ).toMatchObject({ zcode_type: "mobile-log-pull-request", requestId: "r1" });
  49. const meta = {
  50. zcode_type: "mobile-log-pull-response",
  51. requestId: "r1",
  52. success: true,
  53. totalMatched: 1,
  54. returnedCount: 1,
  55. hasMore: false,
  56. sources: ["memory-ring"],
  57. };
  58. expect(parseWebRemoteControlAppPayload({ ...meta, entries: [entry] })).toMatchObject({
  59. entries: [entry],
  60. });
  61. expect(
  62. parseWebRemoteControlAppPayload({ ...meta, encoding: "gzip", data: "H4sI", entryCount: 1 }),
  63. ).toMatchObject({ encoding: "gzip", entryCount: 1 });
  64. expect(
  65. parseWebRemoteControlAppPayload({
  66. zcode_type: "mobile-log-pull-response",
  67. requestId: "r1",
  68. success: false,
  69. reason: "unsupported-action",
  70. error: "not supported",
  71. }),
  72. ).toMatchObject({ success: false, reason: "unsupported-action" });
  73. });
  74. it("拒绝超限的 gzip 分片与超长 cursor", () => {
  75. const meta = {
  76. zcode_type: "mobile-log-pull-response",
  77. requestId: "r1",
  78. success: true,
  79. totalMatched: 1,
  80. returnedCount: 1,
  81. hasMore: true,
  82. sources: [],
  83. };
  84. expect(
  85. parseWebRemoteControlAppPayload({
  86. ...meta,
  87. encoding: "gzip",
  88. data: "a".repeat(WEB_REMOTE_CONTROL_MOBILE_LOG_LIMITS.maxCompressedShardBase64Chars + 1),
  89. entryCount: 1,
  90. }),
  91. ).toBeNull();
  92. expect(
  93. parseWebRemoteControlAppPayload({
  94. ...meta,
  95. cursor: "c".repeat(WEB_REMOTE_CONTROL_MOBILE_LOG_LIMITS.maxCursorLength + 1),
  96. entries: [],
  97. }),
  98. ).toBeNull();
  99. });
  100. });