76 lines
2.8 KiB
TypeScript
76 lines
2.8 KiB
TypeScript
import express from 'express';
|
|
import request from 'supertest';
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
const poolQuery = vi.hoisted(() => vi.fn());
|
|
const tenantCtx = vi.hoisted(() => vi.fn());
|
|
|
|
vi.mock('../src/db', () => ({ pool: { query: poolQuery } }));
|
|
vi.mock('../src/services/tenantDbContext', () => ({
|
|
runWithTenantDbContext: (branch: string, fn: () => Promise<unknown>) => tenantCtx(branch, fn),
|
|
}));
|
|
|
|
import publicOrderOriginRoutes from '../src/routes/publicOrderOrigin';
|
|
|
|
function app() {
|
|
const instance = express();
|
|
instance.use('/api/public', publicOrderOriginRoutes);
|
|
return instance;
|
|
}
|
|
|
|
beforeEach(() => {
|
|
poolQuery.mockReset();
|
|
tenantCtx.mockReset();
|
|
tenantCtx.mockImplementation(async (_branch: string, fn: () => Promise<unknown>) => fn());
|
|
|
|
poolQuery.mockImplementation(async (sql: unknown) => {
|
|
const text = String(sql);
|
|
if (text.includes('main_system_order_import_items')) {
|
|
return {
|
|
rows: [
|
|
{ branch_code: 'BR002', our_order_code: 'ORD_API_20261020_BR002_0004', item_name: '瘦肉' },
|
|
],
|
|
};
|
|
}
|
|
if (text.includes('main_system_sku_mappings')) return { rows: [] };
|
|
if (text.includes('FROM "tenant_br002".picking_items') && text.includes('item_name')) {
|
|
return { rows: [{ sku_code: 'SKU04114073410' }] };
|
|
}
|
|
if (text.includes('FROM "tenant_br002".picking_items')) {
|
|
return { rows: [{ outbound_batches: [{ batch_no: 'STK20261020JZ2ZOW' }] }] };
|
|
}
|
|
if (text.includes('FROM "tenant_br002".inventory')) {
|
|
return { rows: [{ origin: null }] };
|
|
}
|
|
if (text.includes('FROM "tenant_br002".skus')) {
|
|
return { rows: [{ origin: '成都农贸菜市场' }] };
|
|
}
|
|
if (text.includes('information_schema.schemata')) return { rows: [] };
|
|
return { rows: [] };
|
|
});
|
|
});
|
|
|
|
describe('公开产地接口:主订单号 → 供应商订单/SKU 映射', () => {
|
|
it('映射缺失 active 时按导入行品名兜底,返回供应商商品档案产地', async () => {
|
|
const response = await request(app())
|
|
.get('/api/public/order-origin')
|
|
.query({
|
|
main_order_code: 'ORD_WEB_20261020_BR003_0004',
|
|
main_sku: 'SKU98644620619',
|
|
});
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(response.body).toEqual({ origin: '成都农贸菜市场' });
|
|
expect(tenantCtx).toHaveBeenCalledWith('BR002', expect.any(Function));
|
|
const sqls = poolQuery.mock.calls.map((call) => String(call[0]));
|
|
expect(sqls.some((sql) => sql.includes('main_system_order_import_items'))).toBe(true);
|
|
expect(sqls.some((sql) => sql.includes('item_name'))).toBe(true);
|
|
});
|
|
|
|
it('缺参返回 400,且不泄漏订单/SKU', async () => {
|
|
const response = await request(app()).get('/api/public/order-origin');
|
|
expect(response.status).toBe(400);
|
|
expect(response.body).toEqual({ error: 'invalid_request' });
|
|
});
|
|
});
|