来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
85 lines
3.7 KiB
TypeScript
85 lines
3.7 KiB
TypeScript
import { readFileSync } from 'node:fs';
|
|
import path from 'node:path';
|
|
import { describe, expect, it } from 'vitest';
|
|
import type { TenantContext } from '../src/services/tenantScope';
|
|
import {
|
|
ASSISTANT_BRANCH_REQUIRED,
|
|
resolveAssistantScopeBranch,
|
|
withAssistantScope,
|
|
} from '../src/mcp/assistantScope';
|
|
|
|
function ctx(partial: Partial<TenantContext>): TenantContext {
|
|
return {
|
|
userId: 'u1',
|
|
branchCode: null,
|
|
isSuperAdmin: false,
|
|
roles: [],
|
|
permissions: [],
|
|
...partial,
|
|
} as unknown as TenantContext;
|
|
}
|
|
|
|
/**
|
|
* 助手作用域分公司唯一来源(2026-09-22)。
|
|
* 背景:租户 schema 用「操作分公司」,而工具取数/LLM 配置用「档案分公司」→ 超管切分公司后必查空;
|
|
* 未选操作分公司时 middleware 不挂租户上下文 → 工具撞默认 search_path 且什么都不落库。
|
|
*/
|
|
describe('resolveAssistantScopeBranch', () => {
|
|
it('非超管:一律取档案分公司(操作分公司被忽略,行为与改动前一致)', () => {
|
|
expect(
|
|
resolveAssistantScopeBranch(ctx({ branchCode: 'BR002', roles: ['admin'] }), 'BR003')
|
|
).toBe('BR002');
|
|
expect(resolveAssistantScopeBranch(ctx({ branchCode: 'BR002' }), null)).toBe('BR002');
|
|
});
|
|
|
|
it('超管:取操作分公司', () => {
|
|
expect(resolveAssistantScopeBranch(ctx({ isSuperAdmin: true }), 'BR003')).toBe('BR003');
|
|
expect(resolveAssistantScopeBranch(ctx({ isSuperAdmin: true, branchCode: 'BR002' }), 'BR003')).toBe('BR003');
|
|
});
|
|
|
|
it('超管未选操作分公司:返回 null(调用方必须明确拒绝,不得静默降级)', () => {
|
|
expect(resolveAssistantScopeBranch(ctx({ isSuperAdmin: true, branchCode: 'BR002' }), null)).toBeNull();
|
|
expect(resolveAssistantScopeBranch(ctx({ isSuperAdmin: true }), ' ')).toBeNull();
|
|
expect(resolveAssistantScopeBranch(ctx({ isSuperAdmin: true }), undefined)).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('withAssistantScope', () => {
|
|
it('覆写成作用域分公司,且不改原对象', () => {
|
|
const base = ctx({ isSuperAdmin: true, branchCode: null });
|
|
const scoped = withAssistantScope(base, 'BR003');
|
|
expect(scoped.branchCode).toBe('BR003');
|
|
expect(base.branchCode).toBeNull();
|
|
});
|
|
|
|
it('无作用域时原样返回(不伪造分公司)', () => {
|
|
const base = ctx({ branchCode: null });
|
|
expect(withAssistantScope(base, null)).toBe(base);
|
|
});
|
|
});
|
|
|
|
describe('路由接线棘轮', () => {
|
|
const route = readFileSync(path.join(__dirname, '..', 'src', 'routes', 'orderEntryAgent.ts'), 'utf8');
|
|
|
|
it('/chat 与 /confirm 都先要求作用域分公司,缺失返回 branch_required', () => {
|
|
// /chat 的闸门在共用核心 executeAssistantChat 里(返回 {status:400,body}),/confirm 在自己的路由里;
|
|
// 两条路径都必须存在,且都必须是 400 + branch_required。
|
|
const gates = route.match(/ASSISTANT_BRANCH_REQUIRED/g) || [];
|
|
expect(gates.length).toBeGreaterThanOrEqual(2);
|
|
expect(route).toContain('return { status: 400, body: ASSISTANT_BRANCH_REQUIRED };');
|
|
expect(route).toContain('res.status(400).json(ASSISTANT_BRANCH_REQUIRED)');
|
|
expect(ASSISTANT_BRANCH_REQUIRED.error).toBe('branch_required');
|
|
});
|
|
|
|
it('宿主拿到的是作用域 ctx(工具取数 / LLM 配置同源)', () => {
|
|
expect(route).toContain('ctx: scopeCtx,');
|
|
expect(route).toContain('ctx: withAssistantScope(ctx, scopeBranch),');
|
|
});
|
|
|
|
it('聊天/确认落库复用同一作用域分公司(不再在落库处另算一遍)', () => {
|
|
// /chat 与 /confirm 各推导一次,并在落库处复用
|
|
expect((route.match(/const scopeBranch = branchFromReq\(req, ctx\);/g) || []).length).toBeGreaterThanOrEqual(2);
|
|
expect((route.match(/const branchCode = scopeBranch;/g) || []).length).toBeGreaterThanOrEqual(2);
|
|
});
|
|
});
|