|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #5288 — `resolveEffectiveDatasource`: the datasource an object's rows are |
| 5 | + * actually on, by NAME. |
| 6 | + * |
| 7 | + * The engine has five resolution steps (`getDriver`), and everyone who only |
| 8 | + * needed the NAME used to read `object.datasource` — step 1. That value is the |
| 9 | + * DECLARATION, and `ObjectSchema.datasource` carries `.default('default')`, so |
| 10 | + * an object placed by a `datasourceMapping` rule, by the ADR-0057 §3.6 |
| 11 | + * lifecycle split, or by its package's `defaultDatasource` still answered |
| 12 | + * `'default'` — which in `getDriver` means "no explicit binding, keep looking", |
| 13 | + * never "the primary DB". Analytics' `getObjectDatasource` probe was such a |
| 14 | + * reader, and #5033's query-time diagnostic composed from it therefore named a |
| 15 | + * database the rows are not in. |
| 16 | + * |
| 17 | + * These cases pin the accessor against `getDriver` itself: for every routing |
| 18 | + * mechanism, the NAME it answers is the name of the driver `getDriver` picks. |
| 19 | + * One resolution order, two shapes of answer. |
| 20 | + */ |
| 21 | + |
| 22 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 23 | +import { ObjectSchema } from '@objectstack/spec/data'; |
| 24 | +import { ObjectQL } from './engine.js'; |
| 25 | + |
| 26 | +/** Owning package for the probe objects — no manifest is registered for it, so |
| 27 | + * step 4 stays a no-op unless a case registers one. */ |
| 28 | +const PKG = 'com.example.probe'; |
| 29 | + |
| 30 | +function stubDriver(name: string) { |
| 31 | + return { |
| 32 | + name, |
| 33 | + version: '0.0.0', |
| 34 | + supports: {}, |
| 35 | + async connect() {}, |
| 36 | + async disconnect() {}, |
| 37 | + async checkHealth() { |
| 38 | + return true; |
| 39 | + }, |
| 40 | + async execute() { |
| 41 | + return null; |
| 42 | + }, |
| 43 | + async find() { |
| 44 | + return []; |
| 45 | + }, |
| 46 | + async findOne() { |
| 47 | + return null; |
| 48 | + }, |
| 49 | + async create(_o: string, d: Record<string, unknown>) { |
| 50 | + return d; |
| 51 | + }, |
| 52 | + async update() { |
| 53 | + return {}; |
| 54 | + }, |
| 55 | + async upsert() { |
| 56 | + return {}; |
| 57 | + }, |
| 58 | + async delete() { |
| 59 | + return true; |
| 60 | + }, |
| 61 | + async count() { |
| 62 | + return 0; |
| 63 | + }, |
| 64 | + async bulkCreate() { |
| 65 | + return []; |
| 66 | + }, |
| 67 | + async bulkUpdate() { |
| 68 | + return []; |
| 69 | + }, |
| 70 | + async bulkDelete() {}, |
| 71 | + async beginTransaction() { |
| 72 | + return {}; |
| 73 | + }, |
| 74 | + async commit() {}, |
| 75 | + async rollback() {}, |
| 76 | + } as any; |
| 77 | +} |
| 78 | + |
| 79 | +describe('resolveEffectiveDatasource — one name per routing step (#5288)', () => { |
| 80 | + let engine: ObjectQL; |
| 81 | + let primary: any; |
| 82 | + |
| 83 | + beforeEach(async () => { |
| 84 | + engine = new ObjectQL(); |
| 85 | + primary = stubDriver('memory'); |
| 86 | + engine.registerDriver(primary, true); |
| 87 | + await engine.init(); |
| 88 | + }); |
| 89 | + |
| 90 | + // ── Step 1: an explicit binding. The one step the old declared read got right ─ |
| 91 | + |
| 92 | + it('answers the explicit `datasource` — the step the declared read already had', () => { |
| 93 | + engine.registerDriver(stubDriver('warehouse')); |
| 94 | + engine.registry.registerObject({ name: 'wh_fact', datasource: 'warehouse', fields: {} }, PKG); |
| 95 | + |
| 96 | + expect(engine.resolveEffectiveDatasource('wh_fact')).toBe('warehouse'); |
| 97 | + // …and it is the driver `getDriver` picks, not a parallel opinion about it. |
| 98 | + expect(engine.getDriverForObject('wh_fact')).toBe(engine.getDriverByName('warehouse')); |
| 99 | + }); |
| 100 | + |
| 101 | + // ── Step 2: a datasourceMapping rule ──────────────────────────────────────── |
| 102 | + |
| 103 | + it('answers the mapped datasource for an object a mapping rule places (step 2)', () => { |
| 104 | + engine.registerDriver(stubDriver('archive')); |
| 105 | + engine.setDatasourceMapping([{ objectPattern: 'log_*', datasource: 'archive' }]); |
| 106 | + engine.registry.registerObject({ name: 'log_request', fields: {} }, PKG); |
| 107 | + |
| 108 | + // What the probe used to read — the declaration — says nothing at all here. |
| 109 | + expect(engine.getObject('log_request')?.datasource).toBeUndefined(); |
| 110 | + |
| 111 | + expect(engine.resolveEffectiveDatasource('log_request')).toBe('archive'); |
| 112 | + expect(engine.getDriverForObject('log_request')).toBe(engine.getDriverByName('archive')); |
| 113 | + }); |
| 114 | + |
| 115 | + // ── Step 3: the ADR-0057 §3.6 lifecycle split — #5033's own object ────────── |
| 116 | + |
| 117 | + it('answers `telemetry` for a lifecycle-classed ledger (step 3) — the #5033 case', () => { |
| 118 | + engine.registerDriver(stubDriver(ObjectQL.LIFECYCLE_DATASOURCE)); |
| 119 | + engine.registry.registerObject({ |
| 120 | + name: 'sys_audit_log', |
| 121 | + lifecycle: { class: 'audit', retention: { maxAge: '90d' } }, |
| 122 | + fields: {}, |
| 123 | + }, PKG); |
| 124 | + |
| 125 | + expect(engine.resolveEffectiveDatasource('sys_audit_log')).toBe('telemetry'); |
| 126 | + expect(engine.getDriverForObject('sys_audit_log')).toBe( |
| 127 | + engine.getDriverByName(ObjectQL.LIFECYCLE_DATASOURCE), |
| 128 | + ); |
| 129 | + }); |
| 130 | + |
| 131 | + it('is Zod-parse independent: the declared read answers `default`, this answers `telemetry`', () => { |
| 132 | + // The exact shape the defect wore in a real deployment. `ObjectSchema` gives |
| 133 | + // `datasource` a `.default('default')`, so a PARSED object carries the |
| 134 | + // string `'default'` — and the old probe reported it as if it were a |
| 135 | + // database name, for an object the engine had routed elsewhere. |
| 136 | + engine.registerDriver(stubDriver(ObjectQL.LIFECYCLE_DATASOURCE)); |
| 137 | + const parsed = ObjectSchema.parse({ |
| 138 | + name: 'sys_audit_log', |
| 139 | + label: 'Audit log', |
| 140 | + lifecycle: { class: 'audit', retention: { maxAge: '90d' } }, |
| 141 | + fields: { action: { type: 'text', label: 'Action' } }, |
| 142 | + }); |
| 143 | + expect(parsed.datasource).toBe('default'); |
| 144 | + engine.registry.registerObject(parsed, PKG); |
| 145 | + |
| 146 | + expect(engine.resolveEffectiveDatasource('sys_audit_log')).toBe('telemetry'); |
| 147 | + }); |
| 148 | + |
| 149 | + it('does NOT invent lifecycle routing when no telemetry datasource is registered', () => { |
| 150 | + // Step 3 is opt-in by the datasource's existence. Without it the ledger |
| 151 | + // really is on the default store, and saying `'telemetry'` would be the |
| 152 | + // same lie in the other direction. |
| 153 | + engine.registry.registerObject({ |
| 154 | + name: 'sys_audit_log', |
| 155 | + lifecycle: { class: 'audit', retention: { maxAge: '90d' } }, |
| 156 | + fields: {}, |
| 157 | + }, PKG); |
| 158 | + |
| 159 | + expect(engine.resolveEffectiveDatasource('sys_audit_log')).toBeUndefined(); |
| 160 | + expect(engine.getDriverForObject('sys_audit_log')).toBe(primary); |
| 161 | + }); |
| 162 | + |
| 163 | + // ── Step 4: the owning package's defaultDatasource ────────────────────────── |
| 164 | + |
| 165 | + it("answers the owning package's `defaultDatasource` (step 4)", () => { |
| 166 | + engine.registerDriver(stubDriver('billing_db')); |
| 167 | + engine.registerApp({ |
| 168 | + id: 'com.example.billing', |
| 169 | + name: 'billing', |
| 170 | + defaultDatasource: 'billing_db', |
| 171 | + objects: [{ name: 'invoice', fields: {} }], |
| 172 | + }); |
| 173 | + |
| 174 | + expect(engine.getObject('invoice')?.datasource).toBeUndefined(); |
| 175 | + expect(engine.resolveEffectiveDatasource('invoice')).toBe('billing_db'); |
| 176 | + expect(engine.getDriverForObject('invoice')).toBe(engine.getDriverByName('billing_db')); |
| 177 | + }); |
| 178 | + |
| 179 | + it('ignores a package default whose datasource has no driver, exactly as getDriver does', () => { |
| 180 | + engine.registerApp({ |
| 181 | + id: 'com.example.billing', |
| 182 | + name: 'billing', |
| 183 | + defaultDatasource: 'never_connected', |
| 184 | + objects: [{ name: 'invoice', fields: {} }], |
| 185 | + }); |
| 186 | + |
| 187 | + // Step 4 answers only when the driver exists — the rows are on the default |
| 188 | + // store, and that is what both the driver lookup and the name report. |
| 189 | + expect(engine.resolveEffectiveDatasource('invoice')).toBeUndefined(); |
| 190 | + expect(engine.getDriverForObject('invoice')).toBe(primary); |
| 191 | + }); |
| 192 | + |
| 193 | + // ── Step 5 / no routing at all: unchanged, and deliberately `undefined` ───── |
| 194 | + |
| 195 | + it('answers `undefined` for an object nothing binds — it rides the default driver', () => { |
| 196 | + // Unchanged from what the declared read answered for such an object, and |
| 197 | + // deliberate: the default driver keeps its NATURAL name (#3826, here |
| 198 | + // `memory`), so that name identifies a DRIVER, not a datasource anyone bound |
| 199 | + // this object to. Callers that want it have `getDefaultDriverName()`. |
| 200 | + engine.registry.registerObject({ name: 'biz_account', fields: {} }, PKG); |
| 201 | + |
| 202 | + expect(engine.resolveEffectiveDatasource('biz_account')).toBeUndefined(); |
| 203 | + expect(engine.getDriverForObject('biz_account')).toBe(primary); |
| 204 | + expect(engine.getDefaultDriverName()).toBe('memory'); |
| 205 | + }); |
| 206 | + |
| 207 | + it('answers `undefined` for an object this engine has never heard of', () => { |
| 208 | + expect(engine.resolveEffectiveDatasource('no_such_object')).toBeUndefined(); |
| 209 | + }); |
| 210 | + |
| 211 | + // ── Precedence and the broken-deployment case ────────────────────────────── |
| 212 | + |
| 213 | + it('keeps getDriver’s precedence: an explicit binding outranks lifecycle routing', () => { |
| 214 | + engine.registerDriver(stubDriver(ObjectQL.LIFECYCLE_DATASOURCE)); |
| 215 | + engine.registerDriver(stubDriver('special')); |
| 216 | + engine.registry.registerObject({ |
| 217 | + name: 'probe_pinned', |
| 218 | + datasource: 'special', |
| 219 | + lifecycle: { class: 'telemetry', retention: { maxAge: '14d' } }, |
| 220 | + fields: {}, |
| 221 | + }, PKG); |
| 222 | + |
| 223 | + expect(engine.resolveEffectiveDatasource('probe_pinned')).toBe('special'); |
| 224 | + }); |
| 225 | + |
| 226 | + it('keeps getDriver’s precedence: a mapping rule outranks lifecycle routing', () => { |
| 227 | + engine.registerDriver(stubDriver(ObjectQL.LIFECYCLE_DATASOURCE)); |
| 228 | + engine.registerDriver(stubDriver('archive')); |
| 229 | + engine.setDatasourceMapping([{ objectPattern: 'sys_audit_*', datasource: 'archive' }]); |
| 230 | + engine.registry.registerObject({ |
| 231 | + name: 'sys_audit_log', |
| 232 | + lifecycle: { class: 'audit', retention: { maxAge: '90d' } }, |
| 233 | + fields: {}, |
| 234 | + }, PKG); |
| 235 | + |
| 236 | + expect(engine.resolveEffectiveDatasource('sys_audit_log')).toBe('archive'); |
| 237 | + expect(engine.getDriverForObject('sys_audit_log')).toBe(engine.getDriverByName('archive')); |
| 238 | + }); |
| 239 | + |
| 240 | + it('names a binding whose driver is missing instead of throwing', () => { |
| 241 | + // A naming probe exists to be READ, including while the deployment is |
| 242 | + // broken: `getDriver` refuses to serve this object (it will not silently |
| 243 | + // write to the default store — #4462), and the name it refuses ON is |
| 244 | + // precisely what a diagnostic needs to print. |
| 245 | + engine.registry.registerObject({ name: 'wh_fact', datasource: 'warehouse', fields: {} }, PKG); |
| 246 | + |
| 247 | + expect(engine.resolveEffectiveDatasource('wh_fact')).toBe('warehouse'); |
| 248 | + expect(() => engine.getDriverForObject('wh_fact')).not.toThrow(); |
| 249 | + expect(engine.getDriverForObject('wh_fact')).toBeUndefined(); |
| 250 | + }); |
| 251 | +}); |
0 commit comments