liveProfiles.test.mjs 4.56 KB
import assert from 'node:assert/strict'
import fs from 'node:fs'
import path from 'node:path'
import test from 'node:test'
import { fileURLToPath, pathToFileURL } from 'node:url'

const projectRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..')
const profilesPath = path.join(projectRoot, 'tests/order-lifecycle/liveProfiles.mjs')

async function loadProfiles() {
  assert.equal(fs.existsSync(profilesPath), true, 'live profile resolver must exist')
  return import(pathToFileURL(profilesPath))
}

test('resolves the approved development and test profiles from runtime-only credentials', async () => {
  const { resolveLiveProfile } = await loadProfiles()
  const development = resolveLiveProfile('regional-dev-login', {
    LIVE_REGIONAL_DEV_MOBILE: 'runtime-mobile-dev',
    LIVE_REGIONAL_DEV_CODE: 'runtime-code-dev',
  })
  const functional = resolveLiveProfile('regional-test', {
    LIVE_REGIONAL_TEST_MOBILE: 'runtime-mobile-test',
    LIVE_REGIONAL_TEST_CODE: 'runtime-code-test',
  })

  assert.deepEqual(development, {
    name: 'regional-dev-login',
    environment: 'development',
    mode: 'login-only',
    baseUrl: 'http://dj.dev.glsx.com.cn/rop-dj-car-boutique',
    mobile: 'runtime-mobile-dev',
    code: 'runtime-code-dev',
  })
  assert.equal(functional.name, 'regional-test')
  assert.equal(functional.environment, 'test')
  assert.equal(functional.mode, 'functional')
  assert.equal(functional.yqName, '流程测试园区门店')
  assert.equal(functional.storeName, '波波的单店门店')
  assert.equal(functional.sceneName, '精品升级')
})

test('requires explicit selection and only the declared credential variables', async () => {
  const { resolveLiveProfile } = await loadProfiles()

  assert.throws(() => resolveLiveProfile('', {}), /profile.*required/i)
  assert.throws(() => resolveLiveProfile('unknown', {}), /unknown live profile/i)
  assert.throws(
    () => resolveLiveProfile('regional-test', {}),
    /LIVE_REGIONAL_TEST_MOBILE.*LIVE_REGIONAL_TEST_CODE/,
  )
  assert.throws(
    () =>
      resolveLiveProfile('regional-dev-login', {
        LIVE_REGIONAL_TEST_MOBILE: 'wrong-mobile',
        LIVE_REGIONAL_TEST_CODE: 'wrong-code',
      }),
    /LIVE_REGIONAL_DEV_MOBILE.*LIVE_REGIONAL_DEV_CODE/,
  )
})

test('development is login-only while test writes require an independent gate', async () => {
  const { resolveLiveProfile, assertLiveWriteAllowed } = await loadProfiles()
  const development = resolveLiveProfile('regional-dev-login', {
    LIVE_REGIONAL_DEV_MOBILE: 'runtime-mobile',
    LIVE_REGIONAL_DEV_CODE: 'runtime-code',
  })
  const functional = resolveLiveProfile('regional-test', {
    LIVE_REGIONAL_TEST_MOBILE: 'runtime-mobile',
    LIVE_REGIONAL_TEST_CODE: 'runtime-code',
  })

  assert.throws(
    () => assertLiveWriteAllowed(development, { LIVE_ALLOW_WRITES: '1' }),
    /does not allow Live writes/,
  )
  assert.throws(() => assertLiveWriteAllowed(functional, {}), /LIVE_ALLOW_WRITES=1/)
  assert.equal(
    assertLiveWriteAllowed(functional, { LIVE_ALLOW_WRITES: '1' }),
    true,
  )
})

test('profile metadata never contains credential literals', async () => {
  const { LIVE_PROFILE_DEFINITIONS } = await loadProfiles()
  const serialized = JSON.stringify(LIVE_PROFILE_DEFINITIONS)

  assert.doesNotMatch(serialized, /\b1\d{10}\b/)
  assert.doesNotMatch(serialized, /(?:mobile|code)Value/i)
})

test('order E2E accepts only the functional profile and keeps guarded legacy support', async () => {
  const { loadE2EConfig } = await import('./e2e/testConfig.mjs')
  const profile = loadE2EConfig({
    ORDER_E2E_PROFILE: 'regional-test',
    LIVE_REGIONAL_TEST_MOBILE: 'runtime-mobile',
    LIVE_REGIONAL_TEST_CODE: 'runtime-code',
    LIVE_ALLOW_WRITES: '1',
    RUN_ORDER_E2E: '1',
  })

  assert.equal(profile.profile, 'regional-test')
  assert.equal(profile.baseUrl, 'http://dj.test.glsx.com.cn/rop-dj-car-boutique')
  assert.equal(profile.storeName, '波波的单店门店')
  assert.throws(
    () =>
      loadE2EConfig({
        ORDER_E2E_PROFILE: 'regional-dev-login',
        LIVE_REGIONAL_DEV_MOBILE: 'runtime-mobile',
        LIVE_REGIONAL_DEV_CODE: 'runtime-code',
        LIVE_ALLOW_WRITES: '1',
        RUN_ORDER_E2E: '1',
      }),
    /does not allow Live writes/,
  )
  assert.throws(
    () =>
      loadE2EConfig({
        RUN_ORDER_E2E: '1',
        ORDER_E2E_BASE_URL: 'http://dj.test.glsx.com.cn/rop-dj-car-boutique',
        ORDER_E2E_MOBILE: 'runtime-mobile',
        ORDER_E2E_CODE: 'runtime-code',
        ORDER_E2E_YQ_NAME: '流程测试园区门店',
        ORDER_E2E_STORE_NAME: '波波的单店门店',
      }),
    /LIVE_ALLOW_WRITES=1/,
  )
})