Commit 98836795 by 张立顺

test: cover order lifecycle API contracts

1 parent 5ba455d3
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
"test:dynamicForm": "node --test packageDynamicForm/tests/**/*.test.mjs", "test:dynamicForm": "node --test packageDynamicForm/tests/**/*.test.mjs",
"test:createOrder": "node --experimental-detect-module --test tabWorkOrder/pages/createOrder/tests/**/*.test.mjs", "test:createOrder": "node --experimental-detect-module --test tabWorkOrder/pages/createOrder/tests/**/*.test.mjs",
"test:orderLifecycle": "node --experimental-detect-module --test tests/order-lifecycle/*.test.mjs", "test:orderLifecycle": "node --experimental-detect-module --test tests/order-lifecycle/*.test.mjs",
"test:orderLifecycle:contract": "node --experimental-detect-module --test tests/order-lifecycle/apiContracts.test.mjs",
"test:quotation": "node --experimental-detect-module --test packageEquity/tests/quotation*.test.mjs packageEquity/tests/ruleFn.test.mjs", "test:quotation": "node --experimental-detect-module --test packageEquity/tests/quotation*.test.mjs packageEquity/tests/ruleFn.test.mjs",
"test:components": "node --test tests/popupBehavior.test.mjs tests/calendarMonthMode.test.mjs tests/componentsImageUrl.test.mjs tests/technicianSelect.helpers.test.mjs tests/sideSelectDataSource.test.mjs tests/storePickerConfirm.test.mjs", "test:components": "node --test tests/popupBehavior.test.mjs tests/calendarMonthMode.test.mjs tests/componentsImageUrl.test.mjs tests/technicianSelect.helpers.test.mjs tests/sideSelectDataSource.test.mjs tests/storePickerConfirm.test.mjs",
"gen:component-index": "node scripts/gen-component-page-index.mjs" "gen:component-index": "node scripts/gen-component-page-index.mjs"
......
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { loadMiniProgramApi } from './support/loadMiniProgramApi.mjs'
const contracts = [
['tabWorkOrder/api.js', 'createWithPay', 'dj.api.smartcarlife.order.createWithPay'],
['tabWorkOrder/api.js', 'createPrerecordedOrder', 'dj.api.smartcarlife.order.preliminaryOrder'],
['tabWorkOrder/api.js', 'supplementOrder', 'dj.api.smartcarlife.order.supplementOrder'],
['tabWorkOrder/api.js', 'correctionOrder', 'dj.api.smartcarlife.order.correctionOrder'],
['tabWorkOrder/api.js', 'createPayOrder', 'dj.api.smartcarlife.payOrder.create'],
['api/api.js', 'getOrderRefundStep', 'dj.api.smartcarlife.refundApplyRecord.isExist'],
['pages/tabWorkOrder/api.js', 'revokeOrder', 'dj.api.smartcarlife.order.revoke'],
['tabWorkOrder/api.js', 'showFlag', 'dj.api.smartcarlife.construction.get.showFlag'],
['tabWorkOrder/api.js', 'batchAdd', 'dj.api.smartcarlife.construction.batch.add'],
['tabRepairOrder/api.js', 'confirmConstruction', 'dj.api.smartcarlife.construction.addOrUpdate'],
['tabRepairOrder/api.js', 'outboundAPI', 'dj.api.smartcarlife.construction.outbound'],
['tabRepairOrder/api.js', 'completeConstructionOrder', 'dj.api.smartcarlife.construction.complete'],
['tabRepairOrder/api.js', 'reworkAPI', 'dj.api.smartcarlife.construction.rework']
]
const createSpy = (implementation = async () => ({ code: 0, data: null })) => {
const calls = []
return {
calls,
request: {
async post(method, payload) {
calls.push({ method, payload })
return implementation(method, payload)
}
}
}
}
describe('order lifecycle API wrapper contracts', () => {
contracts.forEach(([sourceFile, wrapper, method], index) => {
const id = String(index + 1).padStart(3, '0')
it(`IT-OL-CONTRACT-${id}: ${wrapper} forwards method and payload`, async () => {
const spy = createSpy()
const api = await loadMiniProgramApi(sourceFile, spy.request)
const payload = { marker: id }
await api[wrapper](payload)
assert.equal(spy.calls.length, 1)
assert.equal(spy.calls[0].method, method)
assert.equal(spy.calls[0].payload, payload)
})
})
it('IT-OL-CONTRACT-014: request rejection propagates', async () => {
const expected = new Error('transport unavailable')
const spy = createSpy(async () => {
throw expected
})
const api = await loadMiniProgramApi('tabWorkOrder/api.js', spy.request)
await assert.rejects(api.createWithPay({ marker: 'reject' }), error => {
assert.equal(error, expected)
return true
})
})
it('IT-OL-CONTRACT-015: statByStatus adds sceneType without dropping fields', async () => {
const spy = createSpy()
const api = await loadMiniProgramApi('pages/tabWorkOrder/api.js', spy.request)
const payload = { marker: 'status', payType: 2 }
await api.statByStatus(payload)
assert.deepEqual(spy.calls[0].payload, {
marker: 'status',
payType: 2,
sceneType: 1
})
})
it('IT-OL-CONTRACT-016: construction wrappers keep nested arrays unchanged', async () => {
const spy = createSpy()
const api = await loadMiniProgramApi('tabRepairOrder/api.js', spy.request)
const nested = [{ subOrderCode: 'SUB-1', mainTechnicianList: [{ userId: 7 }] }]
const wrappers = [
'confirmConstruction',
'outboundAPI',
'completeConstructionOrder',
'reworkAPI'
]
for (const wrapper of wrappers) {
const payload = { subOrderList: nested }
await api[wrapper](payload)
const call = spy.calls.at(-1)
assert.equal(call.payload, payload)
assert.equal(call.payload.subOrderList, nested)
}
})
})
import { readFile } from 'node:fs/promises'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
const repositoryRoot = fileURLToPath(new URL('../../../', import.meta.url))
const requestImportPattern =
/import\s+api\s+from\s+['"]~\/utils\/request\/request\.js['"];?/
const abnormalFlowImportPattern =
/import\s+\*\s+as\s+abnormalFlowApi\s+from\s+['"]\.\/abnormalFlowDeclaration\.js['"];?/
const exportsPattern = /module\.exports\s*=/
const replaceRequired = (source, pattern, replacement, label) => {
if (!pattern.test(source)) {
throw new Error(`Unable to transform mini-program API module: missing ${label}`)
}
return source.replace(pattern, replacement)
}
export async function loadMiniProgramApi(relativePath, requestSpy) {
if (!requestSpy || typeof requestSpy.post !== 'function') {
throw new TypeError('requestSpy.post must be a function')
}
const resolved = path.resolve(repositoryRoot, relativePath)
const relative = path.relative(repositoryRoot, resolved)
if (relative.startsWith('..') || path.isAbsolute(relative)) {
throw new Error(`API source must be inside repository: ${relativePath}`)
}
const spyKey = `__orderLifecycleApiSpy_${Date.now()}_${Math.random()
.toString(36)
.slice(2)}`
let source = await readFile(resolved, 'utf8')
source = replaceRequired(
source,
requestImportPattern,
`const api = globalThis[${JSON.stringify(spyKey)}]`,
'request import'
)
if (abnormalFlowImportPattern.test(source)) {
source = source.replace(
abnormalFlowImportPattern,
'const abnormalFlowApi = Object.create(null)'
)
}
source = replaceRequired(
source,
exportsPattern,
'export default',
'module.exports assignment'
)
source += `\n//# sourceURL=${relativePath.replaceAll('\\', '/')}?spy=${spyKey}\n`
globalThis[spyKey] = requestSpy
try {
const encoded = Buffer.from(source, 'utf8').toString('base64')
const module = await import(`data:text/javascript;base64,${encoded}`)
return module.default
} finally {
delete globalThis[spyKey]
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!