|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// #27521 — the metadata half of the Invitations screens (rows 7, 8 and 9 of |
| 4 | +// the 18.3.0 RC walkthrough): |
| 5 | +// |
| 6 | +// - row 7: `sys_invitation.status` options carry display labels, read from a |
| 7 | +// map keyed by the spec enum, instead of the raw lowercase values; |
| 8 | +// - row 8: every picker that grants a membership role explains each role in |
| 9 | +// one shared help text, and Change Role names the member it changes; |
| 10 | +// - row 9: Resend Invitation is a confirmation, not an editable form: its |
| 11 | +// email and role are carried over from the row, and every list of |
| 12 | +// invitations projects both; |
| 13 | +// - the translated bundles carry a translation of every new leaf, never the |
| 14 | +// English source `--fill=default` seeds a new key with. |
| 15 | + |
| 16 | +import { describe, expect, it } from 'vitest'; |
| 17 | +import { BUILTIN_MEMBERSHIP_ROLE_OPTIONS, InvitationStatus } from '@objectstack/spec/identity'; |
| 18 | +import { enObjects } from '../apps/translations/en.objects.generated.js'; |
| 19 | +import { zhCNObjects } from '../apps/translations/zh-CN.objects.generated.js'; |
| 20 | +import { jaJPObjects } from '../apps/translations/ja-JP.objects.generated.js'; |
| 21 | +import { esESObjects } from '../apps/translations/es-ES.objects.generated.js'; |
| 22 | +import { MEMBERSHIP_ROLE_PARAM_HELP } from './membership-role-help.js'; |
| 23 | +import { SysInvitation } from './sys-invitation.object.js'; |
| 24 | +import { SysMember } from './sys-member.object.js'; |
| 25 | +import { SysOrganization } from './sys-organization.object.js'; |
| 26 | +import { SysUser } from './sys-user.object.js'; |
| 27 | + |
| 28 | +type Action = Record<string, any>; |
| 29 | +type Param = Record<string, any>; |
| 30 | + |
| 31 | +function action(object: unknown, name: string): Action { |
| 32 | + const found = ((object as { actions?: Action[] }).actions ?? []).find((a) => a.name === name); |
| 33 | + if (!found) throw new Error(`no action ${name}`); |
| 34 | + return found; |
| 35 | +} |
| 36 | + |
| 37 | +function param(declared: Action, name: string): Param { |
| 38 | + const found = ((declared.params ?? []) as Param[]).find((p) => (p.field ?? p.name) === name); |
| 39 | + if (!found) throw new Error(`no param ${name} on ${declared.name}`); |
| 40 | + return found; |
| 41 | +} |
| 42 | + |
| 43 | +/** Every role picker that grants a membership role, as `[object, action, declaration]`. */ |
| 44 | +const ROLE_PICKERS: Array<[string, string, unknown]> = [ |
| 45 | + ['sys_user', 'invite_user', SysUser], |
| 46 | + ['sys_member', 'invite_user', SysMember], |
| 47 | + ['sys_invitation', 'invite_user', SysInvitation], |
| 48 | + ['sys_organization', 'invite_user', SysOrganization], |
| 49 | + ['sys_member', 'add_member', SysMember], |
| 50 | + ['sys_member', 'update_member_role', SysMember], |
| 51 | +]; |
| 52 | + |
| 53 | +const TRANSLATED = [ |
| 54 | + ['zh-CN', zhCNObjects], |
| 55 | + ['ja-JP', jaJPObjects], |
| 56 | + ['es-ES', esESObjects], |
| 57 | +] as const; |
| 58 | + |
| 59 | +const bundleObject = (bundle: unknown, object: string): Record<string, any> | undefined => |
| 60 | + (bundle as Record<string, Record<string, any>>)?.[object]; |
| 61 | + |
| 62 | +describe('row 7 — invitation status options are labelled', () => { |
| 63 | + const options = (SysInvitation as any).fields.status.options as Array<{ value: string; label: string }>; |
| 64 | + |
| 65 | + it('labels every value of the spec enum, in its order', () => { |
| 66 | + expect(options.map((o) => o.value)).toEqual([...InvitationStatus.options]); |
| 67 | + for (const option of options) { |
| 68 | + expect(option.label, option.value).not.toBe(option.value); |
| 69 | + expect(option.label[0], option.value).toBe(option.label[0].toUpperCase()); |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + it('reads `rejected` as "Declined", the invitee\'s own action', () => { |
| 74 | + expect(options.find((o) => o.value === 'rejected')?.label).toBe('Declined'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('serves the labels in the English bundle', () => { |
| 78 | + const en = bundleObject(enObjects, 'sys_invitation')?.fields?.status?.options; |
| 79 | + expect(en).toEqual(Object.fromEntries(options.map((o) => [o.value, o.label]))); |
| 80 | + }); |
| 81 | +}); |
| 82 | + |
| 83 | +describe('row 8 — one role help text, a line per role', () => { |
| 84 | + it('has one line per membership role, labelled and ordered as the options are', () => { |
| 85 | + const lines = MEMBERSHIP_ROLE_PARAM_HELP.split('\n'); |
| 86 | + expect(lines.map((line) => line.split(': ')[0])).toEqual(BUILTIN_MEMBERSHIP_ROLE_OPTIONS.map((o) => o.label)); |
| 87 | + for (const line of lines) expect(line.split(': ')[1]?.length, line).toBeGreaterThan(10); |
| 88 | + }); |
| 89 | + |
| 90 | + it.each(ROLE_PICKERS)('%s.%s declares it on its role param', (_object, name, declaration) => { |
| 91 | + expect(param(action(declaration, name), 'role').helpText).toBe(MEMBERSHIP_ROLE_PARAM_HELP); |
| 92 | + }); |
| 93 | + |
| 94 | + it('Change Role names the member it changes, with a fallback for a row without the user', () => { |
| 95 | + const changeRole = action(SysMember, 'update_member_role'); |
| 96 | + expect(changeRole.description).toContain("${record.user_id?.name ?? 'this member'}"); |
| 97 | + expect(changeRole.confirmText).toBeUndefined(); |
| 98 | + }); |
| 99 | +}); |
| 100 | + |
| 101 | +describe('row 9 — Resend Invitation is a confirmation', () => { |
| 102 | + const resend = action(SysInvitation, 'resend_invitation'); |
| 103 | + |
| 104 | + it('asks its question in `description`, naming the invitee', () => { |
| 105 | + expect(resend.description).toContain('${record.email}'); |
| 106 | + // The spec refuses `confirmText` beside `params` on a registered action. |
| 107 | + expect(resend.confirmText).toBeUndefined(); |
| 108 | + }); |
| 109 | + |
| 110 | + it.each(['email', 'role'])('carries %s over from the row, unchanged', (name) => { |
| 111 | + expect(param(resend, name)).toMatchObject({ required: true, defaultFromRow: true, carryOver: true }); |
| 112 | + }); |
| 113 | + |
| 114 | + it.each(Object.keys((SysInvitation as any).listViews))( |
| 115 | + 'the %s list projects both carried-over columns, so its rows can resend', |
| 116 | + (view) => { |
| 117 | + const columns = ((SysInvitation as any).listViews[view].columns as Array<string | { field: string }>) |
| 118 | + .map((c) => (typeof c === 'string' ? c : c.field)); |
| 119 | + expect(columns).toEqual(expect.arrayContaining(['email', 'role'])); |
| 120 | + }, |
| 121 | + ); |
| 122 | +}); |
| 123 | + |
| 124 | +describe('the new copy in the translated bundles', () => { |
| 125 | + const leaves = (bundle: unknown): Array<[string, unknown]> => [ |
| 126 | + ...ROLE_PICKERS.map(([object, name]): [string, unknown] => [ |
| 127 | + `${object}.${name}.params.role.helpText`, |
| 128 | + bundleObject(bundle, object)?._actions?.[name]?.params?.role?.helpText, |
| 129 | + ]), |
| 130 | + ['sys_invitation.resend_invitation.description', bundleObject(bundle, 'sys_invitation')?._actions?.resend_invitation?.description], |
| 131 | + ['sys_member.update_member_role.description', bundleObject(bundle, 'sys_member')?._actions?.update_member_role?.description], |
| 132 | + ]; |
| 133 | + |
| 134 | + const english = new Map(leaves(enObjects)); |
| 135 | + |
| 136 | + it.each(TRANSLATED)('%s translates every new leaf', (_locale, bundle) => { |
| 137 | + for (const [path, value] of leaves(bundle)) { |
| 138 | + expect(typeof value, path).toBe('string'); |
| 139 | + expect(value, path).not.toBe(english.get(path)); |
| 140 | + } |
| 141 | + }); |
| 142 | + |
| 143 | + it.each(TRANSLATED)('%s keeps the holes the console fills', (_locale, bundle) => { |
| 144 | + const byPath = new Map(leaves(bundle)); |
| 145 | + expect(byPath.get('sys_invitation.resend_invitation.description')).toContain('${record.email}'); |
| 146 | + expect(byPath.get('sys_member.update_member_role.description')).toMatch(/\$\{record\.user_id\?\.name \?\? '[^']+'\}/); |
| 147 | + }); |
| 148 | + |
| 149 | + it.each(TRANSLATED)('%s keeps one help line per role', (_locale, bundle) => { |
| 150 | + const help = bundleObject(bundle, 'sys_member')?._actions?.add_member?.params?.role?.helpText as string; |
| 151 | + expect(help.split('\n')).toHaveLength(BUILTIN_MEMBERSHIP_ROLE_OPTIONS.length); |
| 152 | + }); |
| 153 | +}); |
0 commit comments