|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#24951] A unique refusal's user-facing sentence names the object and the |
| 5 | + * field by their declared `label`. |
| 6 | + * |
| 7 | + * Every door keeps `DuplicateRecordError.message` as thrown, so a refused record |
| 8 | + * save and a screen flow both show it to the end user verbatim — and it used to |
| 9 | + * read `Duplicate record refused on 'crm_account': …`, an API name the user |
| 10 | + * cannot act on. The engine now resolves the labels from its registry at the |
| 11 | + * write, falling back to the API name only where none is declared; the API |
| 12 | + * names stay on `object` / `field` and open `developerMessage`. |
| 13 | + * |
| 14 | + * Asserted through the real write doors (`insert` single and batch, `update` |
| 15 | + * by id and by predicate) on a registry double, so the pin covers the label lookup at each |
| 16 | + * call site and not only the sentence builder. |
| 17 | + */ |
| 18 | + |
| 19 | +import { describe, it, expect, beforeEach, vi } from 'vitest'; |
| 20 | +import { ObjectQL } from './engine'; |
| 21 | +import { DuplicateRecordError } from './duplicate-record-error'; |
| 22 | +import { SchemaRegistry } from './registry'; |
| 23 | +import type { IDataDriver } from '@objectstack/spec/contracts'; |
| 24 | + |
| 25 | +vi.mock('./registry', async () => { |
| 26 | + const { createRegistryModuleMock } = await import('./registry-module-mock.js'); |
| 27 | + return createRegistryModuleMock(); |
| 28 | +}); |
| 29 | + |
| 30 | +const registryDouble = SchemaRegistry as unknown as { getObject: ReturnType<typeof vi.fn> }; |
| 31 | + |
| 32 | +/** better-sqlite3: names the column, so the envelope carries `field`. */ |
| 33 | +const namedColumn = () => |
| 34 | + Object.assign( |
| 35 | + new Error('insert into `crm_account` (`id`, `name`) values (?, ?) - UNIQUE constraint failed: crm_account.name'), |
| 36 | + { code: 'SQLITE_CONSTRAINT_UNIQUE' }, |
| 37 | + ); |
| 38 | + |
| 39 | +/** mysql2: names the INDEX only, so the envelope carries no `field`. */ |
| 40 | +const indexOnly = () => |
| 41 | + Object.assign(new Error("Duplicate entry 'Acme' for key 'idx_account_name'"), { code: 'ER_DUP_ENTRY', errno: 1062 }); |
| 42 | + |
| 43 | +const LABELLED = { |
| 44 | + name: 'crm_account', |
| 45 | + label: 'Account', |
| 46 | + fields: { name: { type: 'text', label: 'Account Name' } }, |
| 47 | +}; |
| 48 | + |
| 49 | +const UNLABELLED = { |
| 50 | + name: 'crm_account', |
| 51 | + fields: { name: { type: 'text' } }, |
| 52 | +}; |
| 53 | + |
| 54 | +function makeRig(schema: unknown, refuse: () => unknown) { |
| 55 | + registryDouble.getObject.mockReturnValue(schema); |
| 56 | + const driver: any = { |
| 57 | + name: 'fake', |
| 58 | + version: '0.0.0', |
| 59 | + supports: {}, |
| 60 | + connect: vi.fn().mockResolvedValue(undefined), |
| 61 | + disconnect: vi.fn().mockResolvedValue(undefined), |
| 62 | + checkHealth: vi.fn().mockResolvedValue(true), |
| 63 | + execute: vi.fn(), |
| 64 | + find: vi.fn(async () => []), |
| 65 | + findOne: vi.fn(async () => ({ id: 'r1', name: 'Old' })), |
| 66 | + create: vi.fn(async () => { |
| 67 | + throw refuse(); |
| 68 | + }), |
| 69 | + bulkCreate: vi.fn(async () => { |
| 70 | + throw refuse(); |
| 71 | + }), |
| 72 | + update: vi.fn(async () => { |
| 73 | + throw refuse(); |
| 74 | + }), |
| 75 | + updateMany: vi.fn(async () => { |
| 76 | + throw refuse(); |
| 77 | + }), |
| 78 | + delete: vi.fn(), |
| 79 | + count: vi.fn(), |
| 80 | + }; |
| 81 | + const engine = new ObjectQL(); |
| 82 | + engine.registerDriver(driver as IDataDriver, true); |
| 83 | + return engine; |
| 84 | +} |
| 85 | + |
| 86 | +async function refusalOf(run: () => Promise<unknown>): Promise<any> { |
| 87 | + return run().then( |
| 88 | + () => { |
| 89 | + throw new Error('expected the write to be refused'); |
| 90 | + }, |
| 91 | + (e) => e as any, |
| 92 | + ); |
| 93 | +} |
| 94 | + |
| 95 | +const DOORS: Array<[string, (engine: ObjectQL) => Promise<unknown>]> = [ |
| 96 | + ['insert', (engine) => engine.insert('crm_account', { name: 'Acme' })], |
| 97 | + ['batch insert', (engine) => engine.insert('crm_account', [{ name: 'Acme' }, { name: 'Acme' }])], |
| 98 | + ['update by id', (engine) => engine.update('crm_account', { id: 'r1', name: 'Acme' })], |
| 99 | + ['update by predicate', (engine) => engine.update('crm_account', { name: 'Acme' }, { where: { name: 'Old' }, multi: true } as any)], |
| 100 | +]; |
| 101 | + |
| 102 | +describe('a unique refusal names the object and field by label (#24951)', () => { |
| 103 | + beforeEach(() => { |
| 104 | + vi.clearAllMocks(); |
| 105 | + }); |
| 106 | + |
| 107 | + describe.each(DOORS)('on %s', (_door, write) => { |
| 108 | + it('a labelled object and field: the sentence names both labels, and no API name', async () => { |
| 109 | + const engine = makeRig(LABELLED, namedColumn); |
| 110 | + await engine.init(); |
| 111 | + |
| 112 | + const failure = await refusalOf(() => write(engine)); |
| 113 | + |
| 114 | + expect(failure).toBeInstanceOf(DuplicateRecordError); |
| 115 | + expect(failure.code).toBe('DUPLICATE_RECORD'); |
| 116 | + expect(failure.status).toBe(409); |
| 117 | + expect(failure.message).toBe( |
| 118 | + "Duplicate record refused on 'Account': a unique constraint on 'Account Name' already holds this value. " + |
| 119 | + 'No record was written.', |
| 120 | + ); |
| 121 | + expect(failure.message).not.toContain('crm_account'); |
| 122 | + // The API names stay where diagnostics read them. |
| 123 | + expect(failure.object).toBe('crm_account'); |
| 124 | + expect(failure.field).toBe('name'); |
| 125 | + }); |
| 126 | + |
| 127 | + it('an unlabelled object and field: the sentence falls back to the API names', async () => { |
| 128 | + const engine = makeRig(UNLABELLED, namedColumn); |
| 129 | + await engine.init(); |
| 130 | + |
| 131 | + const failure = await refusalOf(() => write(engine)); |
| 132 | + |
| 133 | + expect(failure.code).toBe('DUPLICATE_RECORD'); |
| 134 | + expect(failure.status).toBe(409); |
| 135 | + expect(failure.message).toBe( |
| 136 | + "Duplicate record refused on 'crm_account': a unique constraint on 'name' already holds this value. " + |
| 137 | + 'No record was written.', |
| 138 | + ); |
| 139 | + }); |
| 140 | + |
| 141 | + it('no determinable field: the field-less sentence, naming the object by label', async () => { |
| 142 | + const engine = makeRig(LABELLED, indexOnly); |
| 143 | + await engine.init(); |
| 144 | + |
| 145 | + const failure = await refusalOf(() => write(engine)); |
| 146 | + |
| 147 | + expect(failure.code).toBe('DUPLICATE_RECORD'); |
| 148 | + expect(failure.status).toBe(409); |
| 149 | + expect(failure.field).toBeUndefined(); |
| 150 | + expect(failure.message).toBe( |
| 151 | + "Duplicate record refused on 'Account': a unique constraint already holds these values. No record was written.", |
| 152 | + ); |
| 153 | + }); |
| 154 | + |
| 155 | + it('`developerMessage` names the API names, never the labels', async () => { |
| 156 | + const engine = makeRig(LABELLED, namedColumn); |
| 157 | + await engine.init(); |
| 158 | + |
| 159 | + const failure = await refusalOf(() => write(engine)); |
| 160 | + |
| 161 | + expect(failure.developerMessage).toMatch(/^Duplicate record refused on object 'crm_account', field 'name'\. /); |
| 162 | + expect(failure.developerMessage).not.toContain('Account Name'); |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + it('a field-less refusal\'s `developerMessage` names the object API name alone', async () => { |
| 167 | + const engine = makeRig(LABELLED, indexOnly); |
| 168 | + await engine.init(); |
| 169 | + |
| 170 | + const failure = await refusalOf(() => engine.insert('crm_account', { name: 'Acme' })); |
| 171 | + |
| 172 | + expect(failure.developerMessage).toMatch(/^Duplicate record refused on object 'crm_account'\. /); |
| 173 | + }); |
| 174 | +}); |
0 commit comments