|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + * @vitest-environment-options {"url":"https://www.sim.ai"} |
| 4 | + */ |
| 5 | +import { afterEach, describe, expect, it, vi } from 'vitest' |
| 6 | + |
| 7 | +vi.hoisted(() => { |
| 8 | + vi.stubEnv('NEXT_PUBLIC_APP_URL', '') |
| 9 | + vi.stubEnv('NEXT_PUBLIC_FORCE_HOSTED', 'false') |
| 10 | + vi.stubEnv('NODE_ENV', 'production') |
| 11 | + document.documentElement.id = '__next_error__' |
| 12 | +}) |
| 13 | + |
| 14 | +vi.unmock('@/lib/core/config/env') |
| 15 | +vi.unmock('@/lib/core/config/env-flags') |
| 16 | +vi.mock('@/lib/oauth/utils', () => ({ getScopesForService: () => [] })) |
| 17 | +vi.mock('@/providers/utils', () => ({ getProviderFromModel: () => 'openai' })) |
| 18 | + |
| 19 | +import { getEnv, PUBLIC_ENV_ATTRIBUTE } from '@/lib/core/config/env' |
| 20 | +import { isHosted } from '@/lib/core/config/env-flags' |
| 21 | +import { evaluateSubBlockCondition } from '@/lib/workflows/subblocks/visibility' |
| 22 | +import { getApiKeyCondition } from '@/blocks/utils' |
| 23 | +import { getHostedModels } from '@/providers/models' |
| 24 | + |
| 25 | +describe('hosted detection during client recovery', () => { |
| 26 | + afterEach(() => { |
| 27 | + document.documentElement.removeAttribute(PUBLIC_ENV_ATTRIBUTE) |
| 28 | + Reflect.deleteProperty(window, '__ENV') |
| 29 | + }) |
| 30 | + |
| 31 | + it('hides hosted model keys before the recovered layout installs runtime configuration', () => { |
| 32 | + expect(window.__ENV).toBeUndefined() |
| 33 | + expect(document.documentElement.getAttribute(PUBLIC_ENV_ATTRIBUTE)).toBeNull() |
| 34 | + expect(isHosted).toBe(true) |
| 35 | + |
| 36 | + for (const model of ['gpt-5.6-sol', 'claude-sonnet-5', 'gemini-2.5-pro']) { |
| 37 | + expect(getHostedModels()).toContain(model) |
| 38 | + expect(evaluateSubBlockCondition(getApiKeyCondition(), { model })).toBe(false) |
| 39 | + } |
| 40 | + |
| 41 | + document.documentElement.setAttribute( |
| 42 | + PUBLIC_ENV_ATTRIBUTE, |
| 43 | + JSON.stringify({ NEXT_PUBLIC_APP_URL: 'https://www.sim.ai' }) |
| 44 | + ) |
| 45 | + |
| 46 | + expect(getEnv('NEXT_PUBLIC_APP_URL')).toBe('https://www.sim.ai') |
| 47 | + expect(isHosted).toBe(true) |
| 48 | + expect(evaluateSubBlockCondition(getApiKeyCondition(), { model: 'gpt-5.6-sol' })).toBe(false) |
| 49 | + }) |
| 50 | + |
| 51 | + it('still requires keys for models outside the hosted catalog', () => { |
| 52 | + expect(evaluateSubBlockCondition(getApiKeyCondition(), { model: 'custom/model' })).toBe(true) |
| 53 | + }) |
| 54 | +}) |
0 commit comments