'use strict'; const assert = require('node:assert/strict'); const StorageExpiryService = require('../../../../app/service/systems/storage/expiry'); const StorageExpiryMethodService = require('../../../../app/service/systemMethods/storage/expiry'); function createServiceHarness(options = {}) { const calls = { findArgs: null, }; const Op = { and: '$and', gt: '$gt', ne: '$ne', }; const rows = options.rows || [{ get() { return { id: 11, warehouse_id: 100000, store_id: 100001, space_id: 9, stock_id: 88, batch_no: 'BATCH-1', inventory_type: 'ZP', quantity: 10, locked_quantity: 1, available_quantity: 9, production_date: '2026-04-01 00:00:00', expiry_date: '2026-04-10 00:00:00', inbound_time: '2026-04-02 00:00:00', created_at: '2026-04-02 00:00:00', updated_at: '2026-04-03 00:00:00', stock: { id: 88, item_code: 'SKU-88', goods_code: 'G-88', item_name: '测试商品', sku_property: '默认规格', stock_unit: '件', store_id: 100001, warehouse_id: 100000, is_shelf_life_mgmt: 'Y', remaining_threshold_days: 15, }, space: { id: 9, space_code: 'A-01', custom_name: '一号位', }, }; }, }]; const model = { Storage: { async findAndCountAll(args) { calls.findArgs = args; return { count: options.total ?? rows.length, rows, }; }, }, StockWarehouse: {}, LocationSpace: {}, }; const app = { Sequelize: { Op, literal: value => ({ literal: value }) }, model, formatDate(value) { return String(value).slice(0, 10); }, }; const ctx = { app, model, user: { store_id: 100000, }, logger: { info() {}, warn() {}, error() {}, }, }; return { app, ctx, model, calls, Op, }; } describe('test/app/service/systems/storage.expiry.test.js', () => { it('should query expiry list by current warehouse and shelf-life stock include', async () => { const harness = createServiceHarness(); const service = new StorageExpiryService(harness.ctx); harness.ctx.whereParsed = { dynamicConditions: [ { inventory_type: 'ZP' }, ], bucketConditions: { stock: [ { item_code: { $like: '%SKU%' } }, ], }, }; const result = await service.list({ where: { item_code: { operator: 'like', value: 'SKU', }, }, }, { page: 2, pageSize: 5, }); assert.equal(result.code, 200); assert.equal(result.message, '查询成功'); assert.equal(result.data.pagination.page, 2); assert.equal(result.data.pagination.pageSize, 5); assert.equal(result.data.list.length, 1); assert.equal(result.data.list[0].threshold_days, 15); assert.equal(result.data.list[0].remaining_days >= 0, true); assert.equal(result.data.list[0].stock.item_code, 'SKU-88'); assert.ok(harness.calls.findArgs); assert.equal(harness.calls.findArgs.include[0].as, 'stock'); assert.equal(harness.calls.findArgs.where.$and[0].warehouse_id, 100000); }); it('should return empty pagination when user has no warehouse', async () => { const harness = createServiceHarness(); harness.ctx.user.store_id = 0; const service = new StorageExpiryService(harness.ctx); const result = await service.list(); assert.equal(result.code, 400); assert.equal(result.data.list.length, 0); assert.equal(result.data.pagination.total, 0); }); it('should pass normalized where and pagination through systemMethods wrapper', async () => { const captured = { where: null, pagination: null, }; const Op = { and: '$and', gt: '$gt', ne: '$ne', like: '$like', }; const ctx = { app: { Sequelize: { Op }, }, service: { systems: { storage: { expiry: { async list(where, pagination) { captured.where = where; captured.pagination = pagination; return { code: 200, message: '查询成功', data: { list: [], pagination: { page: 1, pageSize: 20, total: 0, totalPages: 0, hasPrevious: false, hasNext: false, }, }, }; }, }, }, }, }, logger: { info() {}, warn() {}, error() {}, }, user: { store_id: 100000, }, }; const methodService = new StorageExpiryMethodService(ctx); const result = await methodService.list({ where: { item_name: { operator: 'like', value: '苹果', }, }, pagination: { page: 3, pageSize: 7, }, }); assert.equal(result.flag, 'success'); assert.equal(result.code, 200); assert.equal(captured.pagination.page, 3); assert.equal(captured.pagination.pageSize, 7); assert.equal(captured.where.item_name.operator, 'like'); }); });