|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #25788 — `ObjectStackProtocolImplementation.getPackagedReportBase`, against a |
| 5 | + * REAL `SchemaRegistry`: the twin of `protocol-packaged-dashboard-base.test.ts`. |
| 6 | + * |
| 7 | + * A report is overlay-editable (ADR-0126 Regime O), and the translation bundle |
| 8 | + * now carries `reports.<name>.label` / `.description`, so `translateReport` |
| 9 | + * must lose to a string an org re-authored (ADR-0029 D9.2a). It decides that by |
| 10 | + * comparing against the PACKAGED declaration, and that comparison is only as |
| 11 | + * good as the body this accessor returns: the one the code package shipped, |
| 12 | + * never an overlay hydrated under the plain registry key. |
| 13 | + */ |
| 14 | + |
| 15 | +import { describe, it, expect } from 'vitest'; |
| 16 | +import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol'; |
| 17 | +import { translateReport, type TranslationBundle } from '@objectstack/spec/system'; |
| 18 | +import { SchemaRegistry } from './registry.js'; |
| 19 | + |
| 20 | +const PKG = 'com.example.showcase'; |
| 21 | +const REPORT = 'opps_by_stage'; |
| 22 | + |
| 23 | +const SHIPPED_LABEL = 'Opportunities by Stage'; |
| 24 | +const EDITED_LABEL = 'Pipeline by stage (ours)'; |
| 25 | + |
| 26 | +/** The report as the code package ships it. */ |
| 27 | +const packagedBody = () => ({ |
| 28 | + name: REPORT, |
| 29 | + label: SHIPPED_LABEL, |
| 30 | + description: 'Summary of opportunities grouped by stage', |
| 31 | + type: 'summary', |
| 32 | + dataset: 'pipeline', |
| 33 | + rows: ['stage'], |
| 34 | + values: ['amount'], |
| 35 | +}); |
| 36 | + |
| 37 | +/** The same report as an org's published overlay stores it — the label edited. */ |
| 38 | +const overlayBody = () => ({ ...packagedBody(), label: EDITED_LABEL }); |
| 39 | + |
| 40 | +const BUNDLE: TranslationBundle = { |
| 41 | + 'zh-CN': { reports: { [REPORT]: { label: '各阶段商机', description: '按阶段汇总的商机' } } }, |
| 42 | +}; |
| 43 | + |
| 44 | +function makeSession() { |
| 45 | + const registry = new SchemaRegistry({ multiTenant: false }); |
| 46 | + registry.logLevel = 'silent'; |
| 47 | + const protocol: any = new ObjectStackProtocolImplementation({ registry } as any, undefined, 'env_test'); |
| 48 | + return { protocol, registry }; |
| 49 | +} |
| 50 | + |
| 51 | +describe('#25788 getPackagedReportBase — the packaged declaration, never an overlay', () => { |
| 52 | + it('answers the body the code package shipped', () => { |
| 53 | + const s = makeSession(); |
| 54 | + s.registry.registerItem('report', packagedBody(), 'name', PKG); |
| 55 | + const base = s.protocol.getPackagedReportBase(REPORT); |
| 56 | + expect(base?.label).toBe(SHIPPED_LABEL); |
| 57 | + expect(base?._packageId).toBe(PKG); |
| 58 | + }); |
| 59 | + |
| 60 | + it('is immune to an overlay hydrated under the plain registry key', () => { |
| 61 | + const s = makeSession(); |
| 62 | + s.registry.registerItem('report', packagedBody(), 'name', PKG); |
| 63 | + s.registry.registerItem('report', overlayBody(), 'name'); |
| 64 | + |
| 65 | + // The trap is live: the plain registry read answers the overlay… |
| 66 | + expect((s.registry.getItem('report', REPORT) as any)?.label).toBe(EDITED_LABEL); |
| 67 | + // …and the packaged base does not move. |
| 68 | + expect(s.protocol.getPackagedReportBase(REPORT)?.label).toBe(SHIPPED_LABEL); |
| 69 | + }); |
| 70 | + |
| 71 | + it('is undefined for a report no code package ships, an unknown name, and an empty name', () => { |
| 72 | + const s = makeSession(); |
| 73 | + s.registry.registerItem('report', { ...packagedBody(), name: 'tenant_report' }, 'name'); |
| 74 | + expect(s.protocol.getPackagedReportBase('tenant_report')).toBeUndefined(); |
| 75 | + expect(s.protocol.getPackagedReportBase('no_such_report')).toBeUndefined(); |
| 76 | + expect(s.protocol.getPackagedReportBase('')).toBeUndefined(); |
| 77 | + }); |
| 78 | + |
| 79 | + it('is undefined when the host registry cannot answer', () => { |
| 80 | + const partial: any = new ObjectStackProtocolImplementation({ registry: {} } as any, undefined, 'env_test'); |
| 81 | + expect(partial.getPackagedReportBase(REPORT)).toBeUndefined(); |
| 82 | + const none: any = new ObjectStackProtocolImplementation({} as any, undefined, 'env_test'); |
| 83 | + expect(none.getPackagedReportBase(REPORT)).toBeUndefined(); |
| 84 | + }); |
| 85 | + |
| 86 | + it('hands `translateReport` the base it needs: the edit is kept, the untouched description translated', () => { |
| 87 | + const s = makeSession(); |
| 88 | + s.registry.registerItem('report', packagedBody(), 'name', PKG); |
| 89 | + s.registry.registerItem('report', overlayBody(), 'name'); |
| 90 | + const packagedBase = s.protocol.getPackagedReportBase(REPORT); |
| 91 | + |
| 92 | + const served = translateReport(overlayBody(), BUNDLE, { locale: 'zh-CN', packagedBase }) as any; |
| 93 | + expect(served.label).toBe(EDITED_LABEL); |
| 94 | + expect(served.description).toBe('按阶段汇总的商机'); |
| 95 | + |
| 96 | + const shipped = translateReport(packagedBody(), BUNDLE, { locale: 'zh-CN', packagedBase }) as any; |
| 97 | + expect(shipped.label).toBe('各阶段商机'); |
| 98 | + }); |
| 99 | +}); |
0 commit comments