|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#25582, maintainer ruling 6377844879 letter A, proposal 4] An import row |
| 5 | + * with several bad cells reports every one of them, so one import tells the |
| 6 | + * operator about every bad cell instead of one per re-import. Through the real |
| 7 | + * routes over a real {@link ObjectQL} + {@link ObjectStackProtocolImplementation} |
| 8 | + * + SQLite `:memory:`, with the platform's own `sys_import_job`: the |
| 9 | + * synchronous `POST /data/:object/import` and the async job's |
| 10 | + * `GET /data/import/jobs/:jobId/results`, each on the dry run and the commit. |
| 11 | + * |
| 12 | + * The row carries `errors` (`ImportRowResultSchema.errors`): one element per |
| 13 | + * failed cell, in column order, each `{ field, code, message }`. The |
| 14 | + * first-error keys `field` / `code` / `error` stay and name element 0. Measured |
| 15 | + * at the base (`45de8c6cba`): the same row reported its first cell only, with |
| 16 | + * no `errors` key, on all four paths. |
| 17 | + * |
| 18 | + * A row that failed for another reason omits the key: the dry run's validate |
| 19 | + * verdict (`required` here) and the commit's refused write (the create door's |
| 20 | + * `required` verdict) both name a field, but not as a cell error. |
| 21 | + * |
| 22 | + * The runner's own cases are `packages/core/src/utils/import-runner-cell-errors.test.ts`. |
| 23 | + */ |
| 24 | + |
| 25 | +import { describe, it, expect, afterEach } from 'vitest'; |
| 26 | +import { ObjectQL } from '@objectstack/objectql'; |
| 27 | +import { SqlDriver } from '@objectstack/driver-sql'; |
| 28 | +import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol'; |
| 29 | +import { SysImportJob } from '@objectstack/platform-objects/audit'; |
| 30 | +import { SysMetadataAuditObject, SysMetadataCommitObject, SysMetadataHistoryObject, SysMetadataObject } from '@objectstack/metadata-core'; |
| 31 | +import { RestServer } from './rest-server'; |
| 32 | +import { installDefaultOrganization } from './default-organization.testkit.js'; |
| 33 | + |
| 34 | +const OBJECT = 'supplier_cells_25582'; |
| 35 | + |
| 36 | +const SUPPLIER = { |
| 37 | + name: OBJECT, label: 'Supplier cells 25582', systemFields: false, |
| 38 | + fields: { |
| 39 | + id: { name: 'id', type: 'text' as const, primaryKey: true, label: 'ID' }, |
| 40 | + name: { name: 'name', type: 'text' as const, label: 'Name', required: true }, |
| 41 | + opened: { name: 'opened', type: 'date' as const, label: 'Opened' }, |
| 42 | + qty: { name: 'qty', type: 'number' as const, label: 'Qty' }, |
| 43 | + country: { |
| 44 | + name: 'country', type: 'select' as const, label: 'Country', |
| 45 | + options: [{ label: '中国', value: 'cn' }, { label: '美国', value: 'us' }], |
| 46 | + }, |
| 47 | + }, |
| 48 | +}; |
| 49 | + |
| 50 | +function createMockServer() { |
| 51 | + const noop = () => {}; |
| 52 | + return { get: noop, post: noop, put: noop, delete: noop, patch: noop, use: noop, listen: async () => {}, close: async () => {} }; |
| 53 | +} |
| 54 | + |
| 55 | +function makeRes() { |
| 56 | + const res: any = { |
| 57 | + write: () => true, end: () => {}, |
| 58 | + header: () => res, |
| 59 | + status: (code: number) => { res._status = code; return res; }, |
| 60 | + json: (body: any) => { res._json = body; return res; }, |
| 61 | + }; |
| 62 | + return res; |
| 63 | +} |
| 64 | + |
| 65 | +const liveEngines: ObjectQL[] = []; |
| 66 | +afterEach(async () => { |
| 67 | + while (liveEngines.length) { |
| 68 | + try { await liveEngines.pop()?.destroy(); } catch { /* noop */ } |
| 69 | + } |
| 70 | +}); |
| 71 | + |
| 72 | +async function boot() { |
| 73 | + const engine = new ObjectQL(); |
| 74 | + liveEngines.push(engine); |
| 75 | + engine.registerDriver(new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true }), true); |
| 76 | + await engine.init(); |
| 77 | + engine.registry.registerObject(SUPPLIER as any); |
| 78 | + engine.registry.registerObject(SysImportJob as any); |
| 79 | + await engine.syncSchemas(); |
| 80 | + // A real `single` install's Default Organization: the job row's system |
| 81 | + // writes derive it (ADR-0131 D9). |
| 82 | + await installDefaultOrganization(engine); |
| 83 | + // The route reads the object's field metadata through the protocol, which |
| 84 | + // reads the stored-metadata family: without it every cell passes through |
| 85 | + // uncoerced and the engine's validator answers instead. |
| 86 | + for (const o of [SysMetadataObject, SysMetadataHistoryObject, SysMetadataAuditObject, SysMetadataCommitObject]) { |
| 87 | + if (!engine.registry.getObject(o.name)) engine.registry.registerObject(o as any); |
| 88 | + } |
| 89 | + |
| 90 | + const protocol = new ObjectStackProtocolImplementation(engine as any); |
| 91 | + const rest = new RestServer(createMockServer() as any, protocol as any, { api: { requireAuth: false } } as any); |
| 92 | + (rest as any).resolveExecCtx = async () => ({ userId: 'test-user' }); |
| 93 | + rest.registerRoutes(); |
| 94 | + const call = async (method: string, path: string, body: unknown, params: Record<string, string> = { object: OBJECT }): Promise<{ status: number; body: any }> => { |
| 95 | + const route = rest.getRoutes().find((r: any) => r.method === method && r.path === path); |
| 96 | + expect(route, `${method} ${path}`).toBeDefined(); |
| 97 | + const res = makeRes(); |
| 98 | + await route!.handler({ params, body, query: {}, headers: {} } as any, res); |
| 99 | + return { status: res._status ?? 200, body: res._json }; |
| 100 | + }; |
| 101 | + |
| 102 | + /** The synchronous route's per-row report. */ |
| 103 | + const syncRows = async (rows: Array<Record<string, unknown>>, dryRun: boolean) => { |
| 104 | + const r = await call('POST', '/api/v1/data/:object/import', { format: 'json', rows, dryRun }); |
| 105 | + expect(r.status, JSON.stringify(r.body)).toBe(200); |
| 106 | + return r.body.results as any[]; |
| 107 | + }; |
| 108 | + |
| 109 | + /** The async job's per-row report, read off the results route once the job is terminal. */ |
| 110 | + const jobRows = async (rows: Array<Record<string, unknown>>, dryRun: boolean) => { |
| 111 | + const created = await call('POST', '/api/v1/data/:object/import/jobs', { format: 'json', rows, dryRun }); |
| 112 | + expect(created.status, JSON.stringify(created.body)).toBe(201); |
| 113 | + const jobId = String(created.body.jobId); |
| 114 | + let progress: { status: number; body: any } | undefined; |
| 115 | + for (let i = 0; i < 200; i++) { |
| 116 | + progress = await call('GET', '/api/v1/data/import/jobs/:jobId', undefined, { jobId }); |
| 117 | + if (['succeeded', 'failed', 'cancelled'].includes(progress.body?.status)) break; |
| 118 | + await new Promise((r) => setTimeout(r, 5)); |
| 119 | + } |
| 120 | + expect(progress?.body?.status, JSON.stringify(progress?.body)).toBe('succeeded'); |
| 121 | + const results = await call('GET', '/api/v1/data/import/jobs/:jobId/results', undefined, { jobId }); |
| 122 | + return [...(results.body.results as any[])].sort((a, b) => a.row - b.row); |
| 123 | + }; |
| 124 | + |
| 125 | + return { engine, syncRows, jobRows }; |
| 126 | +} |
| 127 | + |
| 128 | +/** Three bad cells around good ones, in this column order. */ |
| 129 | +const BAD_ROW = { id: 'b1', name: 'Acme', opened: '2011-13-45', qty: 'many', country: 'DE' }; |
| 130 | +const GOOD_ROW = { id: 'g1', name: 'Good', opened: '2011-07-01', qty: '3', country: 'cn' }; |
| 131 | + |
| 132 | +function expectEveryCellError(row: any) { |
| 133 | + expect(row, JSON.stringify(row)).toMatchObject({ row: 1, ok: false, action: 'failed' }); |
| 134 | + expect(row.errors?.map((e: any) => [e.field, e.code]), JSON.stringify(row)).toEqual([ |
| 135 | + ['opened', 'invalid_date'], |
| 136 | + ['qty', 'invalid_number'], |
| 137 | + ['country', 'invalid_option'], |
| 138 | + ]); |
| 139 | + for (const e of row.errors) { |
| 140 | + expect(Object.keys(e).sort()).toEqual(['code', 'field', 'message']); |
| 141 | + expect(typeof e.message).toBe('string'); |
| 142 | + } |
| 143 | + // The first-error keys are element 0, as they were before the list. |
| 144 | + expect({ field: row.field, code: row.code, error: row.error }) |
| 145 | + .toEqual({ field: row.errors[0].field, code: row.errors[0].code, error: row.errors[0].message }); |
| 146 | +} |
| 147 | + |
| 148 | +describe('[#25582] every cell error of a row reaches the report', () => { |
| 149 | + for (const dryRun of [true, false]) { |
| 150 | + const leg = dryRun ? 'dry run' : 'commit'; |
| 151 | + |
| 152 | + it(`sync route, ${leg}`, async () => { |
| 153 | + const b = await boot(); |
| 154 | + const rows = await b.syncRows([BAD_ROW, GOOD_ROW], dryRun); |
| 155 | + expectEveryCellError(rows[0]); |
| 156 | + expect(rows[1]).toMatchObject({ row: 2, ok: true, action: 'created' }); |
| 157 | + expect(rows[1]).not.toHaveProperty('errors'); |
| 158 | + expect(await b.engine.findOne(OBJECT, { where: { id: 'b1' } })).toBeNull(); |
| 159 | + }); |
| 160 | + |
| 161 | + it(`async job, ${leg}`, async () => { |
| 162 | + const b = await boot(); |
| 163 | + const rows = await b.jobRows([BAD_ROW, GOOD_ROW], dryRun); |
| 164 | + expectEveryCellError(rows[0]); |
| 165 | + expect(rows[1]).toMatchObject({ row: 2, ok: true, action: 'created' }); |
| 166 | + expect(rows[1]).not.toHaveProperty('errors'); |
| 167 | + }); |
| 168 | + } |
| 169 | +}); |
| 170 | + |
| 171 | +describe('[#25582] a row that failed for another reason carries no `errors`', () => { |
| 172 | + /** Every cell coerces; `name` is required and missing. */ |
| 173 | + const NO_NAME = { id: 'n1', qty: '3' }; |
| 174 | + |
| 175 | + it('the dry run\'s validate verdict (sync and async)', async () => { |
| 176 | + const b = await boot(); |
| 177 | + for (const rows of [await b.syncRows([NO_NAME], true), await b.jobRows([NO_NAME], true)]) { |
| 178 | + expect(rows[0], JSON.stringify(rows[0])).toMatchObject({ row: 1, ok: false, action: 'failed', field: 'name', code: 'required' }); |
| 179 | + expect(rows[0]).not.toHaveProperty('errors'); |
| 180 | + } |
| 181 | + }); |
| 182 | + |
| 183 | + it('the commit\'s refused write (sync and async)', async () => { |
| 184 | + const b = await boot(); |
| 185 | + for (const rows of [await b.syncRows([NO_NAME], false), await b.jobRows([NO_NAME], false)]) { |
| 186 | + expect(rows[0], JSON.stringify(rows[0])).toMatchObject({ row: 1, ok: false, action: 'failed', field: 'name', code: 'required' }); |
| 187 | + expect(rows[0]).not.toHaveProperty('errors'); |
| 188 | + } |
| 189 | + expect(await b.engine.findOne(OBJECT, { where: { id: 'n1' } })).toBeNull(); |
| 190 | + }); |
| 191 | +}); |
0 commit comments