liveProfiles.test.mjs 2.98 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 approved profiles using environment-only credentials', async () => {
  const { resolveLiveProfile } = await loadProfiles()
  const mingming = resolveLiveProfile('mingming-dev', {
    LIVE_MINGMING_MOBILE: 'runtime-mobile-a',
    LIVE_MINGMING_CODE: 'runtime-code-a',
    LIVE_MINGMING_YQ_NAME: 'runtime-park-a',
  })
  const bobo = resolveLiveProfile('bobo-test', {
    LIVE_BOBO_MOBILE: 'runtime-mobile-b',
    LIVE_BOBO_CODE: 'runtime-code-b',
  })

  assert.equal(mingming.environment, 'development')
  assert.equal(mingming.storeName, '明明崇文4S店')
  assert.equal(mingming.yqName, 'runtime-park-a')
  assert.equal(mingming.mobile, 'runtime-mobile-a')
  assert.equal(bobo.environment, 'test')
  assert.equal(bobo.storeName, '波波的单店门店')
  assert.equal(bobo.sceneName, '精品升级')
})

test('requires explicit selection, secrets, and an independent write gate', async () => {
  const { resolveLiveProfile, assertLiveWriteAllowed } = await loadProfiles()

  assert.throws(() => resolveLiveProfile('', {}), /profile.*required/i)
  assert.throws(() => resolveLiveProfile('unknown', {}), /unknown live profile/i)
  assert.throws(
    () => resolveLiveProfile('bobo-test', {}),
    /LIVE_BOBO_MOBILE.*LIVE_BOBO_CODE/,
  )

  const profile = resolveLiveProfile('bobo-test', {
    LIVE_BOBO_MOBILE: 'runtime-mobile',
    LIVE_BOBO_CODE: 'runtime-code',
  })
  assert.throws(() => assertLiveWriteAllowed(profile, {}), /LIVE_ALLOW_WRITES=1/)
  assert.equal(
    assertLiveWriteAllowed(profile, { 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('existing order lifecycle harness accepts an explicitly write-enabled profile', async () => {
  const { loadE2EConfig } = await import('./e2e/testConfig.mjs')
  const config = loadE2EConfig({
    ORDER_E2E_PROFILE: 'mingming-dev',
    LIVE_MINGMING_MOBILE: 'runtime-mobile',
    LIVE_MINGMING_CODE: 'runtime-code',
    LIVE_MINGMING_YQ_NAME: 'runtime-park',
    LIVE_ALLOW_WRITES: '1',
    RUN_ORDER_E2E: '1',
  })

  assert.equal(config.profile, 'mingming-dev')
  assert.equal(config.baseUrl, 'http://dj.dev.glsx.com.cn/rop-dj-car-boutique')
  assert.equal(config.storeName, '明明崇文4S店')
  assert.equal(config.mobile, 'runtime-mobile')
})