ropClient.test.mjs 5.12 KB
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { createRopClient } from './support/ropClient.mjs'

const TEST_BASE = 'http://dj.test.glsx.com.cn/rop-dj-car-boutique'
const METHOD = 'dj.api.smartcarlife.order.createWithPay'

const jsonResponse = value => ({
  ok: true,
  status: 200,
  async text() {
    return JSON.stringify(value)
  }
})

const createHarness = ({ response = { code: 0, data: { id: 7 } }, fetchError } = {}) => {
  const signerCalls = []
  const fetchCalls = []
  const logCalls = []
  const credentials = {
    appKey: 'test-app-key',
    secret: 'test-secret',
    sign(params, secret, noSignAttr) {
      signerCalls.push({ params: { ...params }, secret, noSignAttr })
      return { ...params, sign: 'test-signature' }
    }
  }
  const fetchImpl = async (url, options) => {
    fetchCalls.push({ url, options })
    if (fetchError) throw fetchError
    return jsonResponse(response)
  }
  const logger = {
    debug(label, details) {
      logCalls.push({ label, details })
    }
  }
  return { credentials, fetchImpl, logger, signerCalls, fetchCalls, logCalls }
}

describe('signed ROP test client', () => {
  it('IT-OL-ROP-001: signs and form-encodes required common fields', async () => {
    const harness = createHarness()
    const client = createRopClient({ baseUrl: TEST_BASE, ...harness })

    const result = await client.post(METHOD, { marker: 'case-1' })

    assert.deepEqual(result, { code: 0, data: { id: 7 } })
    assert.equal(harness.signerCalls.length, 1)
    assert.deepEqual(harness.signerCalls[0].params, {
      marker: 'case-1',
      v: '1.0.0',
      method: METHOD,
      format: 'json',
      locale: 'zh_CN',
      source: 'dj-wechat',
      appKey: 'test-app-key'
    })
    assert.equal(harness.signerCalls[0].secret, 'test-secret')
    assert.deepEqual(harness.signerCalls[0].noSignAttr, [])
    assert.equal(harness.fetchCalls.length, 1)
    assert.equal(
      harness.fetchCalls[0].url,
      `${TEST_BASE}/router?method=${encodeURIComponent(METHOD)}`
    )
    assert.equal(harness.fetchCalls[0].options.method, 'POST')
    assert.equal(
      harness.fetchCalls[0].options.headers['content-type'],
      'application/x-www-form-urlencoded'
    )
    const body = new URLSearchParams(harness.fetchCalls[0].options.body)
    assert.equal(body.get('marker'), 'case-1')
    assert.equal(body.get('method'), METHOD)
    assert.equal(body.get('sign'), 'test-signature')
  })

  it('IT-OL-ROP-002: business failure remains available to workflow assertions', async () => {
    const response = { code: -1, message: 'business rejected', data: null }
    const harness = createHarness({ response })
    const client = createRopClient({ baseUrl: TEST_BASE, ...harness })

    assert.deepEqual(await client.post(METHOD, { marker: 'case-2' }), response)
  })

  it('IT-OL-ROP-003: invalid JSON error names method without leaking request data', async () => {
    const harness = createHarness()
    harness.fetchImpl = async () => ({
      ok: true,
      status: 200,
      async text() {
        return 'not-json'
      }
    })
    const client = createRopClient({ baseUrl: TEST_BASE, ...harness })

    await assert.rejects(
      client.post(METHOD, { customerPhone: 'private-value' }),
      error => {
        assert.match(error.message, new RegExp(METHOD.replaceAll('.', '\\.')))
        assert.doesNotMatch(error.message, /private-value/)
        return true
      }
    )
  })

  it('IT-OL-ROP-004: network error is sanitized', async () => {
    const harness = createHarness({
      fetchError: new Error('network failed with private-value')
    })
    const client = createRopClient({ baseUrl: TEST_BASE, ...harness })

    await assert.rejects(client.post(METHOD, { sessionId: 'private-value' }), error => {
      assert.match(error.message, new RegExp(METHOD.replaceAll('.', '\\.')))
      assert.doesNotMatch(error.message, /private-value/)
      return true
    })
  })

  it('IT-OL-ROP-005: diagnostic logs are recursively redacted', async () => {
    const harness = createHarness()
    const client = createRopClient({ baseUrl: TEST_BASE, ...harness })

    await client.post(METHOD, {
      mobile: 'private-mobile',
      nested: { sessionId: 'private-session', ordinary: 'visible' }
    })

    const serialized = JSON.stringify(harness.logCalls)
    assert.doesNotMatch(serialized, /private-mobile|private-session|test-app-key|test-signature/)
    assert.match(serialized, /visible/)
  })

  it('IT-OL-ROP-006: project adapter loads the selected test signer without exposing values', async () => {
    const { loadProjectRopCredentials } = await import(
      './support/projectRopCredentials.mjs'
    )
    const credentials = await loadProjectRopCredentials()

    assert.equal(typeof credentials.appKey, 'string')
    assert.ok(credentials.appKey.length > 0)
    assert.equal(typeof credentials.secret, 'string')
    assert.ok(credentials.secret.length > 0)
    assert.equal(typeof credentials.sign, 'function')
    const signed = credentials.sign(
      { appKey: credentials.appKey, method: 'dj.api.test', marker: 'adapter' },
      credentials.secret,
      []
    )
    assert.match(signed.sign, /^[0-9A-F]{40}$/)
  })
})