| 1 | // Real Sheets app and jobs; synthesized workbook and controlled Google replies. |
| 2 | import assert from 'node:assert/strict' |
| 3 | const playwright = await import(process.env.PLAYWRIGHT || 'playwright') |
| 4 | const config = JSON.parse(process.env.SHEETS_GOOGLE_IMPORT_TEST) |
| 5 | const fixture = Buffer.from(config.fixture, 'base64') |
| 6 | const gis = `window.google={accounts:{oauth2:{initTokenClient(c){return{requestAccessToken(){c.callback({access_token:'controlled-google-bearer',scope:'https://www.googleapis.com/auth/drive.file'})}}},hasGrantedAllScopes(r,s){return r.scope===s}}}};` |
| 7 | const gapi = `window.gapi={load(name,c){google.picker={Action:{PICKED:'picked',CANCEL:'cancel'},DocsView:class{setMimeTypes(){return this}},PickerBuilder:class{setDeveloperKey(){return this}setAppId(){return this}setOAuthToken(){return this}setOrigin(){return this}addView(){return this}setCallback(fn){this.cb=fn;return this}build(){const cb=this.cb;let el;return{setVisible(){el=document.createElement('button');el.textContent='Select import fixture';el.onclick=()=>cb({action:'picked',docs:[{id:'controlled_file',name:'Import fixture',mimeType:'application/vnd.google-apps.spreadsheet'}]});document.body.append(el)},dispose(){el?.remove()}}}}};c.callback()}};` |
| 8 | for (const engine of ['chromium', 'webkit']) { |
| 9 | const browser = await playwright[engine].launch() |
| 10 | try { |
| 11 | const context = await browser.newContext({ ignoreHTTPSErrors: true, viewport: { width: 1050, height: 850 } }) |
| 12 | const cookies = config.cookies.map(c => ({ name: c.Name, value: c.Value, url: config.origin, httpOnly: c.HttpOnly, secure: c.Secure, sameSite: 'Lax' })) |
| 13 | await context.addCookies(cookies) |
| 14 | const page = await context.newPage() |
| 15 | const failures = [], violations = [] |
| 16 | let exports = 0, leaked = false |
| 17 | page.on('pageerror', e => failures.push(e.message)) |
| 18 | await page.addInitScript(() => document.addEventListener('securitypolicyviolation', e => console.log('IMPORT-CSP:' + e.violatedDirective))) |
| 19 | page.on('console', m => { if (m.text().startsWith('IMPORT-CSP:')) violations.push(m.text()) }) |
| 20 | page.on('request', r => { if (r.url().startsWith(config.origin) && ((r.postData() || '').includes('controlled-google-bearer') || JSON.stringify(r.headers()).includes('controlled-google-bearer'))) leaked = true }) |
| 21 | await context.route('https://accounts.google.com/gsi/client', r => r.fulfill({ contentType: 'text/javascript', body: gis })) |
| 22 | await context.route('https://apis.google.com/js/api.js', r => r.fulfill({ contentType: 'text/javascript', body: gapi })) |
| 23 | await context.route('https://www.googleapis.com/drive/v3/files/**', async r => { |
| 24 | exports++ |
| 25 | assert.equal(r.request().headers().authorization, 'Bearer controlled-google-bearer') |
| 26 | await r.fulfill({ headers: { 'Access-Control-Allow-Origin': '*' }, contentType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', body: fixture }) |
| 27 | }) |
| 28 | const waitForReport = async p => { |
| 29 | await p.waitForURL(url => /^\/sheets\/\d+\/import-report$/.test(url.pathname), { timeout: 30000 }) |
| 30 | assert.match(await p.locator('body').innerText(), /formula/i) |
| 31 | assert.match(await p.locator('body').innerText(), /text/i) |
| 32 | return p.url() |
| 33 | } |
| 34 | await page.goto(config.origin + '/sheets/import') |
| 35 | assert.match(await page.locator('body').innerText(), /first\s+worksheet/i) |
| 36 | await page.waitForFunction(() => !document.querySelector('[data-google-choose]')?.disabled) |
| 37 | await page.locator('[data-google-choose]').click() |
| 38 | await page.getByRole('button', { name: 'Select import fixture', exact: true }).click() |
| 39 | const reportURL = await waitForReport(page) |
| 40 | const sheetPath = new URL(reportURL).pathname.replace('/import-report', '') |
| 41 | await page.goto(config.origin + sheetPath) |
| 42 | const cell = (r, c) => page.locator(`td[data-x="${c}"][data-y="${r}"]`) |
| 43 | await cell(0, 0).waitFor({ state: 'visible' }) |
| 44 | for (const [r, c, value] of [[0, 0, '00123'], [0, 1, '7'], [1, 0, '2'], [1, 1, '5'], [2, 0, '=x'], [2, 1, 'TRUE']]) { |
| 45 | await page.waitForFunction(([r,c,v]) => document.querySelector(`td[data-x="${c}"][data-y="${r}"]`)?.textContent.trim() === v, [r,c,value]) |
| 46 | } |
| 47 | await cell(1, 0).dblclick() |
| 48 | const editor = cell(1, 0).locator('input,textarea') |
| 49 | await editor.waitFor() |
| 50 | await editor.fill('10') |
| 51 | await editor.press('Enter') |
| 52 | await page.waitForFunction(() => document.querySelector('td[data-x="1"][data-y="1"]')?.textContent.trim() === '13') |
| 53 | await page.evaluate(() => SheetsCellQueue([], SheetsGrid.post)) |
| 54 | await page.reload() |
| 55 | await page.waitForFunction(() => document.querySelector('td[data-x="1"][data-y="1"]')?.textContent.trim() === '13') |
| 56 | assert.equal((await cell(0, 0).innerText()).trim(), '00123') |
| 57 | assert.equal((await cell(0, 1).innerText()).trim(), '7') |
| 58 | await page.goto(reportURL) |
| 59 | assert.match(await page.locator('body').innerText(), /formula/i) |
| 60 | assert.equal(exports, 1) |
| 61 | assert.equal(leaked, false) |
| 62 | assert.deepEqual(violations, []) |
| 63 | assert.deepEqual(failures, []) |
| 64 | |
| 65 | const plainContext = await browser.newContext({ ignoreHTTPSErrors: true, javaScriptEnabled: false }) |
| 66 | await plainContext.addCookies(cookies) |
| 67 | const plain = await plainContext.newPage() |
| 68 | await plain.goto(config.origin + '/sheets/import') |
| 69 | assert.equal(await plain.locator('[data-google-choose]').isVisible(), false) |
| 70 | await plain.locator('input[type="file"]').setInputFiles({ name: 'scriptless.xlsx', mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', buffer: fixture }) |
| 71 | await plain.locator('form:has(input[type="file"]) button[type="submit"]').click() |
| 72 | // Scriptless job pages use their ordinary refresh link while the job runs. |
| 73 | for (let i = 0; i < 30 && !/\/sheets\/\d+\/import-report$/.test(new URL(plain.url()).pathname); i++) { |
| 74 | await plain.waitForTimeout(100) |
| 75 | await plain.reload() |
| 76 | } |
| 77 | const plainReport = await waitForReport(plain) |
| 78 | await plain.goto(plainReport.replace('/import-report', '/table')) |
| 79 | assert.match(await plain.locator('table[data-cell-table]').innerText(), /00123/) |
| 80 | assert.match(await plain.locator('table[data-cell-table]').innerText(), /=x/) |
| 81 | await plainContext.close() |
| 82 | await context.close() |
| 83 | console.log(engine + ': real import job, typed text, frozen value, supported formula recalculation, reload, report and scriptless upload passed') |
| 84 | } finally { await browser.close() } |
| 85 | } |
| 86 | |