Files
frontend-miniapp/tests/unit/finalizeH5BuildPolicy.spec.ts

87 lines
2.7 KiB
TypeScript

import { createRequire } from 'node:module'
import { describe, expect, it, vi } from 'vitest'
const require = createRequire(import.meta.url)
const {
isTextBuildFile,
resolveH5BuildPolicy,
tencentMapKeyPlaceholder
} = require('../../scripts/finalize-h5-build-policy.cjs') as {
isTextBuildFile: (filePath: string) => boolean
resolveH5BuildPolicy: (options: {
args?: string[]
projectRoot: string
environment?: Record<string, string | undefined>
loadProductionEnv?: (root: string) => Record<string, string | undefined>
}) => {
allowPlaceholder: boolean
requireTencentMapKey: boolean
tencentMapKey: string
}
tencentMapKeyPlaceholder: string
}
const projectRoot = 'C:/museum-guide'
const validTencentMapKey = 'ABCDE-FGHIJ-KLMNO-PQRST-UVWXY-12345'
describe('finalize H5 build policy', () => {
it('keeps test builds isolated from production map credentials', () => {
const loadProductionEnv = vi.fn(() => ({
VITE_TENCENT_MAP_KEY: validTencentMapKey
}))
expect(resolveH5BuildPolicy({
args: ['--allow-placeholder'],
projectRoot,
environment: { VITE_TENCENT_MAP_KEY: validTencentMapKey },
loadProductionEnv
})).toEqual({
allowPlaceholder: true,
requireTencentMapKey: false,
tencentMapKey: ''
})
expect(loadProductionEnv).not.toHaveBeenCalled()
})
it('loads a well-formed production key only for the production gate', () => {
const loadProductionEnv = vi.fn(() => ({
VITE_TENCENT_MAP_KEY: validTencentMapKey
}))
expect(resolveH5BuildPolicy({
args: ['--require-tencent-map-key'],
projectRoot,
environment: {},
loadProductionEnv
})).toEqual({
allowPlaceholder: false,
requireTencentMapKey: true,
tencentMapKey: validTencentMapKey
})
expect(loadProductionEnv).toHaveBeenCalledWith(projectRoot)
})
it('rejects conflicting flags, placeholders, and malformed production keys', () => {
expect(() => resolveH5BuildPolicy({
args: ['--allow-placeholder', '--require-tencent-map-key'],
projectRoot,
environment: {}
})).toThrow('cannot require a Tencent map key')
for (const key of ['', tencentMapKeyPlaceholder, 'fake-key']) {
expect(() => resolveH5BuildPolicy({
args: ['--require-tencent-map-key'],
projectRoot,
environment: { VITE_TENCENT_MAP_KEY: key },
loadProductionEnv: () => ({})
})).toThrow('well-formed non-placeholder')
}
})
it('scans source maps as text build artifacts', () => {
expect(isTextBuildFile('assets/index.js.map')).toBe(true)
expect(isTextBuildFile('assets/index.js')).toBe(true)
expect(isTextBuildFile('assets/model.glb')).toBe(false)
})
})