- 菜单链接携带 nav_pending/nav_days,页面消费后清理 - 订单/售后/入库/加工/分拣/配送进入后切待办并按角标窗口+下单日查询 - 角标接口返回实际天数;配送角标与配送页同口径(orders 主表 LEFT JOIN) - 完成关键动作后即时刷新角标
240 lines
8.4 KiB
TypeScript
240 lines
8.4 KiB
TypeScript
import express from 'express';
|
||
import request from 'supertest';
|
||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||
import type { TenantContext } from '../src/services/tenantScope';
|
||
|
||
const mocks = vi.hoisted(() => ({
|
||
getNavBadges: vi.fn(),
|
||
getNavBadgeDays: vi.fn(),
|
||
getWorkflowCtx: vi.fn(),
|
||
runWithTenantDbContext: vi.fn(),
|
||
}));
|
||
|
||
vi.mock('../src/middleware/auth', () => ({
|
||
authMiddleware: (req: any, res: any, next: any) => {
|
||
if (!req.headers['x-test-user-id']) return res.status(401).json({ error: 'invalid_token' });
|
||
req.userId = String(req.headers['x-test-user-id']);
|
||
req.tokenScope = String(req.headers['x-test-scope'] || 'web');
|
||
next();
|
||
},
|
||
}));
|
||
vi.mock('../src/modules/workflow/context', () => ({
|
||
getWorkflowCtx: (...args: unknown[]) => mocks.getWorkflowCtx(...args),
|
||
sendWorkflowError: (res: any, error: any, fallback: string) =>
|
||
res.status(error?.status || 500).json({ error: 'server_error', error_description: error?.message || fallback }),
|
||
}));
|
||
vi.mock('../src/services/tenantScope', () => ({
|
||
readOperatingBranchCode: (req: any) => {
|
||
const raw = req.headers['x-test-operating-branch'];
|
||
return raw ? String(raw) : null;
|
||
},
|
||
}));
|
||
vi.mock('../src/services/tenantDbContext', () => ({
|
||
runWithTenantDbContext: (...args: unknown[]) => mocks.runWithTenantDbContext(...args),
|
||
}));
|
||
vi.mock('../src/services/navBadgeService', () => ({
|
||
getNavBadges: (...args: unknown[]) => mocks.getNavBadges(...args),
|
||
getNavBadgeDays: (...args: unknown[]) => mocks.getNavBadgeDays(...args),
|
||
NAV_BADGE_COUNTERS: {
|
||
'orders-list': {},
|
||
picking: {},
|
||
'purchase-list': {},
|
||
'purchase-preparations': {},
|
||
'orders-price-qty': {},
|
||
'orders-bid-invitations': {},
|
||
'inspection-list': {},
|
||
'inspection-processing': {},
|
||
delivery: {},
|
||
ops: {},
|
||
},
|
||
}));
|
||
|
||
import { mountNavBadgeWorkflowRoutes } from '../src/modules/workflow/routes/navBadgeRoutes';
|
||
|
||
const ctx = (overrides: Partial<TenantContext> = {}): TenantContext => ({
|
||
userId: 'U1',
|
||
roles: ['admin'],
|
||
permissions: ['order_manage'],
|
||
branchCode: 'BR002',
|
||
supplierCode: null,
|
||
purchaserCode: null,
|
||
isSuperAdmin: false,
|
||
isBranchAdmin: true,
|
||
isInternalStaff: true,
|
||
isSupplierAccount: false,
|
||
...overrides,
|
||
});
|
||
|
||
function buildApp() {
|
||
const app = express();
|
||
const router = express.Router();
|
||
mountNavBadgeWorkflowRoutes(router);
|
||
app.use('/api/workflow', router);
|
||
return app;
|
||
}
|
||
|
||
beforeEach(() => {
|
||
vi.clearAllMocks();
|
||
delete process.env.NAV_BADGES_ENABLED;
|
||
mocks.getWorkflowCtx.mockResolvedValue(ctx());
|
||
mocks.getNavBadges.mockResolvedValue({ 'orders-list': 3 });
|
||
mocks.getNavBadgeDays.mockResolvedValue(30);
|
||
mocks.runWithTenantDbContext.mockImplementation(async (_branch: string, fn: () => Promise<unknown>) => fn());
|
||
});
|
||
afterEach(() => {
|
||
delete process.env.NAV_BADGES_ENABLED;
|
||
});
|
||
|
||
describe('GET /api/workflow/nav-badges', () => {
|
||
it('scope 非法 ⇒ 400', async () => {
|
||
const res = await request(buildApp())
|
||
.get('/api/workflow/nav-badges?scope=week')
|
||
.set('x-test-user-id', 'U1');
|
||
expect(res.status).toBe(400);
|
||
expect(res.body.error).toBe('invalid_scope');
|
||
});
|
||
|
||
it('不传 scope 默认 days;scope=days 合法并读取配置天数', async () => {
|
||
process.env.NAV_BADGES_ENABLED = '1';
|
||
const def = await request(buildApp())
|
||
.get('/api/workflow/nav-badges')
|
||
.set('x-test-user-id', 'U1');
|
||
expect(def.status).toBe(200);
|
||
expect(def.body.scope).toBe('days');
|
||
mocks.getNavBadgeDays.mockResolvedValueOnce(7);
|
||
const days = await request(buildApp())
|
||
.get('/api/workflow/nav-badges?scope=days')
|
||
.set('x-test-user-id', 'U1');
|
||
expect(days.status).toBe(200);
|
||
expect(days.body.scope).toBe('days');
|
||
expect(mocks.getNavBadges).toHaveBeenCalledWith(
|
||
expect.objectContaining({ days: 7 }),
|
||
undefined,
|
||
false
|
||
);
|
||
});
|
||
|
||
it('超管不读分公司配置,固定 days=30', async () => {
|
||
process.env.NAV_BADGES_ENABLED = '1';
|
||
mocks.getWorkflowCtx.mockResolvedValue(
|
||
ctx({ isSuperAdmin: true, isBranchAdmin: false, roles: ['super_admin'], branchCode: null })
|
||
);
|
||
const res = await request(buildApp())
|
||
.get('/api/workflow/nav-badges')
|
||
.set('x-test-user-id', 'U1')
|
||
.set('x-test-operating-branch', 'BR002');
|
||
expect(res.status).toBe(200);
|
||
expect(mocks.getNavBadgeDays).not.toHaveBeenCalled();
|
||
expect(mocks.getNavBadges).toHaveBeenCalledWith(
|
||
expect.objectContaining({ days: 30, branchCode: 'BR002' }),
|
||
undefined,
|
||
false
|
||
);
|
||
});
|
||
|
||
it('menus 指定 key 传给 service;refresh=1 透传', async () => {
|
||
process.env.NAV_BADGES_ENABLED = '1';
|
||
const res = await request(buildApp())
|
||
.get('/api/workflow/nav-badges?scope=month&menus=orders-list,picking&refresh=1')
|
||
.set('x-test-user-id', 'U1');
|
||
expect(res.status).toBe(200);
|
||
expect(mocks.getNavBadges).toHaveBeenCalledWith(
|
||
expect.objectContaining({ branchCode: 'BR002', scope: 'month' }),
|
||
['orders-list', 'picking'],
|
||
true
|
||
);
|
||
});
|
||
|
||
it('menus 非法 key ⇒ 400 且不计数', async () => {
|
||
process.env.NAV_BADGES_ENABLED = '1';
|
||
mocks.getNavBadges.mockClear();
|
||
for (const menus of ['nope', 'orders-list,nope', '']) {
|
||
const res = await request(buildApp())
|
||
.get(`/api/workflow/nav-badges?scope=month&menus=${encodeURIComponent(menus)}`)
|
||
.set('x-test-user-id', 'U1');
|
||
expect(res.status, menus).toBe(400);
|
||
expect(res.body.error).toBe('invalid_menu_key');
|
||
}
|
||
expect(mocks.getNavBadges).not.toHaveBeenCalled();
|
||
});
|
||
|
||
it('默认 disabled=true,返回空 data 且不计数', async () => {
|
||
const res = await request(buildApp())
|
||
.get('/api/workflow/nav-badges?scope=today')
|
||
.set('x-test-user-id', 'U1');
|
||
expect(res.status).toBe(200);
|
||
expect(res.body).toEqual({
|
||
data: {},
|
||
disabled: true,
|
||
scope: 'today',
|
||
days: 30,
|
||
generated_at: expect.any(String),
|
||
});
|
||
expect(mocks.getNavBadges).not.toHaveBeenCalled();
|
||
});
|
||
|
||
it('开启后返回 {data,disabled:false},用 ctx 分公司', async () => {
|
||
process.env.NAV_BADGES_ENABLED = '1';
|
||
const res = await request(buildApp())
|
||
.get('/api/workflow/nav-badges?scope=all')
|
||
.set('x-test-user-id', 'U1');
|
||
expect(res.status).toBe(200);
|
||
expect(res.body).toEqual({
|
||
data: { 'orders-list': 3 },
|
||
disabled: false,
|
||
scope: 'all',
|
||
days: 30,
|
||
generated_at: expect.any(String),
|
||
});
|
||
expect(mocks.getNavBadges).toHaveBeenCalledWith(
|
||
expect.objectContaining({ branchCode: 'BR002', userId: 'U1', scope: 'all' }),
|
||
undefined,
|
||
false
|
||
);
|
||
expect(mocks.runWithTenantDbContext).toHaveBeenCalledWith('BR002', expect.any(Function));
|
||
});
|
||
|
||
it('超管未选分公司 ⇒ 400;选了则用 operating branch', async () => {
|
||
process.env.NAV_BADGES_ENABLED = '1';
|
||
mocks.getWorkflowCtx.mockResolvedValue(ctx({ isSuperAdmin: true, branchCode: null }));
|
||
mocks.getNavBadges.mockClear();
|
||
const bad = await request(buildApp())
|
||
.get('/api/workflow/nav-badges?scope=today')
|
||
.set('x-test-user-id', 'SU');
|
||
expect(bad.status).toBe(400);
|
||
expect(bad.body.error).toBe('branch_required');
|
||
expect(mocks.getNavBadges).not.toHaveBeenCalled();
|
||
|
||
mocks.getWorkflowCtx.mockResolvedValue(ctx({ isSuperAdmin: true, branchCode: null }));
|
||
const ok = await request(buildApp())
|
||
.get('/api/workflow/nav-badges?scope=today')
|
||
.set('x-test-user-id', 'SU')
|
||
.set('x-test-operating-branch', 'BR009');
|
||
expect(ok.status).toBe(200);
|
||
expect(mocks.runWithTenantDbContext).toHaveBeenCalledWith('BR009', expect.any(Function));
|
||
});
|
||
|
||
it('supplier / supplier_integration token 与供应商账号 ⇒ 403', async () => {
|
||
for (const scope of ['supplier', 'supplier_integration']) {
|
||
const res = await request(buildApp())
|
||
.get('/api/workflow/nav-badges?scope=today')
|
||
.set('x-test-user-id', 'S1')
|
||
.set('x-test-scope', scope);
|
||
expect(res.status, scope).toBe(403);
|
||
}
|
||
mocks.getWorkflowCtx.mockResolvedValue(ctx({ isSupplierAccount: true }));
|
||
const supplierAccount = await request(buildApp())
|
||
.get('/api/workflow/nav-badges?scope=today')
|
||
.set('x-test-user-id', 'S2');
|
||
expect(supplierAccount.status).toBe(403);
|
||
});
|
||
|
||
it('mobile token ⇒ 403', async () => {
|
||
const res = await request(buildApp())
|
||
.get('/api/workflow/nav-badges?scope=today')
|
||
.set('x-test-user-id', 'U1')
|
||
.set('x-test-scope', 'mobile');
|
||
expect(res.status).toBe(403);
|
||
});
|
||
});
|