|
| 1 | +// Copyright (c) 2026 ObjectStack contributors. Apache-2.0 license. |
| 2 | +// |
| 3 | +// The extractor half of `action.submitText`, the verb on the submit button of |
| 4 | +// the dialog an action opens to collect its `params`. |
| 5 | +// |
| 6 | +// `translateAction` overlays `objects.OBJECT._actions.ACTION.submitText` |
| 7 | +// (`globalActions.ACTION.submitText` for an object-less action) only onto an |
| 8 | +// action with a non-empty `params`, because no other action opens that dialog. |
| 9 | +// `os i18n extract` scaffolds the slot on exactly that population: beside a |
| 10 | +// non-empty `params` it is an optional slot like `description`, and anywhere |
| 11 | +// else there is no slot at all. The last test walks ONE fixture action through |
| 12 | +// all three surfaces — `ActionSchema` accepts it, the extractor emits its key, |
| 13 | +// the translation schema accepts the bundle written at that key — because each |
| 14 | +// half passing alone does not prove the addresses agree. |
| 15 | + |
| 16 | +import { describe, it, expect } from 'vitest'; |
| 17 | +import { collectExpectedEntries, extractTranslations } from '../src/utils/i18n-extract'; |
| 18 | +import { computeI18nCoverage } from '../src/utils/i18n-coverage'; |
| 19 | +import { ActionSchema } from '@objectstack/spec/ui'; |
| 20 | +import { ObjectTranslationDataSchema } from '@objectstack/spec/system'; |
| 21 | + |
| 22 | +const paths = (config: any) => collectExpectedEntries(config).map((e) => e.path.join('.')); |
| 23 | + |
| 24 | +const PARAMS = [{ name: 'subject', label: 'Subject', type: 'text' }]; |
| 25 | + |
| 26 | +describe('the extractor emits action-level `submitText` beside a non-empty `params`', () => { |
| 27 | + it('emits it for a TOP-LEVEL object action that declares one', () => { |
| 28 | + const keys = paths({ |
| 29 | + actions: [{ name: 'send_email', objectName: 'crm_contact', label: 'Send Email', params: PARAMS, submitText: 'Send' }], |
| 30 | + }); |
| 31 | + |
| 32 | + expect(keys).toContain('objects.crm_contact._actions.send_email.submitText'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('emits it for a GLOBAL action that declares one', () => { |
| 36 | + const keys = paths({ |
| 37 | + actions: [{ name: 'log_call', label: 'Log Call', params: PARAMS, submitText: 'Log Call' }], |
| 38 | + }); |
| 39 | + |
| 40 | + expect(keys).toContain('globalActions.log_call.submitText'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('emits it for an action declared INLINE on the object', () => { |
| 44 | + const keys = paths({ |
| 45 | + objects: [ |
| 46 | + { |
| 47 | + name: 'crm_contact', |
| 48 | + label: 'Contact', |
| 49 | + actions: [{ name: 'send_email', label: 'Send Email', params: PARAMS, submitText: 'Send' }], |
| 50 | + }, |
| 51 | + ], |
| 52 | + }); |
| 53 | + |
| 54 | + expect(keys).toContain('objects.crm_contact._actions.send_email.submitText'); |
| 55 | + }); |
| 56 | + |
| 57 | + it('records a seed-less slot for an action with params that names no verb — the dialog keeps "Confirm"', () => { |
| 58 | + const config = { |
| 59 | + objects: [{ name: 'crm_contact', label: 'Contact', actions: [{ name: 'send_email', label: 'Send Email', params: PARAMS }] }], |
| 60 | + }; |
| 61 | + const key = 'objects.crm_contact._actions.send_email.submitText'; |
| 62 | + const entry = collectExpectedEntries(config).find((e) => e.path.join('.') === key); |
| 63 | + |
| 64 | + expect(entry).toBeDefined(); |
| 65 | + expect(entry?.inline).toBeUndefined(); |
| 66 | + expect(entry?.sourceValue).toBeUndefined(); |
| 67 | + |
| 68 | + const { bundles } = extractTranslations(config, { defaultLocale: 'en', fill: 'default' }); |
| 69 | + expect(bundles.en.objects?.crm_contact?._actions?.send_email).not.toHaveProperty('submitText'); |
| 70 | + expect(computeI18nCoverage({ ...config, translations: [{ 'zh-CN': {} }] }).issues.map((i) => i.key)) |
| 71 | + .not.toContain(key); |
| 72 | + }); |
| 73 | + |
| 74 | + it('emits NO slot for an action without params — no dialog opens, so there is no button to name', () => { |
| 75 | + for (const params of [undefined, []]) { |
| 76 | + const keys = paths({ |
| 77 | + objects: [ |
| 78 | + { |
| 79 | + name: 'crm_contact', |
| 80 | + label: 'Contact', |
| 81 | + actions: [{ name: 'archive', label: 'Archive', ...(params ? { params } : {}) }], |
| 82 | + }, |
| 83 | + ], |
| 84 | + actions: [{ name: 'refresh_all', label: 'Refresh', ...(params ? { params } : {}) }], |
| 85 | + }); |
| 86 | + |
| 87 | + expect(keys).toContain('objects.crm_contact._actions.archive.label'); |
| 88 | + expect(keys).not.toContain('objects.crm_contact._actions.archive.submitText'); |
| 89 | + expect(keys).toContain('globalActions.refresh_all.label'); |
| 90 | + expect(keys).not.toContain('globalActions.refresh_all.submitText'); |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + it('carries the declared verb into the bundle, and gates it until it is translated', () => { |
| 95 | + const config: any = { |
| 96 | + objects: [ |
| 97 | + { |
| 98 | + name: 'crm_contact', |
| 99 | + label: 'Contact', |
| 100 | + actions: [{ name: 'send_email', label: 'Send Email', params: PARAMS, submitText: 'Send' }], |
| 101 | + }, |
| 102 | + ], |
| 103 | + translations: [ |
| 104 | + { 'zh-CN': { objects: { crm_contact: { label: '联系人', _actions: { send_email: { label: '发送邮件', params: { subject: { label: '主题' } } } } } } } }, |
| 105 | + ], |
| 106 | + }; |
| 107 | + const key = 'objects.crm_contact._actions.send_email.submitText'; |
| 108 | + |
| 109 | + const entry = collectExpectedEntries(config).find((e) => e.path.join('.') === key); |
| 110 | + expect(entry?.source).toBe('action'); |
| 111 | + expect(entry?.inline).toBe('Send'); |
| 112 | + |
| 113 | + const { bundles } = extractTranslations(config, { defaultLocale: 'en', fill: 'default' }); |
| 114 | + expect(bundles.en.objects?.crm_contact?._actions?.send_email.submitText).toBe('Send'); |
| 115 | + |
| 116 | + expect(computeI18nCoverage(config).issues.map((i) => i.key)).toContain(key); |
| 117 | + config.translations[0]['zh-CN'].objects.crm_contact._actions.send_email.submitText = '发送'; |
| 118 | + expect(computeI18nCoverage(config).issues.map((i) => i.key)).not.toContain(key); |
| 119 | + }); |
| 120 | +}); |
| 121 | + |
| 122 | +describe('one fixture action, all three surfaces (submitText end-to-end)', () => { |
| 123 | + const authored = { |
| 124 | + name: 'send_email', |
| 125 | + objectName: 'crm_contact', |
| 126 | + label: 'Send Email', |
| 127 | + type: 'script', |
| 128 | + target: 'sendEmail', |
| 129 | + params: PARAMS, |
| 130 | + submitText: 'Send', |
| 131 | + }; |
| 132 | + |
| 133 | + it('parses as metadata, extracts to a key, and that key round-trips a bundle', () => { |
| 134 | + const parsed = ActionSchema.safeParse(authored); |
| 135 | + expect(parsed.success).toBe(true); |
| 136 | + expect(parsed.success && parsed.data.submitText).toBe('Send'); |
| 137 | + |
| 138 | + const { bundles } = extractTranslations({ actions: [authored] }, { |
| 139 | + defaultLocale: 'en', |
| 140 | + locales: ['zh-CN'], |
| 141 | + fill: 'empty', |
| 142 | + }); |
| 143 | + const bundle: any = bundles['zh-CN']; |
| 144 | + expect(bundle.objects.crm_contact._actions.send_email).toHaveProperty('submitText'); |
| 145 | + |
| 146 | + bundle.objects.crm_contact._actions.send_email.submitText = '发送'; |
| 147 | + const translated = ObjectTranslationDataSchema.safeParse(bundle.objects.crm_contact); |
| 148 | + expect(translated.success).toBe(true); |
| 149 | + expect(translated.success && translated.data._actions?.send_email?.submitText).toBe('发送'); |
| 150 | + }); |
| 151 | +}); |
0 commit comments