controlMessageCompat.test.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { describe, expect, test } from "bun:test";
  2. import { normalizeControlMessageKeys } from "../controlMessageCompat";
  3. describe("normalizeControlMessageKeys", () => {
  4. // --- basic camelCase to snake_case ---
  5. test("converts requestId to request_id", () => {
  6. const obj = { requestId: "123" };
  7. const result = normalizeControlMessageKeys(obj);
  8. expect(result).toEqual({ request_id: "123" });
  9. expect((result as any).requestId).toBeUndefined();
  10. });
  11. test("leaves request_id unchanged", () => {
  12. const obj = { request_id: "123" };
  13. normalizeControlMessageKeys(obj);
  14. expect(obj).toEqual({ request_id: "123" });
  15. });
  16. // --- both present: snake_case wins ---
  17. test("keeps snake_case when both requestId and request_id exist", () => {
  18. const obj = { requestId: "camel", request_id: "snake" };
  19. const result = normalizeControlMessageKeys(obj) as any;
  20. expect(result.request_id).toBe("snake");
  21. // requestId is NOT deleted when request_id already exists
  22. // because the condition `!('request_id' in record)` prevents the branch
  23. expect(result.requestId).toBe("camel");
  24. });
  25. // --- nested response ---
  26. test("normalizes nested response.requestId", () => {
  27. const obj = { response: { requestId: "456" } };
  28. normalizeControlMessageKeys(obj);
  29. expect((obj as any).response.request_id).toBe("456");
  30. expect((obj as any).response.requestId).toBeUndefined();
  31. });
  32. test("leaves nested response.request_id unchanged", () => {
  33. const obj = { response: { request_id: "789" } };
  34. normalizeControlMessageKeys(obj);
  35. expect((obj as any).response.request_id).toBe("789");
  36. });
  37. test("nested response: snake_case wins when both present", () => {
  38. const obj = {
  39. response: { requestId: "camel", request_id: "snake" },
  40. };
  41. normalizeControlMessageKeys(obj);
  42. expect((obj as any).response.request_id).toBe("snake");
  43. expect((obj as any).response.requestId).toBe("camel");
  44. });
  45. // --- non-object inputs ---
  46. test("returns null as-is", () => {
  47. expect(normalizeControlMessageKeys(null)).toBeNull();
  48. });
  49. test("returns undefined as-is", () => {
  50. expect(normalizeControlMessageKeys(undefined)).toBeUndefined();
  51. });
  52. test("returns string as-is", () => {
  53. expect(normalizeControlMessageKeys("hello")).toBe("hello");
  54. });
  55. test("returns number as-is", () => {
  56. expect(normalizeControlMessageKeys(42)).toBe(42);
  57. });
  58. // --- empty and edge cases ---
  59. test("empty object is unchanged", () => {
  60. const obj = {};
  61. normalizeControlMessageKeys(obj);
  62. expect(obj).toEqual({});
  63. });
  64. test("mutates the original object in place", () => {
  65. const obj = { requestId: "abc", other: "data" };
  66. const result = normalizeControlMessageKeys(obj);
  67. expect(result).toBe(obj); // same reference
  68. expect(obj).toEqual({ request_id: "abc", other: "data" });
  69. });
  70. test("does not affect other keys on the object", () => {
  71. const obj = { requestId: "123", type: "control_request", payload: {} };
  72. normalizeControlMessageKeys(obj);
  73. expect((obj as any).type).toBe("control_request");
  74. expect((obj as any).payload).toEqual({});
  75. expect((obj as any).request_id).toBe("123");
  76. });
  77. test("handles response being null", () => {
  78. const obj = { response: null, requestId: "x" };
  79. normalizeControlMessageKeys(obj);
  80. expect((obj as any).request_id).toBe("x");
  81. expect((obj as any).response).toBeNull();
  82. });
  83. test("handles response being a non-object (string)", () => {
  84. const obj = { response: "not-an-object" };
  85. normalizeControlMessageKeys(obj);
  86. expect((obj as any).response).toBe("not-an-object");
  87. });
  88. });