| 1 | import assert from 'node:assert/strict'; |
| 2 | |
| 3 | export async function checkCellQueue(page) { |
| 4 | const isBatch = request => request.method() === 'POST' && new URL(request.url()).pathname.endsWith('/cells/batch'); |
| 5 | const requests = []; |
| 6 | const record = request => { if (isBatch(request)) requests.push(request); }; |
| 7 | const text = Array.from({length: 25}, (_, r) => Array.from({length: 20}, (_, c) => String(r * 20 + c + 1)).join('\t')).join('\n'); |
| 8 | await page.bringToFront(); |
| 9 | await page.evaluate(async text => navigator.clipboard.writeText(text), text); |
| 10 | await page.locator('[data-sheet-grid] tbody tr:first-child td:nth-child(2)').click(); |
| 11 | await page.evaluate(() => { window.SheetsGrid.worksheet().updateSelectionFromCoords(0, 0, 0, 0); window.SheetsGrid.focus(); }); |
| 12 | page.on('request', record); |
| 13 | try { |
| 14 | const pasted = page.waitForRequest(isBatch); |
| 15 | await page.keyboard.press('Control+v'); |
| 16 | await pasted; |
| 17 | await page.evaluate(() => window.SheetsCellQueue([], window.SheetsGrid.post)); |
| 18 | assert.equal(requests.length, 1, 'paste must send one atomic request'); |
| 19 | assert.equal(requests[0].postDataJSON().cells.length, 500); |
| 20 | await page.waitForFunction(() => window.SheetsGrid.rawAt(24, 19) === '500'); |
| 21 | } finally { |
| 22 | page.off('request', record); |
| 23 | } |
| 24 | |
| 25 | // Cut never writes. A failed paste leaves the source untouched. |
| 26 | await page.evaluate(() => { window.SheetsGrid.worksheet().updateSelectionFromCoords(0, 0, 1, 1); window.SheetsGrid.focus(); }); |
| 27 | const cuts = []; |
| 28 | const refuse = async route => { |
| 29 | cuts.push(route.request().postDataJSON()); |
| 30 | await route.fulfill({status: 500, contentType: 'text/plain', body: ''}); |
| 31 | }; |
| 32 | await page.route('**/cells/batch', refuse); |
| 33 | try { |
| 34 | await page.keyboard.press('Control+x'); |
| 35 | await page.evaluate(() => window.SheetsCellQueue([], window.SheetsGrid.post)); |
| 36 | assert.equal(cuts.length, 0, 'cut must leave the source in place'); |
| 37 | await page.evaluate(() => { window.SheetsGrid.worksheet().updateSelectionFromCoords(3, 3, 3, 3); window.SheetsGrid.focus(); }); |
| 38 | const attempted = page.waitForRequest(isBatch); |
| 39 | await page.keyboard.press('Control+v'); |
| 40 | await attempted; |
| 41 | await page.evaluate(() => window.SheetsCellQueue([], window.SheetsGrid.post)); |
| 42 | assert.equal(cuts.length, 1, 'move must use one atomic batch'); |
| 43 | assert.equal(cuts[0].cells.length, 8); |
| 44 | await page.waitForFunction(() => window.SheetsGrid.rawAt(0, 0) === '1' && window.SheetsGrid.rawAt(1, 1) === '22'); |
| 45 | } finally { |
| 46 | await page.unroute('**/cells/batch', refuse); |
| 47 | } |
| 48 | return {pasteRequests: 1, pasteCells: 500, failedMoveRequests: 1, sourcePreserved: true}; |
| 49 | } |
| 50 | |