diff --git a/.changeset/20680-metadata-protocol-packaged-dashboard-base.md b/.changeset/20680-metadata-protocol-packaged-dashboard-base.md new file mode 100644 index 00000000000..38569ea35f9 --- /dev/null +++ b/.changeset/20680-metadata-protocol-packaged-dashboard-base.md @@ -0,0 +1,11 @@ +--- +'@objectstack/metadata-protocol': minor +--- + +fix(metadata-protocol): `getPackagedDashboardBase(name)` answers the dashboard a code package ships, before any overlay (#20680) + +`ObjectStackProtocolImplementation` gains `getPackagedDashboardBase(name)`, the dashboard twin of `getPackagedObjectBase`. It returns the packaged (code-layer) declaration of a dashboard, which is the `packagedBase` that `translateDashboard` compares a served dashboard against, so that a tenant's published overlay is not overwritten by the packaged translation catalog. + +It reads the artifact registry's code-package entry only. An overlay that was hydrated under the plain registry key can therefore never be returned as the base it is compared against. It returns `undefined` for a dashboard that no code package ships, for an unknown or empty name, and for a registry that cannot answer. A caller treats `undefined` as "no base known", and the catalog applies as before. + +The method is additive. No existing read changes answer: the item and list reads already serve a published org overlay by the same identity the write stored (`type`, name, `package_id`, organization). Nothing is removed or renamed. diff --git a/.changeset/20680-spec-dashboard-catalog-override.md b/.changeset/20680-spec-dashboard-catalog-override.md new file mode 100644 index 00000000000..279b99995a4 --- /dev/null +++ b/.changeset/20680-spec-dashboard-catalog-override.md @@ -0,0 +1,17 @@ +--- +'@objectstack/spec': minor +--- + +fix(spec): `translateDashboard` lets an explicit override beat the packaged catalog when it is handed the packaged base (#20680) + +`translateDashboard` (`@objectstack/spec/system`) now follows the rule ADR-0029 D9.2a records for objects: an explicit override beats a packaged default. When the caller supplies `packagedBase` (the dashboard as its code package ships it, before any tenant overlay), a catalog string replaces a served string only while that string still equals its packaged counterpart. The comparison is made per string: the dashboard `label` and `description`, each widget's `title`, `description` and sub-caption (`options.description`), each global filter's `label`, and each static option `label`. Each string is matched by the key the bundle addresses it by (widget `id`, filter key, option value). A widget, filter or option that the packaged base does not carry counts as authored, so its strings keep their values. + +Why: an org overlay on a packaged dashboard (ADR-0126 Regime O) published, and `?layers=true` reported it as effective, but the served widget title stayed the shipped one whenever the dashboard's bundle carried that title. The platform's `system_overview` is one such dashboard, because `platform-objects` ships an `en` bundle that repeats every widget title. The catalog translated the packaged declaration and was applied over the tenant's edit. + +What changes for a caller: + +- `translateDashboard`'s third parameter is typed `TranslateDocumentOptions` (was `ResolveOptions`). That type is `ResolveOptions` plus the optional `packagedBase`, and `translateMetadataDocument` already passed it through. Every existing call compiles unchanged. +- Without `packagedBase` (`undefined` or `null`), the output is byte-identical to before: the catalog applies. No serving layer in this release passes a dashboard base yet, so no served dashboard changes answer with this package alone. +- An edit back to exactly the shipped string is a no-op: the catalog still translates it. + +Nothing is removed or renamed, and there is nothing to migrate. diff --git a/packages/metadata-protocol/src/protocol.ts b/packages/metadata-protocol/src/protocol.ts index 2c7fc1171fa..a4668598319 100644 --- a/packages/metadata-protocol/src/protocol.ts +++ b/packages/metadata-protocol/src/protocol.ts @@ -7160,6 +7160,48 @@ export class ObjectStackProtocolImplementation implements } } + /** + * [#20680] The PACKAGED (code-layer) declaration of a DASHBOARD — the + * body a code package ships, before any tenant `sys_metadata` overlay. + * The dashboard twin of {@link getPackagedObjectBase}, and the fact + * `translateDashboard` (`@objectstack/spec/system`) compares against as + * its `packagedBase`: the i18n catalog is the packaged translation of + * exactly this body, so it may only replace a string the served + * dashboard still carries unchanged from it (ADR-0029 D9.2a — an explicit + * override beats a packaged default; ADR-0126 Regime O — a packaged + * dashboard is overlay-editable, and a published overlay is what every + * read serves). + * + * Measured before this existed: an org overlay on `system_overview` + * published and was returned by {@link getMetaItem} and + * {@link getMetaItems} — the write and every protocol read resolve ONE + * identity (`type: 'dashboard'`, the name, `package_id: null`, the org) — + * yet the served widget title stayed the shipped one, because the + * translation of the served document had no base to compare with. + * + * {@link lookupArtifactItem}, and nothing wider: it answers only an item + * whose `_packageId` marks a genuine code package and is immune to an + * overlay row hydrated under the plain registry key, so the body it + * returns can never be the overlay it is meant to be compared against. + * A dashboard has no extension fold, so — unlike the object case — the + * artifact item IS the packaged declaration. + * + * Returns `undefined` — "no packaged baseline, infer nothing", which the + * translator reads as the catalog applying unchanged — for a + * runtime/tenant-authored dashboard (no code package ships it), an + * unknown name, and a registry double without the lookup. + */ + getPackagedDashboardBase(name: string): unknown { + if (typeof name !== 'string' || name === '') return undefined; + try { + return this.lookupArtifactItem('dashboard', name); + } catch { + // A read over the in-memory registry; a failure here must never + // turn a served dashboard into a 5xx. + return undefined; + } + } + /** * [#8268, generalising #8038] The REGISTRY-SIDE half of * {@link governServedItem}'s presence convergence: replay the registry's diff --git a/packages/objectql/src/protocol-packaged-dashboard-base.test.ts b/packages/objectql/src/protocol-packaged-dashboard-base.test.ts new file mode 100644 index 00000000000..ea180b78e36 --- /dev/null +++ b/packages/objectql/src/protocol-packaged-dashboard-base.test.ts @@ -0,0 +1,237 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +/** + * #20680 — `ObjectStackProtocolImplementation.getPackagedDashboardBase`, against + * a REAL `SchemaRegistry`, and the read it exists to feed. + * + * ## What was measured, and what it rules out + * + * An org overlay on the platform's own `system_overview` dashboard (ADR-0126 + * Regime O — a packaged dashboard is overlay-editable) published `200`, and + * `?layers=true` reported it as effective, while the `/meta` item and list + * reads kept serving the shipped widget title. Measured on a booted showcase + * with an org-bound admin and a member of the same org: the write stored ONE + * row (`type: 'dashboard'`, the name, `package_id: null`, the org, + * `state: 'active'` after publish), and `getMetaItem` / `getMetaItems` both + * answered it — the overlay branch of the item read hit that row. So the write + * and every protocol read agree on the item's identity; the plain read's + * `_packageId: com.objectstack.plugin-auth` is only the code artifact's + * protection envelope, grafted onto the overlay body by + * `mergeArtifactProtection`. + * + * What replaced the title was the i18n catalog: `platform-objects` ships an + * `en` bundle whose `dashboards.system_overview.widgets..title` repeats + * the shipped string, and `translateDashboard` applied it over whatever the + * document said. The showcase dashboard (the control) ships no widget titles + * in its bundle, so the same overlay reached its reader untouched. + * + * The rule that fixes it is ADR-0029 D9.2a's — an explicit override beats a + * packaged default, decided by comparing against the PACKAGED declaration — + * and that comparison is only as good as the body this accessor returns. Hence + * a real registry: the body must be the one the code package shipped, never an + * overlay the protocol hydrated under the plain registry key. + */ + +import { describe, it, expect } from 'vitest'; +import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol'; +import { translateDashboard, type TranslationBundle } from '@objectstack/spec/system'; +import { SchemaRegistry } from './registry.js'; +import { assertEngineFindOnePredicate } from './engine-findone-predicate.js'; + +const PKG = 'com.objectstack.plugin-auth'; +const DASH = 'system_overview'; +const ORG = 'org_acme'; + +const SHIPPED_TITLE = 'Total Users'; +const EDITED_TITLE = 'Total Users (edited)'; + +/** The dashboard as the code package ships it. */ +const packagedBody = () => ({ + name: DASH, + label: 'System Overview', + columns: 12, + widgets: [ + { id: 'widget_total_users', type: 'metric', title: SHIPPED_TITLE, object: 'sys_user', layout: { x: 0, y: 0, w: 3, h: 2 } }, + { id: 'widget_organizations', type: 'metric', title: 'Organizations', object: 'sys_organization', layout: { x: 3, y: 0, w: 3, h: 2 } }, + ], +}); + +/** The same dashboard as the org's published overlay stores it — one title edited. */ +const overlayBody = () => { + const body = packagedBody(); + body.widgets[0] = { ...body.widgets[0], title: EDITED_TITLE }; + return body; +}; + +/** + * The catalog `platform-objects` ships: `en` repeats the shipped strings, + * `zh-CN` translates them. Both locales, because the defect showed in the + * SOURCE locale — an `en` reader got the shipped English back over the edit. + */ +const BUNDLE: TranslationBundle = { + en: { + dashboards: { + [DASH]: { + widgets: { + widget_total_users: { title: SHIPPED_TITLE }, + widget_organizations: { title: 'Organizations' }, + }, + }, + }, + } as any, + 'zh-CN': { + dashboards: { + [DASH]: { + widgets: { + widget_total_users: { title: '用户总数' }, + widget_organizations: { title: '组织' }, + }, + }, + }, + } as any, +}; + +const matchesWhere = (row: Record, where: Record) => + Object.entries(where ?? {}).every(([k, v]) => { + if (k.startsWith('$')) throw new Error(`fake driver: unsupported operator ${k}`); + if (v === null) return row[k] === null || row[k] === undefined; + return row[k] === v; + }); + +/** + * A real registry and the real protocol over a READ-ONLY in-memory + * `sys_metadata`: the rows are seeded exactly as the measured write left them, + * so no write verb is doubled here at all. + */ +function makeSession() { + const registry = new SchemaRegistry({ multiTenant: false }); + registry.logLevel = 'silent'; + const rows: Record[] = []; + const engine: any = { + registry, + async findOne(table: string, o: { where: Record }) { + // The real engine's #4419 predicate: a `findOne` naming no record is refused. + assertEngineFindOnePredicate(table, o); + if (table !== 'sys_metadata') return null; + return rows.find((r) => matchesWhere(r, o.where)) ?? null; + }, + async find(table: string, o: { where: Record; limit?: number }) { + if (table !== 'sys_metadata') return []; + const matched = rows.filter((r) => matchesWhere(r, o.where)); + // The caller's bound, applied after the filter, by presence. + return typeof o?.limit === 'number' ? matched.slice(0, o.limit) : matched; + }, + }; + const protocol: any = new ObjectStackProtocolImplementation(engine, undefined, 'env_test'); + return { protocol, registry, rows }; +} + +/** The code package's registration (plugin-auth's manifest, in the measured boot). */ +function registerPackaged(registry: SchemaRegistry) { + registry.registerItem('dashboard', packagedBody(), 'name', PKG); +} + +/** The published org overlay row, in the identity the measured write stored. */ +function seedOrgOverlay(rows: Record[]) { + rows.push({ + id: 'r_1', + type: 'dashboard', + name: DASH, + package_id: null, + organization_id: ORG, + state: 'active', + metadata: JSON.stringify(overlayBody()), + }); +} + +const titleOf = (doc: any, id = 'widget_total_users') => + (doc?.widgets as any[] | undefined)?.find((w) => w?.id === id)?.title; + +describe('#20680 getPackagedDashboardBase — the packaged declaration, never an overlay', () => { + it('answers the body the code package shipped', () => { + const s = makeSession(); + registerPackaged(s.registry); + const base = s.protocol.getPackagedDashboardBase(DASH); + expect(titleOf(base)).toBe(SHIPPED_TITLE); + expect(base?._packageId).toBe(PKG); + }); + + it('is immune to an overlay hydrated under the plain registry key', () => { + // An env-wide overlay is hydrated into the registry's PLAIN key on + // publish. The accessor must still answer the packaged body — a base + // that returned the overlay would compare the edit equal to itself and + // hand the catalog straight back. + const s = makeSession(); + registerPackaged(s.registry); + s.registry.registerItem('dashboard', overlayBody(), 'name'); + + // The trap is live: the plain registry read answers the overlay… + expect(titleOf(s.registry.getItem('dashboard', DASH))).toBe(EDITED_TITLE); + // …and the packaged base does not move. + expect(titleOf(s.protocol.getPackagedDashboardBase(DASH))).toBe(SHIPPED_TITLE); + }); + + it('is undefined for a dashboard no code package ships, an unknown name, and an empty name', () => { + // "No packaged baseline" is a real answer: the translator reads it as + // "infer nothing" and keeps the catalog, as before this change. + const s = makeSession(); + s.registry.registerItem('dashboard', { ...packagedBody(), name: 'tenant_board' }, 'name'); + expect(s.protocol.getPackagedDashboardBase('tenant_board')).toBeUndefined(); + expect(s.protocol.getPackagedDashboardBase('no_such_board')).toBeUndefined(); + expect(s.protocol.getPackagedDashboardBase('')).toBeUndefined(); + }); + + it('is undefined when the host registry cannot answer', () => { + const partial: any = new ObjectStackProtocolImplementation({ registry: {} } as any, undefined, 'env_test'); + expect(partial.getPackagedDashboardBase(DASH)).toBeUndefined(); + const none: any = new ObjectStackProtocolImplementation({} as any, undefined, 'env_test'); + expect(none.getPackagedDashboardBase(DASH)).toBeUndefined(); + }); +}); + +describe('#20680 the published org overlay, through the protocol read and the translation', () => { + it('the item read serves the overlay — one identity for the write and the read', async () => { + const s = makeSession(); + registerPackaged(s.registry); + seedOrgOverlay(s.rows); + const served = await s.protocol.getMetaItem({ type: 'dashboard', name: DASH, organizationId: ORG }); + expect(titleOf(served.item)).toBe(EDITED_TITLE); + // The code artifact's envelope rides on the overlay body; it names the + // registering package, not a second identity the overlay missed. + expect(served.item._packageId).toBe(PKG); + }); + + it('the translation serves the edit once it is handed the packaged base — en and zh-CN', async () => { + const s = makeSession(); + registerPackaged(s.registry); + seedOrgOverlay(s.rows); + const served = (await s.protocol.getMetaItem({ type: 'dashboard', name: DASH, organizationId: ORG })).item; + const packagedBase = s.protocol.getPackagedDashboardBase(DASH); + + for (const locale of ['en', 'zh-CN']) { + const out = translateDashboard(served, BUNDLE, { locale, packagedBase }); + expect(titleOf(out), `${locale}: the edited widget keeps the edit`).toBe(EDITED_TITLE); + } + // The widget nobody edited is still translated. + const zh = translateDashboard(served, BUNDLE, { locale: 'zh-CN', packagedBase }); + expect(titleOf(zh, 'widget_organizations')).toBe('组织'); + }); + + it('CONTROL — without the base, the catalog replaces the edit (the measured defect)', async () => { + const s = makeSession(); + registerPackaged(s.registry); + seedOrgOverlay(s.rows); + const served = (await s.protocol.getMetaItem({ type: 'dashboard', name: DASH, organizationId: ORG })).item; + expect(titleOf(translateDashboard(served, BUNDLE, { locale: 'en' }))).toBe(SHIPPED_TITLE); + expect(titleOf(translateDashboard(served, BUNDLE, { locale: 'zh-CN' }))).toBe('用户总数'); + }); + + it('after reset (no overlay row) the shipped body is served and still translated', async () => { + const s = makeSession(); + registerPackaged(s.registry); + const served = (await s.protocol.getMetaItem({ type: 'dashboard', name: DASH, organizationId: ORG })).item; + const packagedBase = s.protocol.getPackagedDashboardBase(DASH); + expect(titleOf(served)).toBe(SHIPPED_TITLE); + expect(titleOf(translateDashboard(served, BUNDLE, { locale: 'zh-CN', packagedBase }))).toBe('用户总数'); + }); +}); diff --git a/packages/spec/src/system/i18n-resolver.test.ts b/packages/spec/src/system/i18n-resolver.test.ts index 6e0170e0cb4..3092c3034b0 100644 --- a/packages/spec/src/system/i18n-resolver.test.ts +++ b/packages/spec/src/system/i18n-resolver.test.ts @@ -1240,6 +1240,146 @@ describe('translateDashboard — global filters (#16772)', () => { }); }); +describe('translateDashboard — the catalog loses to an explicit override (#20680, ADR-0029 D9.2a)', () => { + // What the package shipped — the catalog's subject. + const PACKAGED = { + name: 'system_overview', + label: 'System Overview', + description: 'Platform health', + widgets: [ + { id: 'widget_total_users', title: 'Total Users', description: 'Registered users', options: { description: 'all time' } }, + { id: 'widget_organizations', title: 'Organizations' }, + ], + globalFilters: [ + { name: 'region', field: 'region', label: 'Region', options: [{ value: 'emea', label: 'EMEA' }, { value: 1, label: 'One' }] }, + ], + }; + + // `en` repeats the shipped strings (what `platform-objects` ships); `zh-CN` + // translates them. Both, because the measured defect was in the SOURCE + // locale: an `en` reader got the shipped English back over the edit. + const BUNDLE: TranslationBundle = { + en: { + dashboards: { + system_overview: { + label: 'System Overview', + description: 'Platform health', + widgets: { + widget_total_users: { title: 'Total Users', description: 'Registered users', subCaption: 'all time' }, + widget_organizations: { title: 'Organizations' }, + }, + globalFilters: { region: { label: 'Region', options: { emea: 'EMEA', '1': 'One' } } }, + }, + }, + } as any, + 'zh-CN': { + dashboards: { + system_overview: { + label: '系统概览', + description: '平台健康', + widgets: { + widget_total_users: { title: '用户总数', description: '注册用户', subCaption: '全部时间' }, + widget_organizations: { title: '组织' }, + // An id the package does not ship — addressed only to prove a + // tenant-added widget keeps its own title. + widget_added: { title: 'MUST-NOT-APPLY' }, + }, + globalFilters: { region: { label: '区域', options: { emea: '欧洲', '1': '一' } } }, + }, + }, + } as any, + }; + + const clone = (v: T): T => JSON.parse(JSON.stringify(v)); + const widget = (doc: any, id: string) => doc.widgets.find((w: any) => w.id === id); + + it('an untouched document still gets the catalog — every string', () => { + const out: any = translateDashboard(clone(PACKAGED), BUNDLE, { locale: 'zh-CN', packagedBase: PACKAGED }); + expect(out.label).toBe('系统概览'); + expect(out.description).toBe('平台健康'); + expect(widget(out, 'widget_total_users')).toMatchObject({ title: '用户总数', description: '注册用户', options: { description: '全部时间' } }); + expect(widget(out, 'widget_organizations').title).toBe('组织'); + expect(out.globalFilters[0].label).toBe('区域'); + expect(out.globalFilters[0].options.map((o: any) => o.label)).toEqual(['欧洲', '一']); + }); + + it("an org overlay's edited widget title is served — in the SOURCE locale and in translation", () => { + const doc = clone(PACKAGED); + doc.widgets[0].title = 'Total Users (edited)'; + for (const locale of ['en', 'zh-CN']) { + const out: any = translateDashboard(doc, BUNDLE, { locale, packagedBase: PACKAGED }); + expect(widget(out, 'widget_total_users').title, locale).toBe('Total Users (edited)'); + } + }); + + it('judges each string SEPARATELY — an edited title leaves the other strings translated', () => { + const doc = clone(PACKAGED); + doc.widgets[0].title = 'Total Users (edited)'; + const out: any = translateDashboard(doc, BUNDLE, { locale: 'zh-CN', packagedBase: PACKAGED }); + expect(widget(out, 'widget_total_users').description).toBe('注册用户'); + expect(widget(out, 'widget_total_users').options.description).toBe('全部时间'); + expect(widget(out, 'widget_organizations').title).toBe('组织'); + expect(out.label).toBe('系统概览'); + }); + + it('every translatable string follows the same rule — label, description, widget description, sub-caption, filter label, option label', () => { + const doc = clone(PACKAGED); + doc.label = 'Ops Overview'; + doc.description = 'Our health'; + doc.widgets[0].description = 'People'; + doc.widgets[0].options!.description = 'since launch'; + doc.globalFilters[0].label = 'Territory'; + doc.globalFilters[0].options[0].label = 'Europe'; + const out: any = translateDashboard(doc, BUNDLE, { locale: 'zh-CN', packagedBase: PACKAGED }); + expect(out.label).toBe('Ops Overview'); + expect(out.description).toBe('Our health'); + expect(widget(out, 'widget_total_users').description).toBe('People'); + expect(widget(out, 'widget_total_users').options.description).toBe('since launch'); + expect(out.globalFilters[0].label).toBe('Territory'); + // The edited option keeps its label; its unedited sibling is still + // translated, matched by the value's string spelling (`1` against `'1'`). + expect(out.globalFilters[0].options.map((o: any) => o.label)).toEqual(['Europe', '一']); + }); + + it('a widget the package does not ship was authored after the fact and keeps its title', () => { + const doc: any = clone(PACKAGED); + doc.widgets.push({ id: 'widget_added', title: 'Added by the org' }); + const out: any = translateDashboard(doc, BUNDLE, { locale: 'zh-CN', packagedBase: PACKAGED }); + expect(widget(out, 'widget_added').title).toBe('Added by the org'); + }); + + it('NO packaged base supplied → the pre-#20680 behaviour, catalog applies (the measured defect, as a control)', () => { + const doc = clone(PACKAGED); + doc.widgets[0].title = 'Total Users (edited)'; + expect(widget(translateDashboard(doc, BUNDLE, { locale: 'en' }), 'widget_total_users').title).toBe('Total Users'); + expect(widget(translateDashboard(doc, BUNDLE, { locale: 'zh-CN', packagedBase: undefined }), 'widget_total_users').title).toBe('用户总数'); + expect(widget(translateDashboard(doc, BUNDLE, { locale: 'zh-CN', packagedBase: null }), 'widget_total_users').title).toBe('用户总数'); + }); + + it('the ruled edge — an edit back to exactly the shipped string is a no-op, and the catalog still applies', () => { + const out: any = translateDashboard(clone(PACKAGED), BUNDLE, { locale: 'zh-CN', packagedBase: PACKAGED }); + expect(widget(out, 'widget_total_users').title).toBe('用户总数'); + }); + + it('does not mutate either input document', () => { + const doc = clone(PACKAGED); + doc.widgets[0].title = 'Total Users (edited)'; + translateDashboard(doc, BUNDLE, { locale: 'zh-CN', packagedBase: PACKAGED }); + expect(doc.widgets[0].title).toBe('Total Users (edited)'); + expect(PACKAGED.widgets[0].title).toBe('Total Users'); + }); + + it('reaches the generic dispatcher — translateMetadataDocument carries the base for a dashboard', () => { + // The serving layer never calls `translateDashboard` directly; a base that + // stopped at the dispatcher would leave the defect exactly where it was. + const doc = clone(PACKAGED); + doc.widgets[0].title = 'Total Users (edited)'; + const out = translateMetadataDocument('dashboard', doc, BUNDLE, { locale: 'zh-CN', packagedBase: PACKAGED }); + expect(widget(out, 'widget_total_users').title).toBe('Total Users (edited)'); + expect(widget(out, 'widget_organizations').title).toBe('组织'); + }); +}); + describe('translatePage', () => { const bundle: TranslationBundle = { 'zh-CN': { diff --git a/packages/spec/src/system/i18n-resolver.ts b/packages/spec/src/system/i18n-resolver.ts index ca2633cf141..7be4c844922 100644 --- a/packages/spec/src/system/i18n-resolver.ts +++ b/packages/spec/src/system/i18n-resolver.ts @@ -1227,24 +1227,74 @@ function lookupGlobalFilterOption( return undefined; } +/** A plain (non-array) object, or `undefined`. */ +function asRecord(value: unknown): Record | undefined { + return value !== null && typeof value === 'object' && !Array.isArray(value) + ? (value as Record) + : undefined; +} + +/** A global-filter option value the bundle can address (`String(value)`). */ +function isOptionValue(value: unknown): value is string | number | boolean { + return typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean'; +} + +/** + * [#20680] The packaged counterpart of one addressable part of a dashboard — + * a widget, a global filter, a filter option — found in `parent[key]` (an + * array) by the key the bundle addresses it by. + * + * Three answers, and the difference between the first two is the point: + * + * - `undefined` when `parent` is unknown — no packaged base was supplied, so + * nothing may be inferred and the catalog keeps applying + * ({@link valueOverridesPackagedBase} answers `false` for it); + * - `{}` when the base IS known but carries no such part — the part was + * authored after the fact (a widget the tenant added), so every string on + * it counts as diverged, as a scalar the packaged object never declared + * does under #8284; + * - the packaged part itself otherwise. + */ +function packagedDashboardPart( + parent: Record | undefined, + key: 'widgets' | 'globalFilters' | 'options', + matches: (candidate: Record) => boolean, +): Record | undefined { + if (parent === undefined) return undefined; + const list = parent[key]; + if (!Array.isArray(list)) return {}; + for (const candidate of list) { + const record = asRecord(candidate); + if (record !== undefined && matches(record)) return record; + } + return {}; +} + /** * Overlay `dashboards..globalFilters..{label,options.}` * onto one authored filter (#16772). Returns the input object itself when * nothing resolved, so `translateDashboard` can tell "untouched" from * "rebuilt" by identity and leave `globalFilters` off the copy when no filter * moved. + * + * `packagedFilter` is the same filter in the dashboard's packaged base + * ({@link packagedDashboardPart}); a label that diverged from it is authored + * and keeps its value (#20680, see {@link translateDashboard}). */ function translateGlobalFilter( filter: GlobalFilterLike, bundle: TranslationBundle, dashboardName: string, - opts?: ResolveOptions, + opts: ResolveOptions | undefined, + packagedFilter: Record | undefined, ): GlobalFilterLike { const key = globalFilterKey(filter); if (key === undefined) return filter; let next = filter; - const label = lookupGlobalFilterLabel(bundle, dashboardName, key, opts); + const label = valueOverridesPackagedBase(packagedFilter, 'label', filter.label) + ? undefined + : lookupGlobalFilterLabel(bundle, dashboardName, key, opts); if (label !== undefined) next = { ...next, label }; if (Array.isArray(filter.options)) { @@ -1257,6 +1307,12 @@ function translateGlobalFilter( // is the one spelling every value has; `null`/`undefined`/objects have // no such spelling and are left alone. if (typeof value !== 'string' && typeof value !== 'number' && typeof value !== 'boolean') return option; + const packagedOption = packagedDashboardPart( + packagedFilter, + 'options', + (candidate) => isOptionValue(candidate.value) && String(candidate.value) === String(value), + ); + if (valueOverridesPackagedBase(packagedOption, 'label', option.label)) return option; const translated = lookupGlobalFilterOption(bundle, dashboardName, key, String(value), opts); if (translated === undefined) return option; changed = true; @@ -1319,25 +1375,64 @@ function lookupWidgetAttr( * a string. Only filters the bundle actually addresses are rebuilt, and * `globalFilters` is left off the copy entirely when none moved, so a * dashboard without filters gains no invented key. + * + * ## [#20680] The catalog LOSES to an explicit override — ADR-0029 D9.2a + * + * The catalog is keyed by dashboard name and widget id, and it is the + * packaged translation of the PACKAGED declaration. Consulting it first + * overwrote every string authored on top of that declaration — measured: an + * org overlay on the platform's own `system_overview` (ADR-0126 Regime O, + * "yours to edit directly") published `200`, `?layers=true` showed it as + * effective, the metadata protocol's item and list reads both returned it, + * and the `/meta` item and list doors — the list is what the console draws + * the board from — kept serving the shipped widget title, because + * `platform-objects` ships an `en` bundle whose `widgets..title` repeats + * it. A dashboard whose bundle carries no widget titles (the showcase + * control) served the same overlay correctly. + * + * So every translatable string here follows the one sentence ADR-0029 D9.2a + * records for both object layers — *an explicit override beats a packaged + * default* — by the same comparison #8284 was ruled on: the catalog applies + * only while the served value still equals the one in + * {@link TranslateDocumentOptions.packagedBase}, which the serving layer + * supplies (the packaged dashboard declaration, before any tenant overlay — + * `getPackagedDashboardBase` on the metadata protocol). Each string is judged + * against ITS packaged counterpart, found by the key the bundle addresses it + * by (widget `id`, filter key, option `value`), so an edited widget title + * leaves every other widget translated; a widget, filter or option the base + * does not carry was authored after the fact and counts as diverged (the + * object rule's "a base that declares no such scalar"). No base supplied is + * the pre-#20680 behaviour, unchanged: the catalog applies. */ export function translateDashboard( doc: T, bundle: TranslationBundle | undefined, - opts?: ResolveOptions, + opts?: TranslateDocumentOptions, ): T { if (!doc || typeof doc !== 'object') return doc; const name = doc.name; if (!name || !bundle) return doc; - const label = lookupDashboardAttr(bundle, name, 'label', opts) ?? doc.label; - const description = lookupDashboardAttr(bundle, name, 'description', opts) ?? doc.description; + const base = asRecord(opts?.packagedBase); + const label = valueOverridesPackagedBase(base, 'label', doc.label) + ? doc.label + : lookupDashboardAttr(bundle, name, 'label', opts) ?? doc.label; + const description = valueOverridesPackagedBase(base, 'description', doc.description) + ? doc.description + : lookupDashboardAttr(bundle, name, 'description', opts) ?? doc.description; let globalFilters: GlobalFilterLike[] | undefined; if (Array.isArray(doc.globalFilters)) { let changed = false; const rebuilt = doc.globalFilters.map((filter) => { if (!filter || typeof filter !== 'object') return filter; - const next = translateGlobalFilter(filter, bundle, name, opts); + const key = globalFilterKey(filter); + const packagedFilter = packagedDashboardPart( + base, + 'globalFilters', + (candidate) => key !== undefined && globalFilterKey(candidate) === key, + ); + const next = translateGlobalFilter(filter, bundle, name, opts, packagedFilter); if (next !== filter) changed = true; return next; }); @@ -1348,11 +1443,24 @@ export function translateDashboard( ? doc.widgets.map((w) => { if (!w || typeof w !== 'object' || typeof w.id !== 'string') return w; const next: WidgetLike = { ...w }; - const title = lookupWidgetAttr(bundle, name, w.id, 'title', opts); + const packagedWidget = packagedDashboardPart(base, 'widgets', (candidate) => candidate.id === w.id); + const title = valueOverridesPackagedBase(packagedWidget, 'title', w.title) + ? undefined + : lookupWidgetAttr(bundle, name, w.id, 'title', opts); if (title) next.title = title; - const desc = lookupWidgetAttr(bundle, name, w.id, 'description', opts); + const desc = valueOverridesPackagedBase(packagedWidget, 'description', w.description) + ? undefined + : lookupWidgetAttr(bundle, name, w.id, 'description', opts); if (desc) next.description = desc; - const subCaption = lookupWidgetAttr(bundle, name, w.id, 'subCaption', opts); + // The sub-caption's packaged counterpart is the packaged widget's own + // `options` bag — `{}` when that widget declared none, so an authored + // sub-caption on it still counts as diverged. + const packagedOptions = packagedWidget === undefined + ? undefined + : asRecord(packagedWidget.options) ?? {}; + const subCaption = valueOverridesPackagedBase(packagedOptions, 'description', w.options?.description) + ? undefined + : lookupWidgetAttr(bundle, name, w.id, 'subCaption', opts); if (subCaption) next.options = { ...w.options, description: subCaption }; return next; }) @@ -2534,6 +2642,19 @@ export function scalarOverridesPackagedBase( key: 'label' | 'pluralLabel' | 'description', value: unknown, ): boolean { + return valueOverridesPackagedBase(base, key, value); +} + +/** + * [#20680] The ONE comparison behind {@link scalarOverridesPackagedBase}, + * keyed by any attribute name — the exported predicate's key union names the + * object scalars its two callers ask about, and the dashboard translator + * ({@link translateDashboard}) asks the same question of `title`, which that + * union does not carry. One implementation, so the three conservative edges + * documented on the exported predicate hold for every caller alike; ⛔ never + * a second copy of the comparison. + */ +function valueOverridesPackagedBase(base: unknown, key: string, value: unknown): boolean { if (!base || typeof base !== 'object') return false; if (typeof value !== 'string' || value.length === 0) return false; return (base as Record)[key] !== value; diff --git a/scripts/engine-double-contract.pinned.json b/scripts/engine-double-contract.pinned.json index a26d37c5d7e..a43f4becd1b 100644 --- a/scripts/engine-double-contract.pinned.json +++ b/scripts/engine-double-contract.pinned.json @@ -2066,6 +2066,11 @@ "verb": "update", "pinned": 1 }, + { + "file": "packages/objectql/src/protocol-packaged-dashboard-base.test.ts", + "verb": "findOne", + "pinned": 1 + }, { "file": "packages/objectql/src/protocol-packaged-object-base.test.ts", "verb": "delete",