import { createEditorState, createEditorStateWithSelection, doc, p, table, td, th, tr, } from "@shared/test/editor"; import { Plugin } from "prosemirror-state"; import { TableMap } from "prosemirror-tables"; import { multiplayerPluginKey, type MultiplayerOperations, } from "../lib/multiplayer"; import { FixTablesPlugin } from "./FixTablesPlugin"; describe("FixTablesPlugin", () => { describe("header cell fixing", () => { it("should convert th cells that are not in first row or first column to td", () => { // Create a 3x3 table with th cells in all positions (incorrect state) // The plugin should only convert cells where both row > 0 AND col > 0 const testTable = table([ tr([ th("H1"), // (0,0) - keep th th("H2"), // (0,1) - keep th (first row) th("H3"), // (0,2) - keep th (first row) ]), tr([ th("R1"), // (1,0) - keep th (first column) th("Wrong"), // (1,1) - convert to td th("Wrong"), // (1,2) - convert to td ]), tr([ th("R2"), // (2,0) - keep th (first column) th("Wrong"), // (2,1) - convert to td th("Wrong"), // (2,2) - convert to td ]), ]); const testDoc = doc(testTable); const state = createEditorState(testDoc, [new FixTablesPlugin()]); // Simulate a transaction that triggers the plugin const newState = state.apply(state.tr.insertText("x", 5)); const newTable = newState.doc.firstChild!; // First row should all be th (header row) const firstRow = newTable.child(0); expect(firstRow.child(0).type.name).toBe("th"); expect(firstRow.child(1).type.name).toBe("th"); expect(firstRow.child(2).type.name).toBe("th"); // Second row: first column th preserved, others converted to td const secondRow = newTable.child(1); expect(secondRow.child(0).type.name).toBe("th"); expect(secondRow.child(1).type.name).toBe("td"); expect(secondRow.child(2).type.name).toBe("td"); // Third row: first column th preserved, others converted to td const thirdRow = newTable.child(2); expect(thirdRow.child(0).type.name).toBe("th"); expect(thirdRow.child(1).type.name).toBe("td"); expect(thirdRow.child(2).type.name).toBe("td"); }); it("should not modify valid tables with th only in first row", () => { // Create a correctly structured 2x2 table const testTable = table([ tr([th("Header 1"), th("Header 2")]), tr([td("Cell 1"), td("Cell 2")]), ]); const testDoc = doc(testTable); const state = createEditorState(testDoc, [new FixTablesPlugin()]); // Simulate a transaction const newState = state.apply(state.tr.insertText("x", 5)); // Check structure is unchanged const newTable = newState.doc.firstChild!; const firstRow = newTable.child(0); const secondRow = newTable.child(1); expect(firstRow.child(0).type.name).toBe("th"); expect(firstRow.child(1).type.name).toBe("th"); expect(secondRow.child(0).type.name).toBe("td"); expect(secondRow.child(1).type.name).toBe("td"); }); it("should preserve th in first column when used as row headers", () => { // Create a table with th in first column (row headers) - this is valid const testTable = table([ tr([th("Header 1"), th("Header 2")]), tr([th("Row Header"), td("Cell")]), ]); const testDoc = doc(testTable); const state = createEditorState(testDoc, [new FixTablesPlugin()]); // Simulate a transaction const newState = state.apply(state.tr.insertText("x", 5)); // Check that first column th is preserved const newTable = newState.doc.firstChild!; const secondRow = newTable.child(1); expect(secondRow.child(0).type.name).toBe("th"); expect(secondRow.child(1).type.name).toBe("td"); }); it("should handle 2x2 table with all th - only bottom-right cell converted", () => { // Create a 2x2 table with th cells in all positions // Only the cell at (1,1) should be converted since it's not in first row or first column const testTable = table([ tr([ th("H1"), // (0,0) - keep th("H2"), // (0,1) - keep (first row) ]), tr([ th("R1"), // (1,0) - keep (first column) th("Wrong"), // (1,1) - convert to td ]), ]); const testDoc = doc(testTable); const state = createEditorState(testDoc, [new FixTablesPlugin()]); // Simulate a transaction const newState = state.apply(state.tr.insertText("x", 5)); const newTable = newState.doc.firstChild!; // First row should still be all th (header row) const firstRow = newTable.child(0); expect(firstRow.child(0).type.name).toBe("th"); expect(firstRow.child(1).type.name).toBe("th"); // Second row: first cell th (first column), second cell td const secondRow = newTable.child(1); expect(secondRow.child(0).type.name).toBe("th"); expect(secondRow.child(1).type.name).toBe("td"); }); }); describe("colwidth fixing for single-column tables", () => { it("should remove colwidth from single-column table cells when selection is in table", () => { // Create a single-column table with colwidth set (incorrect state) const testTable = table([ tr([th("Header", { colwidth: [200] })]), tr([td("Cell 1", { colwidth: [200] })]), tr([td("Cell 2", { colwidth: [200] })]), ]); const testDoc = doc(testTable); // Create state with selection inside the table // Position 5 is inside the first cell's paragraph const state = createEditorStateWithSelection(testDoc, 5, [ new FixTablesPlugin(), ]); // Simulate a transaction that modifies the document const newState = state.apply(state.tr.insertText("x", 5)); // Check that colwidth is removed from all cells const newTable = newState.doc.firstChild!; newTable.forEach((row) => { row.forEach((cell) => { expect(cell.attrs.colwidth).toBeNull(); }); }); }); it("should not remove colwidth from multi-column tables", () => { // Create a two-column table with colwidth set (valid state) const testTable = table([ tr([ th("Header 1", { colwidth: [150] }), th("Header 2", { colwidth: [250] }), ]), tr([ td("Cell 1", { colwidth: [150] }), td("Cell 2", { colwidth: [250] }), ]), ]); const testDoc = doc(testTable); const state = createEditorStateWithSelection(testDoc, 5, [ new FixTablesPlugin(), ]); // Simulate a transaction const newState = state.apply(state.tr.insertText("x", 5)); // Check that colwidth is preserved const newTable = newState.doc.firstChild!; const firstRow = newTable.child(0); const secondRow = newTable.child(1); expect(firstRow.child(0).attrs.colwidth).toEqual([150]); expect(firstRow.child(1).attrs.colwidth).toEqual([250]); expect(secondRow.child(0).attrs.colwidth).toEqual([150]); expect(secondRow.child(1).attrs.colwidth).toEqual([250]); }); it("should not modify single-column tables without colwidth", () => { // Create a single-column table without colwidth (already correct) const testTable = table([tr([th("Header")]), tr([td("Cell")])]); const testDoc = doc(testTable); const state = createEditorStateWithSelection(testDoc, 5, [ new FixTablesPlugin(), ]); // Simulate a transaction const newState = state.apply(state.tr.insertText("x", 5)); // Check that structure is unchanged const newTable = newState.doc.firstChild!; expect(newTable.child(0).child(0).attrs.colwidth).toBeNull(); expect(newTable.child(1).child(0).attrs.colwidth).toBeNull(); }); }); describe("combined fixes", () => { it("should fix header cells regardless of selection position", () => { // Create a 2x2 table with th in wrong position const testTable = table([ tr([th("H1"), th("H2")]), tr([th("R1"), th("Wrong")]), // Should become td ]); const testDoc = doc(testTable); const state = createEditorState(testDoc, [new FixTablesPlugin()]); // Simulate a transaction const newState = state.apply(state.tr.insertText("x", 5)); const newTable = newState.doc.firstChild!; // First row: all th preserved expect(newTable.child(0).child(0).type.name).toBe("th"); expect(newTable.child(0).child(1).type.name).toBe("th"); // Second row: first column th preserved, second converted expect(newTable.child(1).child(0).type.name).toBe("th"); expect(newTable.child(1).child(1).type.name).toBe("td"); }); }); describe("cell span fixing", () => { it("should reset invalid spans so every cell is in the table map", () => { const testDoc = doc([ table([tr([td("A", { rowspan: 0 }), td("B", { colspan: 0 })])]), table([tr([td("C"), td("D")])]), ]); const state = createEditorState(testDoc, [new FixTablesPlugin()]); // Edit both tables so the clean table is checked after the broken one. const { state: newState } = state.applyTransaction( state.tr.insertText("y", 18).insertText("x", 4) ); const newTable = newState.doc.firstChild!; const map = TableMap.get(newTable); newTable.firstChild!.forEach((cell, offset) => { expect(cell.attrs.colspan).toBe(1); expect(cell.attrs.rowspan).toBe(1); expect(() => map.findCell(1 + offset)).not.toThrow(); }); }); it("should repair tables loaded in a bad state on the first edit", () => { const testDoc = doc([table([tr([td("A", { rowspan: 0 })])]), p("Text")]); const state = createEditorState(testDoc, [new FixTablesPlugin()]); // Edit outside of the table const newState = state.apply( state.tr.insertText("x", state.doc.firstChild!.nodeSize + 1) ); expect( newState.doc.firstChild!.firstChild!.firstChild!.attrs.rowspan ).toBe(1); }); }); describe("edge cases", () => { it("should handle empty tables gracefully", () => { // Create minimal valid table const testTable = table([tr([td("")])]); const testDoc = doc(testTable); const state = createEditorState(testDoc, [new FixTablesPlugin()]); // Should not throw expect(() => { state.apply(state.tr.insertText("x", 4)); }).not.toThrow(); }); it("should fix unchanged tables on the first edit", () => { // Create two tables, each with th in wrong positions const table1 = table([ tr([th("H1"), th("H2")]), tr([th("R1"), th("Wrong")]), // Should be td ]); const table2 = table([ tr([th("H1"), th("H2"), th("H3")]), tr([th("R1"), th("Wrong"), th("Wrong")]), // Should be td ]); const testDoc = doc([table1, table2]); const state = createEditorState(testDoc, [new FixTablesPlugin()]); // Insert text only in the first table - second table is unchanged const newState = state.apply(state.tr.insertText("x", 5)); const newTable1 = newState.doc.child(0); expect(newTable1.child(0).child(1).type.name).toBe("th"); expect(newTable1.child(1).child(0).type.name).toBe("th"); expect(newTable1.child(1).child(1).type.name).toBe("td"); // Tables loaded in a bad state are fixed even when they did not change const newTable2 = newState.doc.child(1); expect(newTable2.child(0).child(2).type.name).toBe("th"); expect(newTable2.child(1).child(0).type.name).toBe("th"); expect(newTable2.child(1).child(1).type.name).toBe("td"); expect(newTable2.child(1).child(2).type.name).toBe("td"); }); it("should not modify document when no fixes needed", () => { // Create a perfectly valid table const testTable = table([ tr([th("H1"), th("H2")]), tr([th("R1"), td("Cell")]), ]); const testDoc = doc(testTable); const state = createEditorState(testDoc, [new FixTablesPlugin()]); // Apply transaction const newState = state.apply(state.tr.insertText("x", 5)); // The table structure should remain valid const newTable = newState.doc.firstChild!; expect(newTable.child(0).child(0).type.name).toBe("th"); expect(newTable.child(0).child(1).type.name).toBe("th"); expect(newTable.child(1).child(0).type.name).toBe("th"); expect(newTable.child(1).child(1).type.name).toBe("td"); }); }); describe("collaboration", () => { const multiplayer: MultiplayerOperations = { isRemoteTransaction: (transaction) => !!transaction.getMeta("remote"), stopCapturing: () => {}, }; const facade = new Plugin({ key: multiplayerPluginKey, multiplayer }); const brokenTable = () => doc(table([tr([th("H1"), th("H2")]), tr([th("R1"), th("Wrong")])])); it("should not repair tables in response to remote changes", () => { const state = createEditorState(brokenTable(), [ facade, new FixTablesPlugin(), ]); const newState = state.apply( state.tr.insertText("x", 5).setMeta("remote", true) ); expect(newState.doc.firstChild!.child(1).child(1).type.name).toBe("th"); }); it("should repair tables on the first local change after a remote change", () => { const state = createEditorState(brokenTable(), [ facade, new FixTablesPlugin(), ]); const remoteState = state.apply( state.tr.insertText("x", 5).setMeta("remote", true) ); const newState = remoteState.apply(remoteState.tr.insertText("y", 5)); expect(newState.doc.firstChild!.child(1).child(1).type.name).toBe("td"); }); it("should repair tables received after an earlier local change", () => { const state = createEditorState(doc(p("Intro")), [ facade, new FixTablesPlugin(), ]); const localState = state.apply(state.tr.insertText("x", 1)); const remoteState = localState.apply( localState.tr .insert(localState.doc.content.size, brokenTable().content) .setMeta("remote", true) ); const newState = remoteState.apply(remoteState.tr.insertText("y", 1)); expect(newState.doc.child(1).child(1).child(1).type.name).toBe("td"); }); }); });