'use strict'; const { assert } = require('egg-mock/bootstrap'); const OrderWmsService = require('../../../app/service/orderWms'); function createService(rows, pushOne) { const service = Object.create(OrderWmsService.prototype); service.app = { Sequelize: { Op: { in: Symbol('in'), or: Symbol('or'), lt: Symbol('lt'), }, }, }; service.ctx = { model: { OrderStatusQueued: { async findAll(options) { assert(options.limit === 50); return rows; }, }, }, service: { cache: { async getEnabledPrincipalIds() { return [ 1 ]; }, }, orderStatus: { markPushFailed() { return Promise.resolve(); }, }, }, logger: { error() {}, }, }; service.pushOne = pushOne; return service; } describe('test/app/service/orderWms.test.js', () => { it('should push the pending batch concurrently up to its limit', async () => { const rows = Array.from({ length: 50 }, (_, index) => ({ id: index + 1, principal_id: 1 })); let active = 0; let maxActive = 0; const service = createService(rows, async () => { active++; maxActive = Math.max(maxActive, active); await new Promise(resolve => setTimeout(resolve, 10)); active--; return { ok: true }; }); const stat = await service.processPending(); assert(stat.scanned === 50); assert(stat.pushed === 50); assert(maxActive === 50); }); it('should wait for all in-flight pushes before surfacing an error', async () => { let secondFinished = false; const rows = [ { id: 1, principal_id: 1 }, { id: 2, principal_id: 1 }, ]; const service = createService(rows, async id => { if (id === 1) throw new Error('push failed'); await new Promise(resolve => setTimeout(resolve, 10)); secondFinished = true; return { ok: true }; }); let error; try { await service.processPending(); } catch (err) { error = err; } assert(error instanceof AggregateError); assert(secondFinished); }); }); describe('test/app/service/orderWms 虚拟商品过滤', () => { function createFilterService(products) { const service = Object.create(OrderWmsService.prototype); service.app = { Sequelize: { Op: { in: Symbol('in'), or: Symbol('or') }, }, }; service.ctx = { model: { Product: { async findAll() { return products; }, }, }, }; return service; } it('按商品ID或商品编码命中虚拟商品时排除该明细', async () => { const service = createFilterService([ { id: 11, item_code: 'VIRTUAL-1', no_need_ship: 1 }, { id: 12, item_code: 'REAL-1', no_need_ship: 0 }, ]); const result = await service.filterShippableItems({ principal_id: 5 }, [ { id: 1, product_id: 11, item_code: 'VIRTUAL-1' }, { id: 2, product_id: 12, item_code: 'REAL-1' }, { id: 3, product_id: null, item_code: 'VIRTUAL-1' }, ]); assert.deepStrictEqual(result.map(item => item.id), [ 2 ]); }); it('没有虚拟商品时原样返回', async () => { const service = createFilterService([{ id: 12, item_code: 'REAL-1', no_need_ship: 0 }]); const items = [{ id: 1, product_id: 12, item_code: 'REAL-1' }]; const result = await service.filterShippableItems({ principal_id: 5 }, items); assert.strictEqual(result.length, 1); assert.strictEqual(result[0], items[0]); }); it('商品库查不到时不做过滤(交给后续校验报错)', async () => { const service = createFilterService([]); const items = [{ id: 1, product_id: 99, item_code: 'UNKNOWN' }]; const result = await service.filterShippableItems({ principal_id: 5 }, items); assert.strictEqual(result.length, 1); }); });