|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#27947] An import cell is also resolved against a lookup TARGET's unique |
| 5 | + * fields: every field whose `unique` declares a constraint (`isUniqueDeclared`: |
| 6 | + * `true`, `'organization'`, `'global'`), and every `autonumber` field that |
| 7 | + * omits `unique`, which the spec makes `unique: 'organization'` by contract. |
| 8 | + * An autonumber that writes `unique: false` has opted out and is not asked. |
| 9 | + * |
| 10 | + * They are asked AFTER the existing candidates (the id, the lookup's |
| 11 | + * `displayField`, the target's title field, the fixed human keys), in the |
| 12 | + * target's declared field order, each at most once. The existing rules hold: |
| 13 | + * the first field to match wins, and a field matching more than one record is |
| 14 | + * `reference_ambiguous`, never a silent pick. |
| 15 | + * |
| 16 | + * The fake's `findData` answers like the engine: equality over the stored row. |
| 17 | + */ |
| 18 | + |
| 19 | +import { describe, it, expect, vi } from 'vitest'; |
| 20 | +import { runImport, type ImportProtocolLike } from './import-runner'; |
| 21 | +import type { ExportFieldMeta } from './import-field-meta.js'; |
| 22 | + |
| 23 | +type FindArgs = Parameters<ImportProtocolLike['findData']>[0]; |
| 24 | +type Row = Record<string, unknown>; |
| 25 | +type Declaration = { fields: Record<string, Row>; nameField?: string }; |
| 26 | + |
| 27 | +const TEXT = { type: 'text' }; |
| 28 | + |
| 29 | +const DECLARATIONS: Record<string, Declaration> = { |
| 30 | + xu_account: { |
| 31 | + nameField: 'name', |
| 32 | + fields: { |
| 33 | + name: TEXT, |
| 34 | + // The contract default: an autonumber that omits `unique` is unique. |
| 35 | + account_number: { type: 'autonumber', autonumberFormat: 'ACC-{000000}' }, |
| 36 | + region: TEXT, |
| 37 | + tax_id: { type: 'text', unique: true }, |
| 38 | + // An opted-out autonumber: a display-only ordinal, never asked. |
| 39 | + ordinal: { type: 'autonumber', unique: false }, |
| 40 | + external_code: { type: 'text', unique: 'global' }, |
| 41 | + // Already a fixed key: asked once, in its fixed place. |
| 42 | + email: { type: 'email', unique: 'organization' }, |
| 43 | + }, |
| 44 | + }, |
| 45 | +}; |
| 46 | + |
| 47 | +const ROWS: Record<string, Row[]> = { |
| 48 | + xu_account: [ |
| 49 | + { id: 'a1', name: 'Acme', account_number: 'ACC-000012', region: 'EMEA', tax_id: 'TX-1', ordinal: '7', external_code: 'EXT-1' }, |
| 50 | + // A name that equals another record's autonumber: the name is asked first. |
| 51 | + { id: 'a2', name: 'ACC-000013', account_number: 'ACC-000002', region: 'APAC', tax_id: 'TX-2', ordinal: '8' }, |
| 52 | + { id: 'a3', name: 'Globex', account_number: 'ACC-000013', region: 'APAC', tax_id: 'TX-3', ordinal: '9' }, |
| 53 | + // Two holders of one tax id, as a read across organizations can see. |
| 54 | + { id: 'a4', name: 'Initech', account_number: 'ACC-000004', tax_id: 'TX-SHARED', ordinal: '10' }, |
| 55 | + { id: 'a5', name: 'Umbrella', account_number: 'ACC-000005', tax_id: 'TX-SHARED', ordinal: '11' }, |
| 56 | + ], |
| 57 | +}; |
| 58 | + |
| 59 | +const metaMap = new Map<string, ExportFieldMeta>([ |
| 60 | + ['name', { name: 'name', type: 'text' }], |
| 61 | + ['account', { name: 'account', type: 'lookup', reference: 'xu_account' }], |
| 62 | +]); |
| 63 | + |
| 64 | +function protocol() { |
| 65 | + const findData = vi.fn(async (args: FindArgs) => { |
| 66 | + const [[field, value]] = Object.entries(args.query!.where!); |
| 67 | + return (ROWS[args.object] ?? []).filter((r) => r[field] === value); |
| 68 | + }); |
| 69 | + const getMetaItem = vi.fn(async ({ name }: { type: string; name: string }) => |
| 70 | + ({ type: 'object', name, item: { name, ...DECLARATIONS[name] } })); |
| 71 | + const createData = vi.fn(async (a: { data: Row }) => ({ id: 'new', ...a.data })); |
| 72 | + const p: ImportProtocolLike = { findData, getMetaItem, createData, updateData: vi.fn() }; |
| 73 | + return { p, findData, createData }; |
| 74 | +} |
| 75 | + |
| 76 | +const run = (p: ImportProtocolLike, rows: Row[]) => |
| 77 | + runImport({ |
| 78 | + p, context: {}, objectName: 'xu_source', metaMap, writeMode: 'insert', matchFields: [], dryRun: false, |
| 79 | + runAutomations: false, trimWhitespace: true, createMissingOptions: false, skipBlankMatchKey: false, rows, |
| 80 | + }); |
| 81 | + |
| 82 | +/** The `where` keys asked of the target, in order. */ |
| 83 | +const asked = (findData: ReturnType<typeof protocol>['findData']) => |
| 84 | + findData.mock.calls.filter(([a]) => a.object === 'xu_account').map(([a]) => Object.keys(a.query!.where!)[0]); |
| 85 | + |
| 86 | +const verdict = (r: { ok: boolean; code?: string; field?: string }) => ({ ok: r.ok, code: r.code, field: r.field }); |
| 87 | +const OK = { ok: true, code: undefined, field: undefined }; |
| 88 | +const FIXED = ['id', 'name', 'title', 'label', 'full_name', 'email', 'username']; |
| 89 | + |
| 90 | +describe('[#27947] import resolves a lookup cell by the target object\'s unique fields', () => { |
| 91 | + it('an autonumber cell (ACC-000012) resolves, asked after every existing candidate', async () => { |
| 92 | + const { p, findData, createData } = protocol(); |
| 93 | + const summary = await run(p, [{ name: 'r', account: 'ACC-000012' }]); |
| 94 | + expect(summary.results.map(verdict)).toEqual([OK]); |
| 95 | + expect(createData.mock.calls.map(([a]) => a.data.account)).toEqual(['a1']); |
| 96 | + expect(asked(findData)).toEqual([...FIXED, 'account_number']); |
| 97 | + }); |
| 98 | + |
| 99 | + it('another declared-unique field (tax_id, unique: true) resolves', async () => { |
| 100 | + const { p, findData, createData } = protocol(); |
| 101 | + const summary = await run(p, [{ name: 'r', account: 'TX-3' }]); |
| 102 | + expect(summary.results.map(verdict)).toEqual([OK]); |
| 103 | + expect(createData.mock.calls.map(([a]) => a.data.account)).toEqual(['a3']); |
| 104 | + expect(asked(findData)).toEqual([...FIXED, 'account_number', 'tax_id']); |
| 105 | + }); |
| 106 | + |
| 107 | + it('an earlier candidate that matches still wins: the name ACC-000013 links a2, not a3\'s autonumber', async () => { |
| 108 | + const { p, findData, createData } = protocol(); |
| 109 | + const summary = await run(p, [{ name: 'r', account: 'ACC-000013' }]); |
| 110 | + expect(summary.results.map(verdict)).toEqual([OK]); |
| 111 | + expect(createData.mock.calls.map(([a]) => a.data.account)).toEqual(['a2']); |
| 112 | + expect(asked(findData)).toEqual(['id', 'name']); |
| 113 | + }); |
| 114 | + |
| 115 | + it('a value matching nothing still answers reference_not_found, after the unique fields in declared order', async () => { |
| 116 | + const { p, findData, createData } = protocol(); |
| 117 | + const summary = await run(p, [{ name: 'r', account: 'ACC-999999' }]); |
| 118 | + expect(summary.results.map(verdict)).toEqual([{ ok: false, code: 'reference_not_found', field: 'account' }]); |
| 119 | + expect(createData).not.toHaveBeenCalled(); |
| 120 | + // Declared order, `email` not asked twice, the opted-out `ordinal` never. |
| 121 | + expect(asked(findData)).toEqual([...FIXED, 'account_number', 'tax_id', 'external_code']); |
| 122 | + }); |
| 123 | + |
| 124 | + it('an opted-out autonumber (unique: false) is not asked: its value answers reference_not_found', async () => { |
| 125 | + const { p, findData } = protocol(); |
| 126 | + const summary = await run(p, [{ name: 'r', account: '7' }]); |
| 127 | + expect(summary.results.map(verdict)).toEqual([{ ok: false, code: 'reference_not_found', field: 'account' }]); |
| 128 | + expect(asked(findData)).not.toContain('ordinal'); |
| 129 | + }); |
| 130 | + |
| 131 | + it('a unique field two records answer is still reference_ambiguous, and links neither', async () => { |
| 132 | + const { p, findData, createData } = protocol(); |
| 133 | + const summary = await run(p, [{ name: 'r', account: 'TX-SHARED' }]); |
| 134 | + expect(summary.results.map(verdict)).toEqual([{ ok: false, code: 'reference_ambiguous', field: 'account' }]); |
| 135 | + expect(createData).not.toHaveBeenCalled(); |
| 136 | + expect(asked(findData)).toEqual([...FIXED, 'account_number', 'tax_id']); |
| 137 | + }); |
| 138 | +}); |
0 commit comments