diff --git a/packages/drivers/driver-mongodb/src/mongodb-schema-declared-indexes.test.ts b/packages/drivers/driver-mongodb/src/mongodb-schema-declared-indexes.test.ts index f9fb1a3ca8..a426585d4e 100644 --- a/packages/drivers/driver-mongodb/src/mongodb-schema-declared-indexes.test.ts +++ b/packages/drivers/driver-mongodb/src/mongodb-schema-declared-indexes.test.ts @@ -14,6 +14,10 @@ // this package is OPT-IN (it downloads a real server binary, #5517), so a DDL // assertion parked there would not run on any ordinary CI lane — which is // exactly the lane that has to notice if this regresses. +// +// That same reasoning is why the second suite below lives here: #12252's pin on +// the field-level lookup arm was written against the opt-in real-server suite, +// where it can go red in neither direction. import { describe, it, expect } from 'vitest'; import type { Db } from 'mongodb'; @@ -157,3 +161,74 @@ describe('#6810 — syncCollectionSchema materializes declared indexes[]', () => expect(names(created)).toEqual(['idx_id_unique', 'idx_created_at', 'idx_updated_at']); }); }); + +describe('#12252 — the field-level lookup arm, on a lane that actually runs', () => { + it('gives a canonically-spelled `lookup` NO join index, while a `user` field still gets one', async () => { + // ⚠️ [#12252] DIVERGENCE PINNED, DISPOSITION OPEN (#13222). + // + // `mongodb-schema.ts`'s field-level join-index arm gates on + // `field.reference_to` and reads no other relationship key. `reference` is + // the CANONICAL spelling — `reference_to` is a key `FieldSchema` REFUSES + // (`unrecognized_keys`) — so a lookup any author could actually publish + // reaches that arm and falls straight through it: no `idx_company_id_lookup` + // is ever created. That is the divergence, and #13222 owns whether and how + // it closes. + // + // ⛔ This records what the driver DOES, not what it SHOULD do. When #13222 + // teaches the arm to read `reference`, THIS assertion is expected to flip to + // `toContain` — deliberately, in that PR, as the signal to retire the + // divergence note here. Going red is the whole point of the line: it is what + // stops the divergence closing (or widening) in silence. + // + // TWIN PIN — edit either, edit both. The same fact is pinned against a real + // server by #13224, which corrects the `reference_to` fixture in + // `mongodb-driver.test.ts` (`describe('syncSchema')` -> 'should create + // collection and indexes') and inverts its `idx_company_id_lookup` + // assertion in place. That suite is `describe.skipIf(!sharedMongod)`, opt-in + // behind `OS_TEST_MONGODB_MEMORY_SERVER_ENABLED=1` because of #5517's ~123 MB + // binary download, so it runs on no ordinary CI lane — which is why the fact + // is asserted here too. THIS copy is the one that runs. + // + // ⚠️ The `user` half is the LOAD-BEARING control, not decoration. + // `field.type === 'user'` is the arm's unconditional disjunct, so its index + // proves the arm executed, that the harness really called the function, and + // that `idx__lookup` is still the name it builds. Without it, + // `not.toContain` would pass just as happily against a function that created + // no indexes at all, a renamed index, or a harness wired to nothing — the + // exact vacuity that makes a negative assertion worthless. + + // Bound through a variable rather than written inline: the driver's own + // `FieldDef` declares only `reference_to`, so a fresh object literal + // carrying `reference` trips TypeScript's excess-property check — on the + // very key this case exists to record the driver does not read. + const canonicalLookup = { type: 'lookup', reference: 'company' }; + + const { db, created } = fakeDb(); + await syncCollectionSchema(db, 'lead', { + name: 'lead', + fields: { + company_id: canonicalLookup, + owner_id: { type: 'user' }, + }, + }); + + // Positive control: the unconditional disjunct fired, under the name the + // negative assertion below is spelled with. + const owner = byName(created, 'idx_owner_id_lookup'); + expect(owner).toBeDefined(); + expect(owner!.spec).toEqual({ owner_id: 1 }); + + // The divergence itself. + expect(names(created)).not.toContain('idx_company_id_lookup'); + + // Exact set — closes the remaining vacuity routes in one line: a lookup + // index appearing on `company_id` under ANY other name, the `user` index + // being renamed, or the core set drifting. + expect(names(created)).toEqual([ + 'idx_id_unique', + 'idx_created_at', + 'idx_updated_at', + 'idx_owner_id_lookup', + ]); + }); +});