This commit is contained in:
2026-11-05 14:53:57 +08:00
parent 173bbe2d02
commit cc280338f1
5 changed files with 317 additions and 16 deletions
+31 -2
View File
@@ -1,7 +1,11 @@
import test from 'node:test'
import { runInNewContext } from 'node:vm'
import assert from 'node:assert/strict'
import { readFile, readdir } from 'node:fs/promises'
import { mkdtemp, readFile, readdir, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join, resolve } from 'node:path'
import { fileURLToPath, pathToFileURL } from 'node:url'
import { loadEnv } from 'vite'
const read = path => readFile(new URL(path, import.meta.url), 'utf8')
test('PDA H5 has no container publishing entry', async () => {
const files = await readdir(new URL('../deploy/', import.meta.url))
@@ -28,10 +32,12 @@ test('host nginx serves uploaded files and proxies only the API prefix', async (
test('only H5 production overrides the toolchain asset base', async () => {
const source = (await read('../vite.config.js'))
.replace(/^import .*$/gm, '')
.replaceAll('import.meta.url', 'configUrl')
.replace('export default ', '')
for (const platform of ['h5', 'app', 'app-plus', 'app-harmony', undefined]) {
for (const mode of ['production', 'development']) {
const factory = runInNewContext(source, {
URL, fileURLToPath, configUrl: new URL('../vite.config.js', import.meta.url).href,
defineConfig: value => value,
loadEnv: () => ({}),
uni: () => ({}),
@@ -45,8 +51,10 @@ test('only H5 production overrides the toolchain asset base', async () => {
})
test('H5 proxy uses the shared gateway prefix and preserves API suffixes', async () => {
const source = (await read('../vite.config.js')).replace(/^import .*$/gm, '').replace('export default ', '')
const source = (await read('../vite.config.js')).replace(/^import .*$/gm, '')
.replaceAll('import.meta.url', 'configUrl').replace('export default ', '')
const factory = runInNewContext(source, {
URL, fileURLToPath, configUrl: new URL('../vite.config.js', import.meta.url).href,
defineConfig: value => value, loadEnv: () => ({ API_PROXY_TARGET: 'https://gateway.example.com' }),
uni: () => ({}), process: { env: { UNI_PLATFORM: 'h5' }, cwd: () => '/project' }
})
@@ -57,3 +65,24 @@ test('H5 proxy uses the shared gateway prefix and preserves API suffixes', async
assert.equal(proxy['/gateway-api'].rewrite(`/gateway-api${suffix}`), `/api${suffix}`)
}
})
test('App update configuration loads from the project when HBuilderX starts elsewhere', async t => {
const fixturePrefix = join(tmpdir(), 'overseas-pda-env-')
const fixtureDir = await mkdtemp(fixturePrefix)
assert.ok(resolve(fixtureDir).startsWith(resolve(fixturePrefix)))
t.after(() => rm(fixtureDir, { recursive: true, force: true }))
const endpoint = 'https://updates.example.test/pda/overseas/latest'
await writeFile(join(fixtureDir, '.env.local'), `VITE_PDA_UPDATE_URL=${endpoint}\n`)
const source = (await read('../vite.config.js')).replace(/^import .*$/gm, '')
.replaceAll('import.meta.url', 'configUrl').replace('export default ', '')
const factory = runInNewContext(source, {
URL, fileURLToPath, configUrl: pathToFileURL(join(fixtureDir, 'vite.config.js')).href,
defineConfig: value => value, loadEnv, uni: () => ({}),
process: { env: { UNI_PLATFORM: 'app' }, cwd: () => join(fixtureDir, 'HBuilderX') }
})
for (const mode of ['development', 'production']) {
const config = factory({ mode })
assert.equal(JSON.parse(config.define['import.meta.env.VITE_PDA_UPDATE_URL']), endpoint)
assert.equal(resolve(config.envDir), resolve(fixtureDir))
}
})