|
| 1 | +/* eslint-disable testing-library/render-result-naming-convention */ |
| 2 | +/** |
| 3 | + * Vitest browser-mode tests for two-user concurrent type-change |
| 4 | + * suggestions. Same shape as `propChanges.concurrent.test.tsx`. |
| 5 | + * |
| 6 | + * KNOWN BUG: see `typeChanges.test.tsx` – block-type changes in |
| 7 | + * suggestion mode currently throw in y-prosemirror's `deltaToPSteps`. |
| 8 | + * Both tests below are marked `test.fails`; when the upstream bug is |
| 9 | + * fixed they will flip red and we can capture proper snapshots. |
| 10 | + */ |
| 11 | +import { expect, test } from "vitest"; |
| 12 | + |
| 13 | +import { setupConcurrentSuggestionTest } from "./fixtures/concurrentSuggestionFixture.js"; |
| 14 | +import { |
| 15 | + editorHtml, |
| 16 | + ydocXml, |
| 17 | +} from "./fixtures/suggestionFixture.js"; |
| 18 | + |
| 19 | +// Two competing type changes on the same block: A wants a heading, B |
| 20 | +// wants a list item. |
| 21 | +test.fails( |
| 22 | + "concurrent: A → heading, B → list item", |
| 23 | + async () => { |
| 24 | + const { |
| 25 | + userA, |
| 26 | + userB, |
| 27 | + merged, |
| 28 | + baseDoc, |
| 29 | + suggestionDocA, |
| 30 | + suggestionDocB, |
| 31 | + suggestionDocMerged, |
| 32 | + screen, |
| 33 | + seed, |
| 34 | + enableSuggestions, |
| 35 | + sync, |
| 36 | + } = await setupConcurrentSuggestionTest({ |
| 37 | + userAAction: "→ heading", |
| 38 | + userBAction: "→ list item", |
| 39 | + }); |
| 40 | + |
| 41 | + userA.editor.replaceBlocks(userA.editor.document, [ |
| 42 | + { id: "block-hello", type: "paragraph", content: "hello world" }, |
| 43 | + ]); |
| 44 | + seed(); |
| 45 | + await expect |
| 46 | + .element(screen.getByTestId(userA.testId).getByText("hello world")) |
| 47 | + .toBeVisible(); |
| 48 | + |
| 49 | + enableSuggestions(); |
| 50 | + |
| 51 | + const [blockA] = userA.editor.document; |
| 52 | + userA.editor.updateBlock(blockA, { |
| 53 | + type: "heading", |
| 54 | + props: { level: 1 }, |
| 55 | + }); |
| 56 | + |
| 57 | + const [blockB] = userB.editor.document; |
| 58 | + userB.editor.updateBlock(blockB, { type: "bulletListItem" }); |
| 59 | + |
| 60 | + await expect.poll(() => userA.editor.document[0]?.type).toBe("heading"); |
| 61 | + await expect |
| 62 | + .poll(() => userB.editor.document[0]?.type) |
| 63 | + .toBe("bulletListItem"); |
| 64 | + |
| 65 | + sync(); |
| 66 | + |
| 67 | + await expect(screen.getByTestId("editor-root")).toMatchScreenshot( |
| 68 | + "concurrent-heading-vs-list", |
| 69 | + ); |
| 70 | + |
| 71 | + expect(ydocXml(baseDoc)).toMatchInlineSnapshot(); |
| 72 | + expect(ydocXml(suggestionDocA)).toMatchInlineSnapshot(); |
| 73 | + expect(ydocXml(suggestionDocB)).toMatchInlineSnapshot(); |
| 74 | + expect(ydocXml(suggestionDocMerged)).toMatchInlineSnapshot(); |
| 75 | + expect(editorHtml(merged.editor)).toMatchInlineSnapshot(); |
| 76 | + }, |
| 77 | +); |
| 78 | + |
| 79 | +// Mixed: A does a text edit (no type change), B changes the type. |
| 80 | +// Exercises the path where one user's suggestion is a regular text |
| 81 | +// diff and the other's is a block-type swap. |
| 82 | +test.fails( |
| 83 | + "concurrent: A edits text, B → heading", |
| 84 | + async () => { |
| 85 | + const { |
| 86 | + userA, |
| 87 | + userB, |
| 88 | + merged, |
| 89 | + baseDoc, |
| 90 | + suggestionDocA, |
| 91 | + suggestionDocB, |
| 92 | + suggestionDocMerged, |
| 93 | + screen, |
| 94 | + seed, |
| 95 | + enableSuggestions, |
| 96 | + sync, |
| 97 | + } = await setupConcurrentSuggestionTest({ |
| 98 | + userAAction: "world → universe", |
| 99 | + userBAction: "→ heading", |
| 100 | + }); |
| 101 | + |
| 102 | + userA.editor.replaceBlocks(userA.editor.document, [ |
| 103 | + { id: "block-hello", type: "paragraph", content: "hello world" }, |
| 104 | + ]); |
| 105 | + seed(); |
| 106 | + await expect |
| 107 | + .element(screen.getByTestId(userA.testId).getByText("hello world")) |
| 108 | + .toBeVisible(); |
| 109 | + |
| 110 | + enableSuggestions(); |
| 111 | + |
| 112 | + const [blockA] = userA.editor.document; |
| 113 | + userA.editor.updateBlock(blockA, { |
| 114 | + type: "paragraph", |
| 115 | + content: "hello universe", |
| 116 | + }); |
| 117 | + |
| 118 | + const [blockB] = userB.editor.document; |
| 119 | + userB.editor.updateBlock(blockB, { |
| 120 | + type: "heading", |
| 121 | + props: { level: 1 }, |
| 122 | + }); |
| 123 | + |
| 124 | + await expect |
| 125 | + .poll(() => |
| 126 | + userA.editor.prosemirrorState.doc.toString().includes("y-attributed"), |
| 127 | + ) |
| 128 | + .toBe(true); |
| 129 | + await expect.poll(() => userB.editor.document[0]?.type).toBe("heading"); |
| 130 | + |
| 131 | + sync(); |
| 132 | + |
| 133 | + await expect(screen.getByTestId("editor-root")).toMatchScreenshot( |
| 134 | + "concurrent-text-edit-vs-heading", |
| 135 | + ); |
| 136 | + |
| 137 | + expect(ydocXml(baseDoc)).toMatchInlineSnapshot(); |
| 138 | + expect(ydocXml(suggestionDocA)).toMatchInlineSnapshot(); |
| 139 | + expect(ydocXml(suggestionDocB)).toMatchInlineSnapshot(); |
| 140 | + expect(ydocXml(suggestionDocMerged)).toMatchInlineSnapshot(); |
| 141 | + expect(editorHtml(merged.editor)).toMatchInlineSnapshot(); |
| 142 | + }, |
| 143 | +); |
0 commit comments