|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #20680 — `ObjectStackProtocolImplementation.getPackagedDashboardBase`, against |
| 5 | + * a REAL `SchemaRegistry`, and the read it exists to feed. |
| 6 | + * |
| 7 | + * ## What was measured, and what it rules out |
| 8 | + * |
| 9 | + * An org overlay on the platform's own `system_overview` dashboard (ADR-0126 |
| 10 | + * Regime O — a packaged dashboard is overlay-editable) published `200`, and |
| 11 | + * `?layers=true` reported it as effective, while the `/meta` item and list |
| 12 | + * reads kept serving the shipped widget title. Measured on a booted showcase |
| 13 | + * with an org-bound admin and a member of the same org: the write stored ONE |
| 14 | + * row (`type: 'dashboard'`, the name, `package_id: null`, the org, |
| 15 | + * `state: 'active'` after publish), and `getMetaItem` / `getMetaItems` both |
| 16 | + * answered it — the overlay branch of the item read hit that row. So the write |
| 17 | + * and every protocol read agree on the item's identity; the plain read's |
| 18 | + * `_packageId: com.objectstack.plugin-auth` is only the code artifact's |
| 19 | + * protection envelope, grafted onto the overlay body by |
| 20 | + * `mergeArtifactProtection`. |
| 21 | + * |
| 22 | + * What replaced the title was the i18n catalog: `platform-objects` ships an |
| 23 | + * `en` bundle whose `dashboards.system_overview.widgets.<id>.title` repeats |
| 24 | + * the shipped string, and `translateDashboard` applied it over whatever the |
| 25 | + * document said. The showcase dashboard (the control) ships no widget titles |
| 26 | + * in its bundle, so the same overlay reached its reader untouched. |
| 27 | + * |
| 28 | + * The rule that fixes it is ADR-0029 D9.2a's — an explicit override beats a |
| 29 | + * packaged default, decided by comparing against the PACKAGED declaration — |
| 30 | + * and that comparison is only as good as the body this accessor returns. Hence |
| 31 | + * a real registry: the body must be the one the code package shipped, never an |
| 32 | + * overlay the protocol hydrated under the plain registry key. |
| 33 | + */ |
| 34 | + |
| 35 | +import { describe, it, expect } from 'vitest'; |
| 36 | +import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol'; |
| 37 | +import { translateDashboard, type TranslationBundle } from '@objectstack/spec/system'; |
| 38 | +import { SchemaRegistry } from './registry.js'; |
| 39 | +import { assertEngineFindOnePredicate } from './engine-findone-predicate.js'; |
| 40 | + |
| 41 | +const PKG = 'com.objectstack.plugin-auth'; |
| 42 | +const DASH = 'system_overview'; |
| 43 | +const ORG = 'org_acme'; |
| 44 | + |
| 45 | +const SHIPPED_TITLE = 'Total Users'; |
| 46 | +const EDITED_TITLE = 'Total Users (edited)'; |
| 47 | + |
| 48 | +/** The dashboard as the code package ships it. */ |
| 49 | +const packagedBody = () => ({ |
| 50 | + name: DASH, |
| 51 | + label: 'System Overview', |
| 52 | + columns: 12, |
| 53 | + widgets: [ |
| 54 | + { id: 'widget_total_users', type: 'metric', title: SHIPPED_TITLE, object: 'sys_user', layout: { x: 0, y: 0, w: 3, h: 2 } }, |
| 55 | + { id: 'widget_organizations', type: 'metric', title: 'Organizations', object: 'sys_organization', layout: { x: 3, y: 0, w: 3, h: 2 } }, |
| 56 | + ], |
| 57 | +}); |
| 58 | + |
| 59 | +/** The same dashboard as the org's published overlay stores it — one title edited. */ |
| 60 | +const overlayBody = () => { |
| 61 | + const body = packagedBody(); |
| 62 | + body.widgets[0] = { ...body.widgets[0], title: EDITED_TITLE }; |
| 63 | + return body; |
| 64 | +}; |
| 65 | + |
| 66 | +/** |
| 67 | + * The catalog `platform-objects` ships: `en` repeats the shipped strings, |
| 68 | + * `zh-CN` translates them. Both locales, because the defect showed in the |
| 69 | + * SOURCE locale — an `en` reader got the shipped English back over the edit. |
| 70 | + */ |
| 71 | +const BUNDLE: TranslationBundle = { |
| 72 | + en: { |
| 73 | + dashboards: { |
| 74 | + [DASH]: { |
| 75 | + widgets: { |
| 76 | + widget_total_users: { title: SHIPPED_TITLE }, |
| 77 | + widget_organizations: { title: 'Organizations' }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + } as any, |
| 82 | + 'zh-CN': { |
| 83 | + dashboards: { |
| 84 | + [DASH]: { |
| 85 | + widgets: { |
| 86 | + widget_total_users: { title: '用户总数' }, |
| 87 | + widget_organizations: { title: '组织' }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + } as any, |
| 92 | +}; |
| 93 | + |
| 94 | +const matchesWhere = (row: Record<string, unknown>, where: Record<string, unknown>) => |
| 95 | + Object.entries(where ?? {}).every(([k, v]) => { |
| 96 | + if (k.startsWith('$')) throw new Error(`fake driver: unsupported operator ${k}`); |
| 97 | + if (v === null) return row[k] === null || row[k] === undefined; |
| 98 | + return row[k] === v; |
| 99 | + }); |
| 100 | + |
| 101 | +/** |
| 102 | + * A real registry and the real protocol over a READ-ONLY in-memory |
| 103 | + * `sys_metadata`: the rows are seeded exactly as the measured write left them, |
| 104 | + * so no write verb is doubled here at all. |
| 105 | + */ |
| 106 | +function makeSession() { |
| 107 | + const registry = new SchemaRegistry({ multiTenant: false }); |
| 108 | + registry.logLevel = 'silent'; |
| 109 | + const rows: Record<string, unknown>[] = []; |
| 110 | + const engine: any = { |
| 111 | + registry, |
| 112 | + async findOne(table: string, o: { where: Record<string, unknown> }) { |
| 113 | + // The real engine's #4419 predicate: a `findOne` naming no record is refused. |
| 114 | + assertEngineFindOnePredicate(table, o); |
| 115 | + if (table !== 'sys_metadata') return null; |
| 116 | + return rows.find((r) => matchesWhere(r, o.where)) ?? null; |
| 117 | + }, |
| 118 | + async find(table: string, o: { where: Record<string, unknown>; limit?: number }) { |
| 119 | + if (table !== 'sys_metadata') return []; |
| 120 | + const matched = rows.filter((r) => matchesWhere(r, o.where)); |
| 121 | + // The caller's bound, applied after the filter, by presence. |
| 122 | + return typeof o?.limit === 'number' ? matched.slice(0, o.limit) : matched; |
| 123 | + }, |
| 124 | + }; |
| 125 | + const protocol: any = new ObjectStackProtocolImplementation(engine, undefined, 'env_test'); |
| 126 | + return { protocol, registry, rows }; |
| 127 | +} |
| 128 | + |
| 129 | +/** The code package's registration (plugin-auth's manifest, in the measured boot). */ |
| 130 | +function registerPackaged(registry: SchemaRegistry) { |
| 131 | + registry.registerItem('dashboard', packagedBody(), 'name', PKG); |
| 132 | +} |
| 133 | + |
| 134 | +/** The published org overlay row, in the identity the measured write stored. */ |
| 135 | +function seedOrgOverlay(rows: Record<string, unknown>[]) { |
| 136 | + rows.push({ |
| 137 | + id: 'r_1', |
| 138 | + type: 'dashboard', |
| 139 | + name: DASH, |
| 140 | + package_id: null, |
| 141 | + organization_id: ORG, |
| 142 | + state: 'active', |
| 143 | + metadata: JSON.stringify(overlayBody()), |
| 144 | + }); |
| 145 | +} |
| 146 | + |
| 147 | +const titleOf = (doc: any, id = 'widget_total_users') => |
| 148 | + (doc?.widgets as any[] | undefined)?.find((w) => w?.id === id)?.title; |
| 149 | + |
| 150 | +describe('#20680 getPackagedDashboardBase — the packaged declaration, never an overlay', () => { |
| 151 | + it('answers the body the code package shipped', () => { |
| 152 | + const s = makeSession(); |
| 153 | + registerPackaged(s.registry); |
| 154 | + const base = s.protocol.getPackagedDashboardBase(DASH); |
| 155 | + expect(titleOf(base)).toBe(SHIPPED_TITLE); |
| 156 | + expect(base?._packageId).toBe(PKG); |
| 157 | + }); |
| 158 | + |
| 159 | + it('is immune to an overlay hydrated under the plain registry key', () => { |
| 160 | + // An env-wide overlay is hydrated into the registry's PLAIN key on |
| 161 | + // publish. The accessor must still answer the packaged body — a base |
| 162 | + // that returned the overlay would compare the edit equal to itself and |
| 163 | + // hand the catalog straight back. |
| 164 | + const s = makeSession(); |
| 165 | + registerPackaged(s.registry); |
| 166 | + s.registry.registerItem('dashboard', overlayBody(), 'name'); |
| 167 | + |
| 168 | + // The trap is live: the plain registry read answers the overlay… |
| 169 | + expect(titleOf(s.registry.getItem('dashboard', DASH))).toBe(EDITED_TITLE); |
| 170 | + // …and the packaged base does not move. |
| 171 | + expect(titleOf(s.protocol.getPackagedDashboardBase(DASH))).toBe(SHIPPED_TITLE); |
| 172 | + }); |
| 173 | + |
| 174 | + it('is undefined for a dashboard no code package ships, an unknown name, and an empty name', () => { |
| 175 | + // "No packaged baseline" is a real answer: the translator reads it as |
| 176 | + // "infer nothing" and keeps the catalog, as before this change. |
| 177 | + const s = makeSession(); |
| 178 | + s.registry.registerItem('dashboard', { ...packagedBody(), name: 'tenant_board' }, 'name'); |
| 179 | + expect(s.protocol.getPackagedDashboardBase('tenant_board')).toBeUndefined(); |
| 180 | + expect(s.protocol.getPackagedDashboardBase('no_such_board')).toBeUndefined(); |
| 181 | + expect(s.protocol.getPackagedDashboardBase('')).toBeUndefined(); |
| 182 | + }); |
| 183 | + |
| 184 | + it('is undefined when the host registry cannot answer', () => { |
| 185 | + const partial: any = new ObjectStackProtocolImplementation({ registry: {} } as any, undefined, 'env_test'); |
| 186 | + expect(partial.getPackagedDashboardBase(DASH)).toBeUndefined(); |
| 187 | + const none: any = new ObjectStackProtocolImplementation({} as any, undefined, 'env_test'); |
| 188 | + expect(none.getPackagedDashboardBase(DASH)).toBeUndefined(); |
| 189 | + }); |
| 190 | +}); |
| 191 | + |
| 192 | +describe('#20680 the published org overlay, through the protocol read and the translation', () => { |
| 193 | + it('the item read serves the overlay — one identity for the write and the read', async () => { |
| 194 | + const s = makeSession(); |
| 195 | + registerPackaged(s.registry); |
| 196 | + seedOrgOverlay(s.rows); |
| 197 | + const served = await s.protocol.getMetaItem({ type: 'dashboard', name: DASH, organizationId: ORG }); |
| 198 | + expect(titleOf(served.item)).toBe(EDITED_TITLE); |
| 199 | + // The code artifact's envelope rides on the overlay body; it names the |
| 200 | + // registering package, not a second identity the overlay missed. |
| 201 | + expect(served.item._packageId).toBe(PKG); |
| 202 | + }); |
| 203 | + |
| 204 | + it('the translation serves the edit once it is handed the packaged base — en and zh-CN', async () => { |
| 205 | + const s = makeSession(); |
| 206 | + registerPackaged(s.registry); |
| 207 | + seedOrgOverlay(s.rows); |
| 208 | + const served = (await s.protocol.getMetaItem({ type: 'dashboard', name: DASH, organizationId: ORG })).item; |
| 209 | + const packagedBase = s.protocol.getPackagedDashboardBase(DASH); |
| 210 | + |
| 211 | + for (const locale of ['en', 'zh-CN']) { |
| 212 | + const out = translateDashboard(served, BUNDLE, { locale, packagedBase }); |
| 213 | + expect(titleOf(out), `${locale}: the edited widget keeps the edit`).toBe(EDITED_TITLE); |
| 214 | + } |
| 215 | + // The widget nobody edited is still translated. |
| 216 | + const zh = translateDashboard(served, BUNDLE, { locale: 'zh-CN', packagedBase }); |
| 217 | + expect(titleOf(zh, 'widget_organizations')).toBe('组织'); |
| 218 | + }); |
| 219 | + |
| 220 | + it('CONTROL — without the base, the catalog replaces the edit (the measured defect)', async () => { |
| 221 | + const s = makeSession(); |
| 222 | + registerPackaged(s.registry); |
| 223 | + seedOrgOverlay(s.rows); |
| 224 | + const served = (await s.protocol.getMetaItem({ type: 'dashboard', name: DASH, organizationId: ORG })).item; |
| 225 | + expect(titleOf(translateDashboard(served, BUNDLE, { locale: 'en' }))).toBe(SHIPPED_TITLE); |
| 226 | + expect(titleOf(translateDashboard(served, BUNDLE, { locale: 'zh-CN' }))).toBe('用户总数'); |
| 227 | + }); |
| 228 | + |
| 229 | + it('after reset (no overlay row) the shipped body is served and still translated', async () => { |
| 230 | + const s = makeSession(); |
| 231 | + registerPackaged(s.registry); |
| 232 | + const served = (await s.protocol.getMetaItem({ type: 'dashboard', name: DASH, organizationId: ORG })).item; |
| 233 | + const packagedBase = s.protocol.getPackagedDashboardBase(DASH); |
| 234 | + expect(titleOf(served)).toBe(SHIPPED_TITLE); |
| 235 | + expect(titleOf(translateDashboard(served, BUNDLE, { locale: 'zh-CN', packagedBase }))).toBe('用户总数'); |
| 236 | + }); |
| 237 | +}); |
0 commit comments