|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectui#7254 — the dashboard property panel was an all-English island |
| 5 | + * inside a Chinese Studio. |
| 6 | + * |
| 7 | + * The panel is spec-driven on purpose (`dashboardForm` from |
| 8 | + * `@objectstack/spec/ui` fed straight into SchemaForm), and the spec authors |
| 9 | + * its copy in English, so every section heading, field label and hint reached |
| 10 | + * the author untranslated — including "Grid gap (Tailwind units)", a unit only |
| 11 | + * a developer can act on. |
| 12 | + * |
| 13 | + * These pins assert the OVERLAY, and specifically the two things a hand-rolled |
| 14 | + * walker would have got wrong and a shape-only test would not have noticed: |
| 15 | + * the section key is the SLUGGED label (not the label text), and a `composite` |
| 16 | + * field's sub-rows are synthesized from the bundle — completely, or the ones |
| 17 | + * left out vanish from the form. |
| 18 | + */ |
| 19 | +import { describe, expect, it } from 'vitest'; |
| 20 | +import { dashboardForm } from '@objectstack/spec/ui'; |
| 21 | +import { localizeMetadataForm, hasMetadataFormOverlay } from './metadata-form-i18n'; |
| 22 | +import { getDashboardForm } from './dashboard-schema'; |
| 23 | + |
| 24 | +/** The two form shapes these assertions read. `unknown` everywhere else. */ |
| 25 | +interface FormField { |
| 26 | + field?: string; |
| 27 | + type?: string; |
| 28 | + label?: string; |
| 29 | + helpText?: string; |
| 30 | + fields?: FormField[]; |
| 31 | +} |
| 32 | +interface Section { |
| 33 | + label?: string; |
| 34 | + description?: string; |
| 35 | + fields?: FormField[]; |
| 36 | +} |
| 37 | + |
| 38 | +function sections(form: unknown): Section[] { |
| 39 | + return ((form as { sections?: Section[] } | undefined)?.sections ?? []) as Section[]; |
| 40 | +} |
| 41 | + |
| 42 | +function fieldByName(form: unknown, name: string): FormField | undefined { |
| 43 | + for (const s of sections(form)) { |
| 44 | + for (const f of s.fields ?? []) { |
| 45 | + if (f?.field === name) return f; |
| 46 | + } |
| 47 | + } |
| 48 | + return undefined; |
| 49 | +} |
| 50 | + |
| 51 | +describe('localizeMetadataForm — the spec form, in the author’s language', () => { |
| 52 | + it('has an overlay for `dashboard` at zh and none at en', () => { |
| 53 | + expect(hasMetadataFormOverlay('dashboard', 'zh-CN')).toBe(true); |
| 54 | + expect(hasMetadataFormOverlay('dashboard', 'en-US')).toBe(false); |
| 55 | + }); |
| 56 | + |
| 57 | + it('leaves the form untouched (same object) outside zh', () => { |
| 58 | + const form = dashboardForm as unknown as Record<string, unknown>; |
| 59 | + expect(localizeMetadataForm(form, 'dashboard', 'en-US')).toBe(form); |
| 60 | + }); |
| 61 | + |
| 62 | + it('leaves a type the overlay does not carry untouched', () => { |
| 63 | + const form = dashboardForm as unknown as Record<string, unknown>; |
| 64 | + expect(localizeMetadataForm(form, 'report', 'zh-CN')).toBe(form); |
| 65 | + }); |
| 66 | + |
| 67 | + it('translates section headings — addressed by the SLUGGED label, not the label text', () => { |
| 68 | + const zh = localizeMetadataForm( |
| 69 | + dashboardForm as unknown as Record<string, unknown>, |
| 70 | + 'dashboard', |
| 71 | + 'zh-CN', |
| 72 | + ); |
| 73 | + const labels = sections(zh).map((s) => s.label); |
| 74 | + expect(labels).toContain('布局'); // 'Layout' → section key `layout` |
| 75 | + expect(labels).toContain('筛选'); // 'Filters' → `filters` |
| 76 | + expect(labels).not.toContain('Layout'); |
| 77 | + const layout = sections(zh).find((s) => s.label === '布局'); |
| 78 | + expect(layout?.description).toBe('栅格尺寸与刷新频率。'); |
| 79 | + expect(layout?.description).not.toMatch(/refresh cadence/); |
| 80 | + }); |
| 81 | + |
| 82 | + it('translates the layout field labels and hints the card named', () => { |
| 83 | + const zh = localizeMetadataForm( |
| 84 | + dashboardForm as unknown as Record<string, unknown>, |
| 85 | + 'dashboard', |
| 86 | + 'zh-CN', |
| 87 | + ); |
| 88 | + expect(fieldByName(zh, 'columns')).toMatchObject({ label: '列数', helpText: '栅格列数(默认 12)' }); |
| 89 | + expect(fieldByName(zh, 'refreshInterval')?.label).toBe('自动刷新'); |
| 90 | + expect(fieldByName(zh, 'header')?.label).toBe('页眉'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('drops the "Tailwind units" developer vocabulary rather than transliterating it', () => { |
| 94 | + const zh = localizeMetadataForm( |
| 95 | + dashboardForm as unknown as Record<string, unknown>, |
| 96 | + 'dashboard', |
| 97 | + 'zh-CN', |
| 98 | + ); |
| 99 | + const gap = fieldByName(zh, 'gap'); |
| 100 | + expect(gap?.label).toBe('间距'); |
| 101 | + expect(gap?.helpText).not.toMatch(/Tailwind/i); |
| 102 | + // The English source still says it — that copy is the spec's to fix, and |
| 103 | + // this overlay deliberately does not rewrite the producer's own text. |
| 104 | + expect(fieldByName(dashboardForm, 'gap')?.helpText).toMatch(/Tailwind/i); |
| 105 | + }); |
| 106 | + |
| 107 | + it('synthesizes ALL of the `header` composite’s sub-rows — a partial list would hide the rest', () => { |
| 108 | + const zh = localizeMetadataForm( |
| 109 | + dashboardForm as unknown as Record<string, unknown>, |
| 110 | + 'dashboard', |
| 111 | + 'zh-CN', |
| 112 | + ); |
| 113 | + const header = fieldByName(zh, 'header'); |
| 114 | + expect(header?.type).toBe('composite'); |
| 115 | + const children = (header?.fields ?? []).map((f) => f.field); |
| 116 | + // Exactly `DashboardHeaderSchema`'s three properties. SchemaForm prefers a |
| 117 | + // declared `fields` array over the schema-derived one, so anything absent |
| 118 | + // here would disappear from the panel. |
| 119 | + expect(children.sort()).toEqual(['actions', 'showDescription', 'showTitle']); |
| 120 | + const byName = new Map((header!.fields ?? []).map((f) => [f.field, f])); |
| 121 | + expect(byName.get('showTitle')?.label).toBe('显示标题'); |
| 122 | + expect(byName.get('showTitle')?.helpText).toBe('在页眉中显示仪表板名称'); |
| 123 | + expect(byName.get('showDescription')?.label).toBe('显示描述'); |
| 124 | + expect(byName.get('actions')?.label).toBe('操作按钮'); |
| 125 | + }); |
| 126 | +}); |
| 127 | + |
| 128 | +describe('getDashboardForm — the overlay reaches the panel, and caches per locale', () => { |
| 129 | + it('serves the spec’s English by default and Chinese to a zh console', () => { |
| 130 | + const en = getDashboardForm('en-US'); |
| 131 | + const zh = getDashboardForm('zh-CN'); |
| 132 | + expect(sections(en).map((s) => s.label)).toContain('Layout'); |
| 133 | + expect(sections(zh).map((s) => s.label)).toContain('布局'); |
| 134 | + }); |
| 135 | + |
| 136 | + it('does not serve one locale’s copy to another (the single-slot cache bug)', () => { |
| 137 | + // Order matters: ask for zh first, then en. A single memo slot would hand |
| 138 | + // the Chinese form to the English console. |
| 139 | + expect(sections(getDashboardForm('zh-CN')).map((s) => s.label)).toContain('布局'); |
| 140 | + expect(sections(getDashboardForm('en-US')).map((s) => s.label)).toContain('Layout'); |
| 141 | + expect(sections(getDashboardForm('en-US')).map((s) => s.label)).not.toContain('布局'); |
| 142 | + }); |
| 143 | + |
| 144 | + it('is stable per locale', () => { |
| 145 | + expect(getDashboardForm('zh-CN')).toBe(getDashboardForm('zh-CN')); |
| 146 | + }); |
| 147 | + |
| 148 | + it('still prunes the fields the curated inspector owns', () => { |
| 149 | + const zh = getDashboardForm('zh-CN'); |
| 150 | + for (const owned of ['name', 'label', 'description', 'widgets']) { |
| 151 | + expect(fieldByName(zh, owned)).toBeUndefined(); |
| 152 | + } |
| 153 | + }); |
| 154 | +}); |
0 commit comments