import assert from 'node:assert/strict' import { once } from 'node:events' import test from 'node:test' import { createApp } from './app.js' import { createDefaultRuntimeConfig } from './config/defaults.js' import { buildWebhookSignature } from './services/integration/webhook-signature.js' import { createStartupState } from './startup/state.js' test('HTTP health route and explicit CORS preflight', async () => { const startupState = createStartupState() startupState.core.ready = true const config = createDefaultRuntimeConfig('/tmp/order-site') config.cors.allowedOrigins = ['https://order.example.test'] const server = createApp({ startupState, isShutdownStarted: () => false, config, }).listen(0, '127.0.0.1') await once(server, 'listening') try { const address = server.address() assert.ok(address && typeof address === 'object') const baseUrl = `http://127.0.0.1:${address.port}` const health = await fetch(`${baseUrl}/health/live`) assert.equal(health.status, 200) assert.equal((await health.json()).data.status, 'alive') const preflight = await fetch(`${baseUrl}/api/v1/health`, { method: 'OPTIONS', headers: { Origin: 'https://order.example.test' }, }) assert.equal(preflight.status, 204) assert.equal(preflight.headers.get('access-control-allow-origin'), 'https://order.example.test') assert.equal(preflight.headers.get('vary'), 'Origin') } finally { server.close() await once(server, 'close') } }) test('履约状态集成接口要求密钥并接受空快照', async () => { const previousKey = process.env.PLATFORM_FULFILLMENT_INTEGRATION_KEY process.env.PLATFORM_FULFILLMENT_INTEGRATION_KEY = 'integration-test-key' const startupState = createStartupState() startupState.core.ready = true const config = createDefaultRuntimeConfig('/tmp/order-site') const server = createApp({ startupState, isShutdownStarted: () => false, config, }).listen(0, '127.0.0.1') await once(server, 'listening') try { const address = server.address() assert.ok(address && typeof address === 'object') const endpoint = `http://127.0.0.1:${address.port}/api/integration/platform-fulfillment/binding-state` const unauthorized = await fetch(endpoint, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ items: [] }), }) assert.equal(unauthorized.status, 401) const rawBody = JSON.stringify({ eventId: '8a8da35c-46c3-45a8-a914-3d7f28ee91ca', eventType: 'platform_fulfillment.binding_state_changed', items: [], }) const timestamp = String(Math.floor(Date.now() / 1000)) const accepted = await fetch(endpoint, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-Integration-Key': 'integration-test-key', 'X-Integration-Event-Id': '8a8da35c-46c3-45a8-a914-3d7f28ee91ca', 'X-Integration-Timestamp': timestamp, 'X-Integration-Signature': buildWebhookSignature( 'integration-test-key', timestamp, rawBody, ), }, body: rawBody, }) assert.equal(accepted.status, 200) assert.deepEqual((await accepted.json()).data, { received: 0, updated: 0 }) } finally { if (previousKey === undefined) { delete process.env.PLATFORM_FULFILLMENT_INTEGRATION_KEY } else { process.env.PLATFORM_FULFILLMENT_INTEGRATION_KEY = previousKey } server.close() await once(server, 'close') } }) test('售后微信公开接口无需登录且关闭时不泄露联系方式', async () => { const startupState = createStartupState() startupState.core.ready = true const config = createDefaultRuntimeConfig('/tmp/order-site') const server = createApp({ startupState, isShutdownStarted: () => false, config, }).listen(0, '127.0.0.1') await once(server, 'listening') try { const address = server.address() assert.ok(address && typeof address === 'object') const response = await fetch(`http://127.0.0.1:${address.port}/api/v1/worker/support-contact`) assert.equal(response.status, 200) assert.equal(response.headers.get('cache-control'), 'no-store') assert.deepEqual((await response.json()).data, { enabled: false, wechatId: '', wechatQrCodeImage: null, notice: '', }) } finally { server.close() await once(server, 'close') } })