|
| 1 | +import type { Browser, Page } from 'playwright-core' |
| 2 | +import { |
| 3 | + afterAll, |
| 4 | + beforeAll, |
| 5 | + describe, |
| 6 | + expect, |
| 7 | + it |
| 8 | +} from 'vitest' |
| 9 | + |
| 10 | +import { |
| 11 | + launchBrowser, |
| 12 | + startAstroDevServer, |
| 13 | + stopChildProcess, |
| 14 | + type AstroDevServer |
| 15 | +} from './support/astro-browser-test' |
| 16 | +import { |
| 17 | + createNativeAppArchive, |
| 18 | + type PlaywrightUploadFile |
| 19 | +} from './support/app-archive-fixture' |
| 20 | + |
| 21 | +const appTestVariants = [ |
| 22 | + { |
| 23 | + name: 'legacy scanner', |
| 24 | + routeSuffix: '' |
| 25 | + }, |
| 26 | + { |
| 27 | + name: 'worker scanner', |
| 28 | + routeSuffix: '?version=2' |
| 29 | + } |
| 30 | +] as const |
| 31 | + |
| 32 | +describe( 'Apple Silicon app test page', () => { |
| 33 | + let browser: Browser |
| 34 | + let devServer: AstroDevServer |
| 35 | + let appArchive: PlaywrightUploadFile |
| 36 | + |
| 37 | + beforeAll( async () => { |
| 38 | + appArchive = await createNativeAppArchive() |
| 39 | + |
| 40 | + devServer = await startAstroDevServer({ |
| 41 | + env: { |
| 42 | + TEST_RESULT_STORE: '/api/test-results' |
| 43 | + }, |
| 44 | + preferConfiguredBaseUrl: false |
| 45 | + }) |
| 46 | + |
| 47 | + browser = await launchBrowser() |
| 48 | + await Promise.all( appTestVariants.map( variant => { |
| 49 | + return warmAppTestRoute( browser, devServer.baseUrl, variant.routeSuffix ) |
| 50 | + } ) ) |
| 51 | + } ) |
| 52 | + |
| 53 | + afterAll( async () => { |
| 54 | + await browser?.close() |
| 55 | + await stopChildProcess( devServer?.process || null ) |
| 56 | + } ) |
| 57 | + |
| 58 | + it.each( appTestVariants )( 'uploads an app archive through the %s path and renders a native result', async ( variant ) => { |
| 59 | + const page = await browser.newPage() |
| 60 | + const consoleErrors: string[] = [] |
| 61 | + const pageErrors: string[] = [] |
| 62 | + const submittedScans: Record<string, unknown>[] = [] |
| 63 | + |
| 64 | + page.on( 'console', message => { |
| 65 | + if ( message.type() === 'error' ) { |
| 66 | + consoleErrors.push( message.text() ) |
| 67 | + } |
| 68 | + } ) |
| 69 | + |
| 70 | + page.on( 'pageerror', error => { |
| 71 | + pageErrors.push( error.message ) |
| 72 | + } ) |
| 73 | + |
| 74 | + await stubResultStore( page, submittedScans ) |
| 75 | + |
| 76 | + await page.goto( `${ devServer.baseUrl }/apple-silicon-app-test/${ variant.routeSuffix }`, { |
| 77 | + waitUntil: 'load' |
| 78 | + } ) |
| 79 | + |
| 80 | + await page.waitForFunction( () => { |
| 81 | + const island = document.querySelector( 'astro-island[component-url="/pages/apple-silicon-app-test.vue"]' ) |
| 82 | + |
| 83 | + return Boolean( island && !island.hasAttribute( 'ssr' ) ) |
| 84 | + }, { |
| 85 | + timeout: 30 * 1000 |
| 86 | + } ) |
| 87 | + |
| 88 | + await page.locator( 'input[type="file"]' ).setInputFiles( appArchive ) |
| 89 | + await waitForBodyText( page, 'Total Files: 1', { |
| 90 | + consoleErrors, |
| 91 | + devServerOutput: devServer.output.text, |
| 92 | + pageErrors |
| 93 | + } ) |
| 94 | + |
| 95 | + const firstScanRow = page.locator( '.results-container li' ).first() |
| 96 | + |
| 97 | + await waitForBodyText( page, 'Playwright Native App', { |
| 98 | + consoleErrors, |
| 99 | + devServerOutput: devServer.output.text, |
| 100 | + pageErrors |
| 101 | + } ) |
| 102 | + await waitForBodyText( page, '✅ This app is natively compatible with Apple Silicon!', { |
| 103 | + consoleErrors, |
| 104 | + devServerOutput: devServer.output.text, |
| 105 | + pageErrors |
| 106 | + } ) |
| 107 | + |
| 108 | + await firstScanRow.locator( 'summary' ).click() |
| 109 | + |
| 110 | + const rowText = await firstScanRow.textContent() |
| 111 | + |
| 112 | + expect( rowText ).toContain( 'Bundle Identifier' ) |
| 113 | + expect( rowText ).toContain( 'com.doesitarm.playwright-native-app' ) |
| 114 | + |
| 115 | + expect( submittedScans.length, devServer.output.text ).toBe( 1 ) |
| 116 | + expect( submittedScans[ 0 ]?.filename, JSON.stringify( submittedScans[ 0 ] ) ).toBe( 'Playwright Native App.app.zip' ) |
| 117 | + expect( submittedScans[ 0 ]?.result, JSON.stringify( submittedScans[ 0 ] ) ).toBe( '✅' ) |
| 118 | + expect( pageErrors, devServer.output.text ).toEqual( [] ) |
| 119 | + expect( consoleErrors, devServer.output.text ).toEqual( [] ) |
| 120 | + } ) |
| 121 | +} ) |
| 122 | + |
| 123 | +async function stubResultStore ( page: Page, submittedScans: Record<string, unknown>[] ) { |
| 124 | + await page.route( '**/api/test-results', async route => { |
| 125 | + const postData = route.request().postDataJSON() |
| 126 | + |
| 127 | + if ( postData && typeof postData === 'object' ) { |
| 128 | + submittedScans.push( postData as Record<string, unknown> ) |
| 129 | + } |
| 130 | + |
| 131 | + await route.fulfill({ |
| 132 | + status: 200, |
| 133 | + contentType: 'application/json', |
| 134 | + body: JSON.stringify({ |
| 135 | + supportedVersionNumber: null |
| 136 | + }) |
| 137 | + }) |
| 138 | + } ) |
| 139 | +} |
| 140 | + |
| 141 | +async function waitForBodyText ( page: Page, expectedText: string, debugContext: { |
| 142 | + consoleErrors: string[] |
| 143 | + devServerOutput: string |
| 144 | + pageErrors: string[] |
| 145 | +} ) { |
| 146 | + try { |
| 147 | + await page.waitForFunction( textToFind => { |
| 148 | + return Boolean( document.body?.textContent?.includes( textToFind ) ) |
| 149 | + }, expectedText, { |
| 150 | + timeout: 30 * 1000 |
| 151 | + } ) |
| 152 | + } catch ( error ) { |
| 153 | + const bodyText = await page.locator( 'body' ).textContent() |
| 154 | + |
| 155 | + throw new Error( [ |
| 156 | + `Timed out waiting for body text: ${ expectedText }`, |
| 157 | + bodyText || '', |
| 158 | + debugContext.pageErrors.join( '\n' ), |
| 159 | + debugContext.consoleErrors.join( '\n' ), |
| 160 | + debugContext.devServerOutput |
| 161 | + ].filter( Boolean ).join( '\n\n' ), { |
| 162 | + cause: error |
| 163 | + } ) |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +async function warmAppTestRoute ( browser: Browser, baseUrl: string, routeSuffix = '' ) { |
| 168 | + const warmPage = await browser.newPage() |
| 169 | + |
| 170 | + try { |
| 171 | + await warmPage.goto( `${ baseUrl }/apple-silicon-app-test/${ routeSuffix }`, { |
| 172 | + waitUntil: 'load' |
| 173 | + } ) |
| 174 | + await warmPage.waitForTimeout( 5000 ) |
| 175 | + } finally { |
| 176 | + await warmPage.close() |
| 177 | + } |
| 178 | +} |
0 commit comments