|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#34622 — the AI chat's quota exit (`onUpgrade`, the paywall |
| 5 | + * button `ChatbotEnhanced` draws) opens the control plane's declared |
| 6 | + * `upgradeUrl` when this runtime serves one: a deep address, which the Cloud |
| 7 | + * console root's landing never redirects back into the environment. |
| 8 | + * |
| 9 | + * Absent, it is today's exit unchanged: the Cloud origin from |
| 10 | + * `cloudConsoleUrl()`, and NO button (`onUpgrade` undefined) on a runtime that |
| 11 | + * names no upstream cloud (objectui#7253). |
| 12 | + * |
| 13 | + * The pane is rendered for real with `ChatbotEnhanced` replaced by a prop |
| 14 | + * recorder, because the `onUpgrade` prop ChatPane hands down is the contract |
| 15 | + * under assertion. The runtime config is the REAL reader, fed one payload |
| 16 | + * through a replaced `fetch`. |
| 17 | + */ |
| 18 | + |
| 19 | +import '@testing-library/jest-dom/vitest'; |
| 20 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 21 | +import { cleanup, render } from '@testing-library/react'; |
| 22 | +import { MemoryRouter } from 'react-router-dom'; |
| 23 | +import React from 'react'; |
| 24 | + |
| 25 | +/** Props of the last `ChatbotEnhanced` render. */ |
| 26 | +let captured: Record<string, unknown> = {}; |
| 27 | + |
| 28 | +vi.mock('@object-ui/plugin-chatbot', async (importOriginal) => { |
| 29 | + const actual = await importOriginal<Record<string, unknown>>(); |
| 30 | + return { |
| 31 | + ...actual, |
| 32 | + ChatbotEnhanced: (props: Record<string, unknown>) => { |
| 33 | + captured = props; |
| 34 | + return null; |
| 35 | + }, |
| 36 | + useObjectChat: () => ({ |
| 37 | + messages: [], |
| 38 | + isLoading: false, |
| 39 | + error: undefined, |
| 40 | + sendMessage: vi.fn(), |
| 41 | + stop: vi.fn(), |
| 42 | + reload: vi.fn(), |
| 43 | + clear: vi.fn(), |
| 44 | + setMessages: vi.fn(), |
| 45 | + }), |
| 46 | + useAiModels: () => ({ models: [], defaultModelId: undefined }), |
| 47 | + }; |
| 48 | +}); |
| 49 | + |
| 50 | +vi.mock('../../../providers/MetadataProvider', async (importOriginal) => { |
| 51 | + const actual = await importOriginal<Record<string, unknown>>(); |
| 52 | + return { ...actual, useMetadata: () => ({ apps: [] }) }; |
| 53 | +}); |
| 54 | +vi.mock('../../../providers/AdapterProvider', async (importOriginal) => { |
| 55 | + const actual = await importOriginal<Record<string, unknown>>(); |
| 56 | + return { ...actual, useAdapter: () => null }; |
| 57 | +}); |
| 58 | +vi.mock('../../marketplace/marketplaceApi', async (importOriginal) => ({ |
| 59 | + ...(await importOriginal<Record<string, unknown>>()), |
| 60 | + cloudConsoleUrl: vi.fn(), |
| 61 | +})); |
| 62 | + |
| 63 | +import { I18nProvider } from '@object-ui/i18n'; |
| 64 | +import type { AgentDescriptor } from '@object-ui/plugin-chatbot'; |
| 65 | +import { ChatPane } from '../AiChatPage'; |
| 66 | +import { cloudConsoleUrl } from '../../marketplace/marketplaceApi'; |
| 67 | +import { initRuntimeConfig, resetRuntimeConfigForTesting } from '../../../runtime-config'; |
| 68 | + |
| 69 | +window.matchMedia = ((query: string) => ({ |
| 70 | + matches: false, |
| 71 | + media: query, |
| 72 | + onchange: null, |
| 73 | + addEventListener: () => {}, |
| 74 | + removeEventListener: () => {}, |
| 75 | + addListener: () => {}, |
| 76 | + removeListener: () => {}, |
| 77 | + dispatchEvent: () => false, |
| 78 | +})) as unknown as typeof window.matchMedia; |
| 79 | + |
| 80 | +const CLOUD = 'https://cloud.example.test'; |
| 81 | +const UPGRADE = 'https://cloud.example.test/_console/billing?org=acme'; |
| 82 | +const cloudMock = cloudConsoleUrl as unknown as ReturnType<typeof vi.fn>; |
| 83 | +const openMock = vi.fn(); |
| 84 | + |
| 85 | +async function serveRuntimeConfig(payload: Record<string, unknown>) { |
| 86 | + const realFetch = globalThis.fetch; |
| 87 | + globalThis.fetch = vi.fn(async () => ({ ok: true, json: async () => payload })) as unknown as typeof fetch; |
| 88 | + try { |
| 89 | + await initRuntimeConfig(); |
| 90 | + } finally { |
| 91 | + globalThis.fetch = realFetch; |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +/** Render the pane and hand back the `onUpgrade` prop it gave the chatbot. */ |
| 96 | +function renderOnUpgrade(): (() => void) | undefined { |
| 97 | + cleanup(); |
| 98 | + captured = {}; |
| 99 | + render( |
| 100 | + <I18nProvider config={{ defaultLanguage: 'en', detectBrowserLanguage: false }}> |
| 101 | + <MemoryRouter initialEntries={['/ai']}> |
| 102 | + <ChatPane |
| 103 | + agents={[{ name: 'ask', label: 'Ask' } as unknown as AgentDescriptor]} |
| 104 | + agentsLoading={false} |
| 105 | + agentsError={undefined} |
| 106 | + activeAgent="ask" |
| 107 | + chatApi="/api/v1/ai/agents/ask/chat" |
| 108 | + apiBase="/api/v1/ai" |
| 109 | + conversationId="conv-1" |
| 110 | + initialMessages={[]} |
| 111 | + pendingFirstMessageRef={{ current: null }} |
| 112 | + onSent={vi.fn()} |
| 113 | + onShare={vi.fn()} |
| 114 | + /> |
| 115 | + </MemoryRouter> |
| 116 | + </I18nProvider>, |
| 117 | + ); |
| 118 | + expect('onUpgrade' in captured).toBe(true); |
| 119 | + return captured.onUpgrade as (() => void) | undefined; |
| 120 | +} |
| 121 | + |
| 122 | +beforeEach(() => { |
| 123 | + window.localStorage.clear(); |
| 124 | + vi.clearAllMocks(); |
| 125 | + vi.stubGlobal('open', openMock); |
| 126 | + resetRuntimeConfigForTesting(); |
| 127 | + cloudMock.mockReturnValue(CLOUD); |
| 128 | +}); |
| 129 | +afterEach(() => { |
| 130 | + cleanup(); |
| 131 | + resetRuntimeConfigForTesting(); |
| 132 | + vi.unstubAllGlobals(); |
| 133 | +}); |
| 134 | + |
| 135 | +describe('AI chat quota exit — declared upgradeUrl (objectstack#34622)', () => { |
| 136 | + it('declared: the exit opens the declared address, not the Cloud origin', async () => { |
| 137 | + await serveRuntimeConfig({ cloudUrl: CLOUD, upgradeUrl: UPGRADE }); |
| 138 | + const onUpgrade = renderOnUpgrade(); |
| 139 | + expect(onUpgrade).toBeTypeOf('function'); |
| 140 | + onUpgrade!(); |
| 141 | + expect(openMock).toHaveBeenCalledTimes(1); |
| 142 | + expect(openMock).toHaveBeenCalledWith(UPGRADE, '_blank', 'noopener,noreferrer'); |
| 143 | + }); |
| 144 | + |
| 145 | + it('declared on a runtime that names no upstream cloud: the exit is wired, to the declared address', async () => { |
| 146 | + cloudMock.mockReturnValue(''); |
| 147 | + await serveRuntimeConfig({ upgradeUrl: UPGRADE }); |
| 148 | + const onUpgrade = renderOnUpgrade(); |
| 149 | + expect(onUpgrade).toBeTypeOf('function'); |
| 150 | + onUpgrade!(); |
| 151 | + expect(openMock).toHaveBeenCalledWith(UPGRADE, '_blank', 'noopener,noreferrer'); |
| 152 | + }); |
| 153 | + |
| 154 | + // CONTROL — absent, today's exit: the Cloud origin, or no button at all. |
| 155 | + it('absent: the exit opens the Cloud origin, as before', async () => { |
| 156 | + await serveRuntimeConfig({ cloudUrl: CLOUD }); |
| 157 | + const onUpgrade = renderOnUpgrade(); |
| 158 | + expect(onUpgrade).toBeTypeOf('function'); |
| 159 | + onUpgrade!(); |
| 160 | + expect(openMock).toHaveBeenCalledWith(CLOUD, '_blank', 'noopener,noreferrer'); |
| 161 | + }); |
| 162 | + |
| 163 | + it('absent on a runtime that names no upstream cloud: no exit (no upgrade button), as before', async () => { |
| 164 | + cloudMock.mockReturnValue(''); |
| 165 | + await serveRuntimeConfig({ singleEnvironment: true }); |
| 166 | + expect(renderOnUpgrade()).toBeUndefined(); |
| 167 | + expect(openMock).not.toHaveBeenCalled(); |
| 168 | + }); |
| 169 | +}); |
0 commit comments