|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#36980] The `DUPLICATE_MATCH_KEY` row refusal reads in the importing |
| 5 | + * principal's language and names the key by field LABEL, as the cell-coercion |
| 6 | + * failures in the same row report already do. It is rendered from the shared |
| 7 | + * validation catalog (`validation.field.import_duplicate_match_key`) with the |
| 8 | + * request locale and the deployment's override hook; the row numbers stay in |
| 9 | + * the sentence, and the row result keeps its shape (`row`, `ok`, `action`, |
| 10 | + * `code`, `error`). |
| 11 | + * |
| 12 | + * Driven against an `ImportProtocolLike` double; the route over a real engine |
| 13 | + * is pinned in `packages/rest/src/import-upsert-in-file-duplicate-key.test.ts`. |
| 14 | + */ |
| 15 | + |
| 16 | +import { describe, it, expect, vi } from 'vitest'; |
| 17 | +import { renderValidationMessage, type ValidationMessageTranslator } from '@objectstack/spec/system'; |
| 18 | +import { runImport, type ImportProtocolLike } from './import-runner'; |
| 19 | +import type { ExportFieldMeta } from './import-field-meta.js'; |
| 20 | + |
| 21 | +type ValidateArgs = Parameters<NonNullable<ImportProtocolLike['validateData']>>[0]; |
| 22 | + |
| 23 | +const OBJECT = 'crm_lead'; |
| 24 | + |
| 25 | +function protocol(): ImportProtocolLike { |
| 26 | + return { |
| 27 | + findData: vi.fn(async () => []), |
| 28 | + createData: vi.fn(async (args: any) => ({ record: { id: 'r1', ...args.data } })), |
| 29 | + updateData: vi.fn(), |
| 30 | + validateData: vi.fn(async (args: ValidateArgs) => ({ |
| 31 | + object: OBJECT, mode: args.mode ?? 'insert', valid: true, |
| 32 | + results: [{ valid: true, errors: [], warnings: [] }], |
| 33 | + posture: { valueShapeStrict: false, mediaValueShapeStrict: false }, |
| 34 | + })), |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +const meta = (labels: { email?: string; company?: string }) => new Map<string, ExportFieldMeta>([ |
| 39 | + ['first_name', { name: 'first_name', type: 'text', label: 'First Name' }], |
| 40 | + ['email', { name: 'email', type: 'email', ...(labels.email !== undefined ? { label: labels.email } : {}) }], |
| 41 | + ['company', { name: 'company', type: 'text', ...(labels.company !== undefined ? { label: labels.company } : {}) }], |
| 42 | +]); |
| 43 | + |
| 44 | +/** Rows 1 and 3 share the key; row 2 does not. */ |
| 45 | +const ROWS = [ |
| 46 | + { first_name: 'Ada', email: 'ada@example.com', company: 'Acme' }, |
| 47 | + { first_name: 'Bob', email: 'bob@example.com', company: 'Beta' }, |
| 48 | + { first_name: 'Ada again', email: 'ada@example.com', company: 'Acme' }, |
| 49 | +]; |
| 50 | + |
| 51 | +async function importWith(opts: { |
| 52 | + locale?: string; |
| 53 | + labels: { email?: string; company?: string }; |
| 54 | + matchFields?: string[]; |
| 55 | + translate?: ValidationMessageTranslator; |
| 56 | +}) { |
| 57 | + return runImport({ |
| 58 | + p: protocol(), |
| 59 | + objectName: OBJECT, |
| 60 | + context: (opts.locale ? { locale: opts.locale } : {}) as any, |
| 61 | + rows: ROWS, |
| 62 | + metaMap: meta(opts.labels), |
| 63 | + ...(opts.translate ? { translate: opts.translate } : {}), |
| 64 | + writeMode: 'upsert', |
| 65 | + matchFields: opts.matchFields ?? ['email'], |
| 66 | + dryRun: true, |
| 67 | + runAutomations: false, |
| 68 | + trimWhitespace: true, |
| 69 | + createMissingOptions: false, |
| 70 | + skipBlankMatchKey: false, |
| 71 | + }); |
| 72 | +} |
| 73 | + |
| 74 | +const expected = (label: string, locale: string) => |
| 75 | + renderValidationMessage({ messageKey: 'import_duplicate_match_key', label, params: { rows: '1, 3' } }, { locale }); |
| 76 | + |
| 77 | +describe('[#36980] the duplicate-match-key row refusal reads in the request language', () => { |
| 78 | + it('zh-CN: the sentence is the zh-CN catalog entry, names the key by its label and keeps the row numbers', async () => { |
| 79 | + const s = await importWith({ locale: 'zh-CN', labels: { email: '邮箱' } }); |
| 80 | + expect(s).toMatchObject({ ok: 1, errors: 2 }); |
| 81 | + for (const row of [1, 3]) { |
| 82 | + const r = s.results[row - 1]!; |
| 83 | + // The row result keeps its shape: no new member. |
| 84 | + expect(Object.keys(r).sort()).toEqual(['action', 'code', 'error', 'ok', 'row']); |
| 85 | + expect(r).toMatchObject({ row, ok: false, action: 'failed', code: 'DUPLICATE_MATCH_KEY' }); |
| 86 | + expect(r.error).toBe(expected('邮箱', 'zh-CN')); |
| 87 | + expect(r.error).toContain('邮箱'); |
| 88 | + expect(r.error).toContain('1, 3'); |
| 89 | + expect(r.error).not.toContain('email'); |
| 90 | + expect(r.error).toMatch(/\p{Script=Han}/u); |
| 91 | + expect(r.error).not.toBe(expected('邮箱', 'en')); |
| 92 | + } |
| 93 | + expect(s.results[1]).toMatchObject({ row: 2, ok: true }); |
| 94 | + }); |
| 95 | + |
| 96 | + it('en: the English sentence names the key by its label, never the API name', async () => { |
| 97 | + const s = await importWith({ locale: 'en', labels: { email: 'Email' } }); |
| 98 | + const r = s.results[0]!; |
| 99 | + expect(r).toMatchObject({ code: 'DUPLICATE_MATCH_KEY', error: expected('Email', 'en') }); |
| 100 | + expect(r.error).toContain('(Email)'); |
| 101 | + expect(r.error).toContain('1, 3'); |
| 102 | + expect(r.error).not.toMatch(/\p{Script=Han}/u); |
| 103 | + }); |
| 104 | + |
| 105 | + it('no locale on the context renders the English catalog entry', async () => { |
| 106 | + const s = await importWith({ labels: { email: 'Email' } }); |
| 107 | + expect(s.results[0]!.error).toBe(expected('Email', 'en')); |
| 108 | + }); |
| 109 | + |
| 110 | + it('a key set of several fields is named by each label; a field with no label falls back to its API name', async () => { |
| 111 | + const s = await importWith({ locale: 'zh-CN', labels: { email: '邮箱' }, matchFields: ['email', 'company'] }); |
| 112 | + expect(s.results[0]!.error).toBe(expected('邮箱 + company', 'zh-CN')); |
| 113 | + const labelled = await importWith({ locale: 'zh-CN', labels: { email: '邮箱', company: '公司' }, matchFields: ['email', 'company'] }); |
| 114 | + expect(labelled.results[0]!.error).toBe(expected('邮箱 + 公司', 'zh-CN')); |
| 115 | + }); |
| 116 | + |
| 117 | + it("a deployment's `validation.field.import_duplicate_match_key` override wins, filled with the same label and rows", async () => { |
| 118 | + const translate: ValidationMessageTranslator = (key, locale) => |
| 119 | + key === 'validation.field.import_duplicate_match_key' && locale === 'zh-CN' |
| 120 | + ? '重复的匹配键 {{label}}:第 {{rows}} 行' : key; |
| 121 | + const s = await importWith({ locale: 'zh-CN', labels: { email: '邮箱' }, translate }); |
| 122 | + expect(s.results[0]!.error).toBe('重复的匹配键 邮箱:第 1, 3 行'); |
| 123 | + }); |
| 124 | +}); |
0 commit comments