|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#10576] A per-aggregation `filter` (`AggregationNodeSchema.filter`, the |
| 5 | + * contract half of #10413) reaching this compiler DIRECTLY is refused with a |
| 6 | + * wire identity — never silently dropped. |
| 7 | + * |
| 8 | + * Before #10576 this driver's `aggregate()` never read `agg.filter`: the |
| 9 | + * statement it built aggregated EVERY row and reported success, which is the |
| 10 | + * #10413 defect ("won deals" counting every opportunity) at the driver seam. |
| 11 | + * The engine now lowers filtered aggregations in memory and never pushes one |
| 12 | + * down here — so the only caller that can arrive with the key is a direct |
| 13 | + * one, and the honest answers are exactly two: compile a conditional |
| 14 | + * aggregate, or refuse loudly. This backend refuses (NOT_IMPLEMENTED/501, the |
| 15 | + * #5907 class for "declared by the spec, not compiled by this face"). |
| 16 | + * |
| 17 | + * Every case asserts `code` AND `status`, never merely "it threw" (#6144): a |
| 18 | + * bare-throw assertion is green before and after the envelope exists. |
| 19 | + */ |
| 20 | + |
| 21 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 22 | +import { SqlDriver } from './index.js'; |
| 23 | +import type { QueryAST } from '@objectstack/spec/data'; |
| 24 | + |
| 25 | +interface WireBearingError extends Error { |
| 26 | + code?: string; |
| 27 | + status?: number; |
| 28 | +} |
| 29 | + |
| 30 | +describe('[#10576] SqlDriver refuses a per-aggregation filter it cannot compile', () => { |
| 31 | + let driver: SqlDriver; |
| 32 | + |
| 33 | + beforeEach(async () => { |
| 34 | + driver = new SqlDriver({ |
| 35 | + client: 'better-sqlite3', |
| 36 | + connection: { filename: ':memory:' }, |
| 37 | + useNullAsDefault: true, |
| 38 | + }); |
| 39 | + await driver.initObjects([ |
| 40 | + { |
| 41 | + name: 'deal', |
| 42 | + fields: { |
| 43 | + id: { type: 'text', name: 'id' }, |
| 44 | + stage: { type: 'text', name: 'stage' }, |
| 45 | + amount: { type: 'number', name: 'amount' }, |
| 46 | + }, |
| 47 | + } as any, |
| 48 | + ]); |
| 49 | + await driver.create('deal', { id: '1', stage: 'closed_won', amount: 500 }); |
| 50 | + await driver.create('deal', { id: '2', stage: 'open', amount: 900 }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('refuses NOT_IMPLEMENTED/501, naming the aggregation and the engine lowering', async () => { |
| 54 | + const ast = { |
| 55 | + object: 'deal', |
| 56 | + aggregations: [ |
| 57 | + { function: 'count', alias: 'won_count', filter: { stage: 'closed_won' } }, |
| 58 | + ], |
| 59 | + } as unknown as QueryAST; |
| 60 | + |
| 61 | + let thrown: WireBearingError | undefined; |
| 62 | + try { |
| 63 | + await driver.aggregate('deal', ast); |
| 64 | + } catch (e) { |
| 65 | + thrown = e as WireBearingError; |
| 66 | + } |
| 67 | + |
| 68 | + expect(thrown).toBeDefined(); |
| 69 | + expect(thrown!.code).toBe('NOT_IMPLEMENTED'); |
| 70 | + expect(thrown!.status).toBe(501); |
| 71 | + expect(thrown!.message).toContain( |
| 72 | + 'Per-aggregation `filter` on "won_count" is not supported by this backend (driver-sql).', |
| 73 | + ); |
| 74 | + // The remedy is named: the engine's in-memory lowering serves this query. |
| 75 | + expect(thrown!.message).toContain('`engine.aggregate` lowers filtered aggregations in memory'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('control: the same aggregation WITHOUT a filter still computes (only the refusal was added)', async () => { |
| 79 | + const rows = await driver.aggregate('deal', { |
| 80 | + object: 'deal', |
| 81 | + aggregations: [{ function: 'count', alias: 'n' }], |
| 82 | + } as unknown as QueryAST); |
| 83 | + expect(rows).toEqual([{ n: 2 }]); |
| 84 | + }); |
| 85 | + |
| 86 | + it('control: an EMPTY filter object is vacuous (the where/having convention) and computes', async () => { |
| 87 | + const rows = await driver.aggregate('deal', { |
| 88 | + object: 'deal', |
| 89 | + aggregations: [{ function: 'sum', field: 'amount', alias: 'total', filter: {} }], |
| 90 | + } as unknown as QueryAST); |
| 91 | + expect(rows).toEqual([{ total: 1400 }]); |
| 92 | + }); |
| 93 | +}); |
0 commit comments