|
| 1 | +import test, { expect } from '@playwright/test'; |
| 2 | +import { Dictionaries } from '../fixtures/Dictionaries.js'; |
| 3 | +import { Parcels } from '../fixtures/Parcels.js'; |
| 4 | +import { Workspace } from '../fixtures/Workspace.js'; |
| 5 | +import { WorkspaceSaveConflict } from '../fixtures/WorkspaceSaveConflict.js'; |
| 6 | +import { Workspaces } from '../fixtures/Workspaces.js'; |
| 7 | +import { setupTest, teardownTest, type BrowserSetupResult } from '../utilities/api.js'; |
| 8 | +import { generateRandomName } from '../utilities/helpers.js'; |
| 9 | + |
| 10 | +// Simultaneous-edit protection: a save is rejected (412) when the file changed underneath the |
| 11 | +// editor since it was opened. We simulate the "other editor" with a second browser context for |
| 12 | +// the same 'test' user editing the same workspace file. |
| 13 | + |
| 14 | +let setup: BrowserSetupResult; // primary editor — the one under test |
| 15 | +let setupOther: BrowserSetupResult; // concurrent editor — changes the file first |
| 16 | + |
| 17 | +let dictionaries: Dictionaries; |
| 18 | +let parcels: Parcels; |
| 19 | +let workspaces: Workspaces; |
| 20 | +let workspace: Workspace; // primary page |
| 21 | +let otherWorkspace: Workspace; // concurrent page (same workspace) |
| 22 | +let conflict: WorkspaceSaveConflict; |
| 23 | +let workspaceId: string; |
| 24 | +let workspaceName: string; |
| 25 | + |
| 26 | +test.beforeAll(async ({ baseURL, browser }) => { |
| 27 | + test.setTimeout(120000); |
| 28 | + |
| 29 | + setup = await setupTest(browser, { model: false }); |
| 30 | + dictionaries = new Dictionaries(setup.page); |
| 31 | + parcels = new Parcels(setup.page); |
| 32 | + workspaces = new Workspaces(setup.page, parcels, baseURL); |
| 33 | + |
| 34 | + await dictionaries.goto(); |
| 35 | + await dictionaries.createCommandDictionary(); |
| 36 | + await parcels.goto(); |
| 37 | + await parcels.createParcel(dictionaries.commandDictionaryName, baseURL); |
| 38 | + |
| 39 | + await workspaces.goto(); |
| 40 | + workspaceId = await workspaces.createWorkspace(); |
| 41 | + workspaceName = workspaces.workspaceName; |
| 42 | + workspace = new Workspace(setup.page, workspaceId, workspaceName, baseURL); |
| 43 | + conflict = new WorkspaceSaveConflict(setup.page); |
| 44 | + |
| 45 | + // Second context (same 'test' user) editing the same workspace concurrently. |
| 46 | + setupOther = await setupTest(browser, { model: false }); |
| 47 | + otherWorkspace = new Workspace(setupOther.page, workspaceId, workspaceName, baseURL); |
| 48 | + |
| 49 | + await workspace.goto(); |
| 50 | +}); |
| 51 | + |
| 52 | +test.afterAll(async () => { |
| 53 | + await workspaces.goto(); |
| 54 | + await workspaces.deleteWorkspace(workspaceName); |
| 55 | + await parcels.goto(); |
| 56 | + await parcels.deleteParcel(); |
| 57 | + await dictionaries.goto(); |
| 58 | + await dictionaries.deleteCommandDictionary(); |
| 59 | + |
| 60 | + await teardownTest(setup); |
| 61 | + await teardownTest(setupOther); |
| 62 | +}); |
| 63 | + |
| 64 | +/** Create a sequence file on the primary page and open it (capturing its base version). */ |
| 65 | +async function createAndOpenSequence(): Promise<string> { |
| 66 | + const { sequenceName } = await workspace.createSequence(undefined, `${generateRandomName()}.seq`); |
| 67 | + await workspace.searchForFileAndWait(sequenceName); |
| 68 | + await workspace.clickFile(sequenceName); |
| 69 | + await expect(workspace.saveSequenceButton).toBeVisible({ timeout: 10000 }); |
| 70 | + return sequenceName; |
| 71 | +} |
| 72 | + |
| 73 | +/** The concurrent editor opens the same file and saves a change, advancing the server version. */ |
| 74 | +async function otherEditorSaves(sequenceName: string, content: string): Promise<void> { |
| 75 | + await otherWorkspace.goto(); |
| 76 | + await otherWorkspace.searchForFileAndWait(sequenceName); |
| 77 | + await otherWorkspace.clickFile(sequenceName); |
| 78 | + await expect(otherWorkspace.saveSequenceButton).toBeVisible({ timeout: 10000 }); |
| 79 | + await otherWorkspace.fillSequenceContent(content); |
| 80 | + await otherWorkspace.saveSequence(); |
| 81 | +} |
| 82 | + |
| 83 | +/** Edit on the primary page and click Save, expecting the concurrency check to reject it. */ |
| 84 | +async function editAndSaveExpectingConflict(content: string): Promise<void> { |
| 85 | + await workspace.fillSequenceContent(content); |
| 86 | + await expect(workspace.saveSequenceButton).toBeEnabled(); |
| 87 | + await workspace.saveSequenceButton.click(); |
| 88 | +} |
| 89 | + |
| 90 | +test.describe.serial('Workspace simultaneous-edit protection', () => { |
| 91 | + test('consecutive saves of the same file succeed (token round-trips on PUT)', async () => { |
| 92 | + // No concurrent editor here: this confirms a save advances the stored token so the next |
| 93 | + // save against the same editor does not spuriously conflict. |
| 94 | + await createAndOpenSequence(); |
| 95 | + |
| 96 | + await workspace.fillSequenceContent('// first change'); |
| 97 | + await workspace.saveSequence(); |
| 98 | + await expect(workspace.saveSequenceButton).toBeDisabled(); |
| 99 | + |
| 100 | + await workspace.fillSequenceContent('// second change'); |
| 101 | + await workspace.saveSequence(); |
| 102 | + await expect(workspace.saveSequenceButton).toBeDisabled(); |
| 103 | + }); |
| 104 | + |
| 105 | + test('a stale save shows the conflict modal with a diff', async () => { |
| 106 | + const sequenceName = await createAndOpenSequence(); |
| 107 | + await otherEditorSaves(sequenceName, '// their change'); |
| 108 | + |
| 109 | + await editAndSaveExpectingConflict('// my change'); |
| 110 | + |
| 111 | + await conflict.waitForConflict(); |
| 112 | + |
| 113 | + // Cancel leaves the document dirty (Save still enabled) and keeps the stale token. |
| 114 | + await conflict.cancel(); |
| 115 | + await expect(workspace.saveSequenceButton).toBeEnabled(); |
| 116 | + |
| 117 | + // Resolve the still-dirty document so the next test starts from a clean editor: |
| 118 | + // saving again re-checks the (still stale) token, then take theirs to rebase clean. |
| 119 | + await workspace.saveSequenceButton.click(); |
| 120 | + await conflict.waitForConflict(); |
| 121 | + await conflict.takeTheirs(); |
| 122 | + await expect(workspace.saveSequenceButton).toBeDisabled(); |
| 123 | + }); |
| 124 | + |
| 125 | + test('"Keep theirs" rebases the editor and the next save succeeds', async () => { |
| 126 | + const sequenceName = await createAndOpenSequence(); |
| 127 | + await otherEditorSaves(sequenceName, '// their change'); |
| 128 | + |
| 129 | + await editAndSaveExpectingConflict('// my change'); |
| 130 | + await conflict.waitForConflict(); |
| 131 | + await conflict.takeTheirs(); |
| 132 | + |
| 133 | + // Editor now reflects the server version; saving a fresh edit succeeds (token advanced, |
| 134 | + // no immediate re-conflict). |
| 135 | + await workspace.fillSequenceContent('// after taking theirs'); |
| 136 | + await workspace.saveSequence(); |
| 137 | + await expect(workspace.saveSequenceButton).toBeDisabled(); |
| 138 | + }); |
| 139 | + |
| 140 | + test('"Keep theirs" is undoable — Ctrl/Cmd+Z restores the discarded edits', async () => { |
| 141 | + const sequenceName = await createAndOpenSequence(); |
| 142 | + await otherEditorSaves(sequenceName, '// their change'); |
| 143 | + |
| 144 | + await editAndSaveExpectingConflict('// my change'); |
| 145 | + await conflict.waitForConflict(); |
| 146 | + await conflict.takeTheirs(); |
| 147 | + |
| 148 | + // Right after taking theirs the editor shows the server version. |
| 149 | + const editor = setup.page.locator('.cm-content').first(); |
| 150 | + await expect(editor).toContainText('// their change'); |
| 151 | + |
| 152 | + // The rebase is recorded in the undo history, so a single undo restores the discarded edits |
| 153 | + // and the document goes dirty again (restored content differs from the new server baseline). |
| 154 | + await editor.click(); |
| 155 | + await setup.page.keyboard.press('ControlOrMeta+z'); |
| 156 | + await expect(editor).toContainText('// my change'); |
| 157 | + await expect(workspace.saveSequenceButton).toBeEnabled(); |
| 158 | + |
| 159 | + // Leave a clean editor for the next serial test. "Keep theirs" already advanced the stored |
| 160 | + // token to the server version, so this save succeeds without re-conflicting. Without it the |
| 161 | + // dirty doc leaks: the next test's createAndOpenSequence auto-selects its new file, which pops |
| 162 | + // a "Navigate Away" modal whose backdrop blocks the file-row click. |
| 163 | + await workspace.saveSequence(); |
| 164 | + await expect(workspace.saveSequenceButton).toBeDisabled(); |
| 165 | + }); |
| 166 | + |
| 167 | + test('"Keep mine" saves the local version and the next save succeeds', async () => { |
| 168 | + const sequenceName = await createAndOpenSequence(); |
| 169 | + await otherEditorSaves(sequenceName, '// their change'); |
| 170 | + |
| 171 | + await editAndSaveExpectingConflict('// my change'); |
| 172 | + await conflict.waitForConflict(); |
| 173 | + await conflict.takeMine(); |
| 174 | + |
| 175 | + // Force-overwrite saved successfully and advanced the token. |
| 176 | + await workspace.waitForToast('Workspace File Saved Successfully'); |
| 177 | + await expect(workspace.saveSequenceButton).toBeDisabled(); |
| 178 | + |
| 179 | + await workspace.fillSequenceContent('// after taking mine'); |
| 180 | + await workspace.saveSequence(); |
| 181 | + await expect(workspace.saveSequenceButton).toBeDisabled(); |
| 182 | + }); |
| 183 | + |
| 184 | + test('Ctrl/Cmd+S is ignored while the conflict modal is open (no stacked save)', async () => { |
| 185 | + const sequenceName = await createAndOpenSequence(); |
| 186 | + await otherEditorSaves(sequenceName, '// their change'); |
| 187 | + |
| 188 | + await editAndSaveExpectingConflict('// my change'); |
| 189 | + await conflict.waitForConflict(); |
| 190 | + |
| 191 | + // The global save shortcut must not fire another save / stack a second modal. |
| 192 | + await setup.page.keyboard.press('ControlOrMeta+s'); |
| 193 | + await expect(conflict.takeMineButton).toHaveCount(1); |
| 194 | + await expect(conflict.conflictTitle).toBeVisible(); |
| 195 | + |
| 196 | + await conflict.takeTheirs(); |
| 197 | + }); |
| 198 | + |
| 199 | + test('a file deleted underneath shows the deleted/moved variant — Recreate restores it', async () => { |
| 200 | + const sequenceName = await createAndOpenSequence(); |
| 201 | + |
| 202 | + // Concurrent editor deletes the file out from under the open editor. |
| 203 | + await otherWorkspace.goto(); |
| 204 | + await otherWorkspace.searchForFileAndWait(sequenceName); |
| 205 | + await otherWorkspace.deleteFile(sequenceName); |
| 206 | + |
| 207 | + await editAndSaveExpectingConflict('// my change after delete'); |
| 208 | + await conflict.waitForDeleted(); |
| 209 | + await conflict.recreate(); |
| 210 | + |
| 211 | + await workspace.waitForToast('Workspace File Saved Successfully'); |
| 212 | + // The recreated file is back in the tree. |
| 213 | + await workspace.searchForFileAndWait(sequenceName); |
| 214 | + await expect(workspace.getFileRow(sequenceName)).toBeVisible(); |
| 215 | + }); |
| 216 | + |
| 217 | + test('the deleted/moved variant — "Discard & close" closes the document', async () => { |
| 218 | + const sequenceName = await createAndOpenSequence(); |
| 219 | + |
| 220 | + await otherWorkspace.goto(); |
| 221 | + await otherWorkspace.searchForFileAndWait(sequenceName); |
| 222 | + await otherWorkspace.deleteFile(sequenceName); |
| 223 | + |
| 224 | + await editAndSaveExpectingConflict('// my change after delete'); |
| 225 | + await conflict.waitForDeleted(); |
| 226 | + await conflict.discardAndClose(); |
| 227 | + |
| 228 | + // The unsaved edits are discarded — the editor is no longer dirty (Save disabled) and |
| 229 | + // the discarded content is gone. |
| 230 | + await expect(workspace.saveSequenceButton).toBeDisabled(); |
| 231 | + await expect(setup.page.locator('.cm-content').first()).not.toContainText('my change after delete'); |
| 232 | + }); |
| 233 | +}); |
0 commit comments