This commit is contained in:
2026-09-30 14:27:37 +08:00
parent 8398d5ead9
commit 244208c690
12 changed files with 62 additions and 33 deletions
+20 -6
View File
@@ -6,7 +6,7 @@ const read = path => readFile(new URL(path, import.meta.url), 'utf8')
test('H5 image copies only compiled assets and static nginx configuration', async () => {
const dockerfile = await read('../deploy/Dockerfile.h5')
const copies = dockerfile.split(/\r?\n/).filter(line => line.startsWith('COPY '))
assert.deepEqual(copies, ['COPY deploy/nginx.conf /etc/nginx/conf.d/default.conf', 'COPY dist/build/h5/ /srv/pda-h5/pda-overseas/'])
assert.deepEqual(copies, ['COPY deploy/nginx.conf /etc/nginx/conf.d/default.conf', 'COPY dist/build/h5/ /srv/pda-h5/'])
assert.match(dockerfile, /ARG NGINX_BASE_IMAGE=/)
assert.match(dockerfile, /FROM \$\{NGINX_BASE_IMAGE\}/)
assert.doesNotMatch(dockerfile, /RUN |npm|node_modules|COPY \. /)
@@ -15,14 +15,14 @@ test('H5 image copies only compiled assets and static nginx configuration', asyn
assert.ok(ignore.includes('!dist/build/h5/**'))
assert.doesNotMatch(ignore, /!\.env|!node_modules|!src/)
})
test('static server preserves the app prefix and returns genuine missing-resource errors', async () => {
test('static server serves the domain root and returns genuine missing-resource errors', async () => {
const nginx = await read('../deploy/nginx.conf')
assert.match(nginx, /listen 80;/)
assert.ok(nginx.includes('location ^~ /pda-overseas/'))
assert.ok(nginx.includes('location /'))
assert.match(nginx, /root \/srv\/pda-h5;/)
assert.match(nginx, /try_files \$uri \$uri\/ =404;/)
assert.match(nginx, /Cache-Control "no-store" always/)
assert.match(nginx, /location \/ \{ return 404; \}/)
assert.doesNotMatch(nginx, /pda-domestic|pda-overseas/)
assert.match(nginx, /location = \/healthz/)
assert.doesNotMatch(nginx, /proxy_pass|gateway-api|try_files[^;]*index\.html/)
})
@@ -36,7 +36,7 @@ test('TEST H5 deployment has a required image tag and internal-only dual health
assert.match(compose, /external: true/)
assert.match(compose, /name: firstleg-test-network/)
assert.match(compose, /expose:\s*\n\s*- "80"/)
assert.ok(compose.includes('http://127.0.0.1/healthz && wget -qO /dev/null http://127.0.0.1/pda-overseas/'))
assert.ok(compose.includes('http://127.0.0.1/healthz && wget -qO /dev/null http://127.0.0.1/'))
assert.doesNotMatch(compose, /\bports:|volumes:|API_UPSTREAM|privileged:|pid: host/)
})
@@ -53,8 +53,22 @@ test('only H5 production overrides the toolchain asset base', async () => {
process: { env: { UNI_PLATFORM: platform }, cwd: () => '/project' }
})
assert.equal(factory({ mode }).base,
platform === 'h5' && mode === 'production' ? '/pda-overseas/' : undefined,
platform === 'h5' && mode === 'production' ? '/' : undefined,
`${platform}:${mode}`)
}
}
})
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 factory = runInNewContext(source, {
defineConfig: value => value, loadEnv: () => ({ API_PROXY_TARGET: 'https://gateway.example.com' }),
uni: () => ({}), process: { env: { UNI_PLATFORM: 'h5' }, cwd: () => '/project' }
})
const proxy = factory({ mode: 'development' }).server.proxy
assert.deepEqual(Object.keys(proxy), ['/gateway-api'])
assert.equal(proxy['/gateway-api'].target, 'https://gateway.example.com')
for (const suffix of ['/auth/login', '/pda/v1/last-leg/photos/1/download?preview=1']) {
assert.equal(proxy['/gateway-api'].rewrite(`/gateway-api${suffix}`), `/api${suffix}`)
}
})
+10 -10
View File
@@ -42,7 +42,7 @@ test('H5 proxy and App API roots normalize request upload and download paths to
globalThis.uni.uploadFile = (options) => { uploads.push(options.url); options.success({ statusCode: 200, data: JSON.stringify({ code: '200', data: { fileId: '1' } }), header: {} }) }
globalThis.uni.downloadFile = (options) => { downloads.push(options.url); options.success({ statusCode: 200, tempFilePath: '/tmp/photo.jpg', header: {} }) }
session.apiBase = '/api'
session.apiBase = '/gateway-api'
await resolvePallet('PLT-1')
await overseasUpload('/api/pda/v1/last-leg/photos/files', '/tmp/photo.jpg')
await overseasDownload('/api/pda/v1/last-leg/photos/files/1/download')
@@ -53,15 +53,15 @@ test('H5 proxy and App API roots normalize request upload and download paths to
await overseasDownload('/api/pda/v1/last-leg/photos/files/1/download')
assert.deepEqual(requests, [
'/api/pda/v1/last-leg/pallet-operations/pallets/resolve',
'/gateway-api/pda/v1/last-leg/pallet-operations/pallets/resolve',
'https://overseas.example.com/api/pda/v1/last-leg/pallet-operations/pallets/resolve'
])
assert.deepEqual(uploads, [
'/api/pda/v1/last-leg/photos/files',
'/gateway-api/pda/v1/last-leg/photos/files',
'https://overseas.example.com/api/pda/v1/last-leg/photos/files'
])
assert.deepEqual(downloads, [
'/api/pda/v1/last-leg/photos/files/1/download',
'/gateway-api/pda/v1/last-leg/photos/files/1/download',
'https://overseas.example.com/api/pda/v1/last-leg/photos/files/1/download'
])
for (const url of [...requests, ...uploads, ...downloads]) assert.doesNotMatch(url, /\/api\/api\//)
@@ -175,10 +175,10 @@ test('H5 blob photo uses authenticated multipart fetch instead of uni.uploadFile
return new Response(JSON.stringify({ code: '200', data: { fileId: 'h5-1' } }), { status: 200 })
}
try {
session.apiBase = '/api'
session.apiBase = '/gateway-api'
assert.deepEqual(await overseasUpload('/api/pda/v1/last-leg/photos/files', 'blob:http://localhost/photo'), { fileId: 'h5-1' })
assert.equal(uploadFileCalls, 0)
assert.equal(uploaded.url, '/api/pda/v1/last-leg/photos/files')
assert.equal(uploaded.url, '/gateway-api/pda/v1/last-leg/photos/files')
assert.equal(uploaded.options.headers.Authorization, `Bearer ${value.accessToken}`)
assert.ok(uploaded.options.body instanceof FormData)
} finally {
@@ -200,14 +200,14 @@ test('H5 File photo is appended directly without rereading a temporary blob URL'
let calls = 0
globalThis.uni.uploadFile = (options) => {
calls += 1
assert.equal(options.url, '/api/pda/photo')
assert.equal(options.url, '/gateway-api/pda/photo')
assert.equal(options.header.Authorization, `Bearer ${value.accessToken}`)
assert.equal(options.files[0].file, selected)
assert.equal(options.files[0].name, 'file')
options.success({ statusCode: 200, data: JSON.stringify({ code: '200', data: { fileId: 'direct-file' } }), header: {} })
}
try {
session.apiBase = '/api'
session.apiBase = '/gateway-api'
assert.deepEqual(await overseasUpload('/pda/photo', selected), { fileId: 'direct-file' })
assert.equal(calls, 1)
} finally {
@@ -222,7 +222,7 @@ test('native file runtime rejects an H5-relative overseas root before upload wit
prepareSession()
const originalLocation = globalThis.location
globalThis.location = { protocol: 'file:' }
session.apiBase = '/api'
session.apiBase = '/gateway-api'
let uploadCalls = 0
globalThis.uni.uploadFile = () => { uploadCalls += 1 }
try {
@@ -267,7 +267,7 @@ test('authenticated photo download maps the public api path through the H5 overs
}
assert.equal(await overseasDownload('/api/pda/v1/last-leg/receiving-containers/current/unload-photos/files/99/download'), '/tmp/restored.jpg')
assert.equal(calls.length, 1)
assert.equal(calls[0].url, '/api/pda/v1/last-leg/receiving-containers/current/unload-photos/files/99/download')
assert.equal(calls[0].url, '/gateway-api/pda/v1/last-leg/receiving-containers/current/unload-photos/files/99/download')
assert.equal(calls[0].header.Authorization, `Bearer ${value.accessToken}`)
})
+1 -1
View File
@@ -48,7 +48,7 @@ test('login has no committed demo credentials and inbound operation implements t
assert.match(auth, /authRequest\('\/auth\/login', \{ loginId, password \}\)/)
assert.doesNotMatch(auth, /authRequest\('\/auth\/login', \{ username, password \}\)/)
assert.match(runtime, /runtimeEnvironment\.DEV|VITE_PDA_DEV_USERNAME|VITE_PDA_DEV_PASSWORD/)
assert.match(runtime, /'\/api'/)
assert.match(runtime, /'\/gateway-api'/)
assert.match(runtime, /#ifdef H5[\s\S]*?VITE_H5_API_BASE_URL/)
assert.match(runtime, /#ifdef H5[\s\S]*?VITE_H5_API_BASE_URL/)
assert.match(operation, /startScanBroadcast/)