json-merge-patch.test.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. ModelOptionMapError,
  4. applyOrderedJsonMergePatches,
  5. compileModelOptionMaps,
  6. type NamedJsonMergePatch,
  7. } from "../src/index.js";
  8. describe("ordered JSON Merge Patch", () => {
  9. it("compiles once and applies reasoning before max output", () => {
  10. const maps = compileModelOptionMaps({
  11. reasoningLevel: { map: "{'reasoning': {'effort': reasoningLevel}}" },
  12. maxOutputTokens: { map: "{'max_output_tokens': maxOutputTokens}" },
  13. });
  14. expect(
  15. maps.apply({ model: "example" }, { reasoningLevel: "high", maxOutputTokens: 32_000 }),
  16. ).toEqual({
  17. model: "example",
  18. reasoning: { effort: "high" },
  19. max_output_tokens: 32_000,
  20. });
  21. });
  22. it("fails closed when a declared reasoning map has no effective value", () => {
  23. const maps = compileModelOptionMaps({
  24. reasoningLevel: { map: "{'reasoning_effort': reasoningLevel}" },
  25. maxOutputTokens: { map: "{'max_completion_tokens': maxOutputTokens}" },
  26. });
  27. expect(() => maps.apply({}, { maxOutputTokens: 32_000 })).toThrow(
  28. "reasoningLevel requires an effective value",
  29. );
  30. });
  31. it("implements RFC 7396 add, replace, recursive merge, array replace and null delete", () => {
  32. const result = applyOrderedJsonMergePatches(
  33. {
  34. keep: true,
  35. nested: { remove: 1, replace: 2 },
  36. array: [1, 2],
  37. },
  38. [
  39. {
  40. option: "reasoningLevel",
  41. patch: {
  42. added: "yes",
  43. array: [3],
  44. nested: { remove: null, replace: 3 },
  45. },
  46. },
  47. ],
  48. );
  49. expect(result).toEqual({
  50. added: "yes",
  51. array: [3],
  52. keep: true,
  53. nested: { replace: 3 },
  54. });
  55. });
  56. it("allows sibling leaf writes from ordered options", () => {
  57. expect(
  58. applyOrderedJsonMergePatches({}, [
  59. { option: "reasoningLevel", patch: { reasoning: { effort: "high" } } },
  60. { option: "maxOutputTokens", patch: { reasoning: { max_tokens: 32000 } } },
  61. ]),
  62. ).toEqual({ reasoning: { effort: "high", max_tokens: 32000 } });
  63. });
  64. it.each<{ patches: NamedJsonMergePatch[] }>([
  65. {
  66. patches: [
  67. { option: "reasoningLevel", patch: { value: 1 } },
  68. { option: "maxOutputTokens", patch: { value: 2 } },
  69. ],
  70. },
  71. {
  72. patches: [
  73. { option: "reasoningLevel", patch: { reasoning: null } },
  74. { option: "maxOutputTokens", patch: { reasoning: { max_tokens: 2 } } },
  75. ],
  76. },
  77. {
  78. patches: [
  79. { option: "reasoningLevel", patch: { reasoning: { effort: "high" } } },
  80. { option: "maxOutputTokens", patch: { reasoning: [] } },
  81. ],
  82. },
  83. ])("rejects overlapping option write paths", ({ patches }) => {
  84. expect(() => applyOrderedJsonMergePatches({}, patches)).toThrow(ModelOptionMapError);
  85. expect(() => applyOrderedJsonMergePatches({}, patches)).toThrow("conflicting JSON path");
  86. });
  87. it("does not mutate the source body or patches", () => {
  88. const body = { nested: { before: true } };
  89. const patch = { nested: { after: true } };
  90. const result = applyOrderedJsonMergePatches(body, [{ option: "reasoningLevel", patch }]);
  91. expect(result).toEqual({ nested: { after: true, before: true } });
  92. expect(body).toEqual({ nested: { before: true } });
  93. expect(patch).toEqual({ nested: { after: true } });
  94. });
  95. });