20260917212907

This commit is contained in:
oneao committed 2026-09-17 21:29:08 +08:00
1 parent 6fa3d8535e
commit 5cbf745bbf
115 files changed
+4104 -85

No files matched your search

@@ -1,5 +1,6 @@
import { describe, expect, it } from 'vitest'
import {
advancedQueryFromAst,
astToSql,
buildAdvancedAst,
buildQuickAst,
@@ -148,3 +149,89 @@ describe('查询条件 AST 构造', () => {
expect(emptyConditionValue({ b_type: 'select' }, 'eq')).toBeUndefined()
})
})
describe('高级条件 AST → 界面状态(advancedQueryFromAst)', () => {
let seq = 0
const idFactory = () => `n${(seq += 1)}`
it('空 AST / 空组都给空结构且不降级', () => {
expect(advancedQueryFromAst(null, { idFactory })).toEqual({
nodes: [],
groups: [],
degraded: false,
})
expect(advancedQueryFromAst({ type: 'group', children: [] }, { idFactory })).toEqual({
nodes: [],
groups: [],
degraded: false,
})
})
it('单层「行 + 组」往返:还原后再构造语义等价', () => {
const ast = buildAdvancedAst({
nodes: [
{ join: 'and', field: 'name', operator: 'like', value: 'a' },
{ join: 'or', field: 'name', operator: 'like', value: 'b' },
],
groups: [
{
join: 'and',
nodes: [
{ join: 'and', field: 'amount', operator: 'gt', value: 100 },
{ join: 'or', field: 'status', operator: 'eq', value: 'done' },
],
},
],
})
const state = advancedQueryFromAst(ast, { idFactory })
expect(state.degraded).toBe(false)
expect(state.nodes.map((node) => [node.field, node.join])).toEqual([
['name', 'and'],
['name', 'or'],
])
expect(state.groups).toHaveLength(1)
expect(state.groups[0].nodes.map((node) => [node.field, node.join])).toEqual([
['amount', 'and'],
['status', 'or'],
])
expect(astToSql(buildAdvancedAst(state), fields)).toBe(astToSql(ast, fields))
})
it('组内再嵌套组:拍平成单层并置 degraded(根层的组不算降级)', () => {
const ast = {
type: 'group',
children: [
{ type: 'condition', field: 'name', operator: 'like', value: 'a', join: 'and' },
{
type: 'group',
join: 'or',
children: [
{ type: 'condition', field: 'amount', operator: 'gt', value: 1, join: 'and' },
{
type: 'group',
join: 'and',
children: [{ type: 'condition', field: 'amount', operator: 'lt', value: 9 }],
},
],
},
],
}
const state = advancedQueryFromAst(ast, { idFactory })
expect(state.degraded).toBe(true)
expect(state.nodes.map((node) => node.field)).toEqual(['name'])
expect(state.groups).toHaveLength(1)
expect(state.groups[0].nodes.map((node) => node.field)).toEqual(['amount', 'amount'])
expect(state.groups[0].join).toBe('or')
})
it('单条件 AST(非 group)也能还原成一行', () => {
const state = advancedQueryFromAst(
{ type: 'condition', field: 'status', operator: 'eq', value: 'draft' },
{ idFactory },
)
expect(state.nodes).toHaveLength(1)
expect(state.nodes[0]).toMatchObject({ field: 'status', operator: 'eq', value: 'draft' })
})
})