-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathtracked-rewrite.integration.test.ts
More file actions
427 lines (366 loc) · 14.7 KB
/
tracked-rewrite.integration.test.ts
File metadata and controls
427 lines (366 loc) · 14.7 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import { afterEach, describe, expect, it } from 'vitest';
import { initTestEditor } from '@tests/helpers/helpers.js';
import { TrackDeleteMarkName, TrackInsertMarkName } from '../../extensions/track-changes/constants.js';
import { compilePlan } from './compiler.ts';
import { executeTextRewrite } from './executor.ts';
function makeEditor(paragraphs: string[] = ['hello world']) {
return initTestEditor({
loadFromSchema: true,
content: {
type: 'doc',
content: paragraphs.map((text) => ({
type: 'paragraph',
attrs: {},
content: [
{
type: 'run',
attrs: {},
content: [{ type: 'text', text }],
},
],
})),
},
user: { name: 'Integration User', email: 'integration@example.com' },
}).editor;
}
function getFirstMatchRef(editor: any, pattern: string): string {
const match = editor.doc.query.match({
select: { type: 'text', pattern },
require: 'first',
});
const ref = match?.items?.[0]?.handle?.ref;
if (!ref) {
throw new Error(`Could not resolve ref for pattern "${pattern}"`);
}
return ref;
}
function paragraphTexts(editor: any): string[] {
const paragraphs: string[] = [];
editor.state.doc.forEach((node: any) => {
if (node.type.name === 'paragraph') {
paragraphs.push(node.textContent);
}
});
return paragraphs;
}
function compileSingleRewrite(editor: any, pattern: string, text: string) {
const step = {
id: 'rewrite-step',
op: 'text.rewrite',
where: { by: 'ref', ref: getFirstMatchRef(editor, pattern) },
args: {
replacement: { text },
style: { inline: { mode: 'preserve' } },
},
} as const;
const compiled = compilePlan(editor, [step as any]);
const compiledStep = compiled.mutationSteps[0];
return { step, target: compiledStep.targets[0] };
}
describe('doc.replace multi-paragraph integration', () => {
let editor: any | undefined;
afterEach(() => {
editor?.destroy();
editor = undefined;
});
it('creates sibling paragraphs in direct mode for a full-paragraph text replacement', () => {
editor = makeEditor();
const receipt = editor.doc.replace(
{
ref: getFirstMatchRef(editor, 'hello world'),
text: 'Alpha\n\nBeta',
},
{ changeMode: 'direct' },
);
expect(receipt.success).toBe(true);
expect(paragraphTexts(editor)).toEqual(['Alpha', 'Beta']);
});
it('preserves sibling paragraphs in tracked mode for a full-paragraph text replacement', () => {
editor = makeEditor();
const receipt = editor.doc.replace(
{
ref: getFirstMatchRef(editor, 'hello world'),
text: 'Alpha\n\nBeta',
},
{ changeMode: 'tracked' },
);
expect(receipt.success).toBe(true);
expect(paragraphTexts(editor)).toEqual(['hello world', 'Alpha', 'Beta']);
const insertedTexts: string[] = [];
const deletedTexts: string[] = [];
editor.state.doc.descendants((node: any) => {
if (!node.isText || !node.text) return;
if (node.marks.some((mark: any) => mark.type.name === TrackInsertMarkName)) {
insertedTexts.push(node.text);
}
if (node.marks.some((mark: any) => mark.type.name === TrackDeleteMarkName)) {
deletedTexts.push(node.text);
}
});
expect(insertedTexts).toEqual(expect.arrayContaining(['Alpha', 'Beta']));
expect(deletedTexts.join('')).toContain('hello world');
});
it('splits a middle-of-paragraph direct replacement into sibling paragraphs', () => {
editor = makeEditor(['hey guys, hello world and this stuff is great']);
const receipt = editor.doc.replace(
{
ref: getFirstMatchRef(editor, 'hello world'),
text: 'Alpha\n\nBeta',
},
{ changeMode: 'direct' },
);
expect(receipt.success).toBe(true);
expect(paragraphTexts(editor)).toEqual(['hey guys, Alpha', 'Beta and this stuff is great']);
});
it('keeps middle-of-paragraph structural rewrites intact through applyTransaction', () => {
editor = makeEditor(['hey guys, hello world and this stuff is great']);
const { step, target } = compileSingleRewrite(editor, 'hello world', 'Alpha\n\nBeta');
const tr = editor.state.tr;
const outcome = executeTextRewrite(editor, tr, target as any, step as any, tr.mapping as any);
expect(outcome).toEqual({ changed: true });
expect(tr.doc.childCount).toBe(2);
expect(tr.doc.child(0).textContent).toBe('hey guys, Alpha');
expect(tr.doc.child(1).textContent).toBe('Beta and this stuff is great');
const applied = editor.state.applyTransaction(tr);
const appliedParagraphs: string[] = [];
applied.state.doc.forEach((node: any) => {
if (node.type.name === 'paragraph') {
appliedParagraphs.push(node.textContent);
}
});
expect(appliedParagraphs).toEqual(['hey guys, Alpha', 'Beta and this stuff is great']);
});
it('preserves paragraph structure in tracked mode for a middle-of-paragraph replacement', () => {
editor = makeEditor(['hey guys, hello world and this stuff is great']);
const receipt = editor.doc.replace(
{
ref: getFirstMatchRef(editor, 'hello world'),
text: 'Alpha\n\nBeta',
},
{ changeMode: 'tracked' },
);
expect(receipt.success).toBe(true);
expect(paragraphTexts(editor)).toHaveLength(2);
expect(paragraphTexts(editor)[0]).toContain('hey guys,');
expect(paragraphTexts(editor)[0]).toContain('hello world');
expect(paragraphTexts(editor)[0]).toContain('Alpha');
expect(paragraphTexts(editor)[1]).toBe('Beta and this stuff is great');
const insertedTexts: string[] = [];
const deletedTexts: string[] = [];
editor.state.doc.descendants((node: any) => {
if (!node.isText || !node.text) return;
if (node.marks.some((mark: any) => mark.type.name === TrackInsertMarkName)) {
insertedTexts.push(node.text);
}
if (node.marks.some((mark: any) => mark.type.name === TrackDeleteMarkName)) {
deletedTexts.push(node.text);
}
});
expect(insertedTexts).toEqual(expect.arrayContaining(['Alpha', 'Beta']));
expect(deletedTexts.join('')).toContain('hello world');
});
it('preserves paragraph structure for tracked text.insert plans inside a paragraph', () => {
editor = makeEditor(['hey guys, and this stuff is great']);
const receipt = editor.doc.mutations.apply({
atomic: true,
changeMode: 'tracked',
steps: [
{
id: 'insert-after-prefix',
op: 'text.insert',
where: {
by: 'select',
select: { type: 'text', pattern: 'hey guys,' },
require: 'first',
},
args: {
position: 'after',
content: { text: '\nAlpha\n\nBeta' },
},
},
],
});
expect(receipt.success).toBe(true);
expect(paragraphTexts(editor)).toEqual(['hey guys,', 'Alpha', 'Beta and this stuff is great']);
const insertedTexts: string[] = [];
editor.state.doc.descendants((node: any) => {
if (!node.isText || !node.text) return;
if (node.marks.some((mark: any) => mark.type.name === TrackInsertMarkName)) {
insertedTexts.push(node.text);
}
});
expect(insertedTexts).toEqual(expect.arrayContaining(['Alpha', 'Beta']));
});
it('creates an empty paragraph before the replacement when text has a leading newline (direct)', () => {
editor = makeEditor();
const receipt = editor.doc.replace(
{
ref: getFirstMatchRef(editor, 'hello world'),
text: '\nAlpha',
},
{ changeMode: 'direct' },
);
expect(receipt.success).toBe(true);
expect(paragraphTexts(editor)).toEqual(['', 'Alpha']);
});
it('creates an empty paragraph after the replacement when text has a trailing newline (direct)', () => {
editor = makeEditor();
const receipt = editor.doc.replace(
{
ref: getFirstMatchRef(editor, 'hello world'),
text: 'Alpha\n',
},
{ changeMode: 'direct' },
);
expect(receipt.success).toBe(true);
expect(paragraphTexts(editor)).toEqual(['Alpha', '']);
});
it('preserves paragraph structure with leading newline in tracked mode', () => {
editor = makeEditor();
const receipt = editor.doc.replace(
{
ref: getFirstMatchRef(editor, 'hello world'),
text: '\nAlpha',
},
{ changeMode: 'tracked' },
);
expect(receipt.success).toBe(true);
const texts = paragraphTexts(editor);
expect(texts.length).toBeGreaterThanOrEqual(2);
const insertedTexts: string[] = [];
const deletedTexts: string[] = [];
editor.state.doc.descendants((node: any) => {
if (!node.isText || !node.text) return;
if (node.marks.some((mark: any) => mark.type.name === TrackInsertMarkName)) {
insertedTexts.push(node.text);
}
if (node.marks.some((mark: any) => mark.type.name === TrackDeleteMarkName)) {
deletedTexts.push(node.text);
}
});
expect(insertedTexts).toEqual(expect.arrayContaining(['Alpha']));
expect(deletedTexts.join('')).toContain('hello world');
});
it('preserves paragraph structure with trailing newline in tracked mode', () => {
editor = makeEditor();
const receipt = editor.doc.replace(
{
ref: getFirstMatchRef(editor, 'hello world'),
text: 'Alpha\n',
},
{ changeMode: 'tracked' },
);
expect(receipt.success).toBe(true);
const texts = paragraphTexts(editor);
expect(texts.length).toBeGreaterThanOrEqual(2);
const insertedTexts: string[] = [];
const deletedTexts: string[] = [];
editor.state.doc.descendants((node: any) => {
if (!node.isText || !node.text) return;
if (node.marks.some((mark: any) => mark.type.name === TrackInsertMarkName)) {
insertedTexts.push(node.text);
}
if (node.marks.some((mark: any) => mark.type.name === TrackDeleteMarkName)) {
deletedTexts.push(node.text);
}
});
expect(insertedTexts).toEqual(expect.arrayContaining(['Alpha']));
expect(deletedTexts.join('')).toContain('hello world');
});
// SD-3044: when the word-diff produces multiple groups with EQUAL tokens
// between them, inserted text used to anchor on the previous result op's
// end instead of the EQUAL token's end, piling all granular insertions on
// the first deletion site.
it('SD-3044: tracked rewrite with shared suffix anchors inserts correctly', () => {
editor = makeEditor(['[insert] of [insert], [insert] ("Investor")']);
const receipt = editor.doc.replace(
{
ref: getFirstMatchRef(editor, '[insert] of [insert], [insert] ("Investor")'),
text: 'John James Smith of [insert address], [insert] ("Investor")',
},
{ changeMode: 'tracked' },
);
expect(receipt.success).toBe(true);
// Accepted view: drop trackDelete marks, keep everything else.
const acceptedParts: string[] = [];
editor.state.doc.descendants((node: any) => {
if (!node.isText || !node.text) return;
const isDeleted = node.marks.some((mark: any) => mark.type.name === TrackDeleteMarkName);
if (!isDeleted) acceptedParts.push(node.text);
});
expect(acceptedParts.join('')).toBe('John James Smith of [insert address], [insert] ("Investor")');
// Specifically guard against the buggy strings reported in the ticket.
const accepted = acceptedParts.join('');
expect(accepted).not.toContain('JohnJames');
expect(accepted).not.toContain('Smith address');
});
// Customer-reported crash ("Empty text nodes are not allowed"): a non-empty
// replacement whose new text is fully contained in the old text's prefix +
// suffix trims to an EMPTY delta. The single-change branch must delete the
// removed text rather than build schema.text('') (which ProseMirror rejects).
it('rewrites a replacement that trims to an empty delta as a deletion (executor)', () => {
editor = makeEditor(['the Company refers to: the following terms']);
const { step, target } = compileSingleRewrite(editor, 'refers to:', 'to:');
const tr = editor.state.tr;
const outcome = executeTextRewrite(editor, tr, target as any, step as any, tr.mapping as any);
expect(outcome).toEqual({ changed: true });
expect(tr.doc.textContent).toBe('the Company to: the following terms');
});
it('handles a tracked replace that trims to an empty delta (deletion only)', () => {
editor = makeEditor(['We will use our best endeavours to: deliver']);
const receipt = editor.doc.replace(
{
ref: getFirstMatchRef(editor, 'best endeavours to:'),
text: 'endeavours to:',
},
{ changeMode: 'tracked' },
);
expect(receipt.success).toBe(true);
// Accepted view: drop trackDelete marks, keep everything else.
const acceptedParts: string[] = [];
editor.state.doc.descendants((node: any) => {
if (!node.isText || !node.text) return;
const isDeleted = node.marks.some((mark: any) => mark.type.name === TrackDeleteMarkName);
if (!isDeleted) acceptedParts.push(node.text);
});
expect(acceptedParts.join('')).toBe('We will use our endeavours to: deliver');
// The trimmed-away prefix "best " must be represented as a tracked deletion.
const deletedTexts: string[] = [];
editor.state.doc.descendants((node: any) => {
if (!node.isText || !node.text) return;
if (node.marks.some((mark: any) => mark.type.name === TrackDeleteMarkName)) {
deletedTexts.push(node.text);
}
});
expect(deletedTexts.join('')).toContain('best');
});
it('SD-3044: tracked rewrite of long block preserves spacing across multiple equal anchors', () => {
editor = makeEditor([
'[insert] Pty Limited a company incorporated in Australia having its registered office at [insert] (ACN [insert])("Company")',
]);
const target =
'Working Title Group Limited a company incorporated in New Zealand having its registered office at 29 Park Hill Road, Birkenhead, Auckland, 0626, NZ (NZBN 9429050880331)("Company")';
const receipt = editor.doc.replace(
{
ref: getFirstMatchRef(
editor,
'[insert] Pty Limited a company incorporated in Australia having its registered office at [insert] (ACN [insert])("Company")',
),
text: target,
},
{ changeMode: 'tracked' },
);
expect(receipt.success).toBe(true);
const acceptedParts: string[] = [];
editor.state.doc.descendants((node: any) => {
if (!node.isText || !node.text) return;
const isDeleted = node.marks.some((mark: any) => mark.type.name === TrackDeleteMarkName);
if (!isDeleted) acceptedParts.push(node.text);
});
const accepted = acceptedParts.join('');
expect(accepted).toBe(target);
expect(accepted).not.toContain('PtyTitle');
expect(accepted).not.toContain('AustraliaNew');
expect(accepted).not.toContain('(ACNPark');
});
});