|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { act } from 'react' |
| 5 | +import { createRoot, type Root } from 'react-dom/client' |
| 6 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | + |
| 8 | +const { mockUseWorkspaceHostContextQuery } = vi.hoisted(() => ({ |
| 9 | + mockUseWorkspaceHostContextQuery: vi.fn(), |
| 10 | +})) |
| 11 | + |
| 12 | +vi.mock('@/hooks/queries/workspace-host', () => ({ |
| 13 | + useWorkspaceHostContextQuery: mockUseWorkspaceHostContextQuery, |
| 14 | +})) |
| 15 | + |
| 16 | +vi.mock('@/app/workspace/[workspaceId]/components/workspace-access-denied', () => ({ |
| 17 | + WorkspaceAccessDenied: () => <output data-testid='denied' />, |
| 18 | +})) |
| 19 | + |
| 20 | +import type { WorkspaceHostContext } from '@/lib/api/contracts/workspaces' |
| 21 | +import { |
| 22 | + getDeploymentShape, |
| 23 | + resetDeploymentShape, |
| 24 | + resolveDeploymentShape, |
| 25 | +} from '@/lib/core/config/deployment-shape' |
| 26 | +import { |
| 27 | + useWorkspaceHostContext, |
| 28 | + WorkspaceHostProvider, |
| 29 | +} from '@/app/workspace/[workspaceId]/providers/workspace-host-provider' |
| 30 | + |
| 31 | +;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true |
| 32 | + |
| 33 | +const HOST_CONTEXT: WorkspaceHostContext = { |
| 34 | + workspace: { |
| 35 | + id: 'workspace-1', |
| 36 | + name: 'Workspace', |
| 37 | + workspaceMode: 'organization', |
| 38 | + billedAccountUserId: 'owner-1', |
| 39 | + }, |
| 40 | + hostOrganizationId: 'org-1', |
| 41 | + ownerBilling: { |
| 42 | + plan: 'team', |
| 43 | + status: 'active', |
| 44 | + isPaid: true, |
| 45 | + isPro: false, |
| 46 | + isTeam: true, |
| 47 | + isEnterprise: false, |
| 48 | + isOrgScoped: true, |
| 49 | + organizationId: 'org-1', |
| 50 | + billingInterval: 'month', |
| 51 | + billingBlocked: false, |
| 52 | + billingBlockedReason: null, |
| 53 | + }, |
| 54 | + viewer: { |
| 55 | + permission: 'admin', |
| 56 | + isHostOrganizationMember: true, |
| 57 | + isHostOrganizationAdmin: true, |
| 58 | + }, |
| 59 | + deployment: { |
| 60 | + ...resolveDeploymentShape(), |
| 61 | + hosted: true, |
| 62 | + billingEnabled: true, |
| 63 | + }, |
| 64 | +} |
| 65 | + |
| 66 | +/** Reads the getter during render, the way block conditions do. */ |
| 67 | +function GetterReader() { |
| 68 | + return <output data-testid='getter'>{String(getDeploymentShape().hosted)}</output> |
| 69 | +} |
| 70 | + |
| 71 | +function ContextReader() { |
| 72 | + const { deployment } = useWorkspaceHostContext() |
| 73 | + return <output data-testid='context'>{String(deployment?.billingEnabled)}</output> |
| 74 | +} |
| 75 | + |
| 76 | +let host: HTMLDivElement |
| 77 | +let root: Root |
| 78 | + |
| 79 | +function renderProvider(initialContext: WorkspaceHostContext) { |
| 80 | + act(() => |
| 81 | + root.render( |
| 82 | + <WorkspaceHostProvider workspaceId='workspace-1' initialContext={initialContext}> |
| 83 | + <GetterReader /> |
| 84 | + <ContextReader /> |
| 85 | + </WorkspaceHostProvider> |
| 86 | + ) |
| 87 | + ) |
| 88 | +} |
| 89 | + |
| 90 | +function textOf(testId: string): string | undefined { |
| 91 | + return host.querySelector(`[data-testid="${testId}"]`)?.textContent ?? undefined |
| 92 | +} |
| 93 | + |
| 94 | +beforeEach(() => { |
| 95 | + resetDeploymentShape() |
| 96 | + mockUseWorkspaceHostContextQuery.mockReturnValue({ data: undefined, error: null }) |
| 97 | + host = document.createElement('div') |
| 98 | + document.body.appendChild(host) |
| 99 | + root = createRoot(host) |
| 100 | +}) |
| 101 | + |
| 102 | +afterEach(() => { |
| 103 | + act(() => root.unmount()) |
| 104 | + host.remove() |
| 105 | + vi.clearAllMocks() |
| 106 | +}) |
| 107 | + |
| 108 | +describe('WorkspaceHostProvider', () => { |
| 109 | + it('seeds the server deployment shape before workspace children render', () => { |
| 110 | + renderProvider(HOST_CONTEXT) |
| 111 | + |
| 112 | + expect(textOf('getter')).toBe('true') |
| 113 | + expect(textOf('context')).toBe('true') |
| 114 | + expect(getDeploymentShape()).toBe(HOST_CONTEXT.deployment) |
| 115 | + }) |
| 116 | + |
| 117 | + it('follows the refetched host context over the initial one', () => { |
| 118 | + mockUseWorkspaceHostContextQuery.mockReturnValue({ |
| 119 | + data: { |
| 120 | + ...HOST_CONTEXT, |
| 121 | + deployment: { ...HOST_CONTEXT.deployment!, billingEnabled: false }, |
| 122 | + }, |
| 123 | + error: null, |
| 124 | + }) |
| 125 | + |
| 126 | + renderProvider(HOST_CONTEXT) |
| 127 | + |
| 128 | + expect(textOf('context')).toBe('false') |
| 129 | + expect(getDeploymentShape().billingEnabled).toBe(false) |
| 130 | + }) |
| 131 | + |
| 132 | + it('keeps the env fallback for a host context that predates deployment projection', () => { |
| 133 | + const { deployment: _legacy, ...legacyContext } = HOST_CONTEXT |
| 134 | + |
| 135 | + renderProvider(legacyContext) |
| 136 | + |
| 137 | + expect(textOf('getter')).toBe('false') |
| 138 | + expect(textOf('context')).toBe('undefined') |
| 139 | + }) |
| 140 | +}) |
0 commit comments