|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// #6810 — `syncCollectionSchema` materializes the object's DECLARED `indexes[]`. |
| 4 | +// |
| 5 | +// This driver used to read a field-level `indexed` flag instead. That flag was |
| 6 | +// never a `FieldSchema` key (#2377 / ADR-0049 removed it, and `FieldSchema` is a |
| 7 | +// `strictObject` that rejects it by name), so its one remaining producer — the |
| 8 | +// kernel's `organization_id` injection — was stamping every registry-backed |
| 9 | +// object with a document the platform's own schema refused. The declaration |
| 10 | +// moved to `indexes[]`; this pins that the DDL outcome came with it, rather than |
| 11 | +// the fix quietly deleting an index the flag used to build. |
| 12 | +// |
| 13 | +// Driven against a fake `Db` on purpose: the `mongodb-memory-server` suite in |
| 14 | +// this package is OPT-IN (it downloads a real server binary, #5517), so a DDL |
| 15 | +// assertion parked there would not run on any ordinary CI lane — which is |
| 16 | +// exactly the lane that has to notice if this regresses. |
| 17 | + |
| 18 | +import { describe, it, expect } from 'vitest'; |
| 19 | +import type { Db } from 'mongodb'; |
| 20 | +import { syncCollectionSchema } from './mongodb-schema.js'; |
| 21 | + |
| 22 | +interface CreatedIndex { |
| 23 | + spec: Record<string, unknown>; |
| 24 | + options: Record<string, unknown>; |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * The narrow slice of `Db` `syncCollectionSchema` touches, recording every |
| 29 | + * `createIndex` call in order. Nothing is stubbed beyond that slice — the |
| 30 | + * function under test runs verbatim. |
| 31 | + */ |
| 32 | +function fakeDb(existingCollections: string[] = []) { |
| 33 | + const created: CreatedIndex[] = []; |
| 34 | + const collectionsCreated: string[] = []; |
| 35 | + const db = { |
| 36 | + listCollections: ({ name }: { name: string }) => ({ |
| 37 | + toArray: async () => (existingCollections.includes(name) ? [{ name }] : []), |
| 38 | + }), |
| 39 | + createCollection: async (name: string) => { |
| 40 | + collectionsCreated.push(name); |
| 41 | + }, |
| 42 | + collection: () => ({ |
| 43 | + createIndex: async (spec: Record<string, unknown>, options: Record<string, unknown>) => { |
| 44 | + created.push({ spec, options }); |
| 45 | + }, |
| 46 | + }), |
| 47 | + } as unknown as Db; |
| 48 | + return { db, created, collectionsCreated }; |
| 49 | +} |
| 50 | + |
| 51 | +/** Every index name the sync asked MongoDB to create, core indexes included. */ |
| 52 | +const names = (created: CreatedIndex[]) => created.map((c) => c.options.name); |
| 53 | + |
| 54 | +/** The one recorded creation for `name`, or `undefined`. */ |
| 55 | +const byName = (created: CreatedIndex[], name: string) => |
| 56 | + created.find((c) => c.options.name === name); |
| 57 | + |
| 58 | +describe('#6810 — syncCollectionSchema materializes declared indexes[]', () => { |
| 59 | + it('creates the kernel-declared tenant index, byte-identical to what the retired flag built', async () => { |
| 60 | + // THE regression pin. Before #6810 this DDL came from |
| 61 | + // `organization_id: { …, indexed: true }`; it now comes from the object's |
| 62 | + // `indexes[]`. Same collection, same key spec, same index NAME — a |
| 63 | + // re-synced deployment finds its index already present rather than |
| 64 | + // building a second one under a new name. |
| 65 | + const { db, created } = fakeDb(); |
| 66 | + await syncCollectionSchema(db, 'lead', { |
| 67 | + name: 'lead', |
| 68 | + fields: { |
| 69 | + first_name: { type: 'text' }, |
| 70 | + organization_id: { type: 'lookup' }, |
| 71 | + }, |
| 72 | + indexes: [{ fields: ['organization_id'] }], |
| 73 | + }); |
| 74 | + |
| 75 | + const tenant = byName(created, 'idx_organization_id'); |
| 76 | + expect(tenant).toBeDefined(); |
| 77 | + expect(tenant!.spec).toEqual({ organization_id: 1 }); |
| 78 | + // A plain lookup index, never a constraint. |
| 79 | + expect(tenant!.options.unique).toBeUndefined(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('declares no tenant index when the object declares none', async () => { |
| 83 | + // The single-tenant half: `multiTenant: false` now declares NOTHING rather |
| 84 | + // than a `false` flag, so absence has to stay absence here. |
| 85 | + const { db, created } = fakeDb(); |
| 86 | + await syncCollectionSchema(db, 'lead', { |
| 87 | + name: 'lead', |
| 88 | + fields: { first_name: { type: 'text' }, organization_id: { type: 'lookup' } }, |
| 89 | + }); |
| 90 | + |
| 91 | + expect(names(created)).not.toContain('idx_organization_id'); |
| 92 | + // The core set is untouched by any of this. |
| 93 | + expect(names(created)).toEqual(['idx_id_unique', 'idx_created_at', 'idx_updated_at']); |
| 94 | + }); |
| 95 | + |
| 96 | + it('honours an explicit index name and a multi-column declaration', async () => { |
| 97 | + const { db, created } = fakeDb(); |
| 98 | + await syncCollectionSchema(db, 'lead', { |
| 99 | + name: 'lead', |
| 100 | + fields: { organization_id: { type: 'lookup' }, code: { type: 'text' } }, |
| 101 | + indexes: [{ name: 'lead_scope_idx', fields: ['organization_id', 'code'] }], |
| 102 | + }); |
| 103 | + |
| 104 | + const idx = byName(created, 'lead_scope_idx'); |
| 105 | + expect(idx).toBeDefined(); |
| 106 | + // Key order follows the declaration — a compound index is order-sensitive. |
| 107 | + expect(Object.keys(idx!.spec)).toEqual(['organization_id', 'code']); |
| 108 | + }); |
| 109 | + |
| 110 | + it('materializes a declared unique index, at every scope, over the columns VERBATIM', async () => { |
| 111 | + // `'organization'` is NOT scoped up with a tenant key part here, and that is |
| 112 | + // deliberate — the same call `FieldDef.unique` documents in the source. |
| 113 | + // This driver implements no row-level tenancy and refuses to boot into a |
| 114 | + // multi-tenant deployment (#3724), so a `(tenant, field)` index would |
| 115 | + // advertise an isolation it does not deliver. |
| 116 | + for (const scope of [true, 'global', 'organization'] as const) { |
| 117 | + const { db, created } = fakeDb(); |
| 118 | + await syncCollectionSchema(db, 'lead', { |
| 119 | + name: 'lead', |
| 120 | + fields: { organization_id: { type: 'lookup' }, code: { type: 'text' } }, |
| 121 | + indexes: [{ fields: ['code'], unique: scope }], |
| 122 | + }); |
| 123 | + |
| 124 | + const idx = byName(created, 'idx_code_unique'); |
| 125 | + expect(idx, String(scope)).toBeDefined(); |
| 126 | + expect(idx!.spec).toEqual({ code: 1 }); |
| 127 | + expect(idx!.options.unique).toBe(true); |
| 128 | + expect(names(created)).not.toContain('idx_organization_id_code_unique'); |
| 129 | + } |
| 130 | + }); |
| 131 | + |
| 132 | + it('a declared index and a field-level `unique` on the same column converge on ONE index', async () => { |
| 133 | + // Why the generated name mirrors the field-level convention: two routes |
| 134 | + // asking for the same constraint must land on the same name, or MongoDB |
| 135 | + // sees two indexes over one key spec. |
| 136 | + const { db, created } = fakeDb(); |
| 137 | + await syncCollectionSchema(db, 'lead', { |
| 138 | + name: 'lead', |
| 139 | + fields: { email: { type: 'email', unique: true } }, |
| 140 | + indexes: [{ fields: ['email'], unique: true }], |
| 141 | + }); |
| 142 | + |
| 143 | + expect(names(created).filter((n) => n === 'idx_email_unique')).toHaveLength(2); |
| 144 | + const [a, b] = created.filter((c) => c.options.name === 'idx_email_unique'); |
| 145 | + expect(a.spec).toEqual(b.spec); |
| 146 | + expect(a.options).toEqual(b.options); |
| 147 | + }); |
| 148 | + |
| 149 | + it('skips a declaration with no usable fields rather than emitting empty DDL', async () => { |
| 150 | + const { db, created } = fakeDb(); |
| 151 | + await syncCollectionSchema(db, 'lead', { |
| 152 | + name: 'lead', |
| 153 | + fields: { code: { type: 'text' } }, |
| 154 | + indexes: [{ fields: [] }, { name: 'ghost' } as { name: string }], |
| 155 | + }); |
| 156 | + |
| 157 | + expect(names(created)).toEqual(['idx_id_unique', 'idx_created_at', 'idx_updated_at']); |
| 158 | + }); |
| 159 | +}); |
0 commit comments