Files
SupplierSystem/server/tests/gatewayFilterOp.test.ts
T
Ubuntu deb3e72750
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库: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 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

69 lines
2.8 KiB
TypeScript

import { readFileSync } from 'node:fs';
import path from 'node:path';
import { describe, expect, it } from 'vitest';
import { buildWhere } from '../src/services/queryExecutor';
/**
* M2:网关未知过滤操作符**不得静默丢弃条件**(2026-09-24 修)。
*
* 原行为:`switch (filter.op)` 的 `default: break;` → 不认识的操作符被整体丢掉,
* 该条件不产生任何 SQL,查询从"按条件查"退化成"不过滤",返回更大范围的数据,
* 而且**不报错**(前端以为加了条件,实际看到的是范围错的数据)。
* 修后:未知 op 抛 `bad_request`,由 `routes/data.ts` 映射成 HTTP 400。
*
* 落地前预检(全仓 `op: '...'` 取值集合):调用方只用到
* `eq / in / neq / is / or / lte / lt / ilike / gte / gt / expr`,全部受支持 →
* 对现有功能无影响(本测试同时把这些 op 逐个跑通,防未来改名单向漂移)。
*/
const SRC = (rel: string) => readFileSync(path.join(__dirname, '..', rel), 'utf8');
describe('M2 网关过滤操作符', () => {
it('未知 op 必须报 bad_request(旧行为是静默丢弃条件)', () => {
expect(() =>
buildWhere('t', [{ column: 'status', op: 'contains' as never, value: 1 }])
).toThrowError(/不支持的过滤操作符/);
try {
buildWhere('t', [{ column: 'created_at', op: 'between' as never, value: [1, 2] }]);
throw new Error('应当抛错但没有');
} catch (error: any) {
expect(error.code).toBe('bad_request');
expect(String(error.message)).toContain('between');
}
});
it('全部受支持 op 仍照常生成条件(现有调用方只用到这些)', () => {
const binary: Array<[string, string]> = [
['eq', '= $1'],
['neq', '<> $1'],
['gt', '> $1'],
['gte', '>= $1'],
['lt', '< $1'],
['lte', '<= $1'],
['in', '= ANY($1)'],
['ilike', 'ILIKE $1'],
];
for (const [op, fragment] of binary) {
const { whereSql, params } = buildWhere('t', [
{ column: 'c', op: op as never, value: op === 'in' ? [1] : 1 },
]);
expect(whereSql, `op=${op}`).toContain(fragment);
expect(params.length).toBe(1);
}
expect(buildWhere('t', [{ column: 'c', op: 'is' as never, value: null }]).whereSql).toContain(
'IS NULL'
);
expect(
buildWhere('t', [{ column: 'c', op: 'empty_or_null' as never }]).whereSql
).toContain('BTRIM');
// 无 filter 时不生成 WHERE(不能因本次改动变成恒假/恒真)
expect(buildWhere('t', []).whereSql).toBe('');
});
it('契约:不得回退成静默 break', () => {
const src = SRC('src/services/queryExecutor.ts');
expect(src).toContain('不支持的过滤操作符');
expect(src).not.toContain(' default:\n break;\n }');
});
});