来源仓库: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 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
125 lines
4.0 KiB
TypeScript
125 lines
4.0 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { applyTenantScope, type TenantContext } from '../src/services/tenantScope';
|
|
import type { QuerySpec } from '../src/services/queryExecutor';
|
|
import { generateSkuProductImage } from '../src/services/skuImageService';
|
|
|
|
const getSkuAiImageConfig = vi.fn();
|
|
|
|
vi.mock('../src/services/skuImageConfigService', async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import('../src/services/skuImageConfigService')>();
|
|
return {
|
|
...actual,
|
|
getSkuAiImageConfig: (...args: unknown[]) => getSkuAiImageConfig(...args),
|
|
};
|
|
});
|
|
|
|
function ctx(partial: Partial<TenantContext> & Pick<TenantContext, 'userId'>): TenantContext {
|
|
return {
|
|
userId: partial.userId,
|
|
roles: partial.roles || [],
|
|
permissions: partial.permissions || [],
|
|
branchCode: partial.branchCode ?? 'BR001',
|
|
supplierCode: partial.supplierCode ?? null,
|
|
purchaserCode: partial.purchaserCode ?? null,
|
|
isSuperAdmin: partial.isSuperAdmin ?? false,
|
|
isBranchAdmin: partial.isBranchAdmin ?? false,
|
|
isInternalStaff: partial.isInternalStaff ?? false,
|
|
isSupplierAccount: partial.isSupplierAccount ?? false,
|
|
};
|
|
}
|
|
|
|
const superAdmin = ctx({
|
|
userId: 'su',
|
|
isSuperAdmin: true,
|
|
roles: ['super_admin'],
|
|
branchCode: 'BR001',
|
|
});
|
|
|
|
const branchAdmin = ctx({
|
|
userId: 'ba',
|
|
isBranchAdmin: true,
|
|
roles: ['admin'],
|
|
branchCode: 'BR001',
|
|
});
|
|
|
|
describe('sku_ai_image tenantScope', () => {
|
|
it('超管 insert sku_ai_image 拒绝', () => {
|
|
const spec: QuerySpec = {
|
|
table: 'system_configs',
|
|
action: 'insert',
|
|
insertData: {
|
|
config_key: 'sku_ai_image',
|
|
config_value: { enabled: true },
|
|
branch_code: 'BR001',
|
|
},
|
|
};
|
|
expect(() => applyTenantScope(spec, superAdmin)).toThrow(/分公司管理员/);
|
|
});
|
|
|
|
it('超管按 config_key 更新 sku_ai_image 拒绝', () => {
|
|
const spec: QuerySpec = {
|
|
table: 'system_configs',
|
|
action: 'update',
|
|
filters: [{ op: 'eq', column: 'config_key', value: 'sku_ai_image' }],
|
|
updateData: { config_value: { enabled: true } },
|
|
};
|
|
expect(() => applyTenantScope(spec, superAdmin)).toThrow(/分公司管理员/);
|
|
});
|
|
|
|
it('超管按 id 更新其它配置时排除 sku_ai_image', () => {
|
|
const spec: QuerySpec = {
|
|
table: 'system_configs',
|
|
action: 'update',
|
|
filters: [{ op: 'eq', column: 'id', value: 'cfg-1' }],
|
|
updateData: { config_value: { value: 1 } },
|
|
};
|
|
const out = applyTenantScope(spec, superAdmin);
|
|
expect(out.filters).toEqual(
|
|
expect.arrayContaining([
|
|
{ op: 'eq', column: 'id', value: 'cfg-1' },
|
|
{ op: 'neq', column: 'config_key', value: 'sku_ai_image' },
|
|
])
|
|
);
|
|
});
|
|
|
|
it('分公司管理员 insert 强制本公司 branch_code', () => {
|
|
const spec: QuerySpec = {
|
|
table: 'system_configs',
|
|
action: 'insert',
|
|
insertData: {
|
|
config_key: 'sku_ai_image',
|
|
config_value: { enabled: true, auto_generate: false },
|
|
branch_code: 'OTHER',
|
|
},
|
|
};
|
|
const out = applyTenantScope(spec, branchAdmin);
|
|
expect(out.insertData).toEqual([
|
|
expect.objectContaining({
|
|
config_key: 'sku_ai_image',
|
|
branch_code: 'BR001',
|
|
}),
|
|
]);
|
|
});
|
|
});
|
|
|
|
describe('generateSkuProductImage 开关', () => {
|
|
beforeEach(() => {
|
|
getSkuAiImageConfig.mockReset();
|
|
});
|
|
|
|
it('未开启时拒绝,不调用生图', async () => {
|
|
getSkuAiImageConfig.mockResolvedValue({ enabled: false, auto_generate: false });
|
|
await expect(
|
|
generateSkuProductImage({ sku_name: '青菜', sku_code: 'SKU1', branchCode: 'BR001' })
|
|
).rejects.toThrow(/未开启商品 AI 生图/);
|
|
});
|
|
|
|
it('无分公司码视为关闭', async () => {
|
|
getSkuAiImageConfig.mockResolvedValue({ enabled: false, auto_generate: false });
|
|
await expect(
|
|
generateSkuProductImage({ sku_name: '青菜', sku_code: 'SKU1' })
|
|
).rejects.toThrow(/未开启商品 AI 生图/);
|
|
expect(getSkuAiImageConfig).toHaveBeenCalledWith(undefined);
|
|
});
|
|
});
|