|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#20497] `POST /api/v1/data/:object/import` reads a comma in a number cell |
| 5 | + * as a thousands separator ONLY where it groups thousands — 1 to 3 leading |
| 6 | + * digits, then groups of exactly three, and only before any `.` — and refuses |
| 7 | + * every other comma as that row's `invalid_number` error, through the real |
| 8 | + * route over a real `SqlDriver` (better-sqlite3 `:memory:`). |
| 9 | + * |
| 10 | + * Measured through this route, JSON rows, `writeMode: 'insert'`, on |
| 11 | + * `InMemoryDriver` and on `SqlDriver` (better-sqlite3) alike, at the base |
| 12 | + * (`9449512a31`) and at the head of the PR that landed this file: |
| 13 | + * |
| 14 | + * | cell | base: stored · answer | head | |
| 15 | + * |:--|:--|:--| |
| 16 | + * | `'3,14'` | `314` · ok 1, errors 0 | row refused, `invalid_number`, nothing stored | |
| 17 | + * | `'1,5'` | `15` · ok 1, errors 0 | row refused, `invalid_number`, nothing stored | |
| 18 | + * | `'1.000,5'` | `1.0005` · ok 1, errors 0 | row refused, `invalid_number`, nothing stored | |
| 19 | + * | `'1,2,3'` | `123` · ok 1, errors 0 | row refused, `invalid_number`, nothing stored | |
| 20 | + * | `'1,000'` / `'12,345.67'` / `'(1,234)'` | `1000` / `12345.67` / `-1234` | unchanged | |
| 21 | + * |
| 22 | + * The `InMemoryDriver` row is this file's by construction, not by a second |
| 23 | + * arm: the cell is judged by the import's own reader (`parseNumberCell`) |
| 24 | + * before any driver is reached, so one verdict holds on every driver. A test |
| 25 | + * import of `@objectstack/driver-memory` is also not this file's to add — its |
| 26 | + * test consumers are a ruled, ledgered set |
| 27 | + * (`scripts/driver-memory-census.ledger.json`, `pnpm check:driver-memory-census`). |
| 28 | + * |
| 29 | + * The plain create door already answered `400 VALIDATION_FAILED` with field |
| 30 | + * code `invalid_number` for each of the four; the import row now answers the |
| 31 | + * same code. The reader's own case table (every documented form, the comma |
| 32 | + * probes) is `import-coerce.test.ts`'s `parseNumberCell` block; this file pins |
| 33 | + * the door. |
| 34 | + */ |
| 35 | + |
| 36 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 37 | +import { ObjectQL } from '@objectstack/objectql'; |
| 38 | +import { SqlDriver } from '@objectstack/driver-sql'; |
| 39 | +import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol'; |
| 40 | +import { RestServer } from './rest-server'; |
| 41 | + |
| 42 | +const OBJECT = 'import_thousands_20497'; |
| 43 | + |
| 44 | +const LEDGER = { |
| 45 | + name: OBJECT, label: 'Ledger 20497', systemFields: false, |
| 46 | + fields: { |
| 47 | + id: { name: 'id', type: 'text' as const, primaryKey: true }, |
| 48 | + amount: { name: 'amount', type: 'number' as const, label: 'Amount' }, |
| 49 | + }, |
| 50 | +}; |
| 51 | + |
| 52 | +/** The card's four cells: each used to be stored as a different number. */ |
| 53 | +const REFUSED = ['3,14', '1,5', '1.000,5', '1,2,3'] as const; |
| 54 | + |
| 55 | +/** The admitted controls: a well-formed thousands grouping keeps its reading. */ |
| 56 | +const ADMITTED: ReadonlyArray<readonly [cell: string, stored: number]> = [ |
| 57 | + ['1,000', 1000], |
| 58 | + ['12,345.67', 12345.67], |
| 59 | + ['(1,234)', -1234], |
| 60 | +]; |
| 61 | + |
| 62 | +function makeSqliteDriver() { |
| 63 | + return new SqlDriver({ |
| 64 | + client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true, |
| 65 | + }); |
| 66 | +} |
| 67 | + |
| 68 | +function createMockServer() { |
| 69 | + const noop = () => {}; |
| 70 | + return { get: noop, post: noop, put: noop, delete: noop, patch: noop, use: noop, listen: async () => {}, close: async () => {} }; |
| 71 | +} |
| 72 | + |
| 73 | +function makeRes() { |
| 74 | + const res: any = { |
| 75 | + write: () => true, end: () => {}, |
| 76 | + header: () => res, |
| 77 | + status: (code: number) => { res._status = code; return res; }, |
| 78 | + json: (body: any) => { res._json = body; return res; }, |
| 79 | + }; |
| 80 | + return res; |
| 81 | +} |
| 82 | + |
| 83 | +const liveEngines: ObjectQL[] = []; |
| 84 | +afterEach(async () => { |
| 85 | + while (liveEngines.length) { |
| 86 | + try { await liveEngines.pop()?.destroy(); } catch { /* noop */ } |
| 87 | + } |
| 88 | +}); |
| 89 | + |
| 90 | +async function boot() { |
| 91 | + const engine = new ObjectQL(); |
| 92 | + liveEngines.push(engine); |
| 93 | + engine.registerDriver(makeSqliteDriver(), true); |
| 94 | + await engine.init(); |
| 95 | + engine.registry.registerObject(LEDGER as any); |
| 96 | + await engine.syncSchemas(); |
| 97 | + const protocol = new ObjectStackProtocolImplementation(engine as any); |
| 98 | + const rest = new RestServer(createMockServer() as any, protocol as any, { api: { requireAuth: false } } as any); |
| 99 | + (rest as any).resolveExecCtx = async () => ({ userId: 'test-user' }); |
| 100 | + rest.registerRoutes(); |
| 101 | + const find = (method: string, path: string) => |
| 102 | + rest.getRoutes().find((r: any) => r.method === method && r.path === path) as any; |
| 103 | + const importRoute = find('POST', '/api/v1/data/:object/import'); |
| 104 | + const createRoute = find('POST', '/api/v1/data/:object'); |
| 105 | + expect(importRoute).toBeDefined(); |
| 106 | + expect(createRoute).toBeDefined(); |
| 107 | + const send = async (route: any, body: unknown) => { |
| 108 | + const res = makeRes(); |
| 109 | + await route.handler({ params: { object: OBJECT }, body } as any, res); |
| 110 | + return res; |
| 111 | + }; |
| 112 | + return { |
| 113 | + engine, |
| 114 | + importRows: (body: Record<string, unknown>) => send(importRoute, body), |
| 115 | + create: (body: Record<string, unknown>) => send(createRoute, body), |
| 116 | + }; |
| 117 | +} |
| 118 | + |
| 119 | +describe('[#20497] /import — a comma is read only as a thousands group', () => { |
| 120 | + let ctx: Awaited<ReturnType<typeof boot>>; |
| 121 | + beforeEach(async () => { ctx = await boot(); }); |
| 122 | + |
| 123 | + it.each(REFUSED)('refuses %j as that row\'s invalid_number error and writes its sibling row', async (cell) => { |
| 124 | + const res = await ctx.importRows({ |
| 125 | + format: 'json', writeMode: 'insert', |
| 126 | + rows: [{ id: 'bad', amount: cell }, { id: 'good', amount: 7 }], |
| 127 | + }); |
| 128 | + |
| 129 | + expect(res._status ?? 200).toBe(200); |
| 130 | + expect(res._json).toMatchObject({ total: 2, ok: 1, errors: 1, created: 1 }); |
| 131 | + expect(res._json.results[0]).toMatchObject({ |
| 132 | + row: 1, ok: false, action: 'failed', field: 'amount', code: 'invalid_number', |
| 133 | + }); |
| 134 | + // Refused, not stored as some other number. |
| 135 | + expect(await ctx.engine.findOne(OBJECT, { where: { id: 'bad' } })).toBeNull(); |
| 136 | + expect((await ctx.engine.findOne(OBJECT, { where: { id: 'good' } }))?.amount).toBe(7); |
| 137 | + }); |
| 138 | + |
| 139 | + it.each(ADMITTED)('admits the thousands grouping %j and stores %s', async (cell, stored) => { |
| 140 | + const res = await ctx.importRows({ |
| 141 | + format: 'json', writeMode: 'insert', |
| 142 | + rows: [{ id: 'r', amount: cell }], |
| 143 | + }); |
| 144 | + |
| 145 | + expect(res._json).toMatchObject({ total: 1, ok: 1, errors: 0, created: 1 }); |
| 146 | + expect(res._json.results[0]).toMatchObject({ row: 1, ok: true, action: 'created' }); |
| 147 | + expect((await ctx.engine.findOne(OBJECT, { where: { id: 'r' } }))?.amount).toBe(stored); |
| 148 | + }); |
| 149 | + |
| 150 | + it('gives the same verdicts to quoted CSV cells', async () => { |
| 151 | + const csv = [ |
| 152 | + 'ID,Amount', |
| 153 | + ...REFUSED.map((cell, i) => `r${i},"${cell}"`), |
| 154 | + ...ADMITTED.map(([cell], j) => `c${j},"${cell}"`), |
| 155 | + ].join('\n'); |
| 156 | + const res = await ctx.importRows({ format: 'csv', csv, writeMode: 'insert', mapping: { ID: 'id', Amount: 'amount' } }); |
| 157 | + |
| 158 | + expect(res._json).toMatchObject({ total: REFUSED.length + ADMITTED.length, ok: ADMITTED.length, errors: REFUSED.length }); |
| 159 | + const failedFields = res._json.results.filter((r: any) => !r.ok).map((r: any) => [r.field, r.code]); |
| 160 | + expect(failedFields).toEqual(REFUSED.map(() => ['amount', 'invalid_number'])); |
| 161 | + for (const [j, [, stored]] of ADMITTED.entries()) { |
| 162 | + expect((await ctx.engine.findOne(OBJECT, { where: { id: `c${j}` } }))?.amount).toBe(stored); |
| 163 | + } |
| 164 | + }); |
| 165 | + |
| 166 | + it('dry run predicts the same refusals and persists nothing', async () => { |
| 167 | + const res = await ctx.importRows({ |
| 168 | + format: 'json', writeMode: 'insert', dryRun: true, |
| 169 | + rows: REFUSED.map((amount, i) => ({ id: `d${i}`, amount })), |
| 170 | + }); |
| 171 | + |
| 172 | + expect(res._json).toMatchObject({ dryRun: true, total: REFUSED.length, ok: 0, errors: REFUSED.length }); |
| 173 | + for (const r of res._json.results) expect(r).toMatchObject({ ok: false, field: 'amount', code: 'invalid_number' }); |
| 174 | + for (const [i] of REFUSED.entries()) { |
| 175 | + expect(await ctx.engine.findOne(OBJECT, { where: { id: `d${i}` } })).toBeNull(); |
| 176 | + } |
| 177 | + }); |
| 178 | + |
| 179 | + it('answers the code the plain create door answers for the same cells', async () => { |
| 180 | + for (const [i, amount] of REFUSED.entries()) { |
| 181 | + const res = await ctx.create({ id: `p${i}`, amount }); |
| 182 | + expect(res._status).toBe(400); |
| 183 | + expect(res._json).toMatchObject({ code: 'VALIDATION_FAILED' }); |
| 184 | + expect(res._json.fields[0]).toMatchObject({ field: 'amount', code: 'invalid_number' }); |
| 185 | + } |
| 186 | + }); |
| 187 | +}); |
0 commit comments