| new file mode 100644 |
| index 0000000..38eaee9 |
| --- /dev/null |
| +++ b/tools/drive/contextmenu.mjs |
| @@ -0,0 +1,57 @@ |
| +// Right-clicking a cell must not open the grid library's stock context menu |
| +// (B7). Its items ("Delete selected rows", "Add comments", "Copy...", |
| +// "Paste...", "Save as...", "About") act on the library's own model and |
| +// bypass the app's write, comment and export paths. The app configures |
| +// contextMenu to return false, so the browser's own menu shows instead. |
| +// Real server-rendered page and assets, mocked transport; no live data. |
| +// SHEETS_MENU_FIXTURE=/tmp/menus.json go test ./internal/sheets -run TestBrowserMenuFixture -count=1 |
| +// PLAYWRIGHT=/path/to/playwright/index.mjs SHEETS_MENU_FIXTURE=/tmp/menus.json node tools/drive/contextmenu.mjs |
| +import assert from 'node:assert/strict'; |
| +import fs from 'node:fs'; |
| +const pw = await import(process.env.PLAYWRIGHT || 'playwright'); |
| +const fixture = JSON.parse(fs.readFileSync(process.env.SHEETS_MENU_FIXTURE)); |
| +const origin = 'https://sheets.test'; |
| +for (const name of ['chromium', 'webkit']) { |
| + const options = name === 'webkit' && process.env.WEBKIT_EXECUTABLE_PATH ? {executablePath:process.env.WEBKIT_EXECUTABLE_PATH} : {}; |
| + const browser = await pw[name].launch(options); |
| + try { |
| + const page = await browser.newPage({viewport:{width:1440,height:1000}}); |
| + const errors = []; let writes = []; |
| + page.on('pageerror', e=>errors.push(e.message)); |
| + await page.route(origin+'/**', async route => { |
| + const req = route.request(), path = new URL(req.url()).pathname; |
| + if (path in fixture.assets) return route.fulfill({body:fixture.assets[path],contentType: |
| + path.endsWith('.js')?'application/javascript':path.endsWith('.css')?'text/css':'image/svg+xml'}); |
| + if (path.endsWith('/stream')) return route.fulfill({body:'',contentType:'text/event-stream'}); |
| + if (req.method() !== 'GET') writes.push(path); |
| + if (path.startsWith('/api/')) return route.fulfill({json:{version:1,cells:[ |
| + {r:0,c:0,raw:'10',display:'10'},{r:1,c:0,raw:'20',display:'20'}]}}); |
| + return route.fulfill({body:fixture.html,contentType:'text/html'}); |
| + }); |
| + await page.goto(origin+'/sheets/1'); |
| + await page.waitForFunction(()=>window.SheetsGrid?.worksheet()); |
| + const cell = page.locator('td[data-x="0"][data-y="0"]'); |
| + await cell.click(); |
| + writes = []; |
| + await cell.click({button:'right'}); |
| + const shown = await page.evaluate(()=>[...document.querySelectorAll('.jcontextmenu')] |
| + .filter(m=>getComputedStyle(m).display!=='none' && m.offsetHeight>0 && m.children.length>0).map(m=>m.innerText)); |
| + assert.deepEqual(shown, [], `${name}: the library context menu opened: ${JSON.stringify(shown)}`); |
| + for (const label of ['Delete selected rows','Add comments','Save as','About']) { |
| + assert.equal(await page.getByText(label).filter({visible:true}).count(), 0, `${name}: "${label}" is on screen`); |
| + } |
| + // Dispatched as a real contextmenu event; defaultPrevented tells us whether |
| + // anything claimed it (the library's menu does, the browser's does not). |
| + const prevented = await cell.evaluate(td => { |
| + const e = new MouseEvent('contextmenu', {bubbles:true, cancelable:true, button:2, clientX:td.getBoundingClientRect().x+5, clientY:td.getBoundingClientRect().y+5}); |
| + td.dispatchEvent(e); |
| + return e.defaultPrevented; |
| + }); |
| + assert.equal(prevented, false, `${name}: something swallowed the right-click instead of leaving the browser menu`); |
| + assert.deepEqual(writes, [], `${name}: right-click sent writes`); |
| + assert.deepEqual(errors, [], `${name}: page errors`); |
| + console.log(`${name}: no library context menu`); |
| + } finally { |
| + await browser.close(); |
| + } |
| +} |