Files
orca/config/scripts/verify-packaged-mobile-web-bundle.test.mjs
T
Jinwoo Hong cf13e0cf9d fix(sidebar): phone feed list leftovers before the flip (dark) (#30631)
* fix(sidebar): phone feed list leftovers before the flip (dark)

Bump the feed's semantics version for P11's desktop facts; mark switch-on phone page bundles so packaging and orcad staging refuse them; show the page's last-listed rows before the first feed read; draw the host's pending creations; pass the listed host's own label into host sections instead of the running device's platform.

* fix(sidebar): review fixes for the phone feed leftovers (dark)

A dark orcad may stage the dark page (only a marker of an off switch is refused); the bundle tree writer leaves the marker; the pre-feed list keeps the view's filters; a creation names its project only outside that project's header and survives search when its project matches; the feed view and row labels read one resolved desktop platform.

* fix(sidebar): second review fixes for the phone feed leftovers (dark)

Test the off-switch marker check across switches; resolve the desktop platform once for rows and headers; a section search narrows keeps only matching rows.
2026-10-19 04:27:09 -04:00

198 lines
7.7 KiB
JavaScript

import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
import { createRequire } from 'node:module'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { writeMobileWebBundleFixtureTree } from './mobile-web-bundle-fixture-tree.mjs'
const require = createRequire(import.meta.url)
const { assertMobileWebBundleBuilt } = require('./verify-packaged-mobile-web-bundle.cjs')
const electronBuilderConfig = require('../electron-builder.config.cjs')
const { darkBuildMarkers } = require('../dark-build-switches.cjs')
async function withBundle(run) {
const scratch = await mkdtemp(join(tmpdir(), 'orca-mobile-web-guard-'))
const bundleDir = join(scratch, 'mobile-web')
try {
const { manifest } = await writeMobileWebBundleFixtureTree({ outDir: bundleDir })
await run({ bundleDir, manifest })
} finally {
await rm(scratch, { recursive: true, force: true })
}
}
async function rewriteManifest(bundleDir, mutate) {
const manifestPath = join(bundleDir, 'manifest.json')
const manifest = JSON.parse(await readFile(manifestPath, 'utf8'))
mutate(manifest)
await writeFile(manifestPath, JSON.stringify(manifest, null, 2), 'utf8')
}
describe('assertMobileWebBundleBuilt', () => {
beforeEach(() => {
vi.spyOn(console, 'log').mockImplementation(() => {})
})
afterEach(() => {
vi.restoreAllMocks()
})
it('accepts a freshly built bundle', async () => {
await withBundle(({ bundleDir, manifest }) => {
expect(() => assertMobileWebBundleBuilt(bundleDir)).not.toThrow()
expect(manifest.entrypoint).toBe('index.html')
expect(manifest.assets.length).toBeGreaterThanOrEqual(3)
expect(
new Set(manifest.assets.map((asset) => asset.contentType)).size
).toBeGreaterThanOrEqual(2)
})
})
it('fails on a file the manifest does not list, so no stale asset ships inside asar', async () => {
await withBundle(async ({ bundleDir }) => {
// An asset dropped from the manifest keeps its content-addressed name, so nothing ever
// overwrites it; without this check it packs unreachable and unverified.
await writeFile(join(bundleDir, 'assets', 'stale.js'), '// from an earlier build\n', 'utf8')
expect(() => assertMobileWebBundleBuilt(bundleDir)).toThrow(
/does not list: assets\/stale\.js/
)
})
})
it('fails when the manifest is missing', async () => {
const scratch = await mkdtemp(join(tmpdir(), 'orca-mobile-web-guard-'))
try {
expect(() => assertMobileWebBundleBuilt(scratch)).toThrow(/no bundle manifest/)
} finally {
await rm(scratch, { recursive: true, force: true })
}
})
it('fails when the manifest is not JSON', async () => {
await withBundle(async ({ bundleDir }) => {
await writeFile(join(bundleDir, 'manifest.json'), 'not json', 'utf8')
expect(() => assertMobileWebBundleBuilt(bundleDir)).toThrow(/not valid JSON/)
})
})
it('fails when an asset is tampered with on disk', async () => {
await withBundle(async ({ bundleDir, manifest }) => {
const asset = manifest.assets.find((entry) => entry.path.endsWith('.js'))
const bytes = await readFile(join(bundleDir, asset.path))
// Same length, different content: only the hash check can catch this.
bytes[bytes.length - 1] = bytes.at(-1) === 0x20 ? 0x09 : 0x20
await writeFile(join(bundleDir, asset.path), bytes)
expect(() => assertMobileWebBundleBuilt(bundleDir)).toThrow(/hashes to .* on disk/)
})
})
it('fails when an asset is truncated', async () => {
await withBundle(async ({ bundleDir, manifest }) => {
const asset = manifest.assets.find((entry) => entry.path.endsWith('.css'))
await writeFile(join(bundleDir, asset.path), 'truncated', 'utf8')
expect(() => assertMobileWebBundleBuilt(bundleDir)).toThrow(/bytes on disk, manifest says/)
})
})
it('fails when a listed asset was never written', async () => {
await withBundle(async ({ bundleDir, manifest }) => {
const asset = manifest.assets.find((entry) => entry.path.endsWith('.png'))
await rm(join(bundleDir, asset.path))
expect(() => assertMobileWebBundleBuilt(bundleDir)).toThrow(/which is missing from/)
})
})
it('fails when the manifest buildId no longer matches its asset list', async () => {
await withBundle(async ({ bundleDir }) => {
await rewriteManifest(bundleDir, (manifest) => {
manifest.buildId = 'f'.repeat(64)
})
expect(() => assertMobileWebBundleBuilt(bundleDir)).toThrow(/does not match its asset list/)
})
})
it('fails on an unknown schemaVersion', async () => {
await withBundle(async ({ bundleDir }) => {
await rewriteManifest(bundleDir, (manifest) => {
manifest.schemaVersion = 2
})
expect(() => assertMobileWebBundleBuilt(bundleDir)).toThrow(
/unsupported manifest schemaVersion/
)
})
})
it('fails when a required field is dropped', async () => {
await withBundle(async ({ bundleDir }) => {
await rewriteManifest(bundleDir, (manifest) => {
delete manifest.desktopVersion
})
expect(() => assertMobileWebBundleBuilt(bundleDir)).toThrow(/desktopVersion is missing/)
})
})
it('fails when totalBytes disagrees with the asset list', async () => {
await withBundle(async ({ bundleDir }) => {
await rewriteManifest(bundleDir, (manifest) => {
manifest.totalBytes += 1
})
expect(() => assertMobileWebBundleBuilt(bundleDir)).toThrow(/its assets sum to/)
})
})
it('refuses an asset path that escapes the bundle directory', async () => {
await withBundle(async ({ bundleDir }) => {
await rewriteManifest(bundleDir, (manifest) => {
manifest.assets[0].path = '../outside.js'
})
expect(() => assertMobileWebBundleBuilt(bundleDir)).toThrow(/not a safe relative path/)
})
})
it('refuses a manifest whose entrypoint is not one of its assets', async () => {
await withBundle(async ({ bundleDir }) => {
await rewriteManifest(bundleDir, (manifest) => {
manifest.entrypoint = 'index.html'
manifest.assets = manifest.assets.filter((asset) => asset.path !== 'index.html')
})
expect(() => assertMobileWebBundleBuilt(bundleDir)).toThrow(/is not one of its assets/)
})
})
})
describe('electron-builder packaging wiring', () => {
it('does not exclude the built bundle, so out/mobile-web ships like out/web', () => {
const excludesBuiltBundle = electronBuilderConfig.files.some(
(entry) => typeof entry === 'string' && entry.startsWith('!out/mobile-web')
)
expect(excludesBuiltBundle).toBe(false)
})
it('verifies a dark page build, and refuses to package it', async () => {
await withBundle(async ({ bundleDir }) => {
const [marker] = darkBuildMarkers({ ORCA_SIDEBAR_INPUTS: 'main' })
await writeFile(join(bundleDir, marker.fileName), marker.text, 'utf8')
expect(() => assertMobileWebBundleBuilt(bundleDir)).not.toThrow()
expect(() =>
electronBuilderConfig.beforePack(
{ electronPlatformName: process.platform, arch: process.arch === 'arm64' ? 3 : 1 },
bundleDir
)
).toThrow(`${bundleDir} holds an ORCA_SIDEBAR_INPUTS=main build`)
})
})
it('verifies the bundle root it is given, not the repo one', async () => {
// The seam exists so unit tests need no built out/; it would be worthless if the root were
// accepted and then ignored.
await withBundle(async ({ bundleDir }) => {
await rm(join(bundleDir, 'manifest.json'))
expect(() =>
electronBuilderConfig.beforePack(
{ electronPlatformName: process.platform, arch: process.arch === 'arm64' ? 3 : 1 },
bundleDir
)
).toThrow(/no bundle manifest/)
})
})
})