'use strict'; const assert = require('node:assert/strict'); const { Op } = require('sequelize'); const StoreWarehouseStoreService = require('../../../../../app/service/systemMethods/storeWarehouse/store'); function createContext(options = {}) { const { includeList = true, includeSign = true, includeQueryPrintQuantityLogs = true, } = options; const actionCalls = { list: [], sign: [], queryPrintQuantityLogs: [], prepareWhereContext: 0, }; const storeWarehouse = {}; if (includeList) { storeWarehouse.list = async function list(data) { actionCalls.list.push(data); data.touched = true; return { received: data, }; }; } if (includeSign) { storeWarehouse.sign = async function sign(data) { actionCalls.sign.push(data); data.touched = true; return { received: data, }; }; } if (includeQueryPrintQuantityLogs) { storeWarehouse.queryPrintQuantityLogs = async function queryPrintQuantityLogs(data) { actionCalls.queryPrintQuantityLogs.push(data); return { code: 200, data: { list: [{ id: 1 }], total: 1, page: Number((data.pagination && data.pagination.page) || 1), page_size: Number((data.pagination && data.pagination.page_size) || 20), }, }; }; } const ctx = { app: { config: {}, Sequelize: { Op, }, }, service: { systems: { storeWarehouse, }, }, }; return { actionCalls, ctx, }; } describe('test/app/service/systemMethods/storeWarehouse/store.test.js', () => { it('should copy payload before calling list', async () => { const { actionCalls, ctx } = createContext(); const service = new StoreWarehouseStoreService(ctx); const payload = { page: 1, page_size: 20, nested: { keep: true, }, }; const result = await service.list(payload); assert.equal(result.flag, 'success'); assert.equal(result.message, 'ok'); assert.equal(actionCalls.list.length, 1); assert.notStrictEqual(actionCalls.list[0], payload); assert.deepEqual(actionCalls.list[0], { page: 1, page_size: 20, nested: { keep: true, }, touched: true, }); assert.equal(payload.touched, undefined); assert.deepEqual(payload, { page: 1, page_size: 20, nested: { keep: true, }, }); }); it('should copy payload before calling sign', async () => { const { actionCalls, ctx } = createContext(); const service = new StoreWarehouseStoreService(ctx); const payload = { warehouse_id: 1001, nested: { keep: true, }, }; const result = await service.sign(payload); assert.equal(result.flag, 'success'); assert.equal(result.message, 'ok'); assert.equal(actionCalls.sign.length, 1); assert.notStrictEqual(actionCalls.sign[0], payload); assert.deepEqual(actionCalls.sign[0], { warehouse_id: 1001, nested: { keep: true, }, touched: true, }); assert.equal(payload.touched, undefined); assert.deepEqual(payload, { warehouse_id: 1001, nested: { keep: true, }, }); }); it('should keep the failure fallback for missing list method', async () => { const { actionCalls, ctx } = createContext({ includeList: false, }); const service = new StoreWarehouseStoreService(ctx); const result = await service.list({ page: 1, }); assert.equal(result.flag, 'failure'); assert.equal(result.message, '获取签约列表失败'); assert.equal(Number(result.code), 500); assert.equal(actionCalls.list.length, 0); assert.equal(actionCalls.sign.length, 0); }); it('should keep the failure fallback for missing sign method', async () => { const { actionCalls, ctx } = createContext({ includeSign: false, }); const service = new StoreWarehouseStoreService(ctx); const result = await service.sign({ warehouse_id: 1001, }); assert.equal(result.flag, 'failure'); assert.equal(result.message, '签约失败'); assert.equal(Number(result.code), 500); assert.equal(actionCalls.list.length, 0); assert.equal(actionCalls.sign.length, 0); }); it('should normalize where and call queryPrintQuantityLogs', async () => { const { actionCalls, ctx } = createContext(); const service = new StoreWarehouseStoreService(ctx); const originalPrepareWhereContext = service.prepareWhereContext; service.prepareWhereContext = function prepareWhereContext(data, options) { actionCalls.prepareWhereContext += 1; return originalPrepareWhereContext.call(this, data, options); }; const result = await service.queryPrintQuantityLogs({ store_id: 1000001, where: { change_type: 'manual_adjust', }, pagination: { page: 2, page_size: 30, }, unrelated_field: 'ignored', }); assert.equal(actionCalls.queryPrintQuantityLogs.length, 1); assert.deepEqual(actionCalls.queryPrintQuantityLogs[0], { store_id: 1000001, where: { change_type: { operator: 'eq', value: 'manual_adjust', }, }, pagination: { page: 2, page_size: 30, }, }); assert.equal(actionCalls.prepareWhereContext, 1); assert.equal(result.flag, 'success'); assert.equal(result.message, 'ok'); assert.equal(Number(result.code), 200); assert.deepEqual(result.data, { list: [{ id: 1 }], total: 1, page: 2, page_size: 30, }); }); it('should ignore top-level page fields without pagination object', async () => { const { actionCalls, ctx } = createContext(); const service = new StoreWarehouseStoreService(ctx); await service.queryPrintQuantityLogs({ store_id: 1000001, page: 9, page_size: 99, }); assert.equal(actionCalls.queryPrintQuantityLogs.length, 1); assert.deepEqual(actionCalls.queryPrintQuantityLogs[0], { store_id: 1000001, where: {}, pagination: { page: undefined, page_size: undefined, }, }); }); it('should keep the failure fallback for missing queryPrintQuantityLogs method', async () => { const { actionCalls, ctx } = createContext({ includeQueryPrintQuantityLogs: false, }); const service = new StoreWarehouseStoreService(ctx); const result = await service.queryPrintQuantityLogs({ store_id: 1000001, }); assert.equal(result.flag, 'failure'); assert.equal(result.message, '查询面单余额变更日志失败'); assert.equal(Number(result.code), 500); assert.equal(actionCalls.queryPrintQuantityLogs.length, 0); }); });