File size: 8,594 Bytes
84aa3bf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect } from 'vitest';
import { customDeepMerge } from './deepMerge.js';
import { MergeStrategy } from '../config/settingsSchema.js';
describe('customDeepMerge', () => {
it('should merge simple objects', () => {
const target = { a: 1, b: 2 };
const source = { b: 3, c: 4 };
const getMergeStrategy = () => undefined;
const result = customDeepMerge(getMergeStrategy, target, source);
expect(result).toEqual({ a: 1, b: 3, c: 4 });
});
it('should merge nested objects', () => {
const target = { a: { x: 1 }, b: 2 };
const source = { a: { y: 2 }, c: 3 };
const getMergeStrategy = () => undefined;
const result = customDeepMerge(getMergeStrategy, target, source);
expect(result).toEqual({ a: { x: 1, y: 2 }, b: 2, c: 3 });
});
it('should replace arrays by default', () => {
const target = { a: [1, 2] };
const source = { a: [3, 4] };
const getMergeStrategy = () => undefined;
const result = customDeepMerge(getMergeStrategy, target, source);
expect(result).toEqual({ a: [3, 4] });
});
it('should concatenate arrays with CONCAT strategy', () => {
const target = { a: [1, 2] };
const source = { a: [3, 4] };
const getMergeStrategy = (path: string[]) =>
path.join('.') === 'a' ? MergeStrategy.CONCAT : undefined;
const result = customDeepMerge(getMergeStrategy, target, source);
expect(result).toEqual({ a: [1, 2, 3, 4] });
});
it('should union arrays with UNION strategy', () => {
const target = { a: [1, 2, 3] };
const source = { a: [3, 4, 5] };
const getMergeStrategy = (path: string[]) =>
path.join('.') === 'a' ? MergeStrategy.UNION : undefined;
const result = customDeepMerge(getMergeStrategy, target, source);
expect(result).toEqual({ a: [1, 2, 3, 4, 5] });
});
it('should shallow merge objects with SHALLOW_MERGE strategy', () => {
const target = { a: { x: 1, y: 1 } };
const source = { a: { y: 2, z: 2 } };
const getMergeStrategy = (path: string[]) =>
path.join('.') === 'a' ? MergeStrategy.SHALLOW_MERGE : undefined;
const result = customDeepMerge(getMergeStrategy, target, source);
// This is still a deep merge, but the properties of the object are merged.
expect(result).toEqual({ a: { x: 1, y: 2, z: 2 } });
});
it('should handle multiple source objects', () => {
const target = { a: 1 };
const source1 = { b: 2 };
const source2 = { c: 3 };
const getMergeStrategy = () => undefined;
const result = customDeepMerge(getMergeStrategy, target, source1, source2);
expect(result).toEqual({ a: 1, b: 2, c: 3 });
});
it('should return an empty object if no sources are provided', () => {
const getMergeStrategy = () => undefined;
const result = customDeepMerge(getMergeStrategy);
expect(result).toEqual({});
});
it('should return a deep copy of the first source if only one is provided', () => {
const target = { a: { b: 1 } };
const getMergeStrategy = () => undefined;
const result = customDeepMerge(getMergeStrategy, target);
expect(result).toEqual(target);
expect(result).not.toBe(target);
});
it('should not mutate the original source objects', () => {
const target = { a: { x: 1 }, b: [1, 2] };
const source = { a: { y: 2 }, b: [3, 4] };
const originalTarget = JSON.parse(JSON.stringify(target));
const originalSource = JSON.parse(JSON.stringify(source));
const getMergeStrategy = () => undefined;
customDeepMerge(getMergeStrategy, target, source);
expect(target).toEqual(originalTarget);
expect(source).toEqual(originalSource);
});
it('should not mutate sources when merging multiple levels deep', () => {
const s1 = { data: { common: { val: 'from s1' }, s1_only: true } };
const s2 = { data: { common: { val: 'from s2' }, s2_only: true } };
const s1_original = JSON.parse(JSON.stringify(s1));
const s2_original = JSON.parse(JSON.stringify(s2));
const getMergeStrategy = () => undefined;
const result = customDeepMerge(getMergeStrategy, s1, s2);
expect(s1).toEqual(s1_original);
expect(s2).toEqual(s2_original);
expect(result).toEqual({
data: {
common: { val: 'from s2' },
s1_only: true,
s2_only: true,
},
});
});
it('should handle complex nested strategies', () => {
const target = {
level1: {
arr1: [1, 2],
arr2: [1, 2],
obj1: { a: 1 },
},
};
const source = {
level1: {
arr1: [3, 4],
arr2: [2, 3],
obj1: { b: 2 },
},
};
const getMergeStrategy = (path: string[]) => {
const p = path.join('.');
if (p === 'level1.arr1') return MergeStrategy.CONCAT;
if (p === 'level1.arr2') return MergeStrategy.UNION;
if (p === 'level1.obj1') return MergeStrategy.SHALLOW_MERGE;
return undefined;
};
const result = customDeepMerge(getMergeStrategy, target, source);
expect(result).toEqual({
level1: {
arr1: [1, 2, 3, 4],
arr2: [1, 2, 3],
obj1: { a: 1, b: 2 },
},
});
});
it('should not pollute the prototype', () => {
const maliciousSource = JSON.parse('{"__proto__": {"polluted1": "true"}}');
const getMergeStrategy = () => undefined;
let result = customDeepMerge(getMergeStrategy, {}, maliciousSource);
expect(result).toEqual({});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect(({} as any).polluted1).toBeUndefined();
const maliciousSource2 = JSON.parse(
'{"constructor": {"prototype": {"polluted2": "true"}}}',
);
result = customDeepMerge(getMergeStrategy, {}, maliciousSource2);
expect(result).toEqual({});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect(({} as any).polluted2).toBeUndefined();
const maliciousSource3 = JSON.parse('{"prototype": {"polluted3": "true"}}');
result = customDeepMerge(getMergeStrategy, {}, maliciousSource3);
expect(result).toEqual({});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect(({} as any).polluted3).toBeUndefined();
});
it('should use additionalProperties merge strategy for dynamic properties', () => {
// Simulates how hooks work: hooks.disabled uses UNION, but hooks.BeforeTool (dynamic) uses CONCAT
const target = {
hooks: {
BeforeTool: [{ command: 'user-hook-1' }, { command: 'user-hook-2' }],
disabled: ['hook-a'],
},
};
const source = {
hooks: {
BeforeTool: [{ command: 'workspace-hook-1' }],
disabled: ['hook-b'],
},
};
// Mock the getMergeStrategyForPath behavior for hooks
const getMergeStrategy = (path: string[]) => {
const p = path.join('.');
// hooks.disabled uses UNION strategy (explicitly defined in schema)
if (p === 'hooks.disabled') return MergeStrategy.UNION;
// hooks.BeforeTool uses CONCAT strategy (via additionalProperties)
if (p === 'hooks.BeforeTool') return MergeStrategy.CONCAT;
return undefined;
};
const result = customDeepMerge(getMergeStrategy, target, source);
// BeforeTool should concatenate
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((result as any)['hooks']['BeforeTool']).toEqual([
{ command: 'user-hook-1' },
{ command: 'user-hook-2' },
{ command: 'workspace-hook-1' },
]);
// disabled should union (deduplicate)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((result as any)['hooks']['disabled']).toEqual(['hook-a', 'hook-b']);
});
it('should overwrite primitive with object', () => {
const target = { a: 1 };
const source = { a: { b: 2 } };
const getMergeStrategy = () => undefined;
const result = customDeepMerge(getMergeStrategy, target, source);
expect(result).toEqual({ a: { b: 2 } });
});
it('should overwrite object with primitive', () => {
const target = { a: { b: 2 } };
const source = { a: 1 };
const getMergeStrategy = () => undefined;
const result = customDeepMerge(getMergeStrategy, target, source);
expect(result).toEqual({ a: 1 });
});
it('should not overwrite with undefined', () => {
const target = { a: 1 };
const source = { a: undefined };
const getMergeStrategy = () => undefined;
const result = customDeepMerge(getMergeStrategy, target, source);
expect(result).toEqual({ a: 1 });
});
});
|