|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#14428] `MongoDBDriver.update()` answers a missing id with `null`, not with |
| 5 | + * a record it made up. |
| 6 | + * |
| 7 | + * # What was broken |
| 8 | + * |
| 9 | + * The door read: |
| 10 | + * |
| 11 | + * return (updated as Record[string, unknown]) || withoutUndefinedOwnKeys({ id: String(id), ...updateData }); |
| 12 | + * |
| 13 | + * `updateOne({ id })` matching nothing and `findOne({ id })` coming back `null` |
| 14 | + * still produced a row — the caller's own payload plus the `updated_at` this |
| 15 | + * driver had just stamped, under an id that names no document. Since #13878 |
| 16 | + * (PR #14434) `IDataDriver.update()` declares `Promise[Record[string, unknown] |
| 17 | + * | null]`, so "a row for an id that does not exist" is no longer a way of |
| 18 | + * satisfying the declaration: it is a value the declaration distinguishes from. |
| 19 | + * Four of six shipped implementations already answered `null`; this one and |
| 20 | + * Turso's remote face answered "updated". Maintainer ruling 2026-09-03, |
| 21 | + * posture A. |
| 22 | + * |
| 23 | + * # Why this file exists at all |
| 24 | + * |
| 25 | + * The card measured that NO landed test pinned the miss posture on this driver |
| 26 | + * — `mongodb-driver.test.ts:157,175` read `update()` results over rows that |
| 27 | + * EXIST. So this is net-new coverage, and the fabricating posture could have |
| 28 | + * come back without reddening anything. |
| 29 | + * |
| 30 | + * # Why it does not live in `mongodb-driver.test.ts` |
| 31 | + * |
| 32 | + * That suite is `describe.skipIf(!sharedMongod)` and `createTestMongod` skips |
| 33 | + * it unless `OS_TEST_MONGODB_MEMORY_SERVER_ENABLED=1` (#5517 retired the |
| 34 | + * 123 MB binary download that was ejecting unrelated PRs from the merge queue). |
| 35 | + * A pin added there would be GREEN-BY-SKIP on every CI run — a phantom pin, |
| 36 | + * which is worse than none: it reads as coverage in the file list and can |
| 37 | + * never fail. The fake `Db` below is the pattern |
| 38 | + * `mongodb-findone-options.test.ts` established and |
| 39 | + * `mongodb-own-key-undefined.test.ts` reuses: `getCollection` is |
| 40 | + * `this.db.collection(name)`, so replacing `db` observes every call the real |
| 41 | + * code path makes, with no server and no download. |
| 42 | + * |
| 43 | + * # The pins, and what each alone would miss |
| 44 | + * |
| 45 | + * - **The miss pin** is the defect: `findOne` empty ⇒ `null`. |
| 46 | + * - **The positive control** is what stops the fix from being "return `null` |
| 47 | + * always". A driver that had simply deleted the read-back would pass the |
| 48 | + * miss pin and break every update that works. |
| 49 | + * - **The no-fabrication pin** asserts the specific shape that used to come |
| 50 | + * back (the caller's fields, the stamped `updated_at`). `toBeNull()` alone |
| 51 | + * would also be satisfied by a driver that threw and was caught elsewhere; |
| 52 | + * this states what must NOT be synthesized. |
| 53 | + * - **The write-still-issued pin** holds the other half of the contract: the |
| 54 | + * `updateOne` is still sent. A "fix" that short-circuited on a miss by |
| 55 | + * reading FIRST would answer `null` correctly and quietly stop writing. |
| 56 | + * |
| 57 | + * # ⚠️ There is deliberately NO type-level pin in this file — and NOT because |
| 58 | + * # nothing would read one |
| 59 | + * |
| 60 | + * #13878's `memory-update-declared-null.test.ts` pins the declared return type |
| 61 | + * with `Equals`/`IsAny` consts. That instrument reaches this file through only |
| 62 | + * ONE program, and it is not the one whose name is on the package: |
| 63 | + * |
| 64 | + * - **This package's own `typecheck` script cannot see it.** `tsconfig.json` |
| 65 | + * here carries `"exclude": [..., "**\/*.test.ts"]` (escaped so this comment |
| 66 | + * does not terminate early), and `tsc --noEmit` reads that config. Measured, |
| 67 | + * not assumed: `tsc --noEmit --listFiles` in this package lists 0 files |
| 68 | + * ending `.test.ts` (the sibling `driver-turso`, whose tsconfig excludes |
| 69 | + * only `node_modules`/`dist`, lists 43). vitest transpiles without |
| 70 | + * typechecking, and the root `tsconfig.json` excludes `packages` entirely, |
| 71 | + * so neither of those picks it up either. That exclusion is itself a filed |
| 72 | + * defect (#14917), not a design. |
| 73 | + * - **`pnpm check:type-check-debt` DOES compile it.** The ratchet's |
| 74 | + * `--re-measure` leg generates a project that drops the test exclusion and |
| 75 | + * runs `tsc` over this package with its tests un-hidden, then compares the |
| 76 | + * error count to the frozen `TEST_DEBT['@objectstack/driver-mongodb']` entry |
| 77 | + * (10: `TS1309` x7 + `TS2550` x3). ⭐ Not theory: the first head of this |
| 78 | + * branch widened `update()`'s declaration, the three found-arm reads in |
| 79 | + * `mongodb-driver.test.ts` became `TS18047 'result' is possibly 'null'`, and |
| 80 | + * that lane went red at 13 (+3) while `pnpm typecheck` stayed green. CI |
| 81 | + * caught in this file's layer exactly what the package's own typecheck is |
| 82 | + * blind to. |
| 83 | + * |
| 84 | + * So a type pin here would not be a phantom — it would be checked, once, in a |
| 85 | + * lane that reports a break as a ledger COUNT moving rather than as a named |
| 86 | + * assertion failure, and that reports it only when someone runs the whole-repo |
| 87 | + * re-measure. The declaration is pinned by better instruments instead, both of |
| 88 | + * which run in this package's own `typecheck`: |
| 89 | + * |
| 90 | + * - **narrowing the declaration back** to `Promise[Record[string, unknown]]` |
| 91 | + * is a `tsc` error in `mongodb-driver.ts` itself — that file IS in the |
| 92 | + * program, and the body's `?? null` then returns `Record[string, unknown] | |
| 93 | + * null` from a non-null signature. `pnpm --filter @objectstack/driver-mongodb |
| 94 | + * typecheck` reds. |
| 95 | + * - **losing the contract linkage** (should `IDataDriver.update()` drop its |
| 96 | + * `| null` arm) reds the same typecheck through `implements IDataDriver`. |
| 97 | + * - **reverting the behaviour** while keeping the signature reds the runtime |
| 98 | + * pins below. |
| 99 | + * |
| 100 | + * # Reverse verification — predicted direction, then what was OBSERVED |
| 101 | + * |
| 102 | + * Predicted: restoring the `|| withoutUndefinedOwnKeys({ id: String(id), |
| 103 | + * ...updateData })` fallback reds the miss pin and the no-fabrication pin, |
| 104 | + * while the positive control and the write-still-issued pin stay GREEN — they |
| 105 | + * exercise the found arm, which the revert does not touch. |
| 106 | + * |
| 107 | + * Observed, with the mutation proved on disk (injected text counted, deleted |
| 108 | + * text absent) and the restore proved by a `git hash-object` match against the |
| 109 | + * HEAD blob: `Test Files 1 failed (1)`, `Tests 2 failed | 2 passed (4)`. The |
| 110 | + * two reds are the miss pin and the no-fabrication pin, by name. ⚠️ All FOUR |
| 111 | + * cases ran — there is no compile-time leg here, because there is no type pin |
| 112 | + * in this file to red; a prediction that the file would fail to typecheck as a |
| 113 | + * whole would have been wrong for exactly that reason. |
| 114 | + */ |
| 115 | + |
| 116 | +import { describe, it, expect } from 'vitest'; |
| 117 | + |
| 118 | +import { MongoDBDriver } from './mongodb-driver.js'; |
| 119 | + |
| 120 | +/** What the fake collection recorded, so the WRITE half stays observable. */ |
| 121 | +interface Recorded { |
| 122 | + updateOne: Array<{ filter: Record<string, unknown>; update: Record<string, unknown> }>; |
| 123 | + findOne: Array<Record<string, unknown>>; |
| 124 | +} |
| 125 | + |
| 126 | +/** |
| 127 | + * A driver wired to a recording fake `Db` — no `connect()`, no server. |
| 128 | + * |
| 129 | + * `stored` is the document `findOne` answers with; `null` models the miss (a |
| 130 | + * real `findOne` resolves `null` when nothing matches), and an object models |
| 131 | + * the row that exists. |
| 132 | + */ |
| 133 | +function makeDriver(stored: Record<string, unknown> | null) { |
| 134 | + const recorded: Recorded = { updateOne: [], findOne: [] }; |
| 135 | + const collection = { |
| 136 | + async updateOne(filter: Record<string, unknown>, update: Record<string, unknown>) { |
| 137 | + recorded.updateOne.push({ filter, update }); |
| 138 | + return { matchedCount: stored ? 1 : 0, modifiedCount: stored ? 1 : 0 }; |
| 139 | + }, |
| 140 | + async findOne(filter: Record<string, unknown>) { |
| 141 | + recorded.findOne.push(filter); |
| 142 | + return stored; |
| 143 | + }, |
| 144 | + }; |
| 145 | + const driver = new MongoDBDriver({ url: 'mongodb://127.0.0.1:1/probe' }); |
| 146 | + (driver as any).db = { collection: () => collection }; |
| 147 | + return { driver, recorded }; |
| 148 | +} |
| 149 | + |
| 150 | +describe('[#14428] MongoDBDriver.update() on a missing id', () => { |
| 151 | + it('resolves null when no document carries that id', async () => { |
| 152 | + const { driver } = makeDriver(null); |
| 153 | + |
| 154 | + const result = await driver.update('task', 'no-such-id', { title: 'edited' }); |
| 155 | + |
| 156 | + expect(result).toBeNull(); |
| 157 | + // The narrowing the declared type demands of every caller. |
| 158 | + const title = result === null ? 'absent' : result.title; |
| 159 | + expect(title).toBe('absent'); |
| 160 | + }); |
| 161 | + |
| 162 | + it('fabricates nothing — no id, no payload echo, no stamped updated_at', async () => { |
| 163 | + const { driver } = makeDriver(null); |
| 164 | + |
| 165 | + const result = await driver.update('task', 'no-such-id', { title: 'edited', owner: 'u1' }); |
| 166 | + |
| 167 | + // The exact shape the old fallback produced: `{ id, ...updateData }` with |
| 168 | + // `updated_at` stamped a moment earlier. Asserted as a NON-match against a |
| 169 | + // reconstruction of it, so the pin names the thing it forbids rather than |
| 170 | + // only the thing it wants — `toBeNull()` alone would also be satisfied by a |
| 171 | + // driver that threw and was caught somewhere up the stack. |
| 172 | + // |
| 173 | + // ⚠️ NOT written as `expect(result).not.toBeTypeOf('object')`: `typeof |
| 174 | + // null` IS `'object'` in JS, so that assertion fails on the correct value. |
| 175 | + expect(result).toBeNull(); |
| 176 | + expect(result).not.toMatchObject({ id: 'no-such-id' }); |
| 177 | + expect(Object.keys((result as Record<string, unknown> | null) ?? {})).toEqual([]); |
| 178 | + }); |
| 179 | + |
| 180 | + it('still ISSUES the write — the miss is discovered by reading back, not by refusing', async () => { |
| 181 | + const { driver, recorded } = makeDriver(null); |
| 182 | + |
| 183 | + await driver.update('task', 'no-such-id', { title: 'edited' }); |
| 184 | + |
| 185 | + expect(recorded.updateOne).toHaveLength(1); |
| 186 | + expect(recorded.updateOne[0].filter).toEqual({ id: 'no-such-id' }); |
| 187 | + expect((recorded.updateOne[0].update as any).$set.title).toBe('edited'); |
| 188 | + expect(recorded.findOne).toHaveLength(1); |
| 189 | + expect(recorded.findOne[0]).toEqual({ id: 'no-such-id' }); |
| 190 | + }); |
| 191 | + |
| 192 | + it('POSITIVE CONTROL — an id that DOES exist still returns the stored row', async () => { |
| 193 | + const stored = { id: 'task-1', title: 'edited', owner: 'u1', updated_at: new Date('2026-01-01T00:00:00Z') }; |
| 194 | + const { driver } = makeDriver(stored); |
| 195 | + |
| 196 | + const result = await driver.update('task', 'task-1', { title: 'edited' }); |
| 197 | + |
| 198 | + expect(result).not.toBeNull(); |
| 199 | + expect(result!.id).toBe('task-1'); |
| 200 | + expect(result!.title).toBe('edited'); |
| 201 | + }); |
| 202 | +}); |
0 commit comments