|
20 | 20 | import { describe, it, expect, vi, afterEach } from 'vitest'; |
21 | 21 | import { SchemaRegistry } from './registry.js'; |
22 | 22 | import { ObjectQL } from './engine.js'; |
| 23 | +import { fieldPatchTargetRefusals } from '@objectstack/metadata-core'; |
23 | 24 |
|
24 | 25 | const OWNER_PKG = 'com.vendor.crm'; |
25 | 26 | const EXT_PKG = 'com.acme.crm-display'; |
@@ -243,6 +244,150 @@ describe('ADR-0146 D10 — fieldPatches is applied by mergeObjectDefinitions on |
243 | 244 | }); |
244 | 245 | }); |
245 | 246 |
|
| 247 | +describe('#28809 — an option patch is judged against the owner\'s declared options, at the fold as at the doors', () => { |
| 248 | + const TAKEOVER_PKG = 'com.acme.industry-takeover'; |
| 249 | + |
| 250 | + /** |
| 251 | + * `crm_account.industry` taken over through `fields`: the owner's two |
| 252 | + * options, the second relabelled, plus `finance`, which the owner does not |
| 253 | + * declare — or, with `dropRetail`, the owner's `tech` alone. |
| 254 | + */ |
| 255 | + const takenOverIndustry = ({ dropRetail = false } = {}) => ({ |
| 256 | + ...vendorAccount().fields.industry, |
| 257 | + options: dropRetail |
| 258 | + ? [{ value: 'tech', label: 'Tech' }] |
| 259 | + : [ |
| 260 | + { value: 'tech', label: 'Tech' }, |
| 261 | + { value: 'retail', label: 'Retail stores' }, |
| 262 | + { value: 'finance', label: 'Finance' }, |
| 263 | + ], |
| 264 | + }); |
| 265 | + |
| 266 | + function takeOver(reg: SchemaRegistry, priority: number, opts: { dropRetail?: boolean } = {}): void { |
| 267 | + reg.registerObject( |
| 268 | + { name: OBJ, fields: { industry: takenOverIndustry(opts) } } as any, |
| 269 | + TAKEOVER_PKG, undefined, 'extend', priority, |
| 270 | + ); |
| 271 | + } |
| 272 | + |
| 273 | + /** |
| 274 | + * What the install door (`getObjectOwner(name).definition`) and `os validate` |
| 275 | + * (the composed stack's declared object) both ask: the one judge, against the |
| 276 | + * owner's declaration. The fold must answer the same. |
| 277 | + */ |
| 278 | + const doorRefusals = (reg: SchemaRegistry, objectExtensions: unknown[]) => |
| 279 | + fieldPatchTargetRefusals(objectExtensions, (name) => reg.getObjectOwner(name)?.definition).map((r) => r.path); |
| 280 | + |
| 281 | + const optionLabel = (body: any, value: string) => |
| 282 | + industryOf(body).options.find((o: any) => o.value === value)?.label; |
| 283 | + |
| 284 | + const patchLines = (warn: { mock: { calls: unknown[][] } }, needle: string): string[] => |
| 285 | + warn.mock.calls.map((c) => String(c[0])).filter((l) => l.includes(needle)); |
| 286 | + |
| 287 | + it('another extension adds an option by redeclaring the owner field: a patch of that option is not applied, and is named once — as the door and os validate refuse it', () => { |
| 288 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 289 | + const reg = withVendor(); |
| 290 | + takeOver(reg, 100); |
| 291 | + const patch = { industry: { options: { finance: { label: 'Banking' } } } }; |
| 292 | + extend(reg, patch, EXT_PKG, 200); |
| 293 | + |
| 294 | + expect(doorRefusals(reg, [{ extend: OBJ, fieldPatches: patch }])) |
| 295 | + .toEqual(['objectExtensions[0].fieldPatches.industry.options.finance']); |
| 296 | + const served = reg.getObject(OBJ) as any; |
| 297 | + // The takeover itself still serves: the option exists, with its own label. |
| 298 | + expect(optionLabel(served, 'finance')).toBe('Finance'); |
| 299 | + reg.invalidateAll(); |
| 300 | + reg.getObject(OBJ); |
| 301 | + const lines = patchLines(warn, 'fieldPatches.industry.options.finance'); |
| 302 | + expect(lines).toHaveLength(1); |
| 303 | + expect(lines[0]).toContain(EXT_PKG); |
| 304 | + }); |
| 305 | + |
| 306 | + it('…and the same extension redeclaring the field and patching its added option is not applied either, and is named once', () => { |
| 307 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 308 | + const reg = withVendor(); |
| 309 | + const entry = { |
| 310 | + fields: { industry: takenOverIndustry() }, |
| 311 | + fieldPatches: { industry: { options: { finance: { label: 'Banking' } } } }, |
| 312 | + }; |
| 313 | + reg.registerObject({ name: OBJ, ...entry } as any, EXT_PKG, undefined, 'extend', 200); |
| 314 | + |
| 315 | + expect(doorRefusals(reg, [{ extend: OBJ, ...entry }])) |
| 316 | + .toEqual(['objectExtensions[0].fieldPatches.industry.options.finance']); |
| 317 | + expect(optionLabel(reg.getObject(OBJ), 'finance')).toBe('Finance'); |
| 318 | + expect(patchLines(warn, 'fieldPatches.industry.options.finance')).toHaveLength(1); |
| 319 | + }); |
| 320 | + |
| 321 | + it('control: the patcher folded BEFORE the redeclaring extension (lower priority) — not applied and named, as on main', () => { |
| 322 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 323 | + const reg = withVendor(); |
| 324 | + takeOver(reg, 200); |
| 325 | + extend(reg, { industry: { options: { finance: { label: 'Banking' } } } }, EXT_PKG, 100); |
| 326 | + |
| 327 | + expect(optionLabel(reg.getObject(OBJ), 'finance')).toBe('Finance'); |
| 328 | + expect(patchLines(warn, 'fieldPatches.industry.options.finance')).toHaveLength(1); |
| 329 | + }); |
| 330 | + |
| 331 | + it('control: a patch of an option the owner declares still applies onto the redeclared field, and nothing is named', () => { |
| 332 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 333 | + const reg = withVendor(); |
| 334 | + takeOver(reg, 100); |
| 335 | + const patch = { industry: { label: 'Sector', options: { tech: { label: 'Technology' }, retail: { label: 'Shops' } } } }; |
| 336 | + extend(reg, patch, EXT_PKG, 200); |
| 337 | + |
| 338 | + expect(doorRefusals(reg, [{ extend: OBJ, fieldPatches: patch }])).toEqual([]); |
| 339 | + const served = reg.getObject(OBJ) as any; |
| 340 | + expect(industryOf(served).label).toBe('Sector'); |
| 341 | + expect(optionLabel(served, 'tech')).toBe('Technology'); |
| 342 | + expect(optionLabel(served, 'retail')).toBe('Shops'); |
| 343 | + expect(optionLabel(served, 'finance')).toBe('Finance'); |
| 344 | + expect(patchLines(warn, 'fieldPatches')).toEqual([]); |
| 345 | + }); |
| 346 | + |
| 347 | + it('residual: an owner option an earlier extension\'s redeclaration left out — the judge admits the patch, the fold has no such option to relabel, nothing of it is applied, and it is named once', () => { |
| 348 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 349 | + const reg = withVendor(); |
| 350 | + takeOver(reg, 100, { dropRetail: true }); |
| 351 | + const patch = { industry: { label: 'Sector', options: { retail: { label: 'Shops' } } } }; |
| 352 | + extend(reg, patch, EXT_PKG, 200); |
| 353 | + |
| 354 | + // The door and os validate admit it: `retail` is in the owner's declaration. |
| 355 | + expect(doorRefusals(reg, [{ extend: OBJ, fieldPatches: patch }])).toEqual([]); |
| 356 | + const served = reg.getObject(OBJ) as any; |
| 357 | + // The takeover's options are served as the takeover declares them; the |
| 358 | + // patch's field key still applies. |
| 359 | + expect(industryOf(served).options).toEqual([{ value: 'tech', label: 'Tech' }]); |
| 360 | + expect(industryOf(served).label).toBe('Sector'); |
| 361 | + reg.invalidateAll(); |
| 362 | + reg.getObject(OBJ); |
| 363 | + // Named once (what the fold skipped), by the same judge asked of the merged field. |
| 364 | + const lines = patchLines(warn, 'fieldPatches'); |
| 365 | + expect(lines).toHaveLength(1); |
| 366 | + expect(lines[0]).toContain('fieldPatches.industry.options.retail'); |
| 367 | + expect(lines[0]).toContain(EXT_PKG); |
| 368 | + }); |
| 369 | + |
| 370 | + it('residual: a tenant overlay base that leaves an owner option out — admitted, nothing to relabel, named once; an option the tenant adds is not patched', () => { |
| 371 | + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); |
| 372 | + const reg = withVendor(); |
| 373 | + const patch = { industry: { options: { retail: { label: 'Shops' }, wholesale: { label: 'Bulk' } } } }; |
| 374 | + extend(reg, patch, EXT_PKG, 200); |
| 375 | + const overlay = vendorAccount(); |
| 376 | + overlay.fields.industry.options = [{ value: 'tech', label: 'Tech' }, { value: 'wholesale', label: 'Wholesale' }]; |
| 377 | + reg.registerObject({ ...overlay, _provenance: 'org' }, 'sys_metadata', undefined, 'overlay'); |
| 378 | + |
| 379 | + // The doors read the owner: `retail` admitted, `wholesale` (the tenant's) refused. |
| 380 | + expect(doorRefusals(reg, [{ extend: OBJ, fieldPatches: patch }])) |
| 381 | + .toEqual(['objectExtensions[0].fieldPatches.industry.options.wholesale']); |
| 382 | + const served = reg.getObject(OBJ) as any; |
| 383 | + expect(industryOf(served).options).toEqual([{ value: 'tech', label: 'Tech' }, { value: 'wholesale', label: 'Wholesale' }]); |
| 384 | + reg.invalidateAll(); |
| 385 | + reg.getObject(OBJ); |
| 386 | + expect(patchLines(warn, 'fieldPatches.industry.options.retail')).toHaveLength(1); |
| 387 | + expect(patchLines(warn, 'fieldPatches.industry.options.wholesale')).toHaveLength(1); |
| 388 | + }); |
| 389 | +}); |
| 390 | + |
246 | 391 | describe('ADR-0146 D10 — the manifest loop carries fieldPatches onto the extension def', () => { |
247 | 392 | it('a package\'s objectExtensions entry patches the target through ObjectQL.registerApp', () => { |
248 | 393 | const ql = new ObjectQL(); |
|
0 commit comments