@adobe/spacecat-shared-tokowaka-client 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -233,7 +233,7 @@ describe('HeadingsMapper', () => {
233
233
  });
234
234
  });
235
235
 
236
- describe('suggestionToPatch', () => {
236
+ describe('suggestionsToPatches', () => {
237
237
  it('should create patch for heading-empty with transformRules', () => {
238
238
  const suggestion = {
239
239
  getId: () => 'sugg-123',
@@ -248,7 +248,9 @@ describe('HeadingsMapper', () => {
248
248
  }),
249
249
  };
250
250
 
251
- const patch = mapper.suggestionToPatch(suggestion, 'opp-123');
251
+ const patches = mapper.suggestionsToPatches('/path', [suggestion], 'opp-123');
252
+ expect(patches.length).to.equal(1);
253
+ const patch = patches[0];
252
254
 
253
255
  expect(patch).to.deep.include({
254
256
  op: 'replace',
@@ -275,7 +277,9 @@ describe('HeadingsMapper', () => {
275
277
  }),
276
278
  };
277
279
 
278
- const patch = mapper.suggestionToPatch(suggestion, 'opp-123');
280
+ const patches = mapper.suggestionsToPatches('/path', [suggestion], 'opp-123');
281
+ expect(patches.length).to.equal(1);
282
+ const patch = patches[0];
279
283
 
280
284
  expect(patch).to.deep.include({
281
285
  op: 'replace',
@@ -302,7 +306,9 @@ describe('HeadingsMapper', () => {
302
306
  }),
303
307
  };
304
308
 
305
- const patch = mapper.suggestionToPatch(suggestion, 'opp-456');
309
+ const patches = mapper.suggestionsToPatches('/path', [suggestion], 'opp-456');
310
+ expect(patches.length).to.equal(1);
311
+ const patch = patches[0];
306
312
 
307
313
  expect(patch).to.deep.include({
308
314
  op: 'insertAfter',
@@ -330,7 +336,9 @@ describe('HeadingsMapper', () => {
330
336
  }),
331
337
  };
332
338
 
333
- const patch = mapper.suggestionToPatch(suggestion, 'opp-789');
339
+ const patches = mapper.suggestionsToPatches('/path', [suggestion], 'opp-789');
340
+ expect(patches.length).to.equal(1);
341
+ const patch = patches[0];
334
342
 
335
343
  expect(patch).to.deep.include({
336
344
  op: 'replace',
@@ -344,7 +352,7 @@ describe('HeadingsMapper', () => {
344
352
  expect(patch.tag).to.be.undefined;
345
353
  });
346
354
 
347
- it('should return null for heading-missing-h1 without transformRules', () => {
355
+ it('should return empty array for heading-missing-h1 without transformRules', () => {
348
356
  const suggestion = {
349
357
  getId: () => 'sugg-999',
350
358
  getData: () => ({
@@ -353,12 +361,12 @@ describe('HeadingsMapper', () => {
353
361
  }),
354
362
  };
355
363
 
356
- const patch = mapper.suggestionToPatch(suggestion, 'opp-999');
357
-
358
- expect(patch).to.be.null;
364
+ const patches = mapper.suggestionsToPatches('/path', [suggestion], 'opp-999');
365
+ expect(patches.length).to.equal(0);
366
+ expect(patches.length).to.equal(0);
359
367
  });
360
368
 
361
- it('should return null for heading-h1-length without selector in transformRules', () => {
369
+ it('should return empty array for heading-h1-length without selector in transformRules', () => {
362
370
  const suggestion = {
363
371
  getId: () => 'sugg-888',
364
372
  getData: () => ({
@@ -370,9 +378,9 @@ describe('HeadingsMapper', () => {
370
378
  }),
371
379
  };
372
380
 
373
- const patch = mapper.suggestionToPatch(suggestion, 'opp-888');
374
-
375
- expect(patch).to.be.null;
381
+ const patches = mapper.suggestionsToPatches('/path', [suggestion], 'opp-888');
382
+ expect(patches.length).to.equal(0);
383
+ expect(patches.length).to.equal(0);
376
384
  });
377
385
 
378
386
  it('should log warning for heading-missing-h1 with missing transformRules - validation path', () => {
@@ -393,9 +401,9 @@ describe('HeadingsMapper', () => {
393
401
  }),
394
402
  };
395
403
 
396
- const patch = warnMapper.suggestionToPatch(suggestion, 'opp-warn');
404
+ const patches = warnMapper.suggestionsToPatches('/path', [suggestion], 'opp-warn');
397
405
 
398
- expect(patch).to.be.null;
406
+ expect(patches.length).to.equal(0);
399
407
  expect(warnLogged).to.be.true;
400
408
  });
401
409
 
@@ -419,9 +427,9 @@ describe('HeadingsMapper', () => {
419
427
  }),
420
428
  };
421
429
 
422
- const patch = warnMapper.suggestionToPatch(suggestion, 'opp-defensive');
430
+ const patches = warnMapper.suggestionsToPatches('/path', [suggestion], 'opp-defensive');
423
431
 
424
- expect(patch).to.be.null;
432
+ expect(patches.length).to.equal(0);
425
433
  expect(warnMessage).to.include('cannot be deployed');
426
434
  });
427
435
  });
@@ -0,0 +1,148 @@
1
+ /*
2
+ * Copyright 2024 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ /* eslint-env mocha */
14
+
15
+ import { expect } from 'chai';
16
+ import { mergePatches } from '../../src/utils/patch-utils.js';
17
+
18
+ describe('Patch Utils', () => {
19
+ describe('mergePatches', () => {
20
+ it('should merge individual patches with same key', () => {
21
+ const existingPatches = [
22
+ {
23
+ op: 'replace',
24
+ opportunityId: 'opp-headings',
25
+ suggestionId: 'sugg-123',
26
+ value: 'old-value',
27
+ },
28
+ ];
29
+
30
+ const newPatches = [
31
+ {
32
+ op: 'replace',
33
+ opportunityId: 'opp-headings',
34
+ suggestionId: 'sugg-123',
35
+ value: 'new-value',
36
+ },
37
+ ];
38
+
39
+ const result = mergePatches(existingPatches, newPatches);
40
+ expect(result.patches).to.have.lengthOf(1);
41
+ expect(result.patches[0].suggestionId).to.equal('sugg-123');
42
+ expect(result.patches[0].value).to.equal('new-value');
43
+ expect(result.updateCount).to.equal(1);
44
+ expect(result.addCount).to.equal(0);
45
+ });
46
+
47
+ it('should keep individual patches with different keys', () => {
48
+ const existingPatches = [
49
+ {
50
+ op: 'replace',
51
+ opportunityId: 'opp-headings',
52
+ suggestionId: 'sugg-1',
53
+ value: 'value-1',
54
+ },
55
+ ];
56
+
57
+ const newPatches = [
58
+ {
59
+ op: 'replace',
60
+ opportunityId: 'opp-headings',
61
+ suggestionId: 'sugg-2',
62
+ value: 'value-2',
63
+ },
64
+ ];
65
+
66
+ const result = mergePatches(existingPatches, newPatches);
67
+ expect(result.patches).to.have.lengthOf(2);
68
+ expect(result.updateCount).to.equal(0);
69
+ expect(result.addCount).to.equal(1);
70
+ });
71
+
72
+ it('should handle empty existing patches', () => {
73
+ const newPatches = [
74
+ {
75
+ op: 'appendChild',
76
+ opportunityId: 'opp-123',
77
+ suggestionId: 'sugg-new',
78
+ value: 'new-value',
79
+ },
80
+ ];
81
+
82
+ const result = mergePatches([], newPatches);
83
+ expect(result.patches).to.have.lengthOf(1);
84
+ expect(result.patches[0]).to.deep.equal(newPatches[0]);
85
+ expect(result.updateCount).to.equal(0);
86
+ expect(result.addCount).to.equal(1);
87
+ });
88
+
89
+ it('should handle empty new patches', () => {
90
+ const existingPatches = [
91
+ {
92
+ op: 'appendChild',
93
+ opportunityId: 'opp-123',
94
+ suggestionId: 'sugg-old',
95
+ value: 'old-value',
96
+ },
97
+ ];
98
+
99
+ const result = mergePatches(existingPatches, []);
100
+ expect(result.patches).to.have.lengthOf(1);
101
+ expect(result.patches[0]).to.deep.equal(existingPatches[0]);
102
+ expect(result.updateCount).to.equal(0);
103
+ expect(result.addCount).to.equal(0);
104
+ });
105
+
106
+ it('should handle patch without suggestionId (heading patch)', () => {
107
+ const existingPatches = [];
108
+ const newPatches = [
109
+ {
110
+ op: 'appendChild',
111
+ opportunityId: 'opp-123',
112
+ value: { type: 'element', tagName: 'h2' },
113
+ // No suggestionId - this is a heading patch
114
+ },
115
+ ];
116
+
117
+ const result = mergePatches(existingPatches, newPatches);
118
+
119
+ expect(result.patches).to.have.lengthOf(1);
120
+ expect(result.addCount).to.equal(1);
121
+ });
122
+
123
+ it('should merge heading patches with same opportunityId', () => {
124
+ const existingPatches = [
125
+ {
126
+ op: 'appendChild',
127
+ opportunityId: 'opp-123',
128
+ value: { type: 'element', tagName: 'h2', children: [{ type: 'text', value: 'Old' }] },
129
+ // No suggestionId
130
+ },
131
+ ];
132
+ const newPatches = [
133
+ {
134
+ op: 'appendChild',
135
+ opportunityId: 'opp-123',
136
+ value: { type: 'element', tagName: 'h2', children: [{ type: 'text', value: 'New' }] },
137
+ // No suggestionId
138
+ },
139
+ ];
140
+
141
+ const result = mergePatches(existingPatches, newPatches);
142
+
143
+ expect(result.patches).to.have.lengthOf(1);
144
+ expect(result.updateCount).to.equal(1);
145
+ expect(result.patches[0].value.children[0].value).to.equal('New');
146
+ });
147
+ });
148
+ });