|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * CloudPlanStatus (`cloud:plan-status`, objectui#10919) marks ONE plan card on |
| 5 | + * the Cloud pricing page: a "Current plan" badge when the organization's plan, |
| 6 | + * read from the authoritative entitlements summary, equals the card's |
| 7 | + * `properties.plan` — and nothing in every other state, so a plan is never |
| 8 | + * guessed. |
| 9 | + */ |
| 10 | + |
| 11 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 12 | +import { render, screen, waitFor, cleanup } from '@testing-library/react'; |
| 13 | +import React from 'react'; |
| 14 | + |
| 15 | +let fetchImpl: (url: string, init?: RequestInit) => Promise<unknown>; |
| 16 | +const fetchedUrls: string[] = []; |
| 17 | + |
| 18 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 19 | + ...(await importOriginal<Record<string, unknown>>()), |
| 20 | + useAuth: () => ({ activeOrganization: { id: 'org_1' } }), |
| 21 | + createAuthenticatedFetch: () => (url: string, init?: RequestInit) => { |
| 22 | + fetchedUrls.push(url); |
| 23 | + return fetchImpl(url, init); |
| 24 | + }, |
| 25 | +})); |
| 26 | + |
| 27 | +import { I18nProvider } from '@object-ui/i18n'; |
| 28 | +import { ComponentRegistry } from '@object-ui/core'; |
| 29 | +import { AdapterCtx, SchemaRenderer } from '@object-ui/react'; |
| 30 | +import { CloudPlanStatus } from '../CloudPlanStatus'; |
| 31 | + |
| 32 | +/** |
| 33 | + * The badge text resolves from the locale packs, so a real i18n context is |
| 34 | + * needed — without one `t()` returns the raw key and a text assertion would |
| 35 | + * pass against nothing. The adapter is the row-derived fallback's data source; |
| 36 | + * its rows name a production env, so the fallback resolves `derived` for real |
| 37 | + * rather than failing. |
| 38 | + */ |
| 39 | +const adapter = { find: vi.fn().mockResolvedValue([{ id: 'env_1', environment_type: 'production' }]) }; |
| 40 | + |
| 41 | +const renderInShell = (ui: React.ReactElement) => |
| 42 | + render( |
| 43 | + <I18nProvider config={{ defaultLanguage: 'en', detectBrowserLanguage: false }}> |
| 44 | + <AdapterCtx.Provider value={adapter as never}>{ui}</AdapterCtx.Provider> |
| 45 | + </I18nProvider>, |
| 46 | + ); |
| 47 | + |
| 48 | +/** The control plane's `{ success, data }` envelope around a summary naming `plan`. */ |
| 49 | +function summary(plan: string) { |
| 50 | + return { |
| 51 | + ok: true, |
| 52 | + status: 200, |
| 53 | + json: async () => ({ success: true, data: { plan, hasProductionEnv: true } }), |
| 54 | + }; |
| 55 | +} |
| 56 | + |
| 57 | +afterEach(() => { |
| 58 | + cleanup(); |
| 59 | + fetchedUrls.length = 0; |
| 60 | + adapter.find.mockClear(); |
| 61 | +}); |
| 62 | + |
| 63 | +describe('CloudPlanStatus (objectui#10919)', () => { |
| 64 | + it('marks the card whose plan is the organization\'s plan', async () => { |
| 65 | + fetchImpl = async () => summary('free'); |
| 66 | + const { container } = renderInShell(<CloudPlanStatus properties={{ plan: 'free' }} />); |
| 67 | + |
| 68 | + expect(await screen.findByText('Current plan')).toBeTruthy(); |
| 69 | + expect(container.querySelector('[data-plan-status="current"]')).toBeTruthy(); |
| 70 | + }); |
| 71 | + |
| 72 | + it('reads the org-scoped summary endpoint', async () => { |
| 73 | + fetchImpl = async () => summary('free'); |
| 74 | + renderInShell(<CloudPlanStatus properties={{ plan: 'free' }} />); |
| 75 | + |
| 76 | + await screen.findByText('Current plan'); |
| 77 | + expect(fetchedUrls).toEqual(['/api/v1/cloud/environment-entitlements?organizationId=org_1']); |
| 78 | + }); |
| 79 | + |
| 80 | + it('renders nothing on a card whose plan is not the organization\'s', async () => { |
| 81 | + fetchImpl = async () => summary('team'); |
| 82 | + const { container } = renderInShell(<CloudPlanStatus properties={{ plan: 'free' }} />); |
| 83 | + |
| 84 | + // Wait until the summary was actually read, so the empty render is a |
| 85 | + // verdict on it and not the loading state. |
| 86 | + await waitFor(() => expect(fetchedUrls).toHaveLength(1)); |
| 87 | + await new Promise((r) => setTimeout(r, 0)); |
| 88 | + expect(container.innerHTML).toBe(''); |
| 89 | + }); |
| 90 | + |
| 91 | + it('compares the plan code verbatim — no case folding', async () => { |
| 92 | + fetchImpl = async () => summary('free'); |
| 93 | + const { container } = renderInShell(<CloudPlanStatus properties={{ plan: 'Free' }} />); |
| 94 | + |
| 95 | + await waitFor(() => expect(fetchedUrls).toHaveLength(1)); |
| 96 | + await new Promise((r) => setTimeout(r, 0)); |
| 97 | + expect(container.innerHTML).toBe(''); |
| 98 | + }); |
| 99 | + |
| 100 | + it('renders nothing while the summary is loading', () => { |
| 101 | + fetchImpl = () => new Promise(() => {}); |
| 102 | + const { container } = renderInShell(<CloudPlanStatus properties={{ plan: 'free' }} />); |
| 103 | + |
| 104 | + expect(container.innerHTML).toBe(''); |
| 105 | + }); |
| 106 | + |
| 107 | + it('renders nothing when the summary fails — the row-derived fallback carries no plan', async () => { |
| 108 | + fetchImpl = async () => ({ ok: false, status: 500, json: async () => null }); |
| 109 | + const { container } = renderInShell(<CloudPlanStatus properties={{ plan: 'free' }} />); |
| 110 | + |
| 111 | + // The hook fell through to the rows, which resolve (source `derived`) but |
| 112 | + // name no plan — so the card is left unmarked instead of guessed. |
| 113 | + await waitFor(() => expect(adapter.find).toHaveBeenCalledTimes(1)); |
| 114 | + await new Promise((r) => setTimeout(r, 0)); |
| 115 | + expect(container.innerHTML).toBe(''); |
| 116 | + }); |
| 117 | + |
| 118 | + it('renders nothing for a bare (un-enveloped) body that names the plan', async () => { |
| 119 | + // A bare body is a producer contract violation (cloud#1046): it yields no |
| 120 | + // summary, so even a `plan` that matches must not mark the card. |
| 121 | + fetchImpl = async () => ({ ok: true, status: 200, json: async () => ({ plan: 'free' }) }); |
| 122 | + const { container } = renderInShell(<CloudPlanStatus properties={{ plan: 'free' }} />); |
| 123 | + |
| 124 | + await waitFor(() => expect(adapter.find).toHaveBeenCalledTimes(1)); |
| 125 | + await new Promise((r) => setTimeout(r, 0)); |
| 126 | + expect(container.innerHTML).toBe(''); |
| 127 | + }); |
| 128 | + |
| 129 | + it('renders nothing when the node names no plan', async () => { |
| 130 | + fetchImpl = async () => summary('free'); |
| 131 | + const { container } = renderInShell(<CloudPlanStatus />); |
| 132 | + |
| 133 | + await waitFor(() => expect(fetchedUrls).toHaveLength(1)); |
| 134 | + await new Promise((r) => setTimeout(r, 0)); |
| 135 | + expect(container.innerHTML).toBe(''); |
| 136 | + }); |
| 137 | +}); |
| 138 | + |
| 139 | +describe('`cloud:plan-status` registration (objectui#10919)', () => { |
| 140 | + it('is registered under the one key the page authors, with no second spelling', () => { |
| 141 | + expect(ComponentRegistry.has('cloud:plan-status')).toBe(true); |
| 142 | + // `skipFallback: true` — no bare `plan-status` fallback … |
| 143 | + expect(ComponentRegistry.has('plan-status')).toBe(false); |
| 144 | + // … and no `app-shell:`-prefixed twin. |
| 145 | + expect(ComponentRegistry.has('app-shell:cloud:plan-status')).toBe(false); |
| 146 | + }); |
| 147 | + |
| 148 | + it('renders from a page node through SchemaRenderer, reading `properties.plan`', async () => { |
| 149 | + fetchImpl = async () => summary('team'); |
| 150 | + const { container } = renderInShell( |
| 151 | + <SchemaRenderer |
| 152 | + schema={{ |
| 153 | + type: 'cloud:plan-status', |
| 154 | + id: 'plan_team_current', |
| 155 | + responsiveStyles: { large: { alignSelf: 'flex-start' } }, |
| 156 | + properties: { plan: 'team' }, |
| 157 | + } as never} |
| 158 | + />, |
| 159 | + ); |
| 160 | + |
| 161 | + expect(await screen.findByText('Current plan')).toBeTruthy(); |
| 162 | + // The node's `responsiveStyles` scope class reaches the badge through |
| 163 | + // `className`, so the page can place it inside its card. |
| 164 | + const scope = container.querySelector('style[data-os-scope]')?.getAttribute('data-os-scope'); |
| 165 | + expect(scope).toBeTruthy(); |
| 166 | + const badge = container.querySelector('[data-plan-status="current"]'); |
| 167 | + expect(badge?.classList.contains(scope as string)).toBe(true); |
| 168 | + }); |
| 169 | +}); |
0 commit comments