|
21 | 21 |
|
22 | 22 | import { describe, it, expect } from 'vitest'; |
23 | 23 | import { createMemoryI18n } from '@objectstack/core'; |
24 | | -import { TranslationDataSchema, objectLabelKey } from '@objectstack/spec/system'; |
| 24 | +import { SchemaRegistry } from '@objectstack/objectql'; |
| 25 | +import { TranslationDataSchema, objectLabelKey, translateMetadataDocument } from '@objectstack/spec/system'; |
25 | 26 |
|
26 | 27 | import { |
27 | 28 | approvalStepLabelKey, |
28 | 29 | flowLabelKey, |
29 | 30 | flowNameOfProcess, |
30 | 31 | localizeRequestLabels, |
| 32 | + negotiatedReaderLocale, |
31 | 33 | positionLabelKey, |
32 | 34 | readerLabelsFor, |
33 | 35 | type ReaderLabelI18n, |
@@ -108,11 +110,12 @@ describe('[#24863] the pick', () => { |
108 | 110 |
|
109 | 111 | it('negotiates through `resolveBundleLocale` even for a service that resolves exactly', () => { |
110 | 112 | // A service that answers only the exact tag it holds, as `FileI18nAdapter` does. |
| 113 | + // [#35384] Asked of the step's label, the one label still read through `t()`. |
111 | 114 | const exact: ReaderLabelI18n = { |
112 | | - t: (key, locale) => (locale === 'zh-CN' && key === flowLabelKey('f') ? '流程' : key), |
| 115 | + t: (key, locale) => (locale === 'zh-CN' && key === approvalStepLabelKey('f', 'n') ? '步骤' : key), |
113 | 116 | getLocales: () => ['en', 'zh-CN'], |
114 | 117 | }; |
115 | | - expect(readerLabelsFor(exact, 'zh')!.flowLabel('f')).toBe('流程'); |
| 118 | + expect(readerLabelsFor(exact, 'zh')!.stepLabel('f', 'n')).toBe('步骤'); |
116 | 119 | }); |
117 | 120 |
|
118 | 121 | it('picks nothing on a miss: no entry, an empty entry, a blank entry', () => { |
@@ -145,9 +148,9 @@ describe('[#24863] the pick', () => { |
145 | 148 | const asked: string[] = []; |
146 | 149 | const counting: ReaderLabelI18n = { t: (key) => { asked.push(key); return key; } }; |
147 | 150 | const labels = readerLabelsFor(counting, 'zh-CN')!; |
148 | | - labels.flowLabel('f'); |
149 | | - labels.flowLabel('f'); |
150 | | - expect(asked).toEqual([flowLabelKey('f')]); |
| 151 | + labels.stepLabel('f', 'n'); |
| 152 | + labels.stepLabel('f', 'n'); |
| 153 | + expect(asked).toEqual([approvalStepLabelKey('f', 'n')]); |
151 | 154 | }); |
152 | 155 | }); |
153 | 156 |
|
@@ -280,3 +283,181 @@ describe('[#35384] the object label in the declared sourceLocale reads as author |
280 | 283 | expect(readerLabelsFor(throwing, 'zh-CN')!.objectLabel('account')).toBeUndefined(); |
281 | 284 | }); |
282 | 285 | }); |
| 286 | + |
| 287 | +/** |
| 288 | + * [#35384] The flow's and the position's labels are metadata too, so they are |
| 289 | + * resolved by the spec translators of their types — `translateFlow` (the |
| 290 | + * flow runner's reading of `flows.<flow>.label`; `/meta` serves a flow |
| 291 | + * document untranslated) and `translatePosition` (what `GET /meta/position` |
| 292 | + * serves) — with `/meta`'s `ResolveOptions`, and never through `t()`. |
| 293 | + * |
| 294 | + * The scenario: an app authored in English (`sourceLocale: 'en'`) that |
| 295 | + * defaults its users to Chinese (`defaultLocale: 'zh-CN'`, declared fallback |
| 296 | + * `zh-CN`) and ships an `en` pack beside the `zh-CN` one, which holds neither |
| 297 | + * label. `t('flows.<flow>.label', 'en')` falls to the `zh-CN` row; an `en` and |
| 298 | + * an `en-US` reader must read the authored label (the stored snapshot). An |
| 299 | + * `en-US` reader is negotiated to the `en` pack first, the authored language. |
| 300 | + * CONTROLS: a `zh-CN` reader reads the `zh-CN` rows, an `en` pack entry is |
| 301 | + * read by an `en` reader, and with no `sourceLocale` declared an `en` reader |
| 302 | + * walks the declared fallback to the `zh-CN` rows. |
| 303 | + */ |
| 304 | +describe('[#35384] the flow and position labels in the declared sourceLocale read as authored', () => { |
| 305 | + const EN = { [flowLabelKey('other_flow')]: 'Other flow' }; |
| 306 | + const declared = (sourceLocale: string | undefined, en: Record<string, string> = EN) => { |
| 307 | + const i18n = i18nWith({ 'zh-CN': ZH, en }); |
| 308 | + i18n.setDefaultLocale('zh-CN'); |
| 309 | + i18n.setFallbackLocale('zh-CN'); |
| 310 | + if (sourceLocale) i18n.setSourceLocale(sourceLocale); |
| 311 | + // The served adapter's shape (`FileI18nAdapter`): the declared fallback, read back by `getFallbackLocale()`. |
| 312 | + return Object.assign(i18n, { getFallbackLocale: () => 'zh-CN' }); |
| 313 | + }; |
| 314 | + const served = () => ({ |
| 315 | + process_name: 'flow:account_approval', current_step: 'manager_review', object_name: 'account', |
| 316 | + process_label: 'Account Approval', step_label: 'Manager Review', current_step_label: 'Manager Review', |
| 317 | + object_label: 'Account', |
| 318 | + }); |
| 319 | + |
| 320 | + it('the defect\'s precondition: t() answers an en reader with the zh-CN rows', () => { |
| 321 | + const i18n = declared('en'); |
| 322 | + expect(i18n.t(flowLabelKey('account_approval'), 'en')).toBe('客户审批'); |
| 323 | + expect(i18n.t(positionLabelKey('crm_sales_manager'), 'en')).toBe('销售经理'); |
| 324 | + }); |
| 325 | + |
| 326 | + it('an en-US reader is negotiated to the en pack, the authored language', () => { |
| 327 | + expect(negotiatedReaderLocale(declared('en'), 'en-US')).toBe('en'); |
| 328 | + }); |
| 329 | + |
| 330 | + for (const locale of ['en', 'en-US']) { |
| 331 | + it(`an ${locale} reader picks no defaultLocale row: the flow and position labels stand as authored`, () => { |
| 332 | + const labels = readerLabelsFor(declared('en'), locale)!; |
| 333 | + expect(labels.flowLabel('account_approval')).toBeUndefined(); |
| 334 | + expect(labels.positionLabel('crm_sales_manager')).toBeUndefined(); |
| 335 | + const row = served(); |
| 336 | + localizeRequestLabels(row, labels); |
| 337 | + expect(row.process_label).toBe('Account Approval'); |
| 338 | + }); |
| 339 | + } |
| 340 | + |
| 341 | + it('CONTROL: a zh-CN reader still reads the zh-CN rows', () => { |
| 342 | + const labels = readerLabelsFor(declared('en'), 'zh-CN')!; |
| 343 | + expect(labels.flowLabel('account_approval')).toBe('客户审批'); |
| 344 | + expect(labels.positionLabel('crm_sales_manager')).toBe('销售经理'); |
| 345 | + }); |
| 346 | + |
| 347 | + it('CONTROL: an en pack entry is what an en and an en-US reader read', () => { |
| 348 | + const en = { [flowLabelKey('account_approval')]: 'Account sign-off', [positionLabelKey('crm_sales_manager')]: 'Sales lead' }; |
| 349 | + for (const locale of ['en', 'en-US']) { |
| 350 | + const labels = readerLabelsFor(declared('en', en), locale)!; |
| 351 | + expect(labels.flowLabel('account_approval')).toBe('Account sign-off'); |
| 352 | + expect(labels.positionLabel('crm_sales_manager')).toBe('Sales lead'); |
| 353 | + } |
| 354 | + }); |
| 355 | + |
| 356 | + it('CONTROL: with no sourceLocale declared, an en reader walks the declared fallback to the zh-CN rows', () => { |
| 357 | + const labels = readerLabelsFor(declared(undefined), 'en')!; |
| 358 | + expect(labels.flowLabel('account_approval')).toBe('客户审批'); |
| 359 | + expect(labels.positionLabel('crm_sales_manager')).toBe('销售经理'); |
| 360 | + }); |
| 361 | + |
| 362 | + it('reads neither label through t(): a service that cannot hand its translations over picks none', () => { |
| 363 | + const tOnly: ReaderLabelI18n = { t: (key) => (key === flowLabelKey('f') ? '流程' : key === positionLabelKey('p') ? '岗位' : key) }; |
| 364 | + const labels = readerLabelsFor(tOnly, 'zh-CN')!; |
| 365 | + expect(labels.flowLabel('f')).toBeUndefined(); |
| 366 | + expect(labels.positionLabel('p')).toBeUndefined(); |
| 367 | + }); |
| 368 | + |
| 369 | + it('reads each label once per read', () => { |
| 370 | + let reads = 0; |
| 371 | + const i18n = declared('en'); |
| 372 | + const counting: ReaderLabelI18n = { |
| 373 | + t: (key) => key, |
| 374 | + getLocales: () => i18n.getLocales(), |
| 375 | + getTranslations: (code) => { reads += 1; return i18n.getTranslations(code); }, |
| 376 | + }; |
| 377 | + const labels = readerLabelsFor(counting, 'zh-CN')!; |
| 378 | + labels.flowLabel('account_approval'); |
| 379 | + labels.flowLabel('account_approval'); |
| 380 | + labels.positionLabel('crm_sales_manager'); |
| 381 | + // One bundle for the read, built once: a read of each locale the service holds. |
| 382 | + expect(reads).toBe(2); |
| 383 | + }); |
| 384 | +}); |
| 385 | + |
| 386 | +/** |
| 387 | + * [#35384] A renamed packaged object is named by its rename, as `/meta` names |
| 388 | + * it: the object's served definition is translated with its packaged base as |
| 389 | + * `packagedBase` (ADR-0029 D9.2a), the base read off the engine's registry |
| 390 | + * (`getPackagedObjectBase`, the body the metadata protocol's accessor reads). |
| 391 | + * The bundle's `objects.crm_deal.label` translates the PACKAGED label, so it |
| 392 | + * says nothing about the rename, in any locale. |
| 393 | + * |
| 394 | + * The registry is the real `SchemaRegistry`: a packaged `Opportunity` that a |
| 395 | + * code extension renames `Deal`. The oracle is the call `/meta` makes, |
| 396 | + * `translateMetadataDocument('object', served, bundle, { …resolveOptions, |
| 397 | + * packagedBase })`. CONTROLS: the unrenamed object reads the bundle in every |
| 398 | + * locale, and a registry that cannot answer translates the name alone. |
| 399 | + */ |
| 400 | +describe('[#35384] a renamed packaged object is named by its rename, as /meta names it', () => { |
| 401 | + const PKG = 'com.acme.crm'; |
| 402 | + const PAIR = 'com.acme.crm.custom'; |
| 403 | + const engineWith = (renamed: boolean) => { |
| 404 | + const registry = new SchemaRegistry({ multiTenant: false }); |
| 405 | + registry.logLevel = 'silent'; |
| 406 | + registry.registerObject({ name: 'crm_deal', label: 'Opportunity', fields: {} } as never, PKG, undefined, 'own'); |
| 407 | + if (renamed) registry.registerObjectExtension({ extend: 'crm_deal', label: 'Deal' } as never, PAIR); |
| 408 | + return { getSchema: (name: string) => registry.getObject(name), registry }; |
| 409 | + }; |
| 410 | + const i18n = () => { |
| 411 | + const service = i18nWith({ |
| 412 | + 'zh-CN': { [objectLabelKey('crm_deal')]: '商机' }, |
| 413 | + en: { [objectLabelKey('crm_deal')]: 'Opportunity' }, |
| 414 | + }); |
| 415 | + service.setDefaultLocale('zh-CN'); |
| 416 | + service.setFallbackLocale('zh-CN'); |
| 417 | + service.setSourceLocale('en'); |
| 418 | + return Object.assign(service, { getFallbackLocale: () => 'zh-CN' }); |
| 419 | + }; |
| 420 | + /** What `GET /meta/object/crm_deal` serves as the label for a request in `locale`. */ |
| 421 | + const metaLabel = (engine: ReturnType<typeof engineWith>, locale: string): unknown => { |
| 422 | + const service = i18n(); |
| 423 | + const bundle = Object.fromEntries(service.getLocales().map((code) => [code, service.getTranslations(code)])); |
| 424 | + return translateMetadataDocument('object', engine.getSchema('crm_deal'), bundle as never, { |
| 425 | + locale: negotiatedReaderLocale(service, locale), |
| 426 | + fallbackChain: ['zh-CN'], |
| 427 | + defaultLocale: 'en', |
| 428 | + packagedBase: engine.registry.getPackagedObjectBase('crm_deal'), |
| 429 | + }).label; |
| 430 | + }; |
| 431 | + |
| 432 | + it('the fixture: the served label is the rename, the packaged base keeps the old one', () => { |
| 433 | + const engine = engineWith(true); |
| 434 | + expect((engine.getSchema('crm_deal') as { label?: string }).label).toBe('Deal'); |
| 435 | + expect((engine.registry.getPackagedObjectBase('crm_deal') as { label?: string }).label).toBe('Opportunity'); |
| 436 | + }); |
| 437 | + |
| 438 | + for (const locale of ['en', 'en-US', 'zh-CN']) { |
| 439 | + it(`a ${locale} reader reads the rename, as /meta serves it`, () => { |
| 440 | + const engine = engineWith(true); |
| 441 | + expect(metaLabel(engine, locale)).toBe('Deal'); |
| 442 | + expect(readerLabelsFor(i18n(), locale, engine)!.objectLabel('crm_deal')).toBe('Deal'); |
| 443 | + const row = { object_name: 'crm_deal', object_label: 'Deal' }; |
| 444 | + localizeRequestLabels(row, readerLabelsFor(i18n(), locale, engine)!); |
| 445 | + expect(row.object_label).toBe('Deal'); |
| 446 | + }); |
| 447 | + } |
| 448 | + |
| 449 | + it('CONTROL: the unrenamed object reads the bundle, as /meta serves it', () => { |
| 450 | + const engine = engineWith(false); |
| 451 | + expect(readerLabelsFor(i18n(), 'zh-CN', engine)!.objectLabel('crm_deal')).toBe('商机'); |
| 452 | + expect(metaLabel(engine, 'zh-CN')).toBe('商机'); |
| 453 | + expect(readerLabelsFor(i18n(), 'en', engine)!.objectLabel('crm_deal')).toBe('Opportunity'); |
| 454 | + }); |
| 455 | + |
| 456 | + it('CONTROL: a registry that throws is no baseline known, and the read does not throw', () => { |
| 457 | + const engine = engineWith(true); |
| 458 | + const failing = { getSchema: engine.getSchema, registry: { getPackagedObjectBase: () => { throw new Error('registry exploded'); } } }; |
| 459 | + expect(readerLabelsFor(i18n(), 'zh-CN', failing)!.objectLabel('crm_deal')).toBe('商机'); |
| 460 | + const blind = { getSchema: () => { throw new Error('schema exploded'); } }; |
| 461 | + expect(readerLabelsFor(i18n(), 'zh-CN', blind)!.objectLabel('crm_deal')).toBe('商机'); |
| 462 | + }); |
| 463 | +}); |
0 commit comments