|
| 1 | +// Copyright (c) 2026 ObjectStack contributors. Apache-2.0 license. |
| 2 | +// |
| 3 | +// `reports.<name>.…` — the extractor walks the report translation group. |
| 4 | +// |
| 5 | +// A report is listed by its label and description on the app's report index, |
| 6 | +// and headed by its label on the report page. Both are served through |
| 7 | +// `translateReport`, so without this walk `os i18n extract` scaffolded no key |
| 8 | +// for them and `os i18n check` measured a debt of zero while every report |
| 9 | +// listed in the authoring language. |
| 10 | + |
| 11 | +import { describe, it, expect } from 'vitest'; |
| 12 | +import { collectExpectedEntries, extractTranslations } from '../src/utils/i18n-extract.js'; |
| 13 | +import { computeI18nCoverage } from '../src/utils/i18n-coverage.js'; |
| 14 | +import { TranslationDataSchema } from '@objectstack/spec/system'; |
| 15 | + |
| 16 | +const config = () => ({ |
| 17 | + reports: [ |
| 18 | + { |
| 19 | + name: 'opps_by_stage', |
| 20 | + label: 'Opportunities by Stage', |
| 21 | + description: 'Summary of opportunities grouped by stage', |
| 22 | + type: 'summary', |
| 23 | + dataset: 'pipeline', |
| 24 | + rows: ['stage'], |
| 25 | + values: ['amount'], |
| 26 | + }, |
| 27 | + // No description: nothing an author wrote, so nothing is demanded. |
| 28 | + { name: 'leads_by_source', label: 'Leads by Source', type: 'summary', dataset: 'leads', values: ['n'] }, |
| 29 | + ], |
| 30 | +}); |
| 31 | + |
| 32 | +const keysOf = (group: string) => |
| 33 | + collectExpectedEntries(config()) |
| 34 | + .filter((e) => e.path[0] === group) |
| 35 | + .map((e) => e.path.join('.')) |
| 36 | + .sort(); |
| 37 | + |
| 38 | +describe('i18n extract — the `reports` group', () => { |
| 39 | + it('walks each report\'s label and description', () => { |
| 40 | + expect(keysOf('reports')).toEqual([ |
| 41 | + 'reports.leads_by_source.description', |
| 42 | + 'reports.leads_by_source.label', |
| 43 | + 'reports.opps_by_stage.description', |
| 44 | + 'reports.opps_by_stage.label', |
| 45 | + ]); |
| 46 | + }); |
| 47 | + |
| 48 | + it('seeds authored text and records an unwritten string without demanding it', () => { |
| 49 | + const byKey = new Map(collectExpectedEntries(config()).map((e) => [e.path.join('.'), e])); |
| 50 | + expect(byKey.get('reports.opps_by_stage.label')) |
| 51 | + .toMatchObject({ sourceValue: 'Opportunities by Stage', inline: 'Opportunities by Stage', source: 'report' }); |
| 52 | + expect(byKey.get('reports.opps_by_stage.description')) |
| 53 | + .toMatchObject({ sourceValue: 'Summary of opportunities grouped by stage', source: 'report' }); |
| 54 | + const unwritten = byKey.get('reports.leads_by_source.description'); |
| 55 | + expect(unwritten?.sourceValue).toBeUndefined(); |
| 56 | + expect(unwritten?.inline).toBeUndefined(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('`TranslationDataSchema` accepts a bundle written at the extracted paths', () => { |
| 60 | + const out = extractTranslations(config(), { locales: ['zh-CN'] }); |
| 61 | + const zh = out.bundles['zh-CN'] as any; |
| 62 | + expect(Object.keys(zh?.reports ?? {}).sort()).toEqual(['leads_by_source', 'opps_by_stage']); |
| 63 | + expect(zh?.reports?.opps_by_stage?.label).toBeDefined(); |
| 64 | + expect(TranslationDataSchema.safeParse({ reports: zh.reports }).success).toBe(true); |
| 65 | + }); |
| 66 | + |
| 67 | + it('reports an untranslated label under its own bucket', () => { |
| 68 | + const report = computeI18nCoverage({ |
| 69 | + ...config(), |
| 70 | + i18n: { supportedLocales: ['en', 'zh-CN'] }, |
| 71 | + translations: [ |
| 72 | + { 'zh-CN': { reports: { opps_by_stage: { label: '各阶段商机', description: '按阶段汇总的商机' } } } }, |
| 73 | + ], |
| 74 | + }); |
| 75 | + const zhIssues = report.issues.filter((i) => i.locale === 'zh-CN'); |
| 76 | + const keys = zhIssues.map((i) => i.key); |
| 77 | + expect(keys).toContain('reports.leads_by_source.label'); |
| 78 | + expect(keys).not.toContain('reports.opps_by_stage.label'); |
| 79 | + expect(keys).not.toContain('reports.opps_by_stage.description'); |
| 80 | + // Nothing authored ⇒ nothing to translate. |
| 81 | + expect(keys).not.toContain('reports.leads_by_source.description'); |
| 82 | + expect(zhIssues.find((i) => i.key === 'reports.leads_by_source.label')?.source).toBe('report'); |
| 83 | + }); |
| 84 | +}); |
0 commit comments