@adobe/spacecat-shared-tokowaka-client 1.2.4 → 1.3.1
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.
- package/CHANGELOG.md +14 -0
- package/package.json +1 -1
- package/src/index.d.ts +20 -1
- package/src/index.js +59 -5
- package/src/mappers/base-mapper.js +12 -0
- package/src/mappers/generic-mapper.js +117 -0
- package/src/mappers/headings-mapper.js +16 -1
- package/src/mappers/mapper-registry.js +6 -0
- package/src/mappers/prerender-mapper.js +78 -0
- package/src/mappers/toc-mapper.js +116 -0
- package/test/index.test.js +243 -0
- package/test/mappers/generic-mapper.test.js +671 -0
- package/test/mappers/headings-mapper.test.js +154 -3
- package/test/mappers/prerender-mapper.test.js +216 -0
- package/test/mappers/toc-mapper.test.js +616 -0
|
@@ -0,0 +1,671 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2025 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 GenericMapper from '../../src/mappers/generic-mapper.js';
|
|
17
|
+
|
|
18
|
+
describe('GenericMapper', () => {
|
|
19
|
+
let mapper;
|
|
20
|
+
let log;
|
|
21
|
+
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
log = {
|
|
24
|
+
debug: () => {},
|
|
25
|
+
info: () => {},
|
|
26
|
+
warn: () => {},
|
|
27
|
+
error: () => {},
|
|
28
|
+
};
|
|
29
|
+
mapper = new GenericMapper(log);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe('getOpportunityType', () => {
|
|
33
|
+
it('should return generic', () => {
|
|
34
|
+
expect(mapper.getOpportunityType()).to.equal('generic-autofix-edge');
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe('requiresPrerender', () => {
|
|
39
|
+
it('should return true', () => {
|
|
40
|
+
expect(mapper.requiresPrerender()).to.be.true;
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
describe('allowConfigsWithoutPatch', () => {
|
|
45
|
+
it('should return false for generic mapper', () => {
|
|
46
|
+
expect(mapper.allowConfigsWithoutPatch()).to.be.false;
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe('canDeploy', () => {
|
|
51
|
+
it('should return eligible for valid suggestion with all required fields', () => {
|
|
52
|
+
const suggestion = {
|
|
53
|
+
getData: () => ({
|
|
54
|
+
transformRules: {
|
|
55
|
+
action: 'insertAfter',
|
|
56
|
+
selector: '#create-with-multiple-top-ai-models-all-in-one-place',
|
|
57
|
+
},
|
|
58
|
+
patchValue: 'Blah Blah some text',
|
|
59
|
+
url: 'https://www.adobe.com/products/firefly.html',
|
|
60
|
+
}),
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const result = mapper.canDeploy(suggestion);
|
|
64
|
+
|
|
65
|
+
expect(result).to.deep.equal({ eligible: true });
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('should return eligible for insertBefore operation', () => {
|
|
69
|
+
const suggestion = {
|
|
70
|
+
getData: () => ({
|
|
71
|
+
transformRules: {
|
|
72
|
+
action: 'insertBefore',
|
|
73
|
+
selector: 'h1',
|
|
74
|
+
},
|
|
75
|
+
patchValue: 'New content',
|
|
76
|
+
url: 'https://example.com/page',
|
|
77
|
+
}),
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const result = mapper.canDeploy(suggestion);
|
|
81
|
+
|
|
82
|
+
expect(result).to.deep.equal({ eligible: true });
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('should return eligible for replace operation', () => {
|
|
86
|
+
const suggestion = {
|
|
87
|
+
getData: () => ({
|
|
88
|
+
transformRules: {
|
|
89
|
+
action: 'replace',
|
|
90
|
+
selector: '.content',
|
|
91
|
+
},
|
|
92
|
+
patchValue: 'Replaced content',
|
|
93
|
+
url: 'https://example.com/page',
|
|
94
|
+
}),
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const result = mapper.canDeploy(suggestion);
|
|
98
|
+
|
|
99
|
+
expect(result).to.deep.equal({ eligible: true });
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('should return ineligible when transformRules is missing', () => {
|
|
103
|
+
const suggestion = {
|
|
104
|
+
getData: () => ({
|
|
105
|
+
patchValue: 'Some text',
|
|
106
|
+
url: 'https://example.com/page',
|
|
107
|
+
}),
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const result = mapper.canDeploy(suggestion);
|
|
111
|
+
|
|
112
|
+
expect(result).to.deep.equal({
|
|
113
|
+
eligible: false,
|
|
114
|
+
reason: 'transformRules is required',
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('should return ineligible when selector is missing', () => {
|
|
119
|
+
const suggestion = {
|
|
120
|
+
getData: () => ({
|
|
121
|
+
transformRules: {
|
|
122
|
+
action: 'insertAfter',
|
|
123
|
+
},
|
|
124
|
+
patchValue: 'Some text',
|
|
125
|
+
url: 'https://example.com/page',
|
|
126
|
+
}),
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const result = mapper.canDeploy(suggestion);
|
|
130
|
+
|
|
131
|
+
expect(result).to.deep.equal({
|
|
132
|
+
eligible: false,
|
|
133
|
+
reason: 'transformRules.selector is required',
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('should return ineligible when selector is empty string', () => {
|
|
138
|
+
const suggestion = {
|
|
139
|
+
getData: () => ({
|
|
140
|
+
transformRules: {
|
|
141
|
+
action: 'insertAfter',
|
|
142
|
+
selector: '',
|
|
143
|
+
},
|
|
144
|
+
patchValue: 'Some text',
|
|
145
|
+
url: 'https://example.com/page',
|
|
146
|
+
}),
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
const result = mapper.canDeploy(suggestion);
|
|
150
|
+
|
|
151
|
+
expect(result).to.deep.equal({
|
|
152
|
+
eligible: false,
|
|
153
|
+
reason: 'transformRules.selector is required',
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it('should return ineligible when patchValue is missing', () => {
|
|
158
|
+
const suggestion = {
|
|
159
|
+
getData: () => ({
|
|
160
|
+
transformRules: {
|
|
161
|
+
action: 'insertAfter',
|
|
162
|
+
selector: '#selector',
|
|
163
|
+
},
|
|
164
|
+
url: 'https://example.com/page',
|
|
165
|
+
}),
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
const result = mapper.canDeploy(suggestion);
|
|
169
|
+
|
|
170
|
+
expect(result).to.deep.equal({
|
|
171
|
+
eligible: false,
|
|
172
|
+
reason: 'patchValue is required',
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('should return ineligible when patchValue is empty string', () => {
|
|
177
|
+
const suggestion = {
|
|
178
|
+
getData: () => ({
|
|
179
|
+
transformRules: {
|
|
180
|
+
action: 'insertAfter',
|
|
181
|
+
selector: '#selector',
|
|
182
|
+
},
|
|
183
|
+
patchValue: '',
|
|
184
|
+
url: 'https://example.com/page',
|
|
185
|
+
}),
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
const result = mapper.canDeploy(suggestion);
|
|
189
|
+
|
|
190
|
+
expect(result).to.deep.equal({
|
|
191
|
+
eligible: false,
|
|
192
|
+
reason: 'patchValue is required',
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it('should return ineligible when action is missing', () => {
|
|
197
|
+
const suggestion = {
|
|
198
|
+
getData: () => ({
|
|
199
|
+
transformRules: {
|
|
200
|
+
selector: '#selector',
|
|
201
|
+
},
|
|
202
|
+
patchValue: 'Some text',
|
|
203
|
+
url: 'https://example.com/page',
|
|
204
|
+
}),
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
const result = mapper.canDeploy(suggestion);
|
|
208
|
+
|
|
209
|
+
expect(result).to.deep.equal({
|
|
210
|
+
eligible: false,
|
|
211
|
+
reason: 'transformRules.action is required',
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it('should return ineligible when action is empty string', () => {
|
|
216
|
+
const suggestion = {
|
|
217
|
+
getData: () => ({
|
|
218
|
+
transformRules: {
|
|
219
|
+
action: '',
|
|
220
|
+
selector: '#selector',
|
|
221
|
+
},
|
|
222
|
+
patchValue: 'Some text',
|
|
223
|
+
url: 'https://example.com/page',
|
|
224
|
+
}),
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
const result = mapper.canDeploy(suggestion);
|
|
228
|
+
|
|
229
|
+
expect(result).to.deep.equal({
|
|
230
|
+
eligible: false,
|
|
231
|
+
reason: 'transformRules.action is required',
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it('should return ineligible when action is invalid', () => {
|
|
236
|
+
const suggestion = {
|
|
237
|
+
getData: () => ({
|
|
238
|
+
transformRules: {
|
|
239
|
+
action: 'invalidOperation',
|
|
240
|
+
selector: '#selector',
|
|
241
|
+
},
|
|
242
|
+
patchValue: 'Some text',
|
|
243
|
+
url: 'https://example.com/page',
|
|
244
|
+
}),
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
const result = mapper.canDeploy(suggestion);
|
|
248
|
+
|
|
249
|
+
expect(result).to.deep.equal({
|
|
250
|
+
eligible: false,
|
|
251
|
+
reason: 'transformRules.action must be one of: insertBefore, insertAfter, replace. Got: invalidOperation',
|
|
252
|
+
});
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it('should return ineligible when url is missing', () => {
|
|
256
|
+
const suggestion = {
|
|
257
|
+
getData: () => ({
|
|
258
|
+
transformRules: {
|
|
259
|
+
action: 'insertAfter',
|
|
260
|
+
selector: '#selector',
|
|
261
|
+
},
|
|
262
|
+
patchValue: 'Some text',
|
|
263
|
+
}),
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
const result = mapper.canDeploy(suggestion);
|
|
267
|
+
|
|
268
|
+
expect(result).to.deep.equal({
|
|
269
|
+
eligible: false,
|
|
270
|
+
reason: 'url is required',
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it('should return ineligible when url is empty string', () => {
|
|
275
|
+
const suggestion = {
|
|
276
|
+
getData: () => ({
|
|
277
|
+
transformRules: {
|
|
278
|
+
action: 'insertAfter',
|
|
279
|
+
selector: '#selector',
|
|
280
|
+
},
|
|
281
|
+
patchValue: 'Some text',
|
|
282
|
+
url: '',
|
|
283
|
+
}),
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
const result = mapper.canDeploy(suggestion);
|
|
287
|
+
|
|
288
|
+
expect(result).to.deep.equal({
|
|
289
|
+
eligible: false,
|
|
290
|
+
reason: 'url is required',
|
|
291
|
+
});
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
it('should return ineligible when data is null', () => {
|
|
295
|
+
const suggestion = {
|
|
296
|
+
getData: () => null,
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
const result = mapper.canDeploy(suggestion);
|
|
300
|
+
|
|
301
|
+
expect(result).to.deep.equal({
|
|
302
|
+
eligible: false,
|
|
303
|
+
reason: 'transformRules is required',
|
|
304
|
+
});
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
it('should return ineligible when data is undefined', () => {
|
|
308
|
+
const suggestion = {
|
|
309
|
+
getData: () => undefined,
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
const result = mapper.canDeploy(suggestion);
|
|
313
|
+
|
|
314
|
+
expect(result).to.deep.equal({
|
|
315
|
+
eligible: false,
|
|
316
|
+
reason: 'transformRules is required',
|
|
317
|
+
});
|
|
318
|
+
});
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
describe('suggestionsToPatches', () => {
|
|
322
|
+
it('should create patch for valid suggestion with insertAfter', () => {
|
|
323
|
+
const suggestion = {
|
|
324
|
+
getId: () => 'ee8fc5e8-29c1-4894-9391-efc10b8a5f5c',
|
|
325
|
+
getUpdatedAt: () => '2025-11-27T16:22:14.258Z',
|
|
326
|
+
getData: () => ({
|
|
327
|
+
transformRules: {
|
|
328
|
+
action: 'insertAfter',
|
|
329
|
+
selector: '#create-with-multiple-top-ai-models-all-in-one-place',
|
|
330
|
+
},
|
|
331
|
+
patchValue: 'Blah Blah some text',
|
|
332
|
+
url: 'https://www.adobe.com/products/firefly.html',
|
|
333
|
+
contentBefore: '**Create with multiple top AI models, all in one place.**',
|
|
334
|
+
rationale: 'This makes LLMs read more text about blah blah.',
|
|
335
|
+
}),
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
const patches = mapper.suggestionsToPatches(
|
|
339
|
+
'/products/firefly.html',
|
|
340
|
+
[suggestion],
|
|
341
|
+
'7a663e47-e132-4bba-954a-26419e0541b8',
|
|
342
|
+
);
|
|
343
|
+
|
|
344
|
+
expect(patches.length).to.equal(1);
|
|
345
|
+
const patch = patches[0];
|
|
346
|
+
|
|
347
|
+
expect(patch).to.deep.include({
|
|
348
|
+
op: 'insertAfter',
|
|
349
|
+
selector: '#create-with-multiple-top-ai-models-all-in-one-place',
|
|
350
|
+
value: 'Blah Blah some text',
|
|
351
|
+
valueFormat: 'text',
|
|
352
|
+
opportunityId: '7a663e47-e132-4bba-954a-26419e0541b8',
|
|
353
|
+
suggestionId: 'ee8fc5e8-29c1-4894-9391-efc10b8a5f5c',
|
|
354
|
+
prerenderRequired: true,
|
|
355
|
+
});
|
|
356
|
+
expect(patch.lastUpdated).to.be.a('number');
|
|
357
|
+
expect(patch.target).to.equal('ai-bots');
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
it('should create patch for insertBefore operation', () => {
|
|
361
|
+
const suggestion = {
|
|
362
|
+
getId: () => 'sugg-123',
|
|
363
|
+
getUpdatedAt: () => '2025-01-15T10:00:00.000Z',
|
|
364
|
+
getData: () => ({
|
|
365
|
+
transformRules: {
|
|
366
|
+
action: 'insertBefore',
|
|
367
|
+
selector: 'h1',
|
|
368
|
+
},
|
|
369
|
+
patchValue: 'Important notice',
|
|
370
|
+
url: 'https://example.com/page',
|
|
371
|
+
}),
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
const patches = mapper.suggestionsToPatches('/page', [suggestion], 'opp-123');
|
|
375
|
+
|
|
376
|
+
expect(patches.length).to.equal(1);
|
|
377
|
+
const patch = patches[0];
|
|
378
|
+
|
|
379
|
+
expect(patch).to.deep.include({
|
|
380
|
+
op: 'insertBefore',
|
|
381
|
+
selector: 'h1',
|
|
382
|
+
value: 'Important notice',
|
|
383
|
+
valueFormat: 'text',
|
|
384
|
+
opportunityId: 'opp-123',
|
|
385
|
+
suggestionId: 'sugg-123',
|
|
386
|
+
prerenderRequired: true,
|
|
387
|
+
});
|
|
388
|
+
expect(patch.lastUpdated).to.be.a('number');
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
it('should create patch for replace operation', () => {
|
|
392
|
+
const suggestion = {
|
|
393
|
+
getId: () => 'sugg-456',
|
|
394
|
+
getUpdatedAt: () => '2025-01-15T10:00:00.000Z',
|
|
395
|
+
getData: () => ({
|
|
396
|
+
transformRules: {
|
|
397
|
+
action: 'replace',
|
|
398
|
+
selector: '.content',
|
|
399
|
+
},
|
|
400
|
+
patchValue: 'Replaced content text',
|
|
401
|
+
url: 'https://example.com/page2',
|
|
402
|
+
}),
|
|
403
|
+
};
|
|
404
|
+
|
|
405
|
+
const patches = mapper.suggestionsToPatches('/page2', [suggestion], 'opp-456');
|
|
406
|
+
|
|
407
|
+
expect(patches.length).to.equal(1);
|
|
408
|
+
const patch = patches[0];
|
|
409
|
+
|
|
410
|
+
expect(patch).to.deep.include({
|
|
411
|
+
op: 'replace',
|
|
412
|
+
selector: '.content',
|
|
413
|
+
value: 'Replaced content text',
|
|
414
|
+
valueFormat: 'text',
|
|
415
|
+
opportunityId: 'opp-456',
|
|
416
|
+
suggestionId: 'sugg-456',
|
|
417
|
+
prerenderRequired: true,
|
|
418
|
+
});
|
|
419
|
+
expect(patch.lastUpdated).to.be.a('number');
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
it('should handle multiple suggestions', () => {
|
|
423
|
+
const suggestions = [
|
|
424
|
+
{
|
|
425
|
+
getId: () => 'sugg-1',
|
|
426
|
+
getUpdatedAt: () => '2025-01-15T10:00:00.000Z',
|
|
427
|
+
getData: () => ({
|
|
428
|
+
transformRules: {
|
|
429
|
+
action: 'insertAfter',
|
|
430
|
+
selector: '#selector1',
|
|
431
|
+
},
|
|
432
|
+
patchValue: 'Text 1',
|
|
433
|
+
url: 'https://example.com/page',
|
|
434
|
+
}),
|
|
435
|
+
},
|
|
436
|
+
{
|
|
437
|
+
getId: () => 'sugg-2',
|
|
438
|
+
getUpdatedAt: () => '2025-01-15T11:00:00.000Z',
|
|
439
|
+
getData: () => ({
|
|
440
|
+
transformRules: {
|
|
441
|
+
action: 'insertBefore',
|
|
442
|
+
selector: '#selector2',
|
|
443
|
+
},
|
|
444
|
+
patchValue: 'Text 2',
|
|
445
|
+
url: 'https://example.com/page',
|
|
446
|
+
}),
|
|
447
|
+
},
|
|
448
|
+
];
|
|
449
|
+
|
|
450
|
+
const patches = mapper.suggestionsToPatches('/page', suggestions, 'opp-123');
|
|
451
|
+
|
|
452
|
+
expect(patches.length).to.equal(2);
|
|
453
|
+
expect(patches[0].suggestionId).to.equal('sugg-1');
|
|
454
|
+
expect(patches[0].value).to.equal('Text 1');
|
|
455
|
+
expect(patches[1].suggestionId).to.equal('sugg-2');
|
|
456
|
+
expect(patches[1].value).to.equal('Text 2');
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
it('should return empty array for invalid suggestion', () => {
|
|
460
|
+
const suggestion = {
|
|
461
|
+
getId: () => 'sugg-invalid',
|
|
462
|
+
getData: () => ({
|
|
463
|
+
transformRules: {
|
|
464
|
+
action: 'insertAfter',
|
|
465
|
+
selector: '#selector',
|
|
466
|
+
},
|
|
467
|
+
// Missing patchValue
|
|
468
|
+
url: 'https://example.com/page',
|
|
469
|
+
}),
|
|
470
|
+
};
|
|
471
|
+
|
|
472
|
+
const patches = mapper.suggestionsToPatches('/page', [suggestion], 'opp-invalid');
|
|
473
|
+
|
|
474
|
+
expect(patches.length).to.equal(0);
|
|
475
|
+
});
|
|
476
|
+
|
|
477
|
+
it('should skip invalid suggestions but process valid ones', () => {
|
|
478
|
+
const suggestions = [
|
|
479
|
+
{
|
|
480
|
+
getId: () => 'sugg-valid',
|
|
481
|
+
getUpdatedAt: () => '2025-01-15T10:00:00.000Z',
|
|
482
|
+
getData: () => ({
|
|
483
|
+
transformRules: {
|
|
484
|
+
action: 'insertAfter',
|
|
485
|
+
selector: '#valid',
|
|
486
|
+
},
|
|
487
|
+
patchValue: 'Valid text',
|
|
488
|
+
url: 'https://example.com/page',
|
|
489
|
+
}),
|
|
490
|
+
},
|
|
491
|
+
{
|
|
492
|
+
getId: () => 'sugg-invalid',
|
|
493
|
+
getData: () => ({
|
|
494
|
+
transformRules: {
|
|
495
|
+
action: 'insertAfter',
|
|
496
|
+
selector: '#invalid',
|
|
497
|
+
},
|
|
498
|
+
// Missing patchValue
|
|
499
|
+
url: 'https://example.com/page',
|
|
500
|
+
}),
|
|
501
|
+
},
|
|
502
|
+
];
|
|
503
|
+
|
|
504
|
+
const patches = mapper.suggestionsToPatches('/page', suggestions, 'opp-123');
|
|
505
|
+
|
|
506
|
+
expect(patches.length).to.equal(1);
|
|
507
|
+
expect(patches[0].suggestionId).to.equal('sugg-valid');
|
|
508
|
+
});
|
|
509
|
+
|
|
510
|
+
it('should log warning for invalid suggestion', () => {
|
|
511
|
+
let warnMessage = '';
|
|
512
|
+
const warnLog = {
|
|
513
|
+
debug: () => {},
|
|
514
|
+
info: () => {},
|
|
515
|
+
warn: (msg) => { warnMessage = msg; },
|
|
516
|
+
error: () => {},
|
|
517
|
+
};
|
|
518
|
+
const warnMapper = new GenericMapper(warnLog);
|
|
519
|
+
|
|
520
|
+
const suggestion = {
|
|
521
|
+
getId: () => 'sugg-warn',
|
|
522
|
+
getData: () => ({
|
|
523
|
+
transformRules: {
|
|
524
|
+
action: 'insertAfter',
|
|
525
|
+
selector: '#selector',
|
|
526
|
+
},
|
|
527
|
+
// Missing patchValue
|
|
528
|
+
url: 'https://example.com/page',
|
|
529
|
+
}),
|
|
530
|
+
};
|
|
531
|
+
|
|
532
|
+
const patches = warnMapper.suggestionsToPatches('/page', [suggestion], 'opp-warn');
|
|
533
|
+
|
|
534
|
+
expect(patches.length).to.equal(0);
|
|
535
|
+
expect(warnMessage).to.include('Generic suggestion sugg-warn cannot be deployed');
|
|
536
|
+
expect(warnMessage).to.include('patchValue is required');
|
|
537
|
+
});
|
|
538
|
+
|
|
539
|
+
it('should handle complex CSS selectors', () => {
|
|
540
|
+
const suggestion = {
|
|
541
|
+
getId: () => 'sugg-complex',
|
|
542
|
+
getUpdatedAt: () => '2025-01-15T10:00:00.000Z',
|
|
543
|
+
getData: () => ({
|
|
544
|
+
transformRules: {
|
|
545
|
+
action: 'insertAfter',
|
|
546
|
+
selector: '#text-85a9876220 > h2:nth-of-type(1)',
|
|
547
|
+
},
|
|
548
|
+
patchValue: 'Complex selector content',
|
|
549
|
+
url: 'https://example.com/page',
|
|
550
|
+
}),
|
|
551
|
+
};
|
|
552
|
+
|
|
553
|
+
const patches = mapper.suggestionsToPatches('/page', [suggestion], 'opp-complex');
|
|
554
|
+
|
|
555
|
+
expect(patches.length).to.equal(1);
|
|
556
|
+
expect(patches[0].selector).to.equal('#text-85a9876220 > h2:nth-of-type(1)');
|
|
557
|
+
});
|
|
558
|
+
|
|
559
|
+
it('should handle multiline patchValue', () => {
|
|
560
|
+
const suggestion = {
|
|
561
|
+
getId: () => 'sugg-multiline',
|
|
562
|
+
getUpdatedAt: () => '2025-01-15T10:00:00.000Z',
|
|
563
|
+
getData: () => ({
|
|
564
|
+
transformRules: {
|
|
565
|
+
action: 'replace',
|
|
566
|
+
selector: '.content',
|
|
567
|
+
},
|
|
568
|
+
patchValue: 'Line 1\nLine 2\nLine 3',
|
|
569
|
+
url: 'https://example.com/page',
|
|
570
|
+
}),
|
|
571
|
+
};
|
|
572
|
+
|
|
573
|
+
const patches = mapper.suggestionsToPatches('/page', [suggestion], 'opp-multiline');
|
|
574
|
+
|
|
575
|
+
expect(patches.length).to.equal(1);
|
|
576
|
+
expect(patches[0].value).to.equal('Line 1\nLine 2\nLine 3');
|
|
577
|
+
});
|
|
578
|
+
|
|
579
|
+
it('should include tag when provided', () => {
|
|
580
|
+
const suggestion = {
|
|
581
|
+
getId: () => 'sugg-with-tag',
|
|
582
|
+
getUpdatedAt: () => '2025-01-15T10:00:00.000Z',
|
|
583
|
+
getData: () => ({
|
|
584
|
+
transformRules: {
|
|
585
|
+
action: 'insertAfter',
|
|
586
|
+
selector: '#selector',
|
|
587
|
+
},
|
|
588
|
+
patchValue: 'Content with tag',
|
|
589
|
+
format: 'hast',
|
|
590
|
+
tag: 'div',
|
|
591
|
+
url: 'https://example.com/page',
|
|
592
|
+
}),
|
|
593
|
+
};
|
|
594
|
+
|
|
595
|
+
const patches = mapper.suggestionsToPatches('/page', [suggestion], 'opp-tag');
|
|
596
|
+
|
|
597
|
+
expect(patches.length).to.equal(1);
|
|
598
|
+
const patch = patches[0];
|
|
599
|
+
|
|
600
|
+
expect(patch).to.deep.include({
|
|
601
|
+
op: 'insertAfter',
|
|
602
|
+
selector: '#selector',
|
|
603
|
+
value: 'Content with tag',
|
|
604
|
+
valueFormat: 'hast',
|
|
605
|
+
tag: 'div',
|
|
606
|
+
opportunityId: 'opp-tag',
|
|
607
|
+
suggestionId: 'sugg-with-tag',
|
|
608
|
+
prerenderRequired: true,
|
|
609
|
+
});
|
|
610
|
+
expect(patch.lastUpdated).to.be.a('number');
|
|
611
|
+
});
|
|
612
|
+
|
|
613
|
+
it('should not include tag when not provided', () => {
|
|
614
|
+
const suggestion = {
|
|
615
|
+
getId: () => 'sugg-no-tag',
|
|
616
|
+
getUpdatedAt: () => '2025-01-15T10:00:00.000Z',
|
|
617
|
+
getData: () => ({
|
|
618
|
+
transformRules: {
|
|
619
|
+
action: 'insertAfter',
|
|
620
|
+
selector: '#selector',
|
|
621
|
+
},
|
|
622
|
+
patchValue: 'Content without tag',
|
|
623
|
+
url: 'https://example.com/page',
|
|
624
|
+
}),
|
|
625
|
+
};
|
|
626
|
+
|
|
627
|
+
const patches = mapper.suggestionsToPatches('/page', [suggestion], 'opp-no-tag');
|
|
628
|
+
|
|
629
|
+
expect(patches.length).to.equal(1);
|
|
630
|
+
const patch = patches[0];
|
|
631
|
+
|
|
632
|
+
expect(patch.tag).to.be.undefined;
|
|
633
|
+
expect(patch.valueFormat).to.equal('text');
|
|
634
|
+
});
|
|
635
|
+
|
|
636
|
+
it('should not include UI-only fields in patch', () => {
|
|
637
|
+
const suggestion = {
|
|
638
|
+
getId: () => 'sugg-ui',
|
|
639
|
+
getUpdatedAt: () => '2025-01-15T10:00:00.000Z',
|
|
640
|
+
getData: () => ({
|
|
641
|
+
transformRules: {
|
|
642
|
+
action: 'insertAfter',
|
|
643
|
+
selector: '#selector',
|
|
644
|
+
},
|
|
645
|
+
patchValue: 'Text content',
|
|
646
|
+
url: 'https://example.com/page',
|
|
647
|
+
contentBefore: 'Original content',
|
|
648
|
+
expectedContentAfter: 'Expected result',
|
|
649
|
+
rationale: 'This improves SEO',
|
|
650
|
+
aggregationKey: 'some-key',
|
|
651
|
+
}),
|
|
652
|
+
};
|
|
653
|
+
|
|
654
|
+
const patches = mapper.suggestionsToPatches('/page', [suggestion], 'opp-ui');
|
|
655
|
+
|
|
656
|
+
expect(patches.length).to.equal(1);
|
|
657
|
+
const patch = patches[0];
|
|
658
|
+
|
|
659
|
+
// Should not include UI-only fields
|
|
660
|
+
expect(patch.contentBefore).to.be.undefined;
|
|
661
|
+
expect(patch.expectedContentAfter).to.be.undefined;
|
|
662
|
+
expect(patch.rationale).to.be.undefined;
|
|
663
|
+
expect(patch.aggregationKey).to.be.undefined;
|
|
664
|
+
|
|
665
|
+
// Should include only operational fields
|
|
666
|
+
expect(patch.op).to.equal('insertAfter');
|
|
667
|
+
expect(patch.selector).to.equal('#selector');
|
|
668
|
+
expect(patch.value).to.equal('Text content');
|
|
669
|
+
});
|
|
670
|
+
});
|
|
671
|
+
});
|