Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

8.5 KB
Raw Blame
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { describe, expect, it } from 'vitest'
import {
  MOBILE_WEB_APP_ROUTE_ROOT,
  ROUTE_CONTEXT_SOURCE,
  ROUTE_MODULE_SYNCHRONOUS_EXPORTS,
  collectMobileWebAppRouteKeys,
  collectMobileWebAppRoutes,
  routeModuleSynchronousExports
} from './mobile-web-app-route-manifest.mjs'
import { mobileWebAppDependenciesPresent } from './mobile-web-app-bundle-dependencies.mjs'

const projectDir = fileURLToPath(new URL('../../..', import.meta.url))
const appDir = join(projectDir, 'mobile', 'app')

// Root shards omit mobile dependencies; route-tree builds run in mobile_web_app job.
const itBundling = mobileWebAppDependenciesPresent() ? it : it.skip

async function withScratch(run) {
  const scratch = await mkdtemp(join(tmpdir(), 'orca-mobile-web-app-routes-test-'))
  try {
    return await run(scratch)
  } finally {
    await rm(scratch, { recursive: true, force: true })
  }
}

describe('route manifest', () => {
  it('collects the h/ subtree and, above it, only the root layout web sibling', async () => {
    const routes = await collectMobileWebAppRoutes(appDir)
    const keys = routes.map((route) => route.key)
    expect(keys.length).toBeGreaterThan(0)
    for (const key of keys.filter((key) => key !== './_layout.tsx')) {
      expect(key.startsWith(`./${MOBILE_WEB_APP_ROUTE_ROOT}/`)).toBe(true)
    }
    // Root key must resolve web layout or native pairing/settings/notifications enter page bundle.
    expect(routes.find((route) => route.key === './_layout.tsx')?.module).toBe(
      join(appDir, '_layout.web.tsx')
    )
    expect(keys).not.toContain('./pair.tsx')
  })

  it('carries no root layout for a tree without a web sibling of it', async () => {
    await withScratch(async (scratch) => {
      await mkdir(join(scratch, MOBILE_WEB_APP_ROUTE_ROOT), { recursive: true })
      await writeFile(join(scratch, '_layout.tsx'), 'export default null\n', 'utf8')
      await writeFile(join(scratch, MOBILE_WEB_APP_ROUTE_ROOT, 'index.tsx'), 'export default 1\n')
      expect(await collectMobileWebAppRouteKeys(scratch)).toEqual(['./h/index.tsx'])
    })
  })

  it('is sorted, so the generated module is a pure function of the tree', async () => {
    const keys = await collectMobileWebAppRouteKeys(appDir)
    expect(keys).toEqual([...keys].sort())
  })

  it('excludes test files and API routes', async () => {
    // Synthetic tree exercises absent-today forbidden route exports.
    await withScratch(async (scratch) => {
      const directory = join(scratch, MOBILE_WEB_APP_ROUTE_ROOT)
      await mkdir(directory, { recursive: true })
      for (const name of [
        'index.tsx',
        'index.test.tsx',
        'index.spec.tsx',
        'shape.d.ts',
        '+api.ts',
        'tokens+api.ts',
        '+middleware.ts',
        'notes.md'
      ]) {
        await writeFile(join(directory, name), 'export default null\n', 'utf8')
      }
      expect(await collectMobileWebAppRouteKeys(scratch)).toEqual(['./h/index.tsx'])
    })
    expect(await collectMobileWebAppRouteKeys(appDir)).not.toContain('./h/_layout.test.tsx')
  })

  it('refuses an empty subtree rather than emitting a context with no routes', async () => {
    await withScratch(async (scratch) => {
      await mkdir(join(scratch, MOBILE_WEB_APP_ROUTE_ROOT))
      await expect(collectMobileWebAppRouteKeys(scratch)).rejects.toThrow(
        '[mobile-web-app] no routes under'
      )
    })
  })

  it('has no route carrying an export a lazy module would swallow', async () => {
    const routes = await collectMobileWebAppRoutes(appDir)
    expect(routes.length).toBeGreaterThan(0)
    for (const { module } of routes) {
      const { named, starExports } = await routeModuleSynchronousExports(module)
      // Synchronous namespace exports need a static route import; lazy import cannot serve them.
      expect(named, `${module} exports ${named.join(', ')}`).toEqual([])
      expect(starExports, `${module} re-exports all of ${starExports.join(', ')}`).toEqual([])
    }
  })

  // Alias/reexport controls require namespace parse, not declaration-name matching.
  it('reads the names off the namespace, not off a declaration', async () => {
    expect(ROUTE_MODULE_SYNCHRONOUS_EXPORTS).toEqual(['unstable_settings', 'ErrorBoundary'])
    await withScratch(async (scratch) => {
      const exportsOf = async (name, source) => {
        const file = join(scratch, name)
        await writeFile(file, source, 'utf8')
        return routeModuleSynchronousExports(file)
      }
      expect(
        (await exportsOf('declared.tsx', 'export const unstable_settings = { anchor: "x" }\n'))
          .named
      ).toEqual(['unstable_settings'])
      expect(
        (
          await exportsOf(
            'aliased.tsx',
            'const settings = { anchor: "x" }\nexport { settings as unstable_settings }\n'
          )
        ).named
      ).toEqual(['unstable_settings'])
      expect((await exportsOf('classy.tsx', 'export class ErrorBoundary {}\n')).named).toEqual([
        'ErrorBoundary'
      ])
      expect(
        (await exportsOf('forwarded.tsx', 'export { ErrorBoundary } from "./boundary"\n')).named
      ).toEqual(['ErrorBoundary'])
      expect(
        (await exportsOf('plain.tsx', 'export default function Route() { return null }\n')).named
      ).toEqual([])
    })
  }, 60_000)

  it('refuses a star re-export rather than reading it as clean', async () => {
    await withScratch(async (scratch) => {
      const file = join(scratch, 'star.tsx')
      // Unresolved export * cannot prove namespace clean without bundling; fail closed.
      await writeFile(file, 'export * from "./boundary"\nexport default null\n', 'utf8')
      const { named, starExports } = await routeModuleSynchronousExports(file)
      expect(named).toEqual([])
      expect(starExports).toEqual(['./boundary'])
    })
  }, 60_000)

  itBundling(
    'reads a .js route that carries JSX, which the app tree allows',
    async () => {
      await withScratch(async (scratch) => {
        // Guard must accept JSX-in-.js routes like actual RN bundler.
        const file = join(scratch, 'jsx-route.js')
        await writeFile(
          file,
          'const boundary = () => <div />\nexport { boundary as ErrorBoundary }\nexport default () => <div />\n',
          'utf8'
        )
        expect((await routeModuleSynchronousExports(file)).named).toEqual(['ErrorBoundary'])
      })
    },
    60_000
  )

  it('imports a .web.tsx sibling under the native route key', 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() {}\n')
      expect(await collectMobileWebAppRoutes(scratch)).toEqual([
        { key: './h/index.tsx', module: join(directory, 'index.tsx') }
      ])
      await writeFile(join(directory, 'index.web.tsx'), 'export default function Route() {}\n')
      // Preserve native key so web override changes implementation without changing URL.
      expect(await collectMobileWebAppRoutes(scratch)).toEqual([
        { key: './h/index.tsx', module: join(directory, 'index.web.tsx') }
      ])
    })
  })
})

describe('the synthesized RequireContext', () => {
  const build = (modules) =>
    new Function('modules', `${ROUTE_CONTEXT_SOURCE}; return routeContext`)(modules)

  it('answers the four members expo-router reads', () => {
    const context = build({ './h/index.tsx': { default: 'screen' } })
    expect(context.keys()).toEqual(['./h/index.tsx'])
    expect(context('./h/index.tsx')).toEqual({ default: 'screen' })
    expect(context.resolve('./h/index.tsx')).toBe('./h/index.tsx')
    expect(context.id).toBe('orca-mobile-web-app-routes')
  })

  it('hands out a copy of keys, so a caller cannot mutate the route tree', () => {
    const context = build({ './h/index.tsx': {} })
    context.keys().push('./injected.tsx')
    expect(context.keys()).toEqual(['./h/index.tsx'])
  })

  it('throws rather than returning undefined for an unknown key', () => {
    const context = build({ './h/index.tsx': {} })
    expect(() => context('./missing.tsx')).toThrow('no route module')
    expect(() => context.resolve('./missing.tsx')).toThrow('cannot resolve route')
  })

  it('does not answer inherited Object keys', () => {
    const context = build({ './h/index.tsx': {} })
    expect(() => context('constructor')).toThrow('no route module')
  })
})