text-document-editing 1.4.1

Undoable text editing use cases for text-document
Documentation
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//! Additional tests for the SplitTableCell use case.

extern crate text_document_editing as document_editing;
use anyhow::Result;

use document_editing::document_editing_controller;
use document_editing::{InsertTableDto, MergeTableCellsDto, SplitTableCellDto};

use test_harness::{get_document_stats, get_sorted_cells, setup_with_text, table_cell_controller};

// ═══════════════════════════════════════════════════════════════════
// SplitTableCell additional tests
// ═══════════════════════════════════════════════════════════════════

#[test]
fn test_split_cell_horizontal_only() -> Result<()> {
    let (db, hub, mut urm) = setup_with_text("")?;

    // Create table and merge 1x3 (one row, three columns)
    let insert_result = document_editing_controller::insert_table(
        &db,
        &hub,
        &mut urm,
        None,
        &InsertTableDto {
            position: 0,
            anchor: 0,
            rows: 2,
            columns: 3,
        },
    )?;
    let table_id = insert_result.table_id;

    // Merge row 0 columns 0-2
    let merge_result = document_editing_controller::merge_table_cells(
        &db,
        &hub,
        &mut urm,
        None,
        &MergeTableCellsDto {
            table_id,
            start_row: 0,
            start_column: 0,
            end_row: 0,
            end_column: 2,
        },
    )?;

    let merged = table_cell_controller::get(&db, &(merge_result.merged_cell_id as u64))?.unwrap();
    assert_eq!(merged.row_span, 1);
    assert_eq!(merged.column_span, 3);

    // Split horizontally into 3 columns
    let split_result = document_editing_controller::split_table_cell(
        &db,
        &hub,
        &mut urm,
        None,
        &SplitTableCellDto {
            cell_id: merge_result.merged_cell_id,
            split_rows: 1,
            split_columns: 3,
        },
    )?;

    assert_eq!(split_result.new_cell_ids.len(), 3);

    // All cells in row 0 should be 1x1 again
    let cells = get_sorted_cells(&db, &(table_id as u64))?;
    let row0: Vec<_> = cells.iter().filter(|c| c.row == 0).collect();
    assert_eq!(row0.len(), 3);
    for c in &row0 {
        assert_eq!(c.row_span, 1);
        assert_eq!(c.column_span, 1);
    }

    Ok(())
}

#[test]
fn test_split_cell_vertical_only() -> Result<()> {
    let (db, hub, mut urm) = setup_with_text("")?;

    // Create table and merge 3x1 (three rows, one column)
    let insert_result = document_editing_controller::insert_table(
        &db,
        &hub,
        &mut urm,
        None,
        &InsertTableDto {
            position: 0,
            anchor: 0,
            rows: 3,
            columns: 2,
        },
    )?;
    let table_id = insert_result.table_id;

    // Merge column 0, rows 0-2
    let merge_result = document_editing_controller::merge_table_cells(
        &db,
        &hub,
        &mut urm,
        None,
        &MergeTableCellsDto {
            table_id,
            start_row: 0,
            start_column: 0,
            end_row: 2,
            end_column: 0,
        },
    )?;

    let merged = table_cell_controller::get(&db, &(merge_result.merged_cell_id as u64))?.unwrap();
    assert_eq!(merged.row_span, 3);
    assert_eq!(merged.column_span, 1);

    // Split vertically into 3 rows
    let split_result = document_editing_controller::split_table_cell(
        &db,
        &hub,
        &mut urm,
        None,
        &SplitTableCellDto {
            cell_id: merge_result.merged_cell_id,
            split_rows: 3,
            split_columns: 1,
        },
    )?;

    assert_eq!(split_result.new_cell_ids.len(), 3);

    // All cells in column 0 should be back to 1x1
    let cells = get_sorted_cells(&db, &(table_id as u64))?;
    let col0: Vec<_> = cells.iter().filter(|c| c.column == 0).collect();
    assert_eq!(col0.len(), 3);
    for c in &col0 {
        assert_eq!(c.row_span, 1);
        assert_eq!(c.column_span, 1);
    }

    Ok(())
}

#[test]
fn test_split_cell_exceeding_span_fails() -> Result<()> {
    let (db, hub, mut urm) = setup_with_text("")?;

    let insert_result = document_editing_controller::insert_table(
        &db,
        &hub,
        &mut urm,
        None,
        &InsertTableDto {
            position: 0,
            anchor: 0,
            rows: 2,
            columns: 2,
        },
    )?;
    let table_id = insert_result.table_id;

    // Merge 2x2
    let merge_result = document_editing_controller::merge_table_cells(
        &db,
        &hub,
        &mut urm,
        None,
        &MergeTableCellsDto {
            table_id,
            start_row: 0,
            start_column: 0,
            end_row: 1,
            end_column: 1,
        },
    )?;

    // Try to split into 3 rows when span is only 2 — should fail
    let result = document_editing_controller::split_table_cell(
        &db,
        &hub,
        &mut urm,
        None,
        &SplitTableCellDto {
            cell_id: merge_result.merged_cell_id,
            split_rows: 3,
            split_columns: 1,
        },
    );
    assert!(result.is_err());

    // Try to split into 3 columns when span is only 2 — should fail
    let result = document_editing_controller::split_table_cell(
        &db,
        &hub,
        &mut urm,
        None,
        &SplitTableCellDto {
            cell_id: merge_result.merged_cell_id,
            split_rows: 1,
            split_columns: 3,
        },
    );
    assert!(result.is_err());

    Ok(())
}

#[test]
fn test_split_cell_updates_block_count() -> Result<()> {
    let (db, hub, mut urm) = setup_with_text("")?;

    let insert_result = document_editing_controller::insert_table(
        &db,
        &hub,
        &mut urm,
        None,
        &InsertTableDto {
            position: 0,
            anchor: 0,
            rows: 2,
            columns: 2,
        },
    )?;
    let table_id = insert_result.table_id;

    // Merge all 4 cells into 1
    let merge_result = document_editing_controller::merge_table_cells(
        &db,
        &hub,
        &mut urm,
        None,
        &MergeTableCellsDto {
            table_id,
            start_row: 0,
            start_column: 0,
            end_row: 1,
            end_column: 1,
        },
    )?;

    let stats_merged = get_document_stats(&db)?;
    // 1 original empty block + 1 merged cell block (the other 3 were removed)
    assert_eq!(stats_merged.block_count, 2);

    // Split back into 2x2
    document_editing_controller::split_table_cell(
        &db,
        &hub,
        &mut urm,
        None,
        &SplitTableCellDto {
            cell_id: merge_result.merged_cell_id,
            split_rows: 2,
            split_columns: 2,
        },
    )?;

    // Should have 1 original + 4 cell blocks = 5
    let stats_split = get_document_stats(&db)?;
    assert_eq!(stats_split.block_count, 5);

    Ok(())
}

// ═══════════════════════════════════════════════════════════════════
// Undo/redo tests
// ═══════════════════════════════════════════════════════════════════

#[test]
fn test_split_cell_undo_restores_merged_cell() -> Result<()> {
    let (db, hub, mut urm) = setup_with_text("")?;

    let insert_result = document_editing_controller::insert_table(
        &db,
        &hub,
        &mut urm,
        None,
        &InsertTableDto {
            position: 0,
            anchor: 0,
            rows: 2,
            columns: 2,
        },
    )?;
    let table_id = insert_result.table_id;

    // Merge all 4 cells into 1
    let merge_result = document_editing_controller::merge_table_cells(
        &db,
        &hub,
        &mut urm,
        None,
        &MergeTableCellsDto {
            table_id,
            start_row: 0,
            start_column: 0,
            end_row: 1,
            end_column: 1,
        },
    )?;

    let cells_after_merge = get_sorted_cells(&db, &(table_id as u64))?;
    assert_eq!(cells_after_merge.len(), 1);

    // Split into 2x2
    document_editing_controller::split_table_cell(
        &db,
        &hub,
        &mut urm,
        None,
        &SplitTableCellDto {
            cell_id: merge_result.merged_cell_id,
            split_rows: 2,
            split_columns: 2,
        },
    )?;

    let cells_after_split = get_sorted_cells(&db, &(table_id as u64))?;
    assert_eq!(cells_after_split.len(), 4);

    // Undo the split — should restore the merged cell
    urm.undo(None)?;
    let cells_after_undo = get_sorted_cells(&db, &(table_id as u64))?;
    assert_eq!(
        cells_after_undo.len(),
        1,
        "Undo should restore to 1 merged cell"
    );
    assert_eq!(cells_after_undo[0].row_span, 2);
    assert_eq!(cells_after_undo[0].column_span, 2);

    // Redo the split
    urm.redo(None)?;
    let cells_after_redo = get_sorted_cells(&db, &(table_id as u64))?;
    assert_eq!(
        cells_after_redo.len(),
        4,
        "Redo should re-split into 4 cells"
    );
    for c in &cells_after_redo {
        assert_eq!(c.row_span, 1);
        assert_eq!(c.column_span, 1);
    }

    Ok(())
}

#[test]
fn test_split_cell_2x2_grid() -> Result<()> {
    let (db, hub, mut urm) = setup_with_text("")?;

    // Create 3x3 table
    let insert_result = document_editing_controller::insert_table(
        &db,
        &hub,
        &mut urm,
        None,
        &InsertTableDto {
            position: 0,
            anchor: 0,
            rows: 3,
            columns: 3,
        },
    )?;
    let table_id = insert_result.table_id;

    // Merge a 2x2 region (rows 0-1, cols 0-1)
    let merge_result = document_editing_controller::merge_table_cells(
        &db,
        &hub,
        &mut urm,
        None,
        &MergeTableCellsDto {
            table_id,
            start_row: 0,
            start_column: 0,
            end_row: 1,
            end_column: 1,
        },
    )?;

    // 9 original - 3 absorbed = 6 cells
    let cells_after_merge = get_sorted_cells(&db, &(table_id as u64))?;
    assert_eq!(cells_after_merge.len(), 6);

    // Split back into 2x2
    document_editing_controller::split_table_cell(
        &db,
        &hub,
        &mut urm,
        None,
        &SplitTableCellDto {
            cell_id: merge_result.merged_cell_id,
            split_rows: 2,
            split_columns: 2,
        },
    )?;

    // Should be back to 9 cells
    let cells_after_split = get_sorted_cells(&db, &(table_id as u64))?;
    assert_eq!(cells_after_split.len(), 9);

    // Verify all cells are 1x1
    for c in &cells_after_split {
        assert_eq!(
            c.row_span, 1,
            "Cell at ({},{}) should have row_span 1",
            c.row, c.column
        );
        assert_eq!(
            c.column_span, 1,
            "Cell at ({},{}) should have column_span 1",
            c.row, c.column
        );
    }

    Ok(())
}

#[test]
fn test_split_1x1_cell_is_noop_or_error() -> Result<()> {
    let (db, hub, mut urm) = setup_with_text("")?;

    let insert_result = document_editing_controller::insert_table(
        &db,
        &hub,
        &mut urm,
        None,
        &InsertTableDto {
            position: 0,
            anchor: 0,
            rows: 2,
            columns: 2,
        },
    )?;
    let table_id = insert_result.table_id;

    // Get the first cell (already 1x1)
    let cells = get_sorted_cells(&db, &(table_id as u64))?;
    let cell = &cells[0];
    assert_eq!(cell.row_span, 1);
    assert_eq!(cell.column_span, 1);

    // Splitting a 1x1 cell into 1x1 should either be a no-op or error
    let result = document_editing_controller::split_table_cell(
        &db,
        &hub,
        &mut urm,
        None,
        &SplitTableCellDto {
            cell_id: cell.id as i64,
            split_rows: 1,
            split_columns: 1,
        },
    );

    // Either it errors or produces 1 cell (the same one)
    if let Ok(split_result) = result {
        assert_eq!(
            split_result.new_cell_ids.len(),
            1,
            "Splitting 1x1 into 1x1 should produce 1 cell"
        );
    }
    // If it errors, that's also acceptable behavior

    // Table should still have 4 cells
    let cells_after = get_sorted_cells(&db, &(table_id as u64))?;
    assert_eq!(cells_after.len(), 4);

    Ok(())
}

#[test]
fn test_split_cell_invalid_cell_id_errors() -> Result<()> {
    let (db, hub, mut urm) = setup_with_text("")?;

    // Try to split a non-existent cell
    let result = document_editing_controller::split_table_cell(
        &db,
        &hub,
        &mut urm,
        None,
        &SplitTableCellDto {
            cell_id: 999999,
            split_rows: 2,
            split_columns: 2,
        },
    );

    assert!(result.is_err(), "Splitting non-existent cell should fail");

    Ok(())
}