test: share locale assets across renderer and mobile fixtures

This commit is contained in:
Brennan Benson
2026-10-29 12:25:48 -07:00
parent b46c457e02
commit c6156ffe4a
3 changed files with 45 additions and 13 deletions
@@ -0,0 +1,41 @@
import { readdirSync, readFileSync } from 'node:fs'
import { dirname, join, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import { normalizePath, type Plugin } from 'vite'
export function localeCatalogTestAssets(): Plugin {
const directory = fileURLToPath(new URL('../../src/renderer/src/i18n/locales/', import.meta.url))
const filesById = new Map<string, string>()
const idsByPath = new Map<string, string>()
for (const filename of readdirSync(directory)) {
if (/^[A-Za-z-]+\.json$/.test(filename)) {
const id = `\0locale-catalog-test-assets:${filename}.js`
const file = join(directory, filename)
filesById.set(id, file)
idsByPath.set(`${normalizePath(file)}?url`, id)
}
}
return {
name: 'locale-catalog-test-assets',
enforce: 'pre',
resolveId(source, importer) {
if (filesById.has(source)) {
return source
}
if (!source.endsWith('.json?url')) {
return null
}
const file = importer ? resolve(dirname(importer.split('?')[0]), source) : source
return idsByPath.get(normalizePath(file)) ?? null
},
load(id) {
const file = filesById.get(id)
if (!file) {
return null
}
// Virtual modules retain real fetch(data:) parsing without exposing files outside the test root.
const catalog = readFileSync(file).toString('base64')
return `export default ${JSON.stringify(`data:application/json;base64,${catalog}`)}`
}
}
}
+2 -13
View File
@@ -1,4 +1,3 @@
import { readFileSync } from 'node:fs'
import { createRequire } from 'node:module'
import { resolve } from 'node:path'
import { normalizePath, transformWithOxc, type Plugin } from 'vite'
@@ -11,6 +10,7 @@ import { nodeRuntimePool } from './scripts/vitest-node-runtime-pool'
import { readRunFilters } from './scripts/vitest-run-filters.mjs'
import { createWorkspaceSourceResolver } from './scripts/workspace-source-exports.mjs'
import { moduleCacheResolutionKey } from './scripts/vitest-module-cache-resolution-key'
import { localeCatalogTestAssets } from './scripts/locale-catalog-test-assets'
const nodeRuntimeInclude = process.versions.bun
? getNodeRuntimeInclude({ filters: readRunFilters() })
@@ -62,18 +62,7 @@ const transforms = {
}
} satisfies Plugin,
...(moduleCachePath ? [moduleCacheResolutionKey()] : []),
{
// Unit tests have no asset server; retain real catalog parsing through fetch(data:).
name: 'locale-catalog-test-assets',
enforce: 'pre' as const,
load(id: string) {
if (/\/i18n\/locales\/[A-Za-z-]+\.json\?url$/.test(id)) {
const catalog = readFileSync(id.slice(0, -4)).toString('base64')
return `export default ${JSON.stringify(`data:application/json;base64,${catalog}`)}`
}
return null
}
}
localeCatalogTestAssets()
],
define: {
ORCA_FEATURE_WALL_ENABLED: 'true',
+2
View File
@@ -1,5 +1,6 @@
import { fileURLToPath } from 'node:url'
import { defineConfig } from 'vitest/config'
import { localeCatalogTestAssets } from '../config/scripts/locale-catalog-test-assets'
const vitestOxcConfig = { tsconfig: false } as never
@@ -7,6 +8,7 @@ export default defineConfig({
root: import.meta.dirname,
// Tests are excluded from the app tsconfig; disable that config for OXC so test modules still transform.
oxc: vitestOxcConfig,
plugins: [localeCatalogTestAssets()],
resolve: {
// A faithful Node stand-in for the native module, rather than a mock in every test that reaches it.
alias: {