|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// `invite_user`'s user-facing copy — the parameter dialog's subtitle and the |
| 4 | +// success toast — on all three declaration sites (sys_user, sys_invitation, |
| 5 | +// sys_member). |
| 6 | +// |
| 7 | +// What the console does with each key, measured on the pinned objectui: |
| 8 | +// |
| 9 | +// - `description` is the parameter dialog's subtitle. With none declared the |
| 10 | +// dialog falls back to a generic "Please provide the required information |
| 11 | +// to continue.", which says nothing about what Confirm will do. |
| 12 | +// - `successMessage` is the toast after Confirm. On a `type: 'api'` action the |
| 13 | +// runner fills `${result.*}` from the answer (ActionSchema.successMessage). |
| 14 | +// `/api/v1/auth/organization/invite-member` answers the bare invitation row, |
| 15 | +// whose top-level `email` is the invitee's address, so `${result.email}` |
| 16 | +// names the person invited. A copy without the token names nobody. |
| 17 | +// |
| 18 | +// The translated bundles are pinned as well, because the extractor cannot keep |
| 19 | +// them honest by itself: `--fill=default` seeds a NEW key (the description) |
| 20 | +// with the English source in every locale, and merge mode keeps every existing |
| 21 | +// translated value forever, so a revised `successMessage` would go on serving |
| 22 | +// the old, invitee-less sentence. Both states are "in sync" to `check:i18n`. |
| 23 | +import { describe, expect, it } from 'vitest'; |
| 24 | +import { translateMetadataDocument } from '@objectstack/spec/system'; |
| 25 | +import { SetupAppTranslations } from '../apps/translations/setup.translation.js'; |
| 26 | +import { enObjects } from '../apps/translations/en.objects.generated.js'; |
| 27 | +import { zhCNObjects } from '../apps/translations/zh-CN.objects.generated.js'; |
| 28 | +import { jaJPObjects } from '../apps/translations/ja-JP.objects.generated.js'; |
| 29 | +import { esESObjects } from '../apps/translations/es-ES.objects.generated.js'; |
| 30 | +import { SysInvitation } from './sys-invitation.object.js'; |
| 31 | +import { SysMember } from './sys-member.object.js'; |
| 32 | +import { SysUser } from './sys-user.object.js'; |
| 33 | + |
| 34 | +interface CopyAction { |
| 35 | + name?: string; |
| 36 | + description?: unknown; |
| 37 | + successMessage?: unknown; |
| 38 | +} |
| 39 | + |
| 40 | +/** The token the console runner fills from the invite-member answer. */ |
| 41 | +const INVITEE = '${result.email}'; |
| 42 | + |
| 43 | +const MIRRORS: Array<[string, unknown]> = [ |
| 44 | + ['sys_user', SysUser], |
| 45 | + ['sys_invitation', SysInvitation], |
| 46 | + ['sys_member', SysMember], |
| 47 | +]; |
| 48 | + |
| 49 | +const TRANSLATED = [ |
| 50 | + ['zh-CN', zhCNObjects], |
| 51 | + ['ja-JP', jaJPObjects], |
| 52 | + ['es-ES', esESObjects], |
| 53 | +] as const; |
| 54 | + |
| 55 | +function invite(object: unknown): CopyAction { |
| 56 | + const found = ((object as { actions?: CopyAction[] }).actions ?? []).find( |
| 57 | + (a) => a.name === 'invite_user', |
| 58 | + ); |
| 59 | + expect(found, 'invite_user is declared').toBeDefined(); |
| 60 | + return found as CopyAction; |
| 61 | +} |
| 62 | + |
| 63 | +const bundleNode = (bundle: unknown, object: string): CopyAction | undefined => |
| 64 | + (bundle as Record<string, { _actions?: Record<string, CopyAction> }>)?.[object]?._actions |
| 65 | + ?.invite_user; |
| 66 | + |
| 67 | +describe('invite_user — declared copy', () => { |
| 68 | + it.each(MIRRORS)('%s declares a description for the parameter dialog', (_name, object) => { |
| 69 | + const { description } = invite(object); |
| 70 | + expect(typeof description).toBe('string'); |
| 71 | + expect((description as string).trim()).not.toBe(''); |
| 72 | + }); |
| 73 | + |
| 74 | + it.each(MIRRORS)('%s names the invitee in its success message', (_name, object) => { |
| 75 | + expect(invite(object).successMessage).toContain(INVITEE); |
| 76 | + }); |
| 77 | + |
| 78 | + it('the three mirrors say the same thing', () => { |
| 79 | + // One action, three declaration sites: compared to EACH OTHER, so a mirror |
| 80 | + // edited alone goes red without any literal copied into this file. |
| 81 | + const [first, ...rest] = MIRRORS.map(([, object]) => invite(object)); |
| 82 | + for (const other of rest) { |
| 83 | + expect(other.description).toBe(first.description); |
| 84 | + expect(other.successMessage).toBe(first.successMessage); |
| 85 | + } |
| 86 | + }); |
| 87 | +}); |
| 88 | + |
| 89 | +describe('invite_user — translated bundles follow the source', () => { |
| 90 | + it.each(MIRRORS)('en carries the source copy for %s', (name, object) => { |
| 91 | + const node = bundleNode(enObjects, name); |
| 92 | + expect(node?.description).toBe(invite(object).description); |
| 93 | + expect(node?.successMessage).toBe(invite(object).successMessage); |
| 94 | + }); |
| 95 | + |
| 96 | + for (const [locale, bundle] of TRANSLATED) { |
| 97 | + it.each(MIRRORS)(`${locale} translates the description for %s`, (name, object) => { |
| 98 | + const translated = bundleNode(bundle, name)?.description; |
| 99 | + expect(translated, `${locale} ${name}.invite_user.description missing`).toBeTruthy(); |
| 100 | + expect( |
| 101 | + translated, |
| 102 | + `${locale} ${name}.invite_user.description is the English source the extractor seeds a new key with`, |
| 103 | + ).not.toBe(invite(object).description); |
| 104 | + }); |
| 105 | + |
| 106 | + it.each(MIRRORS)(`${locale} keeps the invitee token in the success message for %s`, (name) => { |
| 107 | + // The old translation ("invitation sent") survives every re-extract in |
| 108 | + // merge mode; only the token proves the leaf was revised with the source. |
| 109 | + expect(bundleNode(bundle, name)?.successMessage).toContain(INVITEE); |
| 110 | + }); |
| 111 | + } |
| 112 | +}); |
| 113 | + |
| 114 | +describe('invite_user — the served object metadata', () => { |
| 115 | + // The object-metadata read the console uses localizes through |
| 116 | + // `translateMetadataDocument('object', …)` over the i18n service's bundle; |
| 117 | + // `SetupAppTranslations` is the bundle this package hands that service. |
| 118 | + const served = (object: unknown, locale: string): CopyAction => { |
| 119 | + const doc = translateMetadataDocument('object', object, SetupAppTranslations, { locale }); |
| 120 | + return invite(doc); |
| 121 | + }; |
| 122 | + |
| 123 | + it.each(MIRRORS)('zh-CN serves the translated description for %s', (name, object) => { |
| 124 | + const zh = served(object, 'zh-CN'); |
| 125 | + expect(zh.description).toBe(bundleNode(zhCNObjects, name)?.description); |
| 126 | + expect(zh.description).not.toBe(invite(object).description); |
| 127 | + expect(zh.successMessage).toContain(INVITEE); |
| 128 | + }); |
| 129 | + |
| 130 | + it.each(MIRRORS)('en serves the declared copy for %s', (_name, object) => { |
| 131 | + const en = served(object, 'en'); |
| 132 | + expect(en.description).toBe(invite(object).description); |
| 133 | + expect(en.successMessage).toBe(invite(object).successMessage); |
| 134 | + }); |
| 135 | +}); |
0 commit comments