2026-07-17 13:50:30 +08:00
|
|
|
const assert = require('node:assert/strict')
|
|
|
|
|
const fs = require('node:fs')
|
|
|
|
|
const os = require('node:os')
|
|
|
|
|
const path = require('node:path')
|
|
|
|
|
const test = require('node:test')
|
|
|
|
|
|
|
|
|
|
const { withXuqmConfig } = require('../metro')
|
|
|
|
|
|
2026-07-20 19:31:43 +08:00
|
|
|
const CONFIG_VECTOR =
|
|
|
|
|
'XUQM-CONFIG-V1.AAECAwQFBgcICQoLDA0ODw.EBESExQVFhcYGRob.vhdKh3AhEdeftXCdXp9KusVySkrBNAzmdaA747uf1tVWGFnAsJeCLe2mHKk0dxxez8SRTknIbk1UuRibnXY0Gbot1fmkR-jSN2ssMJO0_zQ8dUu8'
|
|
|
|
|
|
2026-07-17 13:50:30 +08:00
|
|
|
test('withXuqmConfig schedules automatic initialization before the app entry', () => {
|
|
|
|
|
const projectRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'xuqm-metro-'))
|
|
|
|
|
const configDir = path.join(projectRoot, 'src/assets/config')
|
|
|
|
|
const existingPrelude = path.join(projectRoot, 'existing-prelude.js')
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
fs.mkdirSync(configDir, { recursive: true })
|
2026-07-20 19:31:43 +08:00
|
|
|
fs.writeFileSync(path.join(configDir, 'app.xuqmconfig'), CONFIG_VECTOR, 'utf8')
|
2026-07-17 13:50:30 +08:00
|
|
|
|
|
|
|
|
const config = withXuqmConfig({
|
|
|
|
|
projectRoot,
|
|
|
|
|
resolver: {},
|
|
|
|
|
serializer: {
|
|
|
|
|
getModulesRunBeforeMainModule: () => [existingPrelude],
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const preludeModules = config.serializer.getModulesRunBeforeMainModule('index.js')
|
|
|
|
|
assert.equal(preludeModules[0], existingPrelude)
|
2026-07-17 14:14:19 +08:00
|
|
|
assert.match(preludeModules[1], /packages[\\/]common[\\/]src[\\/]internal\.ts$/)
|
2026-07-17 13:50:30 +08:00
|
|
|
|
|
|
|
|
const virtualModule = config.resolver.resolveRequest(
|
|
|
|
|
{ resolveRequest: () => assert.fail('fallback resolver should not run') },
|
|
|
|
|
'@xuqm/autoinit-config',
|
|
|
|
|
'android',
|
|
|
|
|
)
|
|
|
|
|
assert.equal(virtualModule.type, 'sourceFile')
|
2026-07-20 19:31:43 +08:00
|
|
|
const generatedSource = fs.readFileSync(virtualModule.filePath, 'utf8')
|
|
|
|
|
assert.match(generatedSource, /RESOLVED_CONFIG/)
|
|
|
|
|
assert.match(generatedSource, /"appKey":"test-app"/)
|
|
|
|
|
assert.equal(generatedSource.includes('XUQM-CONFIG-V1'), false)
|
2026-07-17 13:50:30 +08:00
|
|
|
} finally {
|
|
|
|
|
fs.rmSync(projectRoot, { recursive: true, force: true })
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('withXuqmConfig leaves common-only hosts unchanged without a config file', () => {
|
|
|
|
|
const projectRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'xuqm-metro-'))
|
|
|
|
|
const original = {
|
|
|
|
|
projectRoot,
|
|
|
|
|
serializer: {
|
|
|
|
|
getModulesRunBeforeMainModule: () => ['existing-prelude.js'],
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
assert.equal(withXuqmConfig(original), original)
|
|
|
|
|
} finally {
|
|
|
|
|
fs.rmSync(projectRoot, { recursive: true, force: true })
|
|
|
|
|
}
|
|
|
|
|
})
|