|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [ADR-0146 D10, second rule] The field-patch target check: the one judge the |
| 5 | + * marketplace install door, `os validate` and the registry fold's boot log ask. |
| 6 | + */ |
| 7 | + |
| 8 | +import { describe, it, expect } from 'vitest'; |
| 9 | +import { |
| 10 | + fieldPatchTargetMisses, |
| 11 | + fieldPatchTargetRefusals, |
| 12 | + fieldPatchTargetRefusalMessage, |
| 13 | +} from './extension-field-patch-targets.js'; |
| 14 | + |
| 15 | +const ACCOUNT = { |
| 16 | + name: 'crm_account', |
| 17 | + fields: { |
| 18 | + industry: { |
| 19 | + type: 'select', |
| 20 | + label: 'Industry', |
| 21 | + options: [{ value: 'tech', label: 'Tech' }, { value: 'retail', label: 'Retail' }], |
| 22 | + }, |
| 23 | + segment: { type: 'select', label: 'Segment', picklist: 'crm_segments' }, |
| 24 | + notes: { type: 'textarea', label: 'Notes' }, |
| 25 | + }, |
| 26 | +}; |
| 27 | + |
| 28 | +describe('fieldPatchTargetMisses', () => { |
| 29 | + it('finds nothing to say about a patch whose fields and option values the target has', () => { |
| 30 | + expect(fieldPatchTargetMisses('crm_account', { |
| 31 | + industry: { label: 'Sector', options: { tech: { label: 'Technology' } } }, |
| 32 | + notes: { placeholder: 'Anything else' }, |
| 33 | + }, ACCOUNT)).toEqual([]); |
| 34 | + }); |
| 35 | + |
| 36 | + it('names a field the target does not have', () => { |
| 37 | + const [miss, ...rest] = fieldPatchTargetMisses('crm_account', { sector: { label: 'Sector' } }, ACCOUNT); |
| 38 | + expect(rest).toEqual([]); |
| 39 | + expect(miss).toMatchObject({ object: 'crm_account', field: 'sector', reason: 'field' }); |
| 40 | + expect(miss!.option).toBeUndefined(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('names an option value the field does not have, and lists the values it does', () => { |
| 44 | + const misses = fieldPatchTargetMisses('crm_account', { industry: { options: { finance: { label: 'Finance' } } } }, ACCOUNT); |
| 45 | + expect(misses).toMatchObject([{ field: 'industry', option: 'finance', reason: 'option' }]); |
| 46 | + expect(misses[0]!.message).toContain('`tech`, `retail`'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('names an option patch on a field without options, and on a picklist-bound field', () => { |
| 50 | + expect(fieldPatchTargetMisses('crm_account', { notes: { options: { a: { label: 'A' } } } }, ACCOUNT)) |
| 51 | + .toMatchObject([{ field: 'notes', option: 'a', reason: 'option' }]); |
| 52 | + expect(fieldPatchTargetMisses('crm_account', { segment: { options: { smb: { label: 'SMB' } } } }, ACCOUNT)) |
| 53 | + .toMatchObject([{ field: 'segment', option: 'smb', reason: 'picklist-option' }]); |
| 54 | + }); |
| 55 | + |
| 56 | + it('reads a target whose fields are a name-bearing array', () => { |
| 57 | + const arrayTarget = { name: 'crm_account', fields: [{ name: 'industry', type: 'text', label: 'Industry' }] }; |
| 58 | + expect(fieldPatchTargetMisses('crm_account', { industry: { label: 'Sector' } }, arrayTarget)).toEqual([]); |
| 59 | + expect(fieldPatchTargetMisses('crm_account', { sector: { label: 'Sector' } }, arrayTarget)).toHaveLength(1); |
| 60 | + }); |
| 61 | + |
| 62 | + it('does not read an inherited name as a field', () => { |
| 63 | + expect(fieldPatchTargetMisses('crm_account', { toString: { label: 'X' } }, ACCOUNT)).toHaveLength(1); |
| 64 | + }); |
| 65 | + |
| 66 | + it('judges nothing when the patch is absent or malformed', () => { |
| 67 | + expect(fieldPatchTargetMisses('crm_account', undefined, ACCOUNT)).toEqual([]); |
| 68 | + expect(fieldPatchTargetMisses('crm_account', ['industry'], ACCOUNT)).toEqual([]); |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +describe('fieldPatchTargetRefusals', () => { |
| 73 | + const extensions = [ |
| 74 | + { extend: 'crm_account', fieldPatches: { industry: { label: 'Sector' } } }, |
| 75 | + { extend: 'crm_account', fieldPatches: { sector: { label: 'X' }, industry: { options: { finance: { label: 'F' } } } } }, |
| 76 | + { extend: 'crm_lead', fieldPatches: { anything: { label: 'Y' } } }, |
| 77 | + { extend: 'crm_account', fields: { tier: { type: 'text' } } }, |
| 78 | + ]; |
| 79 | + const getTarget = (name: string) => (name === 'crm_account' ? ACCOUNT : undefined); |
| 80 | + |
| 81 | + it('locates each miss under its entry, and skips a target the reader does not know', () => { |
| 82 | + expect(fieldPatchTargetRefusals(extensions, getTarget, 'packages[1].manifest.').map((r) => r.path)).toEqual([ |
| 83 | + 'packages[1].manifest.objectExtensions[1].fieldPatches.sector', |
| 84 | + 'packages[1].manifest.objectExtensions[1].fieldPatches.industry.options.finance', |
| 85 | + ]); |
| 86 | + }); |
| 87 | + |
| 88 | + it('answers [] for a manifest with no objectExtensions', () => { |
| 89 | + expect(fieldPatchTargetRefusals(undefined, getTarget)).toEqual([]); |
| 90 | + }); |
| 91 | + |
| 92 | + it('the install sentence names the package and locates the first refusal', () => { |
| 93 | + const refusals = fieldPatchTargetRefusals(extensions, getTarget); |
| 94 | + const message = fieldPatchTargetRefusalMessage('com.acme.display', refusals); |
| 95 | + expect(message).toContain('com.acme.display'); |
| 96 | + expect(message).toContain('objectExtensions[1].fieldPatches.sector'); |
| 97 | + }); |
| 98 | +}); |
0 commit comments