|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#27702] A registered object whose table is missing is answered the same |
| 5 | + * classified error on its insert as on its read — never `OBJECT_NOT_FOUND`. |
| 6 | + * |
| 7 | + * ## What was measured |
| 8 | + * |
| 9 | + * The object is registered after the DDL ran, so the engine resolves it and |
| 10 | + * the driver addresses a table that was never created — the card's setup (a |
| 11 | + * code object bound to a datasource whose table was never provisioned). |
| 12 | + * |
| 13 | + * | door | producer of the answer | before | |
| 14 | + * |---|---|---| |
| 15 | + * | `GET /data/:object`, `GET /data/:object/:id` | driver-sql's declared read-exit fault (`backendStatementFault`) | `500 DATABASE_ERROR` | |
| 16 | + * | `POST /data/:object` | the boundary's text classification of the raw driver error (`classifyDataError`) | `404 OBJECT_NOT_FOUND` "Object '…' is not registered" | |
| 17 | + * |
| 18 | + * The write paths leave the driver as the dialect's raw error by design (the |
| 19 | + * boundary turns a unique violation into a 409, an unknown column into a 400), |
| 20 | + * so the boundary's missing-relation limb is what answered the insert. It read |
| 21 | + * "the relation the caller named is missing" as "the object is not |
| 22 | + * registered" — but no unregistered name reaches a driver: the protocol's |
| 23 | + * registry gate and the engine's own name resolution refuse it first, with the |
| 24 | + * structured `OBJECT_NOT_FOUND` that the classifier matches above every text |
| 25 | + * limb. A missing relation is therefore always a registered object whose |
| 26 | + * storage is missing, and the read's verdict for it is the classified one. |
| 27 | + * |
| 28 | + * ## What is pinned |
| 29 | + * |
| 30 | + * - each door answers the same status and code for the missing table; |
| 31 | + * - that answer is the read's `500 DATABASE_ERROR`; |
| 32 | + * - the control: an unregistered object is still `404 OBJECT_NOT_FOUND` on |
| 33 | + * both doors — the gate's answer, untouched. |
| 34 | + * |
| 35 | + * The SQLite cell always runs. The live Postgres cell (`42P01 relation "…" |
| 36 | + * does not exist`, the card's dialect) runs when `OS_TEST_POSTGRES_URL` is |
| 37 | + * set and is a named skip otherwise. |
| 38 | + * |
| 39 | + * Ablation: restoring the attributed missing-relation 404 in |
| 40 | + * `@objectstack/types`' `classifyDataError` turns the agreement cells and the |
| 41 | + * insert's own cell red (the POST answers 404 again), while the control stays |
| 42 | + * green. |
| 43 | + */ |
| 44 | + |
| 45 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 46 | +import { ObjectQL } from '@objectstack/objectql'; |
| 47 | +import { SqlDriver } from '@objectstack/driver-sql'; |
| 48 | +import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol'; |
| 49 | +import { RestServer } from './rest-server'; |
| 50 | +import { installDefaultOrganization } from './default-organization.testkit.js'; |
| 51 | + |
| 52 | +/** Registered after the DDL, so its table never exists. */ |
| 53 | +const OBJECT = 'wh_note_27702'; |
| 54 | +/** Never registered. */ |
| 55 | +const GHOST = 'wh_ghost_27702'; |
| 56 | + |
| 57 | +const NOTE = { |
| 58 | + name: OBJECT, |
| 59 | + label: 'Warehouse note', |
| 60 | + sharingModel: 'public_read_write', |
| 61 | + fields: { title: { name: 'title', type: 'text', label: 'Title' } }, |
| 62 | +}; |
| 63 | + |
| 64 | +interface Cell { |
| 65 | + id: 'sqlite' | 'pg'; |
| 66 | + label: string; |
| 67 | + env: string | null; |
| 68 | + config: () => Record<string, unknown> | null; |
| 69 | +} |
| 70 | + |
| 71 | +const CELLS: readonly Cell[] = [ |
| 72 | + { id: 'sqlite', label: 'sqlite', env: null, config: () => ({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true }) }, |
| 73 | + { |
| 74 | + id: 'pg', |
| 75 | + label: 'live postgres', |
| 76 | + env: 'OS_TEST_POSTGRES_URL', |
| 77 | + config: () => (process.env.OS_TEST_POSTGRES_URL ? { client: 'pg', connection: process.env.OS_TEST_POSTGRES_URL } : null), |
| 78 | + }, |
| 79 | +]; |
| 80 | + |
| 81 | +function createMockServer() { |
| 82 | + const noop = () => {}; |
| 83 | + return { get: noop, post: noop, put: noop, delete: noop, patch: noop, use: noop, listen: async () => {}, close: async () => {} }; |
| 84 | +} |
| 85 | + |
| 86 | +function makeRes() { |
| 87 | + const res: any = { |
| 88 | + write: () => true, end: () => {}, setHeader: () => {}, |
| 89 | + header: () => res, |
| 90 | + status: (code: number) => { res._status = code; return res; }, |
| 91 | + json: (body: any) => { res._json = body; return res; }, |
| 92 | + }; |
| 93 | + return res; |
| 94 | +} |
| 95 | + |
| 96 | +interface Answer { |
| 97 | + status: number; |
| 98 | + code: unknown; |
| 99 | +} |
| 100 | + |
| 101 | +for (const cell of CELLS) { |
| 102 | + const config = cell.config(); |
| 103 | + describe.skipIf(!config)( |
| 104 | + `[#27702] a registered object whose table is missing — insert and read agree — ${cell.label}${config ? '' : ` (skipped: set ${cell.env} to run this cell)`}`, |
| 105 | + () => { |
| 106 | + let engine: ObjectQL; |
| 107 | + let driver: any; |
| 108 | + let call: (method: string, path: string, req: Record<string, unknown>) => Promise<Answer>; |
| 109 | + |
| 110 | + const dropTable = async () => { |
| 111 | + if (cell.id === 'sqlite') return; |
| 112 | + await driver?.execute(`drop table if exists ${OBJECT}`).catch(() => {}); |
| 113 | + }; |
| 114 | + |
| 115 | + beforeAll(async () => { |
| 116 | + driver = new SqlDriver(config as any); |
| 117 | + await dropTable(); |
| 118 | + engine = new ObjectQL(); |
| 119 | + engine.registerDriver(driver, true); |
| 120 | + await engine.init(); |
| 121 | + await installDefaultOrganization(engine); |
| 122 | + // After the DDL: the registry holds the object, the database has no table for it. |
| 123 | + engine.registry.registerObject(NOTE as any, '@objectstack/rest'); |
| 124 | + |
| 125 | + const protocol = new ObjectStackProtocolImplementation(engine as any); |
| 126 | + const rest = new RestServer(createMockServer() as any, protocol as any, { api: { requireAuth: false } } as any); |
| 127 | + (rest as any).resolveExecCtx = async () => ({ userId: 'test-user' }); |
| 128 | + rest.registerRoutes(); |
| 129 | + call = async (method, path, req) => { |
| 130 | + const route = rest.getRoutes().find((r: any) => r.method === method && r.path === path); |
| 131 | + expect(route, `${method} ${path}`).toBeDefined(); |
| 132 | + const res = makeRes(); |
| 133 | + await route!.handler({ method, params: {}, query: {}, body: {}, headers: {}, ...req } as any, res); |
| 134 | + return { status: res._status ?? 200, code: res._json?.code }; |
| 135 | + }; |
| 136 | + }, 60_000); |
| 137 | + |
| 138 | + afterAll(async () => { |
| 139 | + await dropTable(); |
| 140 | + await engine?.destroy().catch(() => {}); |
| 141 | + }, 60_000); |
| 142 | + |
| 143 | + const insert = (object: string) => |
| 144 | + call('POST', '/api/v1/data/:object', { params: { object }, body: { title: 'first' } }); |
| 145 | + const list = (object: string) => call('GET', '/api/v1/data/:object', { params: { object } }); |
| 146 | + const read = (object: string) => call('GET', '/api/v1/data/:object/:id', { params: { object, id: 'rec_1' } }); |
| 147 | + |
| 148 | + it('premise: the object is registered and its table is missing', async () => { |
| 149 | + expect(engine.getSchema(OBJECT)).toBeDefined(); |
| 150 | + await expect(driver.find(OBJECT, {})).rejects.toBeTruthy(); |
| 151 | + }, 60_000); |
| 152 | + |
| 153 | + it('the read answers the classified store fault: 500 DATABASE_ERROR', async () => { |
| 154 | + expect(await list(OBJECT)).toEqual({ status: 500, code: 'DATABASE_ERROR' }); |
| 155 | + expect(await read(OBJECT)).toEqual({ status: 500, code: 'DATABASE_ERROR' }); |
| 156 | + }, 60_000); |
| 157 | + |
| 158 | + it('the insert answers the same, never OBJECT_NOT_FOUND', async () => { |
| 159 | + const answer = await insert(OBJECT); |
| 160 | + expect(answer.code).not.toBe('OBJECT_NOT_FOUND'); |
| 161 | + expect(answer).toEqual({ status: 500, code: 'DATABASE_ERROR' }); |
| 162 | + }, 60_000); |
| 163 | + |
| 164 | + it('insert and read agree', async () => { |
| 165 | + expect(await insert(OBJECT)).toEqual(await list(OBJECT)); |
| 166 | + }, 60_000); |
| 167 | + |
| 168 | + it('the update and delete doors answer the same', async () => { |
| 169 | + const update = await call('PATCH', '/api/v1/data/:object/:id', { params: { object: OBJECT, id: 'rec_1' }, body: { title: 'second' } }); |
| 170 | + const remove = await call('DELETE', '/api/v1/data/:object/:id', { params: { object: OBJECT, id: 'rec_1' } }); |
| 171 | + expect(update).toEqual({ status: 500, code: 'DATABASE_ERROR' }); |
| 172 | + expect(remove).toEqual({ status: 500, code: 'DATABASE_ERROR' }); |
| 173 | + }, 60_000); |
| 174 | + |
| 175 | + it('control: an unregistered object is still 404 OBJECT_NOT_FOUND on both doors', async () => { |
| 176 | + expect(await insert(GHOST)).toEqual({ status: 404, code: 'OBJECT_NOT_FOUND' }); |
| 177 | + expect(await list(GHOST)).toEqual({ status: 404, code: 'OBJECT_NOT_FOUND' }); |
| 178 | + }, 60_000); |
| 179 | + }, |
| 180 | + ); |
| 181 | +} |
0 commit comments