'use strict'; const Sequelize = require('sequelize'); function createServiceContext(storeId = 100000) { return { app: { Sequelize, config: {}, }, user: { store_id: storeId, }, }; } function buildRows(count, rowFactory = () => ({})) { return Array.from({ length: count }, (_, index) => ({ id: index + 1, warehouse_id: 100000, step: index % 2 === 0 ? 1 : 2, dispatch_lane: 'm4', inspection_status: 'fin', updated_at: '2026-04-12 00:00:00', ...rowFactory(index), })); } /** * 判断模拟行数据是否匹配 where 条件。 * @param {Object|Array|number|string|null} row 待匹配数据。 * @param {Object} where Sequelize where 条件。 * @return {boolean} 是否匹配。 */ function matchesWhere(row, where) { if (!where || typeof where !== 'object') { return true; } const andConditions = where[Sequelize.Op.and]; if (Array.isArray(andConditions)) { return andConditions.every(condition => matchesWhere(row, condition)); } const orConditions = where[Sequelize.Op.or]; if (Array.isArray(orConditions)) { return orConditions.some(condition => matchesWhere(row, condition)); } return Object.entries(where).every(([ fieldName, condition ]) => { if (condition && typeof condition === 'object' && !Array.isArray(condition)) { if (Object.prototype.hasOwnProperty.call(condition, Sequelize.Op.eq)) { return row[fieldName] === condition[Sequelize.Op.eq]; } if (Object.prototype.hasOwnProperty.call(condition, Sequelize.Op.ne)) { return row[fieldName] !== condition[Sequelize.Op.ne]; } if (Object.prototype.hasOwnProperty.call(condition, Sequelize.Op.in)) { const values = condition[Sequelize.Op.in]; return Array.isArray(values) && values.includes(row[fieldName]); } if (Object.prototype.hasOwnProperty.call(condition, Sequelize.Op.notIn)) { const values = condition[Sequelize.Op.notIn]; return Array.isArray(values) && !values.includes(row[fieldName]); } if (Object.keys(condition).length > 0 || Object.getOwnPropertySymbols(condition).length > 0) { return matchesWhere(row[fieldName], condition); } } return row[fieldName] === condition; }); } /** * 创建分页查询用的模拟模型。 * @param {Array} rows 初始行数据。 * @param {Object} capture 调用记录。 * @return {Object} 模拟模型。 */ function createPagedMockModel(rows, capture = {}) { const normalizedRows = Array.isArray(rows) ? rows.map(row => ({ ...row })) : []; return { async count({ where } = {}) { capture.countWhere = where; return normalizedRows.filter(row => matchesWhere(row, where)).length; }, async findAll(options = {}) { capture.findOptions = options; const filteredRows = normalizedRows.filter(row => matchesWhere(row, options.where)); const offset = Number.parseInt(options.offset, 10) || 0; const limitValue = options.limit === undefined ? filteredRows.length : Number.parseInt(options.limit, 10); const limit = Number.isFinite(limitValue) ? Math.max(limitValue, 0) : filteredRows.length; return filteredRows.slice(offset, offset + limit).map(row => ({ ...row })); }, async update() { return [ 1 ]; }, async findOne() { const firstRow = normalizedRows[0]; return firstRow ? { ...firstRow } : null; }, }; } /** * 创建订单服务测试环境。 * @param {Function} ServiceClass 服务类构造器。 * @param {Object} [options] 测试配置。 * @return {{ ctx: Object, service: Object, captures: Array }} 测试上下文。 */ function createOrderHarness(ServiceClass, options = {}) { const ctx = createServiceContext(options.storeId || 100000); const service = new ServiceClass(ctx); const captures = []; const rowsByModule = options.rowsByModule || {}; const defaultRows = Array.isArray(options.defaultRows) ? options.defaultRows : buildRows(1); service.getOrderModel = async moduleName => { const capture = { moduleName, }; captures.push(capture); const rows = Object.prototype.hasOwnProperty.call(rowsByModule, moduleName) ? rowsByModule[moduleName] : defaultRows; return createPagedMockModel(rows, capture); }; return { ctx, service, captures, }; } module.exports = { buildRows, createOrderHarness, createServiceContext, matchesWhere, };