import assert from 'node:assert/strict'; export async function checkCellQueue(page) { const isBatch = request => request.method() === 'POST' && new URL(request.url()).pathname.endsWith('/cells/batch'); const requests = []; const record = request => { if (isBatch(request)) requests.push(request); }; const text = Array.from({length: 25}, (_, r) => Array.from({length: 20}, (_, c) => String(r * 20 + c + 1)).join('\t')).join('\n'); await page.bringToFront(); await page.evaluate(async text => navigator.clipboard.writeText(text), text); await page.locator('[data-sheet-grid] tbody tr:first-child td:nth-child(2)').click(); await page.evaluate(() => { window.SheetsGrid.worksheet().updateSelectionFromCoords(0, 0, 0, 0); window.SheetsGrid.focus(); }); page.on('request', record); try { const pasted = page.waitForRequest(isBatch); await page.keyboard.press('Control+v'); await pasted; await page.evaluate(() => window.SheetsCellQueue([], window.SheetsGrid.post)); assert.equal(requests.length, 1, 'paste must send one atomic request'); assert.equal(requests[0].postDataJSON().cells.length, 500); await page.waitForFunction(() => window.SheetsGrid.rawAt(24, 19) === '500'); } finally { page.off('request', record); } // Cut never writes. A failed paste leaves the source untouched. await page.evaluate(() => { window.SheetsGrid.worksheet().updateSelectionFromCoords(0, 0, 1, 1); window.SheetsGrid.focus(); }); const cuts = []; const refuse = async route => { cuts.push(route.request().postDataJSON()); await route.fulfill({status: 500, contentType: 'text/plain', body: ''}); }; await page.route('**/cells/batch', refuse); try { await page.keyboard.press('Control+x'); await page.evaluate(() => window.SheetsCellQueue([], window.SheetsGrid.post)); assert.equal(cuts.length, 0, 'cut must leave the source in place'); await page.evaluate(() => { window.SheetsGrid.worksheet().updateSelectionFromCoords(3, 3, 3, 3); window.SheetsGrid.focus(); }); const attempted = page.waitForRequest(isBatch); await page.keyboard.press('Control+v'); await attempted; await page.evaluate(() => window.SheetsCellQueue([], window.SheetsGrid.post)); assert.equal(cuts.length, 1, 'move must use one atomic batch'); assert.equal(cuts[0].cells.length, 8); await page.waitForFunction(() => window.SheetsGrid.rawAt(0, 0) === '1' && window.SheetsGrid.rawAt(1, 1) === '22'); } finally { await page.unroute('**/cells/batch', refuse); } return {pasteRequests: 1, pasteCells: 500, failedMoveRequests: 1, sourcePreserved: true}; }