|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * CloudWorkspaceTimezoneNotice (`cloud:workspace-timezone-notice`, |
| 5 | + * objectui#11930) prints ONE line on the Cloud welcome page naming the |
| 6 | + * timezone the workspace was seeded with, read from the authoritative |
| 7 | + * entitlements summary's `workspaceTimezoneSeed` — and nothing in every other |
| 8 | + * state: no seed, a failed request, a body outside the envelope, or loading. |
| 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 { CloudWorkspaceTimezoneNotice } from '../CloudWorkspaceTimezoneNotice'; |
| 31 | + |
| 32 | +const EN = { defaultLanguage: 'en', detectBrowserLanguage: false } as const; |
| 33 | +const ZH = { defaultLanguage: 'zh', detectBrowserLanguage: false } as const; |
| 34 | + |
| 35 | +/** The two packs' sentences for the seed below — the copy a user reads. */ |
| 36 | +const EN_LINE = |
| 37 | + 'The workspace timezone was set to Asia/Shanghai from your browser when the workspace was created. ' |
| 38 | + + 'You can change it in Settings → Localization.'; |
| 39 | +const ZH_LINE = '工作区时区在创建时已按你的浏览器设为 Asia/Shanghai,可在「设置 → 本地化」中修改。'; |
| 40 | + |
| 41 | +/** |
| 42 | + * The line resolves from the locale packs, so a real i18n context is needed — |
| 43 | + * without one `t()` returns the raw key and a text assertion would pass |
| 44 | + * against nothing. The adapter is the row-derived fallback's data source; its |
| 45 | + * rows resolve, so a failed summary falls through to a REAL `derived` state. |
| 46 | + */ |
| 47 | +const adapter = { find: vi.fn().mockResolvedValue([{ id: 'env_1', environment_type: 'production' }]) }; |
| 48 | + |
| 49 | +const renderInShell = (ui: React.ReactElement, config: typeof EN | typeof ZH = EN) => |
| 50 | + render( |
| 51 | + <I18nProvider config={config} persistLanguage={false}> |
| 52 | + <AdapterCtx.Provider value={adapter as never}>{ui}</AdapterCtx.Provider> |
| 53 | + </I18nProvider>, |
| 54 | + ); |
| 55 | + |
| 56 | +/** The control plane's `{ success, data }` envelope around a summary. */ |
| 57 | +function summary(data: Record<string, unknown>) { |
| 58 | + return { ok: true, status: 200, json: async () => ({ success: true, data }) }; |
| 59 | +} |
| 60 | + |
| 61 | +const SEEDED = { plan: 'free', hasProductionEnv: true, workspaceTimezoneSeed: 'Asia/Shanghai' }; |
| 62 | +const UNSEEDED = { plan: 'free', hasProductionEnv: true }; |
| 63 | + |
| 64 | +const noticeOf = (container: HTMLElement) => container.querySelector('[data-workspace-timezone-notice]'); |
| 65 | + |
| 66 | +/** Waits until the summary was read and the hook settled, so an empty render is a verdict, not the loading state. */ |
| 67 | +async function settledAfterSummary() { |
| 68 | + await waitFor(() => expect(fetchedUrls).toHaveLength(1)); |
| 69 | + await new Promise((r) => setTimeout(r, 0)); |
| 70 | +} |
| 71 | + |
| 72 | +afterEach(() => { |
| 73 | + cleanup(); |
| 74 | + fetchedUrls.length = 0; |
| 75 | + adapter.find.mockClear(); |
| 76 | +}); |
| 77 | + |
| 78 | +describe('CloudWorkspaceTimezoneNotice — the seed is present (objectui#11930)', () => { |
| 79 | + it('names the seeded zone in English, verbatim', async () => { |
| 80 | + fetchImpl = async () => summary(SEEDED); |
| 81 | + const { container } = renderInShell(<CloudWorkspaceTimezoneNotice />); |
| 82 | + |
| 83 | + expect(await screen.findByText(EN_LINE)).toBeTruthy(); |
| 84 | + expect(noticeOf(container)?.textContent).toBe(EN_LINE); |
| 85 | + }); |
| 86 | + |
| 87 | + it('names the seeded zone in Chinese, verbatim', async () => { |
| 88 | + fetchImpl = async () => summary(SEEDED); |
| 89 | + const { container } = renderInShell(<CloudWorkspaceTimezoneNotice />, ZH); |
| 90 | + |
| 91 | + expect(await screen.findByText(ZH_LINE)).toBeTruthy(); |
| 92 | + expect(noticeOf(container)?.textContent).toBe(ZH_LINE); |
| 93 | + }); |
| 94 | + |
| 95 | + it('reads the org-scoped summary endpoint', async () => { |
| 96 | + fetchImpl = async () => summary(SEEDED); |
| 97 | + renderInShell(<CloudWorkspaceTimezoneNotice />); |
| 98 | + |
| 99 | + await screen.findByText(EN_LINE); |
| 100 | + expect(fetchedUrls).toEqual(['/api/v1/cloud/environment-entitlements?organizationId=org_1']); |
| 101 | + }); |
| 102 | + |
| 103 | + it('is one line of text — no link, no button, no dismissal', async () => { |
| 104 | + fetchImpl = async () => summary(SEEDED); |
| 105 | + const { container } = renderInShell(<CloudWorkspaceTimezoneNotice />); |
| 106 | + |
| 107 | + await screen.findByText(EN_LINE); |
| 108 | + expect(container.querySelectorAll('a, button')).toHaveLength(0); |
| 109 | + expect(noticeOf(container)?.children).toHaveLength(0); |
| 110 | + }); |
| 111 | +}); |
| 112 | + |
| 113 | +describe('CloudWorkspaceTimezoneNotice — nothing renders without a seed (objectui#11930)', () => { |
| 114 | + it('renders nothing when the summary carries no seed — the control', async () => { |
| 115 | + fetchImpl = async () => summary(UNSEEDED); |
| 116 | + const { container } = renderInShell(<CloudWorkspaceTimezoneNotice />); |
| 117 | + |
| 118 | + await settledAfterSummary(); |
| 119 | + expect(container.innerHTML).toBe(''); |
| 120 | + }); |
| 121 | + |
| 122 | + it('renders nothing for an empty seed', async () => { |
| 123 | + fetchImpl = async () => summary({ ...UNSEEDED, workspaceTimezoneSeed: '' }); |
| 124 | + const { container } = renderInShell(<CloudWorkspaceTimezoneNotice />); |
| 125 | + |
| 126 | + await settledAfterSummary(); |
| 127 | + expect(container.innerHTML).toBe(''); |
| 128 | + }); |
| 129 | + |
| 130 | + it('renders nothing while the summary is loading', () => { |
| 131 | + fetchImpl = () => new Promise(() => {}); |
| 132 | + const { container } = renderInShell(<CloudWorkspaceTimezoneNotice />); |
| 133 | + |
| 134 | + expect(container.innerHTML).toBe(''); |
| 135 | + }); |
| 136 | + |
| 137 | + it('renders nothing, and does not throw, when the summary request fails', async () => { |
| 138 | + fetchImpl = async () => ({ ok: false, status: 500, json: async () => null }); |
| 139 | + const { container } = renderInShell(<CloudWorkspaceTimezoneNotice />); |
| 140 | + |
| 141 | + // The hook fell through to the rows, which resolve (source `derived`) but |
| 142 | + // carry no seed — so the line is left out instead of guessed. |
| 143 | + await waitFor(() => expect(adapter.find).toHaveBeenCalledTimes(1)); |
| 144 | + await new Promise((r) => setTimeout(r, 0)); |
| 145 | + expect(container.innerHTML).toBe(''); |
| 146 | + }); |
| 147 | + |
| 148 | + it('renders nothing, and does not throw, when the summary request rejects', async () => { |
| 149 | + fetchImpl = async () => { |
| 150 | + throw new TypeError('Failed to fetch'); |
| 151 | + }; |
| 152 | + const { container } = renderInShell(<CloudWorkspaceTimezoneNotice />); |
| 153 | + |
| 154 | + await waitFor(() => expect(adapter.find).toHaveBeenCalledTimes(1)); |
| 155 | + await new Promise((r) => setTimeout(r, 0)); |
| 156 | + expect(container.innerHTML).toBe(''); |
| 157 | + }); |
| 158 | + |
| 159 | + it('renders nothing for a bare (un-enveloped) body that carries a seed', async () => { |
| 160 | + // A bare body is a producer contract violation (cloud#1046): it yields no |
| 161 | + // summary, so even a seed in it must not print. |
| 162 | + fetchImpl = async () => ({ ok: true, status: 200, json: async () => SEEDED }); |
| 163 | + const { container } = renderInShell(<CloudWorkspaceTimezoneNotice />); |
| 164 | + |
| 165 | + await waitFor(() => expect(adapter.find).toHaveBeenCalledTimes(1)); |
| 166 | + await new Promise((r) => setTimeout(r, 0)); |
| 167 | + expect(container.innerHTML).toBe(''); |
| 168 | + }); |
| 169 | +}); |
| 170 | + |
| 171 | +describe('`cloud:workspace-timezone-notice` registration (objectui#11930)', () => { |
| 172 | + it('is registered under the one key the page authors, with no second spelling', () => { |
| 173 | + expect(ComponentRegistry.has('cloud:workspace-timezone-notice')).toBe(true); |
| 174 | + // `skipFallback: true` — no bare `workspace-timezone-notice` fallback … |
| 175 | + expect(ComponentRegistry.has('workspace-timezone-notice')).toBe(false); |
| 176 | + // … and no `app-shell:`-prefixed twin. |
| 177 | + expect(ComponentRegistry.has('app-shell:cloud:workspace-timezone-notice')).toBe(false); |
| 178 | + }); |
| 179 | + |
| 180 | + it('renders from a page node through SchemaRenderer', async () => { |
| 181 | + fetchImpl = async () => summary(SEEDED); |
| 182 | + const { container } = renderInShell( |
| 183 | + <SchemaRenderer |
| 184 | + schema={{ |
| 185 | + type: 'cloud:workspace-timezone-notice', |
| 186 | + id: 'welcome_timezone_notice', |
| 187 | + responsiveStyles: { large: { marginTop: '8px' } }, |
| 188 | + } as never} |
| 189 | + />, |
| 190 | + ); |
| 191 | + |
| 192 | + expect(await screen.findByText(EN_LINE)).toBeTruthy(); |
| 193 | + // The node's `responsiveStyles` scope class reaches the line through |
| 194 | + // `className`, so the page can place it. |
| 195 | + const scope = container.querySelector('style[data-os-scope]')?.getAttribute('data-os-scope'); |
| 196 | + expect(scope).toBeTruthy(); |
| 197 | + expect(noticeOf(container)?.classList.contains(scope as string)).toBe(true); |
| 198 | + }); |
| 199 | +}); |
0 commit comments