|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// The `view` panel's `userFilters` tooltip names the legal values of a STRICT |
| 4 | +// enum, and it is the only place that panel names them at all. So that |
| 5 | +// sentence is not ordinary prose: the tokens inside it are what an author — |
| 6 | +// or an AI reading the panel — types into `userFilters.element`, and |
| 7 | +// `UserFiltersSchema` refuses anything else. |
| 8 | +// |
| 9 | +// All three translated catalogs used to render those tokens as ordinary words |
| 10 | +// (下拉 / 标签页 / 开关 · ドロップダウン / タブ / トグル · |
| 11 | +// desplegable / pestañas / interruptor), so a translated-locale author was |
| 12 | +// shown a value the schema rejects. The repair keeps the enum values verbatim |
| 13 | +// English inside the translated sentence and leaves the prose around them |
| 14 | +// translated. |
| 15 | +// |
| 16 | +// This pins THAT leaf, in the three locales, and nothing wider: it reads one |
| 17 | +// key. What keeps it from passing vacuously — or passing while the defect |
| 18 | +// class walks back in — is four controls, each of which fails on its own: |
| 19 | +// |
| 20 | +// 1. THE SET IS DERIVED, NOT LISTED. `SCHEMA_ELEMENT_VALUES` is read off |
| 21 | +// `UserFiltersSchema.shape.element` (unwrapping `.default()`) and asserted |
| 22 | +// SET-EQUAL to the literal below. Every tooltip assertion then runs over |
| 23 | +// the DERIVED set. So enum GROWTH reddens — add a fourth member and this |
| 24 | +// control fails and every locale fails the presence leg — and not only |
| 25 | +// enum shrinkage. A hand-listed set catches shrinkage alone and lets a new |
| 26 | +// legal value go unnamed in all four catalogs with the pin green, which is |
| 27 | +// this card's own defect class reintroduced. |
| 28 | +// 2. THE SCHEMA LEG. Every derived token is `safeParse`d through |
| 29 | +// `UserFiltersSchema` and must be ACCEPTED; the words the translations |
| 30 | +// used to carry, and the capitalised spellings the neighbouring `page` |
| 31 | +// panel shows for its `filter-mode` widget, are `safeParse`d and must be |
| 32 | +// REFUSED. A tooltip rewritten to name some other word fails here. |
| 33 | +// 3. THE EXACT-SET LEG. The tooltip's parenthetical must name the derived |
| 34 | +// values and NOTHING ELSE, in order. Checking only "no fewer" lets |
| 35 | +// `(dropdown / tabs / toggle / chips)` through. |
| 36 | +// 4. THE STILL-TRANSLATED LEG. Each translated helpText must differ from the |
| 37 | +// `en` source and must still carry its own prose at BOTH ends of the |
| 38 | +// sentence. Anchoring the leading fragment alone lets the trailing half |
| 39 | +// revert to English — #19403's defect surviving on the larger half of the |
| 40 | +// sentence. "Fixing" a red by copying the whole English sentence into the |
| 41 | +// three catalogs fails here too. |
| 42 | + |
| 43 | +import { describe, it, expect } from 'vitest'; |
| 44 | +import { UserFiltersSchema } from '@objectstack/spec/ui'; |
| 45 | + |
| 46 | +import { enMetadataForms } from './en.metadata-forms.generated.js'; |
| 47 | +import { zhCNMetadataForms } from './zh-CN.metadata-forms.generated.js'; |
| 48 | +import { jaJPMetadataForms } from './ja-JP.metadata-forms.generated.js'; |
| 49 | +import { esESMetadataForms } from './es-ES.metadata-forms.generated.js'; |
| 50 | + |
| 51 | +/** |
| 52 | + * The values `UserFiltersSchema.element` accepts, READ OFF THE SCHEMA. |
| 53 | + * |
| 54 | + * `element` is `z.enum([...]).default('dropdown')`, so the enum node sits |
| 55 | + * behind the default wrapper. Unwrapped here rather than reached for by a |
| 56 | + * fixed path, and the shape is asserted below — a derivation that silently |
| 57 | + * yielded `undefined` would make every assertion over it vacuous. |
| 58 | + */ |
| 59 | +const SCHEMA_ELEMENT_VALUES: readonly string[] = (() => { |
| 60 | + let node: any = (UserFiltersSchema as any).shape?.element; |
| 61 | + for (let hop = 0; hop < 5 && (node?.def ?? node?._def)?.type === 'default'; hop += 1) { |
| 62 | + node = (node.def ?? node._def).innerType; |
| 63 | + } |
| 64 | + return node?.options ?? []; |
| 65 | +})(); |
| 66 | + |
| 67 | +/** What that set is expected to be — the pin half of control 1. */ |
| 68 | +const EXPECTED_ELEMENT_VALUES = ['dropdown', 'tabs', 'toggle'] as const; |
| 69 | + |
| 70 | +/** |
| 71 | + * Spellings that must stay UNWRITABLE, so the presence assertions above cannot |
| 72 | + * be satisfied by a word the schema would refuse. |
| 73 | + * |
| 74 | + * Two groups. The per-locale rows are what each tooltip used to name. The |
| 75 | + * shared row is the capitalised set the neighbouring `page` Interface panel |
| 76 | + * shows for its `filter-mode` widget (`None / Tabs / Dropdown`): those are UI |
| 77 | + * mode names, not enum tokens — `z.enum` is case-sensitive, so the enum |
| 78 | + * refuses them, and `None` is not a member at all (it stands for the absence |
| 79 | + * of the config). Asserted here so the difference is a measurement rather than |
| 80 | + * a remark. |
| 81 | + * |
| 82 | + * `tabs` is deliberately absent from every per-locale row: its former word is |
| 83 | + * also the locale's ordinary word for the "tab presets" PROSE at the end of |
| 84 | + * the same sentence (标签页预设 · タブプリセット · preajustes de pestañas), |
| 85 | + * which is translated on purpose and must stay. Asserting its absence would |
| 86 | + * demand the prose be de-translated — the inverse defect again — so `tabs` is |
| 87 | + * covered by controls 1, 3 and the presence leg instead. |
| 88 | + */ |
| 89 | +const FORMER_TRANSLATIONS: Readonly<Record<string, readonly string[]>> = { |
| 90 | + 'zh-CN': ['下拉', '开关'], |
| 91 | + 'ja-JP': ['ドロップダウン', 'トグル'], |
| 92 | + 'es-ES': ['desplegable', 'interruptor'], |
| 93 | +}; |
| 94 | +const NEIGHBOURING_PANEL_LABELS = ['None', 'Tabs', 'Dropdown'] as const; |
| 95 | + |
| 96 | +/** |
| 97 | + * Prose each locale must still render in its own language, at BOTH ends of the |
| 98 | + * sentence — the leading clause and the trailing one. |
| 99 | + */ |
| 100 | +const PROSE_ANCHORS: Readonly<Record<string, readonly [string, string]>> = { |
| 101 | + 'zh-CN': ['快速筛选栏', '暴露的字段或标签页预设'], |
| 102 | + 'ja-JP': ['クイックフィルターバー', '公開フィールドまたはタブプリセット'], |
| 103 | + 'es-ES': ['Barra de filtros rápidos', 'campos expuestos o preajustes de pestañas'], |
| 104 | +}; |
| 105 | + |
| 106 | +const TRANSLATED_LOCALES: ReadonlyArray<readonly [string, Record<string, any>]> = [ |
| 107 | + ['zh-CN', zhCNMetadataForms as Record<string, any>], |
| 108 | + ['ja-JP', jaJPMetadataForms as Record<string, any>], |
| 109 | + ['es-ES', esESMetadataForms as Record<string, any>], |
| 110 | +]; |
| 111 | + |
| 112 | +const helpTextOf = (forms: Record<string, any>): string => { |
| 113 | + const help = forms?.view?.fields?.userFilters?.helpText; |
| 114 | + expect(typeof help, 'view.fields.userFilters.helpText is missing from this catalog').toBe('string'); |
| 115 | + return help as string; |
| 116 | +}; |
| 117 | + |
| 118 | +/** |
| 119 | + * The tokens a tooltip names, read out of the parenthetical that carries them. |
| 120 | + * Both ASCII and full-width brackets, because each locale keeps its own |
| 121 | + * punctuation. Returns `[]` when no parenthetical names the first value — a |
| 122 | + * sentence reshaped past this reader's reach, which reddens rather than |
| 123 | + * silently measuring nothing. |
| 124 | + */ |
| 125 | +const namedTokens = (help: string): string[] => { |
| 126 | + const first = SCHEMA_ELEMENT_VALUES[0]; |
| 127 | + if (first === undefined) return []; |
| 128 | + for (const match of help.matchAll(/[((]([^))]*)[))]/g)) { |
| 129 | + const inner = match[1] ?? ''; |
| 130 | + if (inner.includes(first)) { |
| 131 | + return inner.split('/').map((part) => part.trim()).filter((part) => part.length > 0); |
| 132 | + } |
| 133 | + } |
| 134 | + return []; |
| 135 | +}; |
| 136 | + |
| 137 | +describe('view.fields.userFilters helpText names writable element tokens', () => { |
| 138 | + it('control: the asserted set IS the schema\'s set, derived not copied', () => { |
| 139 | + expect( |
| 140 | + Array.isArray(SCHEMA_ELEMENT_VALUES) && SCHEMA_ELEMENT_VALUES.length > 0, |
| 141 | + 'could not read the enum off UserFiltersSchema.shape.element — every assertion below would be vacuous', |
| 142 | + ).toBe(true); |
| 143 | + expect( |
| 144 | + [...SCHEMA_ELEMENT_VALUES].sort(), |
| 145 | + 'UserFiltersSchema.element no longer accepts exactly these values — the tooltip names a set the schema has moved away from, in all four catalogs', |
| 146 | + ).toEqual([...EXPECTED_ELEMENT_VALUES].sort()); |
| 147 | + }); |
| 148 | + |
| 149 | + it('control: every value it names is accepted by the schema', () => { |
| 150 | + for (const value of SCHEMA_ELEMENT_VALUES) { |
| 151 | + expect( |
| 152 | + UserFiltersSchema.safeParse({ element: value }).success, |
| 153 | + `UserFiltersSchema refused '${value}' — the tooltip would be naming an unwritable value`, |
| 154 | + ).toBe(true); |
| 155 | + } |
| 156 | + }); |
| 157 | + |
| 158 | + it('control: the words the translations used to name are refused', () => { |
| 159 | + for (const [locale, words] of Object.entries(FORMER_TRANSLATIONS)) { |
| 160 | + for (const word of words) { |
| 161 | + expect( |
| 162 | + UserFiltersSchema.safeParse({ element: word }).success, |
| 163 | + `UserFiltersSchema accepted '${word}' (${locale}) — this control no longer proves anything`, |
| 164 | + ).toBe(false); |
| 165 | + } |
| 166 | + } |
| 167 | + }); |
| 168 | + |
| 169 | + it('control: the neighbouring panel\'s filter-mode labels are refused too', () => { |
| 170 | + for (const label of NEIGHBOURING_PANEL_LABELS) { |
| 171 | + expect( |
| 172 | + UserFiltersSchema.safeParse({ element: label }).success, |
| 173 | + `UserFiltersSchema accepted '${label}' — the page panel's capitalised labels would then be enum tokens, and this tooltip could borrow their shape`, |
| 174 | + ).toBe(false); |
| 175 | + } |
| 176 | + }); |
| 177 | + |
| 178 | + it('en: the source names every accepted value, and only those', () => { |
| 179 | + const help = helpTextOf(enMetadataForms as Record<string, any>); |
| 180 | + for (const value of SCHEMA_ELEMENT_VALUES) { |
| 181 | + expect(help, `en helpText does not name '${value}'`).toContain(value); |
| 182 | + } |
| 183 | + expect(namedTokens(help), 'en helpText names a different set from the enum').toEqual([ |
| 184 | + ...SCHEMA_ELEMENT_VALUES, |
| 185 | + ]); |
| 186 | + }); |
| 187 | + |
| 188 | + for (const [locale, forms] of TRANSLATED_LOCALES) { |
| 189 | + it(`${locale}: names every accepted value verbatim, and only those`, () => { |
| 190 | + const help = helpTextOf(forms); |
| 191 | + for (const value of SCHEMA_ELEMENT_VALUES) { |
| 192 | + expect( |
| 193 | + help, |
| 194 | + `${locale} helpText does not name '${value}' — a translated-locale author is shown a value the schema refuses, or is never shown a legal one`, |
| 195 | + ).toContain(value); |
| 196 | + } |
| 197 | + expect( |
| 198 | + namedTokens(help), |
| 199 | + `${locale} helpText names a different set from the enum — an extra token here is a value the schema refuses`, |
| 200 | + ).toEqual([...SCHEMA_ELEMENT_VALUES]); |
| 201 | + for (const word of FORMER_TRANSLATIONS[locale]!) { |
| 202 | + expect(help, `${locale} helpText still spells the enum value as '${word}'`).not.toContain(word); |
| 203 | + } |
| 204 | + }); |
| 205 | + |
| 206 | + it(`${locale}: the prose at both ends is still translated`, () => { |
| 207 | + const help = helpTextOf(forms); |
| 208 | + expect(help, `${locale} helpText is a byte copy of the en source`).not.toBe( |
| 209 | + helpTextOf(enMetadataForms as Record<string, any>), |
| 210 | + ); |
| 211 | + const [leading, trailing] = PROSE_ANCHORS[locale]!; |
| 212 | + expect(help, `${locale} helpText lost its leading prose`).toContain(leading); |
| 213 | + expect(help, `${locale} helpText lost its trailing prose`).toContain(trailing); |
| 214 | + }); |
| 215 | + } |
| 216 | +}); |
0 commit comments