|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect } from 'vitest'; |
| 4 | + |
| 5 | +import { |
| 6 | + MAPPING_TARGET_FIELD_UNKNOWN, |
| 7 | + validateMappingTargetFields, |
| 8 | +} from './validate-mapping-target-fields.js'; |
| 9 | + |
| 10 | +// [#20150] The author-time half of the import door's refusal: a mapping target |
| 11 | +// that names no field of its object is red at `os validate`, and a mapping |
| 12 | +// whose targets resolve (declared fields and provisioned columns alike) is |
| 13 | +// green. The verdict is the spec's; this pins the rule's reading of a stack. |
| 14 | + |
| 15 | +const contact = { |
| 16 | + name: 'crm_contact', |
| 17 | + fields: { |
| 18 | + full_name: { type: 'text' }, |
| 19 | + email: { type: 'email' }, |
| 20 | + mailing_address: { type: 'address' }, |
| 21 | + }, |
| 22 | +}; |
| 23 | + |
| 24 | +const mapping = (fieldMapping: unknown[], extra: Record<string, unknown> = {}) => ({ |
| 25 | + name: 'contact_import', |
| 26 | + targetObject: 'crm_contact', |
| 27 | + fieldMapping, |
| 28 | + ...extra, |
| 29 | +}); |
| 30 | + |
| 31 | +describe('validateMappingTargetFields', () => { |
| 32 | + it('reports a target that names no field of the object, located at the mapping (the card\'s shape)', () => { |
| 33 | + const findings = validateMappingTargetFields({ |
| 34 | + objects: [contact], |
| 35 | + mappings: [mapping([ |
| 36 | + { source: 'Name', target: 'full_name' }, |
| 37 | + { source: 'Street', target: 'mailing_address.street' }, |
| 38 | + ])], |
| 39 | + }); |
| 40 | + expect(findings).toHaveLength(1); |
| 41 | + expect(findings[0]).toMatchObject({ |
| 42 | + severity: 'error', |
| 43 | + rule: MAPPING_TARGET_FIELD_UNKNOWN, |
| 44 | + where: 'mapping "contact_import" · object "crm_contact"', |
| 45 | + path: 'mappings[0].fieldMapping[1].target', |
| 46 | + }); |
| 47 | + // Names the target and the object, and says why a dotted path is not one. |
| 48 | + expect(findings[0].message).toContain('"mailing_address.street"'); |
| 49 | + expect(findings[0].message).toContain('"crm_contact"'); |
| 50 | + expect(findings[0].message).toContain('"mailing_address" is a field of "crm_contact"'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('is green on the control mapping — declared fields only', () => { |
| 54 | + expect(validateMappingTargetFields({ |
| 55 | + objects: [contact], |
| 56 | + mappings: [mapping([ |
| 57 | + { source: 'Name', target: 'full_name' }, |
| 58 | + { source: 'Mail', target: 'email', transform: 'map', params: { valueMap: {} } }, |
| 59 | + ])], |
| 60 | + })).toEqual([]); |
| 61 | + }); |
| 62 | + |
| 63 | + it('is green on a target naming a column the platform provisions on this object', () => { |
| 64 | + // The import door accepts these (the registry injects them, the write door |
| 65 | + // admits them); the author-time check must never refuse what the door accepts. |
| 66 | + expect(validateMappingTargetFields({ |
| 67 | + objects: [contact], |
| 68 | + mappings: [mapping([ |
| 69 | + { source: 'Owner', target: 'owner_id', transform: 'lookup' }, |
| 70 | + { source: 'Created', target: 'created_at' }, |
| 71 | + { source: 'Id', target: 'id' }, |
| 72 | + ])], |
| 73 | + })).toEqual([]); |
| 74 | + }); |
| 75 | + |
| 76 | + it('refuses a provisioned column the object does NOT carry (ownership: none has no owner_id)', () => { |
| 77 | + const findings = validateMappingTargetFields({ |
| 78 | + objects: [{ ...contact, ownership: 'none' }], |
| 79 | + mappings: [mapping([{ source: 'Owner', target: 'owner_id' }])], |
| 80 | + }); |
| 81 | + expect(findings.map((f) => f.path)).toEqual(['mappings[0].fieldMapping[0].target']); |
| 82 | + }); |
| 83 | + |
| 84 | + it('judges every element of an array target', () => { |
| 85 | + const findings = validateMappingTargetFields({ |
| 86 | + objects: [contact], |
| 87 | + mappings: [mapping([{ source: 'Full', target: ['full_name', 'nick_name'], transform: 'split' }])], |
| 88 | + }); |
| 89 | + expect(findings.map((f) => f.path)).toEqual(['mappings[0].fieldMapping[0].target[1]']); |
| 90 | + expect(findings[0].message).toContain('"nick_name"'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('folds fields an objectExtensions entry merges into the object — from the stack and from a sibling package', () => { |
| 94 | + const stack = { |
| 95 | + objects: [contact], |
| 96 | + objectExtensions: [{ extend: 'crm_contact', fields: { sla_tier: { type: 'text' } } }], |
| 97 | + packages: [{ manifest: { objectExtensions: [{ extend: 'crm_contact', fields: { region: { type: 'text' } } }] } }], |
| 98 | + mappings: [mapping([ |
| 99 | + { source: 'Tier', target: 'sla_tier' }, |
| 100 | + { source: 'Region', target: 'region' }, |
| 101 | + ])], |
| 102 | + }; |
| 103 | + expect(validateMappingTargetFields(stack)).toEqual([]); |
| 104 | + }); |
| 105 | + |
| 106 | + it('stays silent on a mapping whose object this stack does not define (skip 1)', () => { |
| 107 | + expect(validateMappingTargetFields({ |
| 108 | + objects: [contact], |
| 109 | + mappings: [{ name: 'user_import', targetObject: 'sys_user', fieldMapping: [{ source: 'x', target: 'zzz' }] }], |
| 110 | + })).toEqual([]); |
| 111 | + }); |
| 112 | + |
| 113 | + it('stays silent on an object with no readable field map (skip 2)', () => { |
| 114 | + expect(validateMappingTargetFields({ |
| 115 | + objects: [{ name: 'ext_orders', external: true }], |
| 116 | + mappings: [{ name: 'orders', targetObject: 'ext_orders', fieldMapping: [{ source: 'x', target: 'zzz' }] }], |
| 117 | + })).toEqual([]); |
| 118 | + }); |
| 119 | + |
| 120 | + it('returns nothing for a stack without mappings, and survives junk entries', () => { |
| 121 | + expect(validateMappingTargetFields({ objects: [contact] })).toEqual([]); |
| 122 | + expect(() => validateMappingTargetFields({ objects: [null, contact], mappings: [null, 7] } as never)).not.toThrow(); |
| 123 | + }); |
| 124 | +}); |
0 commit comments