You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

86 lines
3.4 KiB

import { rpc } from '../helpers/rpc'
import { test, expect, chromium, type Browser } from '@playwright/test'
import { spawn } from 'node:child_process'
import { existsSync, mkdtempSync, rmSync } from 'node:fs'
import { resolve, join } from 'node:path'
import { tmpdir } from 'node:os'
import brand from '../../app.config.json'
test('packaged executable loads production protocol, native SQLite and the capability bridge', async () => {
const candidates =
process.platform === 'win32'
? [`dist/win-unpacked/${brand.productName}.exe`]
: process.platform === 'darwin'
? [
`dist/mac/${brand.productName}.app/Contents/MacOS/${brand.productName}`,
`dist/mac-arm64/${brand.productName}.app/Contents/MacOS/${brand.productName}`
]
: [`dist/linux-unpacked/${brand.name}`]
const executable =
process.env['WORKSHOP_PACKAGED_EXECUTABLE'] ||
candidates.map((candidate) => resolve(candidate)).find(existsSync)
expect(executable, 'Run pnpm package first').toBeTruthy()
if (!executable) return
const profile = mkdtempSync(join(tmpdir(), 'workshop-packaged-'))
const environment = { ...process.env }
delete environment['ELECTRON_RUN_AS_NODE']
// Keep packaged Node inspector fuses disabled; this process requests renderer CDP only.
const child = spawn(
executable,
[
'--remote-debugging-port=0',
'--remote-debugging-address=127.0.0.1',
`--profile-dir=${profile}`
],
{ env: environment, windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] }
)
let browser: Browser | undefined
let output = ''
try {
const endpoint = await new Promise<string>((resolveEndpoint, reject) => {
const timer = setTimeout(
() => reject(new Error(`No Chromium endpoint from packaged app: ${output.slice(-2000)}`)),
20000
)
const collect = (data: Buffer): void => {
output += data.toString()
const match = output.match(/DevTools listening on (ws:\/\/127\.0\.0\.1[^\s]+)/)
if (match?.[1]) {
clearTimeout(timer)
resolveEndpoint(match[1])
}
}
child.stdout.on('data', collect)
child.stderr.on('data', collect)
child.once('error', (error) => {
clearTimeout(timer)
reject(error)
})
child.once('exit', (code) => {
clearTimeout(timer)
reject(new Error(`Packaged app exited (${code}): ${output.slice(-2000)}`))
})
})
browser = await chromium.connectOverCDP(endpoint)
const context = browser.contexts()[0]
if (!context) throw new Error('No packaged browser context')
const page = context.pages()[0] ?? (await context.waitForEvent('page'))
await expect(page.getByTestId('note-input')).toBeVisible()
expect(page.url()).toMatch(/^app:\/\/desktop\/index\.html/)
expect(await rpc(page, 'app.info')).toMatchObject({
packaged: true
})
expect(await page.evaluate(() => typeof Reflect.get(window, 'require'))).toBe('undefined')
await page.getByTestId('note-input').fill('Packaged SQLite works')
await page.getByTestId('note-add').click()
await expect(page.locator('.note-list')).toContainText('Packaged SQLite works')
} finally {
await browser?.close()
if (child.exitCode === null) {
const exited = new Promise<void>((done) => child.once('exit', () => done()))
child.kill()
await exited
}
rmSync(profile, { recursive: true, force: true })
}
})