|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#28835] A field name that two contributors of one object both declare in |
| 5 | + * `fields` is named once at the fold, with the definition it serves. |
| 6 | + * |
| 7 | + * `mergeObjectDefinitions` spreads `fields`, so when two extensions of one |
| 8 | + * object both add `x`, or an extension's `fields` redeclares a field the owner |
| 9 | + * declares, the last contribution the fold applies wins: ascending `priority`, |
| 10 | + * and at equal priority the one registered last. That stays the answer (the |
| 11 | + * served definition is not changed here); the fold now says so, once per |
| 12 | + * object, field and set of packages, in the style of the field-patch miss |
| 13 | + * report. `fieldPatches` is not a declaration, and a `.custom` pair's `__c` |
| 14 | + * field never shares a name with a dependent's plain one. |
| 15 | + */ |
| 16 | + |
| 17 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 18 | +import { SchemaRegistry } from './registry.js'; |
| 19 | + |
| 20 | +const OWNER_PKG = 'com.vendor.crm'; |
| 21 | +const DEP_A = 'com.acme.crm-territory'; |
| 22 | +const DEP_B = 'com.globex.crm-regions'; |
| 23 | +const OBJ = 'crm_account'; |
| 24 | + |
| 25 | +function vendorAccount(): any { |
| 26 | + return { |
| 27 | + name: OBJ, |
| 28 | + label: 'Account', |
| 29 | + fields: { |
| 30 | + name: { name: 'name', type: 'text', label: 'Name' }, |
| 31 | + industry: { |
| 32 | + name: 'industry', |
| 33 | + type: 'select', |
| 34 | + label: 'Industry', |
| 35 | + options: [ |
| 36 | + { value: 'tech', label: 'Tech' }, |
| 37 | + { value: 'retail', label: 'Retail' }, |
| 38 | + ], |
| 39 | + }, |
| 40 | + }, |
| 41 | + }; |
| 42 | +} |
| 43 | + |
| 44 | +function withVendor(): SchemaRegistry { |
| 45 | + const reg = new SchemaRegistry({ multiTenant: false, searchCompanion: false } as never); |
| 46 | + reg.logLevel = 'silent'; |
| 47 | + reg.registerObject(vendorAccount(), OWNER_PKG, undefined, 'own'); |
| 48 | + return reg; |
| 49 | +} |
| 50 | + |
| 51 | +function extendWith(reg: SchemaRegistry, pkg: string, body: Record<string, unknown>, priority = 200): void { |
| 52 | + reg.registerObject({ name: OBJ, ...body } as any, pkg, undefined, 'extend', priority); |
| 53 | +} |
| 54 | + |
| 55 | +const regionOf = (pkg: string) => ({ name: 'region', type: 'text', label: `Region (${pkg})` }); |
| 56 | + |
| 57 | +/** The fold's lines about a field declared more than once. */ |
| 58 | +function redeclaredLines(warn: { mock: { calls: unknown[][] } }): string[] { |
| 59 | + return warn.mock.calls.map((c) => String(c[0])).filter((l) => l.includes('is declared in `fields` by')); |
| 60 | +} |
| 61 | + |
| 62 | +/** Every read the fold serves, three times over, with the cache dropped between. */ |
| 63 | +function readEverywhere(reg: SchemaRegistry): void { |
| 64 | + for (let i = 0; i < 3; i += 1) { |
| 65 | + reg.invalidateAll(); |
| 66 | + reg.getObject(OBJ); |
| 67 | + reg.foldObjectExtendersOnto(OBJ, vendorAccount()); |
| 68 | + reg.getArtifactItem('object', OBJ); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +afterEach(() => { |
| 73 | + vi.restoreAllMocks(); |
| 74 | +}); |
| 75 | + |
| 76 | +describe('#28835 — a field two contributors declare in `fields` is named once at the fold, with the definition served', () => { |
| 77 | + it('two dependents both add `region` at equal priority: the one registered last is served, and one line names both and the served one', () => { |
| 78 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 79 | + const reg = withVendor(); |
| 80 | + extendWith(reg, DEP_A, { fields: { region: regionOf(DEP_A) } }); |
| 81 | + extendWith(reg, DEP_B, { fields: { region: regionOf(DEP_B) } }); |
| 82 | + |
| 83 | + const served = reg.getObject(OBJ) as any; |
| 84 | + expect(served.fields.region).toEqual(regionOf(DEP_B)); |
| 85 | + readEverywhere(reg); |
| 86 | + |
| 87 | + const lines = redeclaredLines(warn); |
| 88 | + expect(lines).toHaveLength(1); |
| 89 | + expect(lines[0]).toContain(`Field "region" of object "${OBJ}"`); |
| 90 | + expect(lines[0]).toContain(`extensions from "${DEP_A}", "${DEP_B}"`); |
| 91 | + expect(lines[0]).toContain(`the definition served is the one from "${DEP_B}"`); |
| 92 | + expect(lines[0]).not.toContain(OWNER_PKG); |
| 93 | + }); |
| 94 | + |
| 95 | + it('…by priority, not registration order: the higher-priority dependent registered first is the one served and named', () => { |
| 96 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 97 | + const reg = withVendor(); |
| 98 | + extendWith(reg, DEP_A, { fields: { region: regionOf(DEP_A) } }, 300); |
| 99 | + extendWith(reg, DEP_B, { fields: { region: regionOf(DEP_B) } }, 200); |
| 100 | + |
| 101 | + expect((reg.getObject(OBJ) as any).fields.region).toEqual(regionOf(DEP_A)); |
| 102 | + const lines = redeclaredLines(warn); |
| 103 | + expect(lines).toHaveLength(1); |
| 104 | + expect(lines[0]).toContain(`extensions from "${DEP_B}", "${DEP_A}"`); |
| 105 | + expect(lines[0]).toContain(`the definition served is the one from "${DEP_A}"`); |
| 106 | + }); |
| 107 | + |
| 108 | + it('an extension redeclaring a field the owner declares: the extension\'s definition is served, and one line names the owner, the extension and the served one', () => { |
| 109 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 110 | + const reg = withVendor(); |
| 111 | + const takeover = { name: 'industry', type: 'text', label: 'Industry (free text)' }; |
| 112 | + extendWith(reg, DEP_A, { fields: { industry: takeover } }); |
| 113 | + |
| 114 | + expect((reg.getObject(OBJ) as any).fields.industry).toEqual(takeover); |
| 115 | + readEverywhere(reg); |
| 116 | + |
| 117 | + const lines = redeclaredLines(warn); |
| 118 | + expect(lines).toHaveLength(1); |
| 119 | + expect(lines[0]).toContain(`Field "industry" of object "${OBJ}"`); |
| 120 | + expect(lines[0]).toContain(`the owner "${OWNER_PKG}" and by extensions from "${DEP_A}"`); |
| 121 | + expect(lines[0]).toContain(`the definition served is the one from "${DEP_A}"`); |
| 122 | + }); |
| 123 | + |
| 124 | + it('the owner and two extensions on one field: one line naming all three in fold order', () => { |
| 125 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 126 | + const reg = withVendor(); |
| 127 | + extendWith(reg, DEP_A, { fields: { industry: { name: 'industry', type: 'text', label: 'A' } } }); |
| 128 | + extendWith(reg, DEP_B, { fields: { industry: { name: 'industry', type: 'text', label: 'B' } } }); |
| 129 | + |
| 130 | + expect((reg.getObject(OBJ) as any).fields.industry.label).toBe('B'); |
| 131 | + const lines = redeclaredLines(warn); |
| 132 | + expect(lines).toHaveLength(1); |
| 133 | + expect(lines[0]).toContain(`the owner "${OWNER_PKG}" and by extensions from "${DEP_A}", "${DEP_B}"`); |
| 134 | + expect(lines[0]).toContain(`the definition served is the one from "${DEP_B}"`); |
| 135 | + }); |
| 136 | + |
| 137 | + it('two `.custom` pairs (one base depending on the other) both adding `region__c`: named like any two extensions', () => { |
| 138 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 139 | + const reg = withVendor(); |
| 140 | + extendWith(reg, `${OWNER_PKG}.custom`, { fields: { region__c: { name: 'region__c', type: 'text', label: 'One' } } }); |
| 141 | + extendWith(reg, `${DEP_A}.custom`, { fields: { region__c: { name: 'region__c', type: 'text', label: 'Two' } } }); |
| 142 | + |
| 143 | + expect((reg.getObject(OBJ) as any).fields.region__c.label).toBe('Two'); |
| 144 | + const lines = redeclaredLines(warn); |
| 145 | + expect(lines).toHaveLength(1); |
| 146 | + expect(lines[0]).toContain(`the definition served is the one from "${DEP_A}.custom"`); |
| 147 | + }); |
| 148 | + |
| 149 | + it('a second registry names the same collision again', () => { |
| 150 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 151 | + for (let i = 0; i < 2; i += 1) { |
| 152 | + const reg = withVendor(); |
| 153 | + extendWith(reg, DEP_A, { fields: { region: regionOf(DEP_A) } }); |
| 154 | + extendWith(reg, DEP_B, { fields: { region: regionOf(DEP_B) } }); |
| 155 | + readEverywhere(reg); |
| 156 | + } |
| 157 | + expect(redeclaredLines(warn)).toHaveLength(2); |
| 158 | + }); |
| 159 | + |
| 160 | + describe('controls: nothing is named', () => { |
| 161 | + it('one extension adding a field of its own', () => { |
| 162 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 163 | + const reg = withVendor(); |
| 164 | + extendWith(reg, DEP_A, { fields: { region: regionOf(DEP_A) } }); |
| 165 | + readEverywhere(reg); |
| 166 | + expect((reg.getObject(OBJ) as any).fields.region).toEqual(regionOf(DEP_A)); |
| 167 | + expect(redeclaredLines(warn)).toEqual([]); |
| 168 | + }); |
| 169 | + |
| 170 | + it('a pair\'s `__c` field beside a dependent\'s plain field of the same stem', () => { |
| 171 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 172 | + const reg = withVendor(); |
| 173 | + extendWith(reg, `${OWNER_PKG}.custom`, { fields: { region__c: { name: 'region__c', type: 'text', label: 'Mine' } } }); |
| 174 | + extendWith(reg, DEP_A, { fields: { region: regionOf(DEP_A) } }); |
| 175 | + readEverywhere(reg); |
| 176 | + const served = reg.getObject(OBJ) as any; |
| 177 | + expect(served.fields.region__c.label).toBe('Mine'); |
| 178 | + expect(served.fields.region).toEqual(regionOf(DEP_A)); |
| 179 | + expect(redeclaredLines(warn)).toEqual([]); |
| 180 | + }); |
| 181 | + |
| 182 | + it('a `fieldPatches` entry of an owner field, beside an extension adding a field', () => { |
| 183 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 184 | + const reg = withVendor(); |
| 185 | + extendWith(reg, DEP_A, { fieldPatches: { industry: { label: 'Sector' } } }); |
| 186 | + extendWith(reg, DEP_B, { fields: { region: regionOf(DEP_B) }, fieldPatches: { industry: { label: 'Market' } } }); |
| 187 | + readEverywhere(reg); |
| 188 | + expect((reg.getObject(OBJ) as any).fields.industry.label).toBe('Market'); |
| 189 | + expect(redeclaredLines(warn)).toEqual([]); |
| 190 | + }); |
| 191 | + |
| 192 | + it('a base that already carries the extensions\' fields (a folded body handed back to the fold)', () => { |
| 193 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 194 | + const reg = withVendor(); |
| 195 | + extendWith(reg, DEP_A, { fields: { region: regionOf(DEP_A) } }); |
| 196 | + const folded = reg.getObject(OBJ) as any; |
| 197 | + reg.foldObjectExtendersOnto(OBJ, structuredClone(folded)); |
| 198 | + reg.foldObjectExtendersOnto(OBJ, structuredClone(folded)); |
| 199 | + expect(redeclaredLines(warn)).toEqual([]); |
| 200 | + }); |
| 201 | + }); |
| 202 | +}); |
0 commit comments