|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * WorkspaceSwitcher — ADR-0105 group-posture semantics. |
| 5 | + * |
| 6 | + * Under `group` tenancy posture the active organization is only the WRITE |
| 7 | + * target (reads span every organization the member belongs to), so the |
| 8 | + * dropdown must label it "Working organization" and explain the split. |
| 9 | + * Every other posture — isolated, single, absent, or an unrecognized value |
| 10 | + * from a newer server — keeps today's "Switch organization" rendering. |
| 11 | + */ |
| 12 | + |
| 13 | +import '@testing-library/jest-dom/vitest'; |
| 14 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 15 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 16 | + |
| 17 | +vi.mock('@object-ui/i18n', () => ({ |
| 18 | + useObjectTranslation: () => ({ |
| 19 | + t: (key: string, options?: Record<string, unknown>) => String(options?.defaultValue ?? key), |
| 20 | + }), |
| 21 | +})); |
| 22 | + |
| 23 | +const navigate = vi.fn(); |
| 24 | +vi.mock('react-router-dom', () => ({ |
| 25 | + useNavigate: () => navigate, |
| 26 | +})); |
| 27 | + |
| 28 | +let authState: Record<string, unknown>; |
| 29 | +vi.mock('@object-ui/auth', () => ({ useAuth: () => authState })); |
| 30 | + |
| 31 | +vi.mock('../../console/organizations/resolveHomeUrl', () => ({ resolveRootUrl: () => '/root' })); |
| 32 | + |
| 33 | +// Passthrough dropdown primitives so label/hint render without interaction. |
| 34 | +vi.mock('@object-ui/components', () => ({ |
| 35 | + DropdownMenu: (p: any) => <div>{p.children}</div>, |
| 36 | + DropdownMenuTrigger: (p: any) => <button {...p} />, |
| 37 | + DropdownMenuContent: (p: any) => <div>{p.children}</div>, |
| 38 | + DropdownMenuItem: ({ onClick, children }: any) => <div onClick={onClick}>{children}</div>, |
| 39 | + DropdownMenuLabel: (p: any) => <div {...p} />, |
| 40 | + DropdownMenuSeparator: () => <hr />, |
| 41 | +})); |
| 42 | +vi.mock('lucide-react', () => ({ |
| 43 | + ChevronsUpDown: () => <span />, |
| 44 | + Check: () => <span />, |
| 45 | + Plus: () => <span />, |
| 46 | + Users: () => <span />, |
| 47 | +})); |
| 48 | + |
| 49 | +import { WorkspaceSwitcher } from '../WorkspaceSwitcher'; |
| 50 | + |
| 51 | +const ORGS = [ |
| 52 | + { id: 'org_a', name: 'Plant A', slug: 'plant-a' }, |
| 53 | + { id: 'org_b', name: 'Plant B', slug: 'plant-b' }, |
| 54 | +]; |
| 55 | + |
| 56 | +function setAuthConfig(features: Record<string, unknown>) { |
| 57 | + authState = { |
| 58 | + organizations: ORGS, |
| 59 | + activeOrganization: ORGS[0], |
| 60 | + switchOrganization: vi.fn(), |
| 61 | + getAuthConfig: vi.fn().mockResolvedValue({ features }), |
| 62 | + }; |
| 63 | +} |
| 64 | + |
| 65 | +beforeEach(() => { |
| 66 | + vi.clearAllMocks(); |
| 67 | +}); |
| 68 | + |
| 69 | +describe('WorkspaceSwitcher (ADR-0105 tenancy posture)', () => { |
| 70 | + it('group posture: labels the active org as the working organization and explains the read/write split', async () => { |
| 71 | + setAuthConfig({ multiOrgEnabled: true, tenancyPosture: 'group' }); |
| 72 | + render(<WorkspaceSwitcher />); |
| 73 | + await waitFor(() => { |
| 74 | + expect(screen.getByText('Working organization')).toBeInTheDocument(); |
| 75 | + }); |
| 76 | + expect(screen.getByTestId('workspace-switcher-group-hint')).toHaveTextContent( |
| 77 | + 'New records are created here. Views show data from all your organizations.', |
| 78 | + ); |
| 79 | + expect(screen.queryByText('Switch organization')).not.toBeInTheDocument(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('isolated posture: keeps the plain switch label with no group hint', async () => { |
| 83 | + setAuthConfig({ multiOrgEnabled: true, tenancyPosture: 'isolated' }); |
| 84 | + render(<WorkspaceSwitcher />); |
| 85 | + // The posture fetch resolves async — assert the steady state. |
| 86 | + await waitFor(() => { |
| 87 | + expect((authState.getAuthConfig as ReturnType<typeof vi.fn>).mock.calls.length).toBeGreaterThan(0); |
| 88 | + }); |
| 89 | + expect(screen.getByText('Switch organization')).toBeInTheDocument(); |
| 90 | + expect(screen.queryByTestId('workspace-switcher-group-hint')).not.toBeInTheDocument(); |
| 91 | + }); |
| 92 | + |
| 93 | + it('absent or unrecognized posture fails toward today\'s rendering', async () => { |
| 94 | + setAuthConfig({ multiOrgEnabled: true, tenancyPosture: 'weird-future-value' }); |
| 95 | + render(<WorkspaceSwitcher />); |
| 96 | + await waitFor(() => { |
| 97 | + expect((authState.getAuthConfig as ReturnType<typeof vi.fn>).mock.calls.length).toBeGreaterThan(0); |
| 98 | + }); |
| 99 | + expect(screen.getByText('Switch organization')).toBeInTheDocument(); |
| 100 | + expect(screen.queryByTestId('workspace-switcher-group-hint')).not.toBeInTheDocument(); |
| 101 | + }); |
| 102 | +}); |
0 commit comments