|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * objectui#10918 regression pin — the console action runtime supplies the |
| 11 | + * spec-declared `${ctx.org.*}` interpolation scope. |
| 12 | + * |
| 13 | + * `ActionSchema.target` in `@objectstack/spec` (`ui/action.zod.ts`) declares |
| 14 | + * `${ctx.org.id}` as a member of the action context, and the `onSuccess` |
| 15 | + * contract lists the same scope as "`ctx.user.*`, `ctx.org.*`, `ctx.recordId`, |
| 16 | + * `ctx.selection`". The runtime used to publish the active organization only |
| 17 | + * under the undeclared key `activeOrganization`, so `ActionRunner`'s |
| 18 | + * interpolation scope carried an empty `org` and a declared target such as |
| 19 | + * `/apps/cloud_control/sys_organization/record/${ctx.org.id}` navigated to a |
| 20 | + * path with the id blanked out. |
| 21 | + * |
| 22 | + * Driven end-to-end: the REAL `ConsoleActionRuntimeProvider` (and so the real |
| 23 | + * `<ActionProvider>` and `ActionRunner` target interpolation) runs a |
| 24 | + * `type: 'url'` action from a `useAction()` consumer, and the navigation the |
| 25 | + * runtime hands to the router is what is asserted. |
| 26 | + */ |
| 27 | + |
| 28 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 29 | +import { render, screen, fireEvent, waitFor } from '@testing-library/react'; |
| 30 | +import React from 'react'; |
| 31 | + |
| 32 | +const navigateSpy = vi.fn(); |
| 33 | +vi.mock('react-router-dom', () => ({ useNavigate: () => navigateSpy })); |
| 34 | + |
| 35 | +// `activeOrganization` is what `useAuth()` answers — the auth layer's own |
| 36 | +// `AuthOrganization` shape. Each test sets it before rendering. |
| 37 | +let activeOrganization: Record<string, unknown> | null = null; |
| 38 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 39 | + ...(await importOriginal<Record<string, unknown>>()), |
| 40 | + useAuth: () => ({ user: { id: 'u1', name: 'User', image: null }, activeOrganization }), |
| 41 | + createAuthenticatedFetch: () => vi.fn(), |
| 42 | +})); |
| 43 | + |
| 44 | +vi.mock('@object-ui/i18n', async (importOriginal) => ({ |
| 45 | + ...(await importOriginal<Record<string, unknown>>()), |
| 46 | + useObjectLabel: () => ({ |
| 47 | + fieldLabel: (_o: any, _n: any, l: any) => l, |
| 48 | + fieldOptionLabel: (_o: any, _f: any, _v: any, l: any) => l, |
| 49 | + actionParamText: (_o: any, _a: any, _p: any, _attr: any, fallback: any) => fallback, |
| 50 | + actionParamOptionLabel: (_o: any, _a: any, _p: any, _v: any, fallback: any) => fallback, |
| 51 | + actionDescription: (_o: any, _a: any, fallback: any) => fallback, |
| 52 | + }), |
| 53 | + useObjectTranslation: () => ({ |
| 54 | + t: (key: string, options?: any) => String(options?.defaultValue ?? key), |
| 55 | + }), |
| 56 | +})); |
| 57 | + |
| 58 | +// The client modal transport and the dialogs are not exercised here — inert |
| 59 | +// stubs keep the hook module's import graph cheap. |
| 60 | +vi.mock('../useActionModal', () => ({ |
| 61 | + useActionModal: () => ({ |
| 62 | + modalHandler: vi.fn(async () => ({ success: true })), |
| 63 | + modalElement: null, |
| 64 | + closeModal: () => {}, |
| 65 | + resolveModalTarget: vi.fn(async () => null), |
| 66 | + }), |
| 67 | +})); |
| 68 | +vi.mock('../../views/ActionConfirmDialog', () => ({ ActionConfirmDialog: () => null })); |
| 69 | +vi.mock('../../views/ActionParamDialog', () => ({ ActionParamDialog: () => null })); |
| 70 | +vi.mock('../../views/ActionResultDialog', () => ({ ActionResultDialog: () => null })); |
| 71 | +vi.mock('../../views/FlowRunner', () => ({ FlowRunner: () => null })); |
| 72 | + |
| 73 | +import { ConsoleActionRuntimeProvider } from '../useConsoleActionRuntime'; |
| 74 | +import { useAction } from '@object-ui/react'; |
| 75 | + |
| 76 | +// The shape cloud's welcome-page "invite members" CTA declares (objectui#10918). |
| 77 | +const ORG_RECORD_TARGET = '/apps/cloud_control/sys_organization/record/${ctx.org.id}'; |
| 78 | + |
| 79 | +function Probe() { |
| 80 | + const { execute } = useAction(); |
| 81 | + return ( |
| 82 | + <button onClick={() => execute({ type: 'url', name: 'open_org', target: ORG_RECORD_TARGET } as any)}> |
| 83 | + run |
| 84 | + </button> |
| 85 | + ); |
| 86 | +} |
| 87 | + |
| 88 | +function runOrgTargetAction() { |
| 89 | + render( |
| 90 | + <ConsoleActionRuntimeProvider dataSource={{}} objects={[]}> |
| 91 | + <Probe /> |
| 92 | + </ConsoleActionRuntimeProvider>, |
| 93 | + ); |
| 94 | + fireEvent.click(screen.getByText('run')); |
| 95 | +} |
| 96 | + |
| 97 | +beforeEach(() => { |
| 98 | + navigateSpy.mockReset(); |
| 99 | + activeOrganization = null; |
| 100 | +}); |
| 101 | + |
| 102 | +describe('useConsoleActionRuntime — the spec-declared ctx.org scope (objectui#10918)', () => { |
| 103 | + it('interpolates ${ctx.org.id} in an action target to the active organization id', async () => { |
| 104 | + activeOrganization = { id: 'org_acme', slug: 'acme', name: 'Acme Inc.' }; |
| 105 | + |
| 106 | + runOrgTargetAction(); |
| 107 | + |
| 108 | + await waitFor(() => expect(navigateSpy).toHaveBeenCalledTimes(1)); |
| 109 | + expect(navigateSpy.mock.calls[0][0]).toBe('/apps/cloud_control/sys_organization/record/org_acme'); |
| 110 | + }); |
| 111 | + |
| 112 | + it('interpolates ${ctx.org.id} to an empty segment, never a literal null, when no organization is active', async () => { |
| 113 | + runOrgTargetAction(); |
| 114 | + |
| 115 | + await waitFor(() => expect(navigateSpy).toHaveBeenCalledTimes(1)); |
| 116 | + expect(navigateSpy.mock.calls[0][0]).toBe('/apps/cloud_control/sys_organization/record/'); |
| 117 | + }); |
| 118 | +}); |
0 commit comments