|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#20701] What the import row report and the driver-string branch say about |
| 5 | + * a column, read through the public doors — `POST /api/v1/data/:object/import` |
| 6 | + * (dry run and commit) and `POST /api/v1/data/:object` — over a REAL |
| 7 | + * {@link ObjectQL} + {@link ObjectStackProtocolImplementation} + SQLite |
| 8 | + * `:memory:`. |
| 9 | + * |
| 10 | + * Measured on the base (`ca5408c629`), with the engine half (#20805) already |
| 11 | + * landed: |
| 12 | + * |
| 13 | + * | column | dry run | commit | `POST /data/:object` | |
| 14 | + * |:--|:--|:--|:--| |
| 15 | + * | undeclared `nope` | failed `INVALID_FIELD`, **no `field`** | failed `INVALID_FIELD`, **no `field`** | `400 INVALID_FIELD`, `field: 'nope'` | |
| 16 | + * | declared `late`, no column (drift) | — | — | `400 INVALID_FIELD` "Unknown field 'late'" | |
| 17 | + * | formula `doubled` / readonly `ro` | ok, created | ok, created | `201` + `droppedFields` | |
| 18 | + * | writable `title` | ok, created | ok, created | `201` | |
| 19 | + * | a `unique` value already held | — | failed `UNIQUE_VIOLATION`, **no `field`** | `409 UNIQUE_VIOLATION`, `field` | |
| 20 | + * |
| 21 | + * The first and last rows are `toFailedResult` reading `field` only off a |
| 22 | + * `ValidationError` finding, while the engine's declared-field door and its |
| 23 | + * `DuplicateRecordError` carry it on the envelope (`field`; the declared-field |
| 24 | + * door puts the bare names in `fields`). The second is `mapDataError`'s |
| 25 | + * driver-string branch calling a DECLARED field unknown. |
| 26 | + * |
| 27 | + * ⚠️ Not pinned here: the drop signal on the formula / readonly rows. The dry |
| 28 | + * run's source, the protocol's `validateData`, returns no `droppedFields` at |
| 29 | + * all, so the import cannot read one without a change outside this package |
| 30 | + * (the card's report names it). The formula and readonly cells below pin only |
| 31 | + * that both halves SUCCEED, which is the half of the card's pin that holds. |
| 32 | + */ |
| 33 | + |
| 34 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 35 | +import { ObjectQL } from '@objectstack/objectql'; |
| 36 | +import { SqlDriver } from '@objectstack/driver-sql'; |
| 37 | +import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol'; |
| 38 | +import { RestServer } from './rest-server'; |
| 39 | + |
| 40 | +const OBJECT = 'proj_20701'; |
| 41 | + |
| 42 | +const PROJ = { |
| 43 | + name: OBJECT, label: 'Proj 20701', systemFields: false, |
| 44 | + fields: { |
| 45 | + id: { name: 'id', type: 'text' as const, primaryKey: true }, |
| 46 | + title: { name: 'title', type: 'text' as const }, |
| 47 | + n: { name: 'n', type: 'number' as const }, |
| 48 | + doubled: { name: 'doubled', type: 'formula' as const, expression: 'record.n * 2' }, |
| 49 | + ro: { name: 'ro', type: 'text' as const, readonly: true }, |
| 50 | + }, |
| 51 | +}; |
| 52 | + |
| 53 | +function createMockServer() { |
| 54 | + const noop = () => {}; |
| 55 | + return { get: noop, post: noop, put: noop, delete: noop, patch: noop, use: noop, listen: async () => {}, close: async () => {} }; |
| 56 | +} |
| 57 | + |
| 58 | +function makeRes() { |
| 59 | + const res: any = { |
| 60 | + write: () => true, end: () => {}, |
| 61 | + header: () => res, |
| 62 | + status: (code: number) => { res._status = code; return res; }, |
| 63 | + json: (body: any) => { res._json = body; return res; }, |
| 64 | + }; |
| 65 | + return res; |
| 66 | +} |
| 67 | + |
| 68 | +const liveEngines: ObjectQL[] = []; |
| 69 | +afterEach(async () => { |
| 70 | + while (liveEngines.length) { |
| 71 | + try { await liveEngines.pop()?.destroy(); } catch { /* noop */ } |
| 72 | + } |
| 73 | +}); |
| 74 | + |
| 75 | +async function boot() { |
| 76 | + const engine = new ObjectQL(); |
| 77 | + liveEngines.push(engine); |
| 78 | + engine.registerDriver(new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true }), true); |
| 79 | + await engine.init(); |
| 80 | + engine.registry.registerObject(PROJ as any); |
| 81 | + await engine.syncSchemas(); |
| 82 | + await engine.insert(OBJECT, { id: 'e1', title: 'existing', n: 1 } as any); |
| 83 | + |
| 84 | + const protocol = new ObjectStackProtocolImplementation(engine as any); |
| 85 | + const rest = new RestServer(createMockServer() as any, protocol as any, { api: { requireAuth: false } } as any); |
| 86 | + (rest as any).resolveExecCtx = async () => ({ userId: 'test-user' }); |
| 87 | + rest.registerRoutes(); |
| 88 | + const call = async (method: string, path: string, body: unknown, object: string = OBJECT): Promise<{ status: number; body: any }> => { |
| 89 | + const route = rest.getRoutes().find((r: any) => r.method === method && r.path === path); |
| 90 | + expect(route, `${method} ${path}`).toBeDefined(); |
| 91 | + const res = makeRes(); |
| 92 | + await route!.handler({ params: { object }, body, query: {}, headers: {} } as any, res); |
| 93 | + return { status: res._status ?? 200, body: res._json }; |
| 94 | + }; |
| 95 | + const importRows = (rows: Array<Record<string, unknown>>, opts: { dryRun: boolean; writeMode?: 'insert' | 'upsert' }) => |
| 96 | + call('POST', '/api/v1/data/:object/import', { |
| 97 | + format: 'json', rows, dryRun: opts.dryRun, |
| 98 | + ...(opts.writeMode === 'upsert' ? { writeMode: 'upsert', matchFields: ['id'] } : {}), |
| 99 | + }); |
| 100 | + return { engine, call, importRows }; |
| 101 | +} |
| 102 | + |
| 103 | +type Boot = Awaited<ReturnType<typeof boot>>; |
| 104 | + |
| 105 | +describe('[#20701] an unknown column fails the row on the dry run and on the commit alike, and both rows name it', () => { |
| 106 | + let b: Boot; |
| 107 | + beforeEach(async () => { b = await boot(); }); |
| 108 | + |
| 109 | + it.each([ |
| 110 | + ['insert', [{ title: 't', n: 2, nope: 5 }, { title: 'control', n: 3 }]], |
| 111 | + ['upsert', [{ id: 'e1', title: 't', nope: 5 }, { id: 'n1', title: 'control', n: 3 }]], |
| 112 | + ] as const)('writeMode %s', async (writeMode, rows) => { |
| 113 | + for (const dryRun of [true, false]) { |
| 114 | + const r = await b.importRows(rows as any, { dryRun, writeMode }); |
| 115 | + expect(r.status, JSON.stringify(r.body)).toBe(200); |
| 116 | + expect(r.body.results[0], `dryRun=${dryRun}`).toMatchObject({ |
| 117 | + row: 1, ok: false, action: 'failed', code: 'INVALID_FIELD', field: 'nope', |
| 118 | + }); |
| 119 | + // The second row is the control: the refusal is the row's, not the file's. |
| 120 | + expect(r.body.results[1], `dryRun=${dryRun}`).toMatchObject({ row: 2, ok: true }); |
| 121 | + expect(r.body.errors, `dryRun=${dryRun}`).toBe(1); |
| 122 | + } |
| 123 | + }); |
| 124 | + |
| 125 | + it('the row names the same field the create door names for the same key', async () => { |
| 126 | + const door = await b.call('POST', '/api/v1/data/:object', { title: 't', nope: 5 }); |
| 127 | + expect(door.status).toBe(400); |
| 128 | + expect(door.body).toMatchObject({ code: 'INVALID_FIELD', field: 'nope' }); |
| 129 | + const commit = await b.importRows([{ title: 't', nope: 5 }], { dryRun: false }); |
| 130 | + expect(commit.body.results[0]).toMatchObject({ code: door.body.code, field: door.body.field }); |
| 131 | + }); |
| 132 | +}); |
| 133 | + |
| 134 | +describe('[#20701] a unique conflict row names the column the 409 names — the same envelope member', () => { |
| 135 | + it('the engine\'s DuplicateRecordError carries `field`; the commit row reads it', async () => { |
| 136 | + const b = await boot(); |
| 137 | + const UQ = 'uq_20701'; |
| 138 | + b.engine.registry.registerObject({ |
| 139 | + name: UQ, systemFields: false, |
| 140 | + fields: { id: { name: 'id', type: 'text', primaryKey: true }, code: { name: 'code', type: 'text', unique: true } }, |
| 141 | + } as any); |
| 142 | + await b.engine.syncSchemas(); |
| 143 | + await b.engine.insert(UQ, { id: 'a', code: 'X' } as any); |
| 144 | + const door = await b.call('POST', '/api/v1/data/:object', { code: 'X' }, UQ); |
| 145 | + expect(door.status).toBe(409); |
| 146 | + expect(door.body).toMatchObject({ code: 'UNIQUE_VIOLATION', field: 'code' }); |
| 147 | + const commit = await b.call('POST', '/api/v1/data/:object/import', { format: 'json', rows: [{ code: 'X' }, { code: 'Y' }], dryRun: false }, UQ); |
| 148 | + expect(commit.body.results[0]).toMatchObject({ row: 1, ok: false, action: 'failed', code: door.body.code, field: door.body.field }); |
| 149 | + expect(commit.body.results[1]).toMatchObject({ row: 2, ok: true, action: 'created' }); |
| 150 | + }); |
| 151 | +}); |
| 152 | + |
| 153 | +describe('[#20701] a writable column imports — the control', () => { |
| 154 | + it('dry run and commit both create the row, and the commit stores it', async () => { |
| 155 | + const b = await boot(); |
| 156 | + const preview = await b.importRows([{ id: 'w1', title: 'written', n: 4 }], { dryRun: true }); |
| 157 | + expect(preview.body).toMatchObject({ ok: 1, errors: 0, created: 1 }); |
| 158 | + expect(preview.body.results[0]).toMatchObject({ row: 1, ok: true, action: 'created' }); |
| 159 | + const commit = await b.importRows([{ id: 'w1', title: 'written', n: 4 }], { dryRun: false }); |
| 160 | + expect(commit.body).toMatchObject({ ok: 1, errors: 0, created: 1 }); |
| 161 | + expect(commit.body.results[0]).toMatchObject({ row: 1, ok: true, action: 'created', id: 'w1' }); |
| 162 | + const stored = await b.engine.findOne(OBJECT, { where: { id: 'w1' } }); |
| 163 | + expect(stored).toMatchObject({ title: 'written', n: 4, doubled: 8 }); |
| 164 | + }); |
| 165 | +}); |
| 166 | + |
| 167 | +describe('[#20701] a formula or readonly column: the dry run and the commit both succeed', () => { |
| 168 | + it.each([ |
| 169 | + ['formula', { id: 'f1', title: 't', n: 2, doubled: 5 }], |
| 170 | + ['readonly', { id: 'f1', title: 't', n: 2, ro: 'forged' }], |
| 171 | + ] as const)('%s column', async (_label, row) => { |
| 172 | + const b = await boot(); |
| 173 | + for (const dryRun of [true, false]) { |
| 174 | + const r = await b.importRows([row], { dryRun }); |
| 175 | + expect(r.body, `dryRun=${dryRun}`).toMatchObject({ ok: 1, errors: 0, created: 1 }); |
| 176 | + expect(r.body.results[0], `dryRun=${dryRun}`).toMatchObject({ row: 1, ok: true, action: 'created' }); |
| 177 | + } |
| 178 | + // The supplied value was not stored: the formula answers from `n`, and the readonly column stays empty. |
| 179 | + const stored = await b.engine.findOne(OBJECT, { where: { id: 'f1' } }); |
| 180 | + expect(stored).toMatchObject({ n: 2, doubled: 4 }); |
| 181 | + expect(stored?.ro ?? null).toBeNull(); |
| 182 | + }); |
| 183 | +}); |
| 184 | + |
| 185 | +describe('[#20701] the driver-string branch does not call a declared field unknown', () => { |
| 186 | + it('a declared field whose column is missing: 400 INVALID_FIELD naming it, and the sentence is the database\'s', async () => { |
| 187 | + const b = await boot(); |
| 188 | + // The object declares `late` after the DDL ran, with no re-sync — metadata and the table have drifted. |
| 189 | + b.engine.registry.registerObject({ ...PROJ, fields: { ...PROJ.fields, late: { name: 'late', type: 'text' as const } } } as any); |
| 190 | + const r = await b.call('POST', '/api/v1/data/:object', { title: 't', late: 'x' }); |
| 191 | + expect(r.status, JSON.stringify(r.body)).toBe(400); |
| 192 | + expect(r.body).toMatchObject({ code: 'INVALID_FIELD', field: 'late', object: OBJECT }); |
| 193 | + // The wording IS the fix here, so its first sentence is pinned. |
| 194 | + expect(String(r.body.error).split('. ')[0]).toBe(`The database table of object '${OBJECT}' has no column for field 'late'`); |
| 195 | + expect(String(r.body.error)).not.toMatch(/unknown field/i); |
| 196 | + }); |
| 197 | + |
| 198 | + it('an undeclared key is still refused by the engine\'s own door, in its own words — the control', async () => { |
| 199 | + const b = await boot(); |
| 200 | + const r = await b.call('POST', '/api/v1/data/:object', { title: 't', nope: 'x' }); |
| 201 | + expect(r.status).toBe(400); |
| 202 | + expect(r.body).toMatchObject({ code: 'INVALID_FIELD', field: 'nope', object: OBJECT }); |
| 203 | + expect(String(r.body.error)).toContain("Unknown field 'nope'"); |
| 204 | + }); |
| 205 | +}); |
0 commit comments