Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 65 additions & 82 deletions src/config/comfyApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,89 +16,72 @@ vi.mock('@/scripts/api', () => ({
}
}))

describe('getComfyApiBaseUrl', () => {
const originalConfig = remoteConfig.value

beforeEach(() => {
remoteConfig.value = {}
})

afterEach(() => {
remoteConfig.value = originalConfig
})

it('honors the server-provided override', () => {
remoteConfig.value = { comfy_api_base_url: 'https://my-ephem.example.com' }
expect(getComfyApiBaseUrl()).toBe('https://my-ephem.example.com')
})

it('falls back to the build-time default when the key is absent', () => {
expect(getComfyApiBaseUrl()).toBe('https://stagingapi.comfy.org')
})

it('falls back to the build-time default when the value is empty', () => {
remoteConfig.value = { comfy_api_base_url: '' }
expect(getComfyApiBaseUrl()).toBe('https://stagingapi.comfy.org')
})
})

describe('getComfyCloudBaseUrl', () => {
const originalConfig = remoteConfig.value

beforeEach(() => {
remoteConfig.value = {}
})

afterEach(() => {
remoteConfig.value = originalConfig
})

it('honors the server-provided override', () => {
remoteConfig.value = {
comfy_cloud_base_url: 'https://my-ephem-cloud.example.com'
}
expect(getComfyCloudBaseUrl()).toBe('https://my-ephem-cloud.example.com')
})

it('falls back to the build-time default when the key is absent', () => {
expect(getComfyCloudBaseUrl()).toBe('https://testcloud.comfy.org')
})

it('falls back to the build-time default when the value is empty', () => {
remoteConfig.value = { comfy_cloud_base_url: '' }
expect(getComfyCloudBaseUrl()).toBe('https://testcloud.comfy.org')
})
})

describe('getComfyPlatformBaseUrl', () => {
const originalConfig = remoteConfig.value

beforeEach(() => {
remoteConfig.value = {}
})

afterEach(() => {
remoteConfig.value = originalConfig
})

it('honors the server-provided override', () => {
remoteConfig.value = {
comfy_platform_base_url: 'https://my-ephem-platform.example.com'
}
expect(getComfyPlatformBaseUrl()).toBe(
'https://my-ephem-platform.example.com'
interface BaseUrlCase {
label: string
getBaseUrl: () => string
setOverride: (value: string) => void
defaultUrl: string
}

const baseUrlCases: BaseUrlCase[] = [
{
label: 'API',
getBaseUrl: getComfyApiBaseUrl,
setOverride: (value) => {
remoteConfig.value = { comfy_api_base_url: value }
},
defaultUrl: 'https://stagingapi.comfy.org'
},
{
label: 'Cloud',
getBaseUrl: getComfyCloudBaseUrl,
setOverride: (value) => {
remoteConfig.value = { comfy_cloud_base_url: value }
},
defaultUrl: 'https://testcloud.comfy.org'
},
{
label: 'Platform',
getBaseUrl: getComfyPlatformBaseUrl,
setOverride: (value) => {
remoteConfig.value = { comfy_platform_base_url: value }
},
defaultUrl: 'https://stagingplatform.comfy.org'
}
]

describe.for(baseUrlCases)(
'$label base URL',
({ getBaseUrl, setOverride, defaultUrl }) => {
const originalConfig = remoteConfig.value

beforeEach(() => {
remoteConfig.value = {}
})

afterEach(() => {
remoteConfig.value = originalConfig
})

it('honors a server-provided HTTPS override', () => {
setOverride('https://custom.example.com')
expect(getBaseUrl()).toBe('https://custom.example.com')
})

it('removes a trailing slash from the override', () => {
setOverride('https://custom.example.com/')
expect(getBaseUrl()).toBe('https://custom.example.com')
})

it.for([undefined, '', 'not-a-url', 'http://custom.example.com'])(
'falls back to the build-time default for %s',
(override) => {
if (override !== undefined) setOverride(override)
expect(getBaseUrl()).toBe(defaultUrl)
}
)
})

it('falls back to the build-time default when the key is absent', () => {
expect(getComfyPlatformBaseUrl()).toBe('https://stagingplatform.comfy.org')
})

it('falls back to the build-time default when the value is empty', () => {
remoteConfig.value = { comfy_platform_base_url: '' }
expect(getComfyPlatformBaseUrl()).toBe('https://stagingplatform.comfy.org')
})
})
}
)

describe('compatibility with comfyui servers that predate the override keys', () => {
const originalConfig = remoteConfig.value
Expand Down
36 changes: 21 additions & 15 deletions src/config/comfyApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,32 @@ const BUILD_TIME_PLATFORM_BASE_URL = __USE_PROD_CONFIG__
: (import.meta.env.VITE_STAGING_PLATFORM_BASE_URL ??
STAGING_PLATFORM_BASE_URL)

function resolveBaseUrl(
key:
| 'comfy_api_base_url'
| 'comfy_cloud_base_url'
| 'comfy_platform_base_url',
defaultValue: string
): string {
const value = configValueOrDefault(remoteConfig.value, key, defaultValue)

try {
const url = new URL(value)
if (url.protocol !== 'https:' || !url.hostname) return defaultValue
return url.href.replace(/\/$/, '')
} catch {
return defaultValue
}
}

export function getComfyApiBaseUrl(): string {
return configValueOrDefault(
remoteConfig.value,
'comfy_api_base_url',
BUILD_TIME_API_BASE_URL
)
return resolveBaseUrl('comfy_api_base_url', BUILD_TIME_API_BASE_URL)
}

export function getComfyCloudBaseUrl(): string {
return configValueOrDefault(
remoteConfig.value,
'comfy_cloud_base_url',
BUILD_TIME_CLOUD_BASE_URL
)
return resolveBaseUrl('comfy_cloud_base_url', BUILD_TIME_CLOUD_BASE_URL)
}

export function getComfyPlatformBaseUrl(): string {
return configValueOrDefault(
remoteConfig.value,
'comfy_platform_base_url',
BUILD_TIME_PLATFORM_BASE_URL
)
return resolveBaseUrl('comfy_platform_base_url', BUILD_TIME_PLATFORM_BASE_URL)
}
37 changes: 33 additions & 4 deletions src/platform/remoteConfig/refreshRemoteConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'

import { api } from '@/scripts/api'

import { refreshRemoteConfig } from './refreshRemoteConfig'
import {
invalidateRemoteConfig,
refreshRemoteConfig
} from './refreshRemoteConfig'
import {
cachedLegacyBillingMigrationEnabled,
remoteConfig,
Expand Down Expand Up @@ -47,6 +50,26 @@ describe('refreshRemoteConfig', () => {
window.__CONFIG__ = {}
})

it('retains base URLs while invalidating identity-specific config', () => {
remoteConfig.value = {
subscription_required: true,
comfy_api_base_url: 'https://api.example.com',
comfy_cloud_base_url: 'https://cloud.example.com',
comfy_platform_base_url: 'https://platform.example.com'
}
window.__CONFIG__ = remoteConfig.value

invalidateRemoteConfig()

expect(remoteConfig.value).toEqual({
comfy_api_base_url: 'https://api.example.com',
comfy_cloud_base_url: 'https://cloud.example.com',
comfy_platform_base_url: 'https://platform.example.com'
})
expect(window.__CONFIG__).toEqual(remoteConfig.value)
expect(remoteConfigState.value).toBe('unloaded')
})

describe('with auth (default)', () => {
it('uses api.fetchApi when useAuth is true', async () => {
vi.mocked(api.fetchApi).mockResolvedValue(mockSuccessResponse())
Expand Down Expand Up @@ -214,14 +237,20 @@ describe('refreshRemoteConfig', () => {
expect(window.__CONFIG__).toEqual({})
})

it('clears config on fetch error', async () => {
it('preserves config on fetch error', async () => {
const existingConfig = {
subscription_required: true,
comfy_cloud_base_url: 'https://cloud.example.com'
}
cachedLegacyBillingMigrationEnabled.value = true
remoteConfig.value = existingConfig
window.__CONFIG__ = existingConfig
vi.mocked(api.fetchApi).mockRejectedValue(new Error('Network error'))

await refreshRemoteConfig()

expect(remoteConfig.value).toEqual({})
expect(window.__CONFIG__).toEqual({})
expect(remoteConfig.value).toEqual(existingConfig)
expect(window.__CONFIG__).toEqual(existingConfig)
expect(cachedLegacyBillingMigrationEnabled.value).toBeUndefined()
})

Expand Down
15 changes: 10 additions & 5 deletions src/platform/remoteConfig/refreshRemoteConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {

// Cap the bootstrap fetch so a wedged /features endpoint can never block app.mount indefinitely.
// A same-origin GET against the local comfyui server should resolve in well under a second;
// on timeout the catch below clears remoteConfig and consumers fall back to build-time defaults.
// on timeout consumers retain the last known config or use build-time defaults.
const FEATURES_FETCH_TIMEOUT_MS = 5_000

interface RefreshRemoteConfigOptions {
Expand All @@ -28,8 +28,15 @@ export function invalidateRemoteConfig(): void {
refreshGeneration++
for (const controller of activeRefreshControllers) controller.abort()
activeRefreshControllers.clear()
window.__CONFIG__ = {}
remoteConfig.value = {}
const { comfy_api_base_url, comfy_cloud_base_url, comfy_platform_base_url } =
remoteConfig.value
const retainedConfig = {
...(comfy_api_base_url && { comfy_api_base_url }),
...(comfy_cloud_base_url && { comfy_cloud_base_url }),
...(comfy_platform_base_url && { comfy_platform_base_url })
}
window.__CONFIG__ = retainedConfig
remoteConfig.value = retainedConfig
remoteConfigErrorStatus.value = null
remoteConfigState.value = 'unloaded'
cachedLegacyBillingMigrationEnabled.value = undefined
Expand Down Expand Up @@ -110,8 +117,6 @@ export async function refreshRemoteConfig(
if (generation !== refreshGeneration) return
if (signal?.aborted) return
console.error('Failed to fetch remote config:', error)
window.__CONFIG__ = {}
remoteConfig.value = {}
remoteConfigErrorStatus.value = null
if (useAuth) cachedLegacyBillingMigrationEnabled.value = undefined
remoteConfigState.value = 'error'
Expand Down
Loading