|
| 1 | +// Copyright (c) 2026 ObjectStack contributors. Apache-2.0 license. |
| 2 | +// |
| 3 | +// objectstack#28519 — `cancelText` on the action contract: the label of the |
| 4 | +// dismiss button on the dialog an action opens before it runs (its confirm |
| 5 | +// dialog, or its param dialog), in place of the dialog's default "Cancel". |
| 6 | +// |
| 7 | +// These pins hold the spec half: the parse on both host dialogs (both |
| 8 | +// I18nLabel forms), the blank refusal, the refusal on an action that opens no |
| 9 | +// dialog, the alias, and the translation slot with its `translateAction` |
| 10 | +// overlay. The dangling translation half is pinned in |
| 11 | +// `packages/lint/src/validate-translation-references.test.ts`; its twin, the |
| 12 | +// param dialog's submit label, in `action-submit-text.test.ts`. |
| 13 | + |
| 14 | +import { describe, it, expect } from 'vitest'; |
| 15 | +import { ActionSchema, InlineActionSchema } from './action.zod'; |
| 16 | +import { ObjectTranslationDataSchema, TranslationDataSchema, type TranslationBundle } from '../system/translation.zod'; |
| 17 | +import { translateAction, type ActionLike } from '../system/i18n-resolver'; |
| 18 | + |
| 19 | +/** |
| 20 | + * A param-LESS action that asks first — the confirm dialog's host, in the |
| 21 | + * shape of `sys_invitation`'s Cancel Invitation, the row that started this. |
| 22 | + */ |
| 23 | +const confirmHost = { |
| 24 | + name: 'cancel_invitation', |
| 25 | + label: 'Cancel Invitation', |
| 26 | + objectName: 'sys_invitation', |
| 27 | + type: 'api', |
| 28 | + target: '/api/v1/auth/organization/cancel-invitation', |
| 29 | + confirmText: 'Cancel this invitation? The recipient will no longer be able to accept it.', |
| 30 | +} as const; |
| 31 | + |
| 32 | +/** An action that collects params — the param dialog's host. */ |
| 33 | +const paramHost = { |
| 34 | + name: 'close_case', |
| 35 | + label: 'Close Case', |
| 36 | + objectName: 'crm_case', |
| 37 | + target: 'closeCase', |
| 38 | + params: [{ name: 'resolution', label: 'Resolution', type: 'textarea', required: true }], |
| 39 | +} as const; |
| 40 | + |
| 41 | +/** An action that opens neither dialog: it runs on click. */ |
| 42 | +const { confirmText: _confirmText, ...noDialog } = confirmHost; |
| 43 | + |
| 44 | +const firstIssue = (input: unknown) => { |
| 45 | + const result = ActionSchema.safeParse(input); |
| 46 | + expect(result.success).toBe(false); |
| 47 | + return result.success ? undefined : result.error.issues[0]; |
| 48 | +}; |
| 49 | + |
| 50 | +describe('ActionSchema.cancelText — the parse', () => { |
| 51 | + it('accepts a plain string on the confirm dialog\'s host and keeps it verbatim', () => { |
| 52 | + const result = ActionSchema.safeParse({ ...confirmHost, cancelText: 'Keep invitation' }); |
| 53 | + |
| 54 | + expect(result.success).toBe(true); |
| 55 | + expect(result.success && result.data.cancelText).toBe('Keep invitation'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('accepts it on the param dialog\'s host, beside that dialog\'s `submitText`', () => { |
| 59 | + const result = ActionSchema.safeParse({ ...paramHost, submitText: 'Close Case', cancelText: 'Keep open' }); |
| 60 | + |
| 61 | + expect(result.success).toBe(true); |
| 62 | + expect(result.success && result.data.cancelText).toBe('Keep open'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('accepts the inline locale map, as every I18nLabel does', () => { |
| 66 | + const localized = { en: 'Keep invitation', 'zh-CN': '保留邀请' }; |
| 67 | + const result = ActionSchema.safeParse({ ...confirmHost, cancelText: localized }); |
| 68 | + |
| 69 | + expect(result.success).toBe(true); |
| 70 | + expect(result.success && result.data.cancelText).toEqual(localized); |
| 71 | + }); |
| 72 | + |
| 73 | + it('is optional — an action without it parses, and the dialog keeps "Cancel"', () => { |
| 74 | + for (const host of [confirmHost, paramHost]) { |
| 75 | + const result = ActionSchema.safeParse(host); |
| 76 | + |
| 77 | + expect(result.success).toBe(true); |
| 78 | + expect(result.success && 'cancelText' in result.data).toBe(false); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + it('refuses every non-I18nLabel shape the `label` key refuses', () => { |
| 83 | + for (const value of [42, null, ['a list'], { en: 7 }, { key: 'actions.keep' }] as unknown[]) { |
| 84 | + const asLabel = ActionSchema.safeParse({ ...confirmHost, label: value }).success; |
| 85 | + const asCancel = ActionSchema.safeParse({ ...confirmHost, cancelText: value }).success; |
| 86 | + |
| 87 | + expect({ value, asLabel, asCancel }).toEqual({ value, asLabel: false, asCancel: false }); |
| 88 | + } |
| 89 | + }); |
| 90 | +}); |
| 91 | + |
| 92 | +describe('ActionSchema.cancelText — the refusals', () => { |
| 93 | + it('refuses the empty string and a whitespace-only one on either host, naming the default it would replace', () => { |
| 94 | + for (const host of [confirmHost, paramHost]) { |
| 95 | + for (const blank of ['', ' ']) { |
| 96 | + const issue = firstIssue({ ...host, cancelText: blank }); |
| 97 | + |
| 98 | + expect(issue?.code).toBe('custom'); |
| 99 | + expect(issue?.path).toEqual(['cancelText']); |
| 100 | + expect(issue?.message).toMatch(/^a blank `cancelText` would replace the dialog's default dismiss button label, "Cancel"/); |
| 101 | + } |
| 102 | + } |
| 103 | + }); |
| 104 | + |
| 105 | + it('refuses it on an action that declares neither `confirmText` nor params — no dialog opens to show it', () => { |
| 106 | + for (const input of [{ ...noDialog, cancelText: 'Keep' }, { ...noDialog, params: [], cancelText: 'Keep' }]) { |
| 107 | + const issue = firstIssue(input); |
| 108 | + |
| 109 | + expect(issue?.code).toBe('custom'); |
| 110 | + expect(issue?.path).toEqual(['cancelText']); |
| 111 | + expect(issue?.message).toMatch(/^`cancelText` labels the dismiss button of the dialog an action opens before it runs/); |
| 112 | + } |
| 113 | + }); |
| 114 | + |
| 115 | + it('refuses the inline locale map on a dialog-less action too — the rule reads the key, not its form', () => { |
| 116 | + expect(firstIssue({ ...noDialog, cancelText: { en: 'Keep' } })?.path).toEqual(['cancelText']); |
| 117 | + }); |
| 118 | + |
| 119 | + it('the control: the same dialog-less action parses once the key is dropped', () => { |
| 120 | + expect(ActionSchema.safeParse(noDialog).success).toBe(true); |
| 121 | + }); |
| 122 | +}); |
| 123 | + |
| 124 | +describe('ActionSchema.cancelText — the shape around it', () => { |
| 125 | + it('renames the measured near-miss spelling onto the key', () => { |
| 126 | + const issue = firstIssue({ ...confirmHost, cancelLabel: 'Keep invitation' }); |
| 127 | + |
| 128 | + expect(issue?.code).toBe('unrecognized_keys'); |
| 129 | + expect(issue?.message).toContain('`cancelLabel` → `cancelText`'); |
| 130 | + }); |
| 131 | + |
| 132 | + it('is not picked onto the INLINE action — that surface does not pick `submitText` either', () => { |
| 133 | + const result = InlineActionSchema.safeParse({ |
| 134 | + type: 'api', |
| 135 | + target: '/api/v1/archive', |
| 136 | + label: 'Archive', |
| 137 | + confirmText: 'Archive this record?', |
| 138 | + cancelText: 'Keep it', |
| 139 | + }); |
| 140 | + |
| 141 | + expect(result.success).toBe(false); |
| 142 | + const issue = result.success ? undefined : result.error.issues[0]; |
| 143 | + expect(issue?.code).toBe('unrecognized_keys'); |
| 144 | + expect(issue?.message).toContain('cancelText'); |
| 145 | + }); |
| 146 | +}); |
| 147 | + |
| 148 | +describe('the translation slot and its overlay', () => { |
| 149 | + it('accepts the key at the object-scoped address, beside confirmText', () => { |
| 150 | + const result = ObjectTranslationDataSchema.safeParse({ |
| 151 | + _actions: { cancel_invitation: { label: '取消邀请', confirmText: '取消此邀请?', cancelText: '保留邀请' } }, |
| 152 | + }); |
| 153 | + |
| 154 | + expect(result.success).toBe(true); |
| 155 | + expect(result.success && result.data._actions?.cancel_invitation?.cancelText).toBe('保留邀请'); |
| 156 | + }); |
| 157 | + |
| 158 | + it('accepts it at the globalActions address an object-less action reads', () => { |
| 159 | + const result = TranslationDataSchema.safeParse({ |
| 160 | + globalActions: { close_session: { cancelText: '保持登录' } }, |
| 161 | + }); |
| 162 | + |
| 163 | + expect(result.success).toBe(true); |
| 164 | + expect(result.success && result.data.globalActions?.close_session?.cancelText).toBe('保持登录'); |
| 165 | + }); |
| 166 | + |
| 167 | + it('renames the same near-miss inside a bundle', () => { |
| 168 | + const result = ObjectTranslationDataSchema.safeParse({ |
| 169 | + _actions: { cancel_invitation: { cancelLabel: '保留邀请' } }, |
| 170 | + }); |
| 171 | + |
| 172 | + expect(result.success).toBe(false); |
| 173 | + const issue = result.success ? undefined : result.error.issues[0]; |
| 174 | + expect(issue?.code).toBe('unrecognized_keys'); |
| 175 | + expect(issue?.message).toContain('`cancelLabel` → `cancelText`'); |
| 176 | + }); |
| 177 | + |
| 178 | + const bundle: TranslationBundle = { |
| 179 | + 'zh-CN': { |
| 180 | + objects: { |
| 181 | + sys_invitation: { _actions: { cancel_invitation: { cancelText: '保留邀请' }, resend: { cancelText: '不发送' } } }, |
| 182 | + crm_case: { _actions: { close_case: { cancelText: '保持打开' } } }, |
| 183 | + }, |
| 184 | + globalActions: { close_session: { cancelText: '保持登录' } }, |
| 185 | + }, |
| 186 | + }; |
| 187 | + const confirmAction = { |
| 188 | + name: 'cancel_invitation', |
| 189 | + label: 'Cancel Invitation', |
| 190 | + objectName: 'sys_invitation', |
| 191 | + confirmText: 'Cancel this invitation?', |
| 192 | + cancelText: 'Keep invitation', |
| 193 | + }; |
| 194 | + |
| 195 | + it('translateAction overlays the translated label on the confirm dialog\'s host', () => { |
| 196 | + expect(translateAction(confirmAction, bundle, { locale: 'zh-CN' }).cancelText).toBe('保留邀请'); |
| 197 | + }); |
| 198 | + |
| 199 | + it('overlays it on the param dialog\'s host too', () => { |
| 200 | + const closeCase = { name: 'close_case', label: 'Close Case', objectName: 'crm_case', params: [{ name: 'resolution' }] }; |
| 201 | + expect(translateAction<ActionLike>(closeCase, bundle, { locale: 'zh-CN' }).cancelText).toBe('保持打开'); |
| 202 | + }); |
| 203 | + |
| 204 | + it('reads an object-less action at globalActions', () => { |
| 205 | + const closeSession = { name: 'close_session', label: 'Close Session', confirmText: 'Sign out everywhere?', cancelText: 'Stay signed in' }; |
| 206 | + expect(translateAction(closeSession, bundle, { locale: 'zh-CN' }).cancelText).toBe('保持登录'); |
| 207 | + }); |
| 208 | + |
| 209 | + it('falls back to the authored label when the locale has none', () => { |
| 210 | + expect(translateAction(confirmAction, bundle, { locale: 'ja-JP' }).cancelText).toBe('Keep invitation'); |
| 211 | + }); |
| 212 | + |
| 213 | + it('resolves the translated label for an action that opens a dialog but authors none', () => { |
| 214 | + const { cancelText: _authored, ...unlabelled } = confirmAction; |
| 215 | + const translated: ActionLike = translateAction<ActionLike>(unlabelled, bundle, { locale: 'zh-CN' }); |
| 216 | + expect(translated.cancelText).toBe('保留邀请'); |
| 217 | + }); |
| 218 | + |
| 219 | + it('gives no button to an action that opens no dialog, whatever the bundle carries', () => { |
| 220 | + const resend = { name: 'resend', label: 'Resend', objectName: 'sys_invitation' }; |
| 221 | + expect('cancelText' in translateAction(resend, bundle, { locale: 'zh-CN' })).toBe(false); |
| 222 | + }); |
| 223 | +}); |
0 commit comments