import { mkdir, readFile, writeFile } from 'node:fs/promises' import { join } from 'node:path' import { describe, expect, it } from 'vitest' import { withScratch, copyWrittenBundle } from '../../mobile-web-app/mobile-web-app-bundle-test-fixture.mjs' import { bundleMobileWebApp } from './build-mobile-web-app-bundle.mjs' import { MOBILE_WEB_APP_ROUTE_ROOT } from '../../mobile-web-app/mobile-web-app-route-manifest.mjs' import { verifyMobileWebAppBundle } from './verify-mobile-web-app-bundle.mjs' import { readDesktopVersion, readProtocolWindow, sha256Hex, writeMobileWebBundleTree } from './mobile-web-bundle-manifest.mjs' import { mobileWebAppDependenciesPresent } from '../../mobile-web-app/mobile-web-app-bundle-dependencies.mjs' // Root shards lack mobile deps; real bundling tests run in mobile_web_app CI job. const bundles = mobileWebAppDependenciesPresent() const describeBundling = bundles ? describe : describe.skip const itBundling = bundles ? it : it.skip function allScriptSource({ script, chunks }) { return [script, ...chunks.map((chunk) => chunk.bytes)].map((bytes) => bytes.toString('utf8')) } describeBundling('the app bundle', () => { itBundling( 'refuses to build a route the lazy manifest would strip an export from', async () => { await withScratch(async (scratch) => { const directory = join(scratch, MOBILE_WEB_APP_ROUTE_ROOT) await mkdir(directory, { recursive: true }) await writeFile( join(directory, 'index.tsx'), 'export default function Route() { return null }\n' ) await expect(bundleMobileWebApp({ appDir: scratch })).resolves.toBeTruthy() await writeFile( join(directory, 'settings.tsx'), 'const anchor = { anchor: "index" }\nexport { anchor as unstable_settings }\nexport default function Route() { return null }\n' ) // Fail dropped route exports at build time; otherwise phones show blank screens without logs. await expect(bundleMobileWebApp({ appDir: scratch })).rejects.toThrow( /settings\.tsx.*unstable_settings/s ) }) }, 240_000 ) itBundling( 'refuses a route whose star re-export it cannot read', async () => { await withScratch(async (scratch) => { const directory = join(scratch, MOBILE_WEB_APP_ROUTE_ROOT) await mkdir(directory, { recursive: true }) await writeFile(join(directory, 'boundary.ts'), 'export const value = 1\n') await writeFile( join(directory, 'index.tsx'), 'export * from "./boundary"\nexport default function Route() { return null }\n' ) await expect(bundleMobileWebApp({ appDir: scratch })).rejects.toThrow( /index\.tsx.*boundary/s ) }) }, 240_000 ) it("bundles a route's .web.tsx sibling instead of the native file, changing the bytes", async () => { await withScratch(async (scratch) => { const directory = join(scratch, MOBILE_WEB_APP_ROUTE_ROOT) await mkdir(directory, { recursive: true }) const route = (marker) => `export default function Route() { return '${marker}' }\n` await writeFile(join(directory, 'index.tsx'), route('native-route-marker')) const before = await bundleMobileWebApp({ appDir: scratch }) const has = (bundle, marker) => allScriptSource(bundle).some((source) => source.includes(marker)) expect(has(before, 'native-route-marker')).toBe(true) await writeFile(join(directory, 'index.web.tsx'), route('web-route-marker')) const after = await bundleMobileWebApp({ appDir: scratch }) expect(has(after, 'web-route-marker')).toBe(true) expect(has(after, 'native-route-marker')).toBe(false) expect(after.script.equals(before.script)).toBe(false) }) }, 240_000) }) describe('the verifier', () => { itBundling( "rejects a buildId the manifest's own asset list does not derive", async () => { await withScratch(async (scratch) => { const outDir = join(scratch, 'mobile-web') await copyWrittenBundle(outDir) const manifestPath = join(outDir, 'manifest.json') const manifest = JSON.parse(await readFile(manifestPath, 'utf8')) manifest.buildId = 'f'.repeat(64) await writeFile(manifestPath, JSON.stringify(manifest), 'utf8') await expect(verifyMobileWebAppBundle({ bundleDir: outDir })).rejects.toThrow( 'does not match its asset list' ) }) }, 240_000 ) itBundling( 'rejects a self-consistent bundle a fresh build does not reproduce', async () => { await withScratch(async (scratch) => { const outDir = join(scratch, 'mobile-web') const { manifest } = await copyWrittenBundle(outDir) // Consistent digests/buildId cannot reveal stale sources; verifier’s two fresh builds must detect them. const assets = await Promise.all( manifest.assets.map(async (asset) => ({ ...asset, bytes: await readFile(join(outDir, asset.path)) })) ) const document = assets.find((asset) => asset.path === manifest.entrypoint) document.bytes = Buffer.concat([document.bytes, Buffer.from('\n', 'utf8')]) document.sha256 = sha256Hex(document.bytes) document.byteLength = document.bytes.byteLength const [desktopVersion, protocolWindow] = await Promise.all([ readDesktopVersion(), readProtocolWindow() ]) await writeMobileWebBundleTree({ outDir, written: assets, desktopVersion, protocolWindow }) await expect(verifyMobileWebAppBundle({ bundleDir: outDir })).rejects.toThrow('is stale') }) }, 240_000 ) })