|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #30391 — `pluralLabel` is the plural OF `label`. An object extension that |
| 5 | + * renames the object (`label`) and declares no `pluralLabel` leaves the running |
| 6 | + * plural naming the old label, so the fold (`mergeObjectDefinitions`) takes it |
| 7 | + * off and the object is served as one that declares no `pluralLabel`. |
| 8 | + * |
| 9 | + * What stays: an extension's own plural; the base's plural under an extension |
| 10 | + * that renames nothing (no `label`, or the same one); a plural the tenant |
| 11 | + * authored (the ADR-0029 D9.2a yield, which a tenant-authored `label` also |
| 12 | + * takes); the packaged base the translators compare against. |
| 13 | + * |
| 14 | + * The served answer — `/meta/object` in `en` and `zh-CN`, the catalog's plural |
| 15 | + * included — is pinned in `@objectstack/rest`'s |
| 16 | + * `meta-object-extension-plural-label.test.ts`. |
| 17 | + */ |
| 18 | + |
| 19 | +import { describe, it, expect } from 'vitest'; |
| 20 | +import { SchemaRegistry } from './registry.js'; |
| 21 | + |
| 22 | +const BASE = 'com.acme.crm'; |
| 23 | +const PAIR = 'com.acme.crm.custom'; |
| 24 | +const PARTNER = 'com.partner.crm-ext'; |
| 25 | + |
| 26 | +const TASK = { |
| 27 | + name: 'crm_task', |
| 28 | + label: 'Task', |
| 29 | + pluralLabel: 'Tasks', |
| 30 | + fields: { title: { name: 'title', type: 'text', label: 'Title' } }, |
| 31 | +}; |
| 32 | + |
| 33 | +const clone = <T>(v: T): T => JSON.parse(JSON.stringify(v)) as T; |
| 34 | + |
| 35 | +function registry(): SchemaRegistry { |
| 36 | + const r = new SchemaRegistry({ multiTenant: false }); |
| 37 | + r.logLevel = 'silent'; |
| 38 | + r.registerObject(clone(TASK) as never, BASE, undefined, 'own'); |
| 39 | + return r; |
| 40 | +} |
| 41 | + |
| 42 | +const served = (r: SchemaRegistry) => r.getObject('crm_task') as unknown as Record<string, unknown>; |
| 43 | +const scalars = (doc: Record<string, unknown>) => ({ |
| 44 | + label: doc.label, |
| 45 | + pluralLabel: doc.pluralLabel, |
| 46 | + hasPluralLabel: Object.prototype.hasOwnProperty.call(doc, 'pluralLabel'), |
| 47 | +}); |
| 48 | + |
| 49 | +describe('#30391 — an extension that renames only label', () => { |
| 50 | + it('is served with the new label and no pluralLabel', () => { |
| 51 | + const r = registry(); |
| 52 | + r.registerObjectExtension({ extend: 'crm_task', label: 'Work Item' }, PAIR); |
| 53 | + expect(scalars(served(r))).toEqual({ label: 'Work Item', pluralLabel: undefined, hasPluralLabel: false }); |
| 54 | + }); |
| 55 | + |
| 56 | + it('leaves the packaged base its plural, which the translators compare against', () => { |
| 57 | + const r = registry(); |
| 58 | + r.registerObjectExtension({ extend: 'crm_task', label: 'Work Item' }, PAIR); |
| 59 | + const base = r.getPackagedObjectBase('crm_task') as unknown as Record<string, unknown>; |
| 60 | + expect(scalars(base)).toEqual({ label: 'Task', pluralLabel: 'Tasks', hasPluralLabel: true }); |
| 61 | + expect(base._packageId).toBe(BASE); |
| 62 | + }); |
| 63 | + |
| 64 | + it('is idempotent over an already-folded base', () => { |
| 65 | + const r = registry(); |
| 66 | + r.registerObjectExtension({ extend: 'crm_task', label: 'Work Item' }, PAIR); |
| 67 | + const once = served(r); |
| 68 | + const twice = r.foldObjectExtendersOnto('crm_task', clone(once) as never) as unknown as Record<string, unknown>; |
| 69 | + expect(scalars(twice)).toEqual(scalars(once)); |
| 70 | + }); |
| 71 | +}); |
| 72 | + |
| 73 | +describe('#30391 — controls: the plural stays', () => { |
| 74 | + it('an extension that sets pluralLabel is served as set', () => { |
| 75 | + const r = registry(); |
| 76 | + r.registerObjectExtension({ extend: 'crm_task', label: 'Work Item', pluralLabel: 'Work Items' }, PAIR); |
| 77 | + expect(scalars(served(r))).toEqual({ label: 'Work Item', pluralLabel: 'Work Items', hasPluralLabel: true }); |
| 78 | + }); |
| 79 | + |
| 80 | + it('an extension that sets only pluralLabel keeps the label', () => { |
| 81 | + const r = registry(); |
| 82 | + r.registerObjectExtension({ extend: 'crm_task', pluralLabel: 'Work Items' }, PAIR); |
| 83 | + expect(scalars(served(r))).toEqual({ label: 'Task', pluralLabel: 'Work Items', hasPluralLabel: true }); |
| 84 | + }); |
| 85 | + |
| 86 | + it('an extension that changes neither keeps the base plural', () => { |
| 87 | + const r = registry(); |
| 88 | + r.registerObjectExtension( |
| 89 | + { extend: 'crm_task', fields: { region__c: { name: 'region__c', type: 'text', label: 'Region' } } }, |
| 90 | + PAIR, |
| 91 | + ); |
| 92 | + expect(scalars(served(r))).toEqual({ label: 'Task', pluralLabel: 'Tasks', hasPluralLabel: true }); |
| 93 | + }); |
| 94 | + |
| 95 | + it('an extension that repeats the label it extends renames nothing', () => { |
| 96 | + const r = registry(); |
| 97 | + r.registerObjectExtension({ extend: 'crm_task', label: 'Task' }, PAIR); |
| 98 | + expect(scalars(served(r))).toEqual({ label: 'Task', pluralLabel: 'Tasks', hasPluralLabel: true }); |
| 99 | + }); |
| 100 | + |
| 101 | + it('a plural the tenant authored stays under a rename (ADR-0029 D9.2a)', () => { |
| 102 | + const r = registry(); |
| 103 | + r.registerObject({ ...clone(TASK), pluralLabel: 'To-dos', _provenance: 'org' } as never, BASE, undefined, 'overlay'); |
| 104 | + r.registerObjectExtension({ extend: 'crm_task', label: 'Work Item' }, PAIR); |
| 105 | + expect(scalars(served(r))).toEqual({ label: 'Work Item', pluralLabel: 'To-dos', hasPluralLabel: true }); |
| 106 | + }); |
| 107 | + |
| 108 | + it('a label the tenant authored makes the extension\'s rename yield, plural and all', () => { |
| 109 | + const r = registry(); |
| 110 | + r.registerObject({ ...clone(TASK), label: 'Job', _provenance: 'org' } as never, BASE, undefined, 'overlay'); |
| 111 | + r.registerObjectExtension({ extend: 'crm_task', label: 'Work Item' }, PAIR); |
| 112 | + expect(scalars(served(r))).toEqual({ label: 'Job', pluralLabel: 'Tasks', hasPluralLabel: true }); |
| 113 | + }); |
| 114 | +}); |
| 115 | + |
| 116 | +describe('#30391 — two extenders, in priority order', () => { |
| 117 | + it('a later rename with no plural takes the earlier extender\'s plural off too', () => { |
| 118 | + const r = registry(); |
| 119 | + r.registerObjectExtension({ extend: 'crm_task', label: 'Job', pluralLabel: 'Jobs', priority: 150 }, PARTNER); |
| 120 | + r.registerObjectExtension({ extend: 'crm_task', label: 'Work Item', priority: 250 }, PAIR); |
| 121 | + expect(scalars(served(r))).toEqual({ label: 'Work Item', pluralLabel: undefined, hasPluralLabel: false }); |
| 122 | + }); |
| 123 | + |
| 124 | + it('a later plural is served over an earlier rename', () => { |
| 125 | + const r = registry(); |
| 126 | + r.registerObjectExtension({ extend: 'crm_task', label: 'Work Item', priority: 150 }, PARTNER); |
| 127 | + r.registerObjectExtension({ extend: 'crm_task', pluralLabel: 'Work Items', priority: 250 }, PAIR); |
| 128 | + expect(scalars(served(r))).toEqual({ label: 'Work Item', pluralLabel: 'Work Items', hasPluralLabel: true }); |
| 129 | + }); |
| 130 | +}); |
0 commit comments