|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#6212 batch C — `InMemoryDriver`'s three driver-owned query |
| 5 | + * methods take `DriverQuery`, not a type that repeats the object name. |
| 6 | + * |
| 7 | + * `distinct` / `aggregate` / `performAggregation` are NOT declared on |
| 8 | + * `IDataDriver`, so #5181's narrowing and #6075's follow-through never reached |
| 9 | + * them: their first argument was already the object name while the query type |
| 10 | + * (`QueryInput` / `QueryAST`) still *required* `object`, so a caller holding |
| 11 | + * only a `where` could not name a type for it and reached for `as any` — |
| 12 | + * losing `where`, `orderBy` and `limit` checking in the same stroke |
| 13 | + * (cloud#1053, and `$like` surviving to runtime in cloud#1030). |
| 14 | + * |
| 15 | + * **Why the pins in this file are real.** They are resolved by `tsc`, not by |
| 16 | + * vitest: reverting a signature makes the `@ts-expect-error` directives unused, |
| 17 | + * and an unused directive is itself an error, so |
| 18 | + * `pnpm --filter @objectstack/driver-memory typecheck` goes red. That works |
| 19 | + * here only because this package's `tsconfig.json` does NOT exclude the |
| 20 | + * test-file glob — it has no `TEST_DEBT` entry in |
| 21 | + * `scripts/check-type-check-coverage.mjs` and reports zero errors, which is the |
| 22 | + * measurable baseline these pins move away from. The sibling `driver-mongodb` |
| 23 | + * package DOES exclude its tests, so the identical pin written there would be |
| 24 | + * the phantom check AGENTS.md's `PINS_CHECKED` invariant warns about — it is |
| 25 | + * deliberately not written; mongodb's narrowing is held by `tsc` over its |
| 26 | + * source plus the repo-wide `check:type-check-debt` re-measure. |
| 27 | + * |
| 28 | + * The `expect()` calls only give the assertions a home vitest will run. |
| 29 | + */ |
| 30 | + |
| 31 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 32 | +import type { DriverQuery } from '@objectstack/spec/contracts'; |
| 33 | +import { InMemoryDriver } from './memory-driver.js'; |
| 34 | + |
| 35 | +/** `'dropped'` when `T` does not carry `object` at all; `never` when it does. */ |
| 36 | +type DropsObject<T> = 'object' extends keyof T ? never : 'dropped'; |
| 37 | + |
| 38 | +describe('InMemoryDriver — driver-owned query methods take DriverQuery (#6212 batch C)', () => { |
| 39 | + let driver: InMemoryDriver; |
| 40 | + const tbl = 'narrowing_probe'; |
| 41 | + |
| 42 | + beforeEach(async () => { |
| 43 | + driver = new InMemoryDriver({ persistence: false }); |
| 44 | + await driver.connect(); |
| 45 | + }); |
| 46 | + |
| 47 | + describe('signatures', () => { |
| 48 | + it('reads `object` off neither `distinct` nor the AST arm of `aggregate`', () => { |
| 49 | + // Read off the METHODS rather than off the `DriverQuery` alias. A revert |
| 50 | + // that puts `QueryInput` / `QueryAST` back on one signature while leaving |
| 51 | + // the alias imported would sail past any alias-scoped assertion; here that |
| 52 | + // slot resolves to `never` and the line goes red, naming which method. |
| 53 | + type DistinctQuery = NonNullable<Parameters<InMemoryDriver['distinct']>[2]>; |
| 54 | + // The AST arm is the non-array member of `aggregate`'s union. |
| 55 | + type AggregateArg = Parameters<InMemoryDriver['aggregate']>[1]; |
| 56 | + type AggregateAstArm = Extract<AggregateArg, { object?: unknown } | DriverQuery>; |
| 57 | + |
| 58 | + const perMethod: [DropsObject<DistinctQuery>, DropsObject<AggregateAstArm>] = [ |
| 59 | + 'dropped', |
| 60 | + 'dropped', |
| 61 | + ]; |
| 62 | + expect(perMethod).toHaveLength(2); |
| 63 | + }); |
| 64 | + |
| 65 | + it('keeps the mongo-pipeline arm of `aggregate` — BOTH arms have live producers', () => { |
| 66 | + // ⛔ Neither arm may be retired. The pipeline arm is fed by |
| 67 | + // `memory-analytics.ts` (`this.driver.aggregate(tableName, pipeline)`); |
| 68 | + // the AST arm by objectql's engine and `@objectstack/verify`'s |
| 69 | + // date-bucket parity probe. This pin fails if the union collapses. |
| 70 | + type AggregateArg = Parameters<InMemoryDriver['aggregate']>[1]; |
| 71 | + type PipelineArmKept = Record<string, unknown>[] extends AggregateArg ? 'kept' : never; |
| 72 | + const kept: PipelineArmKept = 'kept'; |
| 73 | + expect(kept).toBe('kept'); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('what the narrowing gives back', () => { |
| 78 | + it('lets `distinct` take a bare `where` — the literal that forced the casts', async () => { |
| 79 | + await driver.create(tbl, { id: '1', role: 'admin', active: true }); |
| 80 | + await driver.create(tbl, { id: '2', role: 'user', active: false }); |
| 81 | + await driver.create(tbl, { id: '3', role: 'user', active: true }); |
| 82 | + |
| 83 | + // No cast. Before the narrowing `object` was REQUIRED on `QueryInput`, so |
| 84 | + // this literal did not compile at all. |
| 85 | + const roles = await driver.distinct(tbl, 'role', { where: { active: true } }); |
| 86 | + expect(roles.sort()).toEqual(['admin', 'user']); |
| 87 | + }); |
| 88 | + |
| 89 | + it('lets `aggregate` take a bare AST literal, with `where`/`groupBy` still checked', async () => { |
| 90 | + await driver.create(tbl, { id: '1', category: 'travel', amount: 100 }); |
| 91 | + await driver.create(tbl, { id: '2', category: 'travel', amount: 50 }); |
| 92 | + await driver.create(tbl, { id: '3', category: 'meals', amount: 30 }); |
| 93 | + |
| 94 | + const rows = await driver.aggregate(tbl, { |
| 95 | + groupBy: ['category'], |
| 96 | + aggregations: [{ function: 'sum', field: 'amount', alias: 'amount' }], |
| 97 | + }); |
| 98 | + const byCat = Object.fromEntries(rows.map((r: any) => [r.category, r.amount])); |
| 99 | + expect(byCat).toEqual({ travel: 150, meals: 30 }); |
| 100 | + }); |
| 101 | + |
| 102 | + it('still runs a real MongoDB pipeline array through Mingo, unchanged', async () => { |
| 103 | + await driver.create(tbl, { id: '1', category: 'travel', amount: 100 }); |
| 104 | + await driver.create(tbl, { id: '2', category: 'meals', amount: 30 }); |
| 105 | + |
| 106 | + const rows = await driver.aggregate(tbl, [ |
| 107 | + { $match: { category: 'travel' } }, |
| 108 | + { $group: { _id: null, total: { $sum: '$amount' } } }, |
| 109 | + ]); |
| 110 | + expect((rows[0] as any).total).toBe(100); |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + // Every pin below sits on a real CALL to the narrowed method, never on a |
| 115 | + // `const x: DriverQuery = …` literal. An alias-scoped pin would stay green |
| 116 | + // through a revert of the signature — `DriverQuery` lacks `object` whatever |
| 117 | + // `distinct` declares — which is the dead-pin shape #5018/#4984 paid for. |
| 118 | + describe('what the narrowing now rejects', () => { |
| 119 | + it('refuses the redundant `object` key on a `distinct` call-site literal', async () => { |
| 120 | + // A caller can pass a typed variable through untouched… |
| 121 | + const q: DriverQuery = { where: { active: true } }; |
| 122 | + expect(await driver.distinct(tbl, 'role', q)).toEqual([]); |
| 123 | + |
| 124 | + // …but may no longer state the object name a second time. |
| 125 | + await driver.distinct(tbl, 'role', { |
| 126 | + // @ts-expect-error - 'object' does not exist in type 'DriverQuery' |
| 127 | + object: tbl, |
| 128 | + where: { active: true }, |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + it('refuses the redundant `object` key on an `aggregate` call-site literal', async () => { |
| 133 | + await driver.aggregate(tbl, { |
| 134 | + // @ts-expect-error - 'object' does not exist in type 'DriverQuery' |
| 135 | + object: tbl, |
| 136 | + aggregations: [{ function: 'count', alias: 'n' }], |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + it('restores the `orderBy` check a blanket cast switched off (#4674)', async () => { |
| 141 | + // `orderBy` is `SortNode[]` (`{ field, order }`), closed since #4721. The |
| 142 | + // `direction` spelling is `IReportService`'s vocabulary and sorted the |
| 143 | + // wrong way in silence. Before this batch the only way to hand `aggregate` |
| 144 | + // a bare AST was `as any`, which switched this check off with it. |
| 145 | + await driver.aggregate(tbl, { |
| 146 | + aggregations: [{ function: 'count', alias: 'n' }], |
| 147 | + // @ts-expect-error - spell the direction `order`, never `direction` |
| 148 | + orderBy: [{ field: 'amount', direction: 'desc' }], |
| 149 | + }); |
| 150 | + }); |
| 151 | + }); |
| 152 | +}); |
0 commit comments