'use strict'; const assert = require('node:assert/strict'); const WarehouseMethodService = require('../../../../app/service/systemMethods/stock/warehouse'); function createHarness() { const captured = { where: null, sortParams: null, callCount: 0, cacheWrites: [], }; const cacheStore = new Map(); const ctx = { app: { config: { jwt: {}, }, Sequelize: {}, async setCacheAsync(key, payload, ttl) { captured.cacheWrites.push({ key, payload, ttl }); cacheStore.set(key, payload); }, async getCacheAsync(key) { return cacheStore.get(key) || null; }, }, logger: { warn() {}, }, service: { systems: { stockStore: { async exportWarehouseStock(where, sortParams) { captured.callCount += 1; captured.where = where; captured.sortParams = sortParams; return { url: 'https://cdn.example.com/export.xlsx', fileKey: 'exports/stock/warehouse/100001/export.xlsx', }; }, }, }, }, }; const service = new WarehouseMethodService(ctx); service.prepareWhereContext = function prepareWhereContext() { return { where: { item_code: { operator: 'like', value: 'SKU', }, }, whereValue: { sort_field: 'item_code', sort_order: 'desc', }, }; }; return { service, captured, }; } describe('test/app/service/systemMethods/stock.warehouse.export.test.js', () => { it('should return submitted task and write task cache when export called', async () => { const { service, captured } = createHarness(); const result = await service.export({ sort: { sortField: 'quantity', sortOrder: 'asc', }, where: { ignored: true, }, }); assert.equal(result.flag, 'success'); assert.equal(result.message, '导出任务已提交'); assert.equal(typeof result.task_id, 'string'); assert.ok(result.task_id.length > 0); assert.equal(result.status, 'submitted'); const submittedCacheWrite = captured.cacheWrites.find(item => item?.payload?.status === 'submitted'); assert.ok(submittedCacheWrite); assert.equal(submittedCacheWrite.ttl, 1800); }); it('should return cached status and download_url in exportTaskProgress', async () => { const { service } = createHarness(); const taskId = 'task-001'; await service.setExportTaskProgress(taskId, { task_id: taskId, status: 'success', progress: 100, message: '导出任务完成', result: { cndUrl: 'https://cdn.example.com/export-from-cnd.xlsx', }, error: '', }); const result = await service.exportTaskProgress({ task_id: taskId }); assert.equal(result.flag, 'success'); assert.equal(result.message, '导出任务完成'); assert.equal(result.task_id, taskId); assert.equal(result.status, 'success'); assert.equal(result.download_url, 'https://cdn.example.com/export-from-cnd.xlsx'); }); });