From b006db72327846a47a1b13c181dd9623b4ed192d Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Thu, 24 Sep 2026 08:41:32 -0400 Subject: [PATCH 1/7] feat(flags): unified_web_session client flag, off by default Reads unified_web_session from /api/features with the ff: overrides, false outside cloud, independent of unified_cloud_auth. Nothing reads it yet. Adds a request-level test pinning the sign-in, token refresh and API call requests with the flag off, for unified_cloud_auth on and off. --- src/composables/__mocks__/useFeatureFlags.ts | 1 + src/composables/useFeatureFlags.test.ts | 99 +++++ src/composables/useFeatureFlags.ts | 10 + src/platform/auth/webSessionFlagOff.test.ts | 373 +++++++++++++++++++ src/platform/remoteConfig/types.ts | 1 + src/storybook/mocks/useFeatureFlags.ts | 1 + 6 files changed, 485 insertions(+) create mode 100644 src/platform/auth/webSessionFlagOff.test.ts diff --git a/src/composables/__mocks__/useFeatureFlags.ts b/src/composables/__mocks__/useFeatureFlags.ts index f12cf601d49..5d879e0e4b5 100644 --- a/src/composables/__mocks__/useFeatureFlags.ts +++ b/src/composables/__mocks__/useFeatureFlags.ts @@ -29,6 +29,7 @@ const defaultFlags: FeatureFlags = { hostedBillingWebEnabled: false, showSignInButton: undefined, unifiedCloudAuthEnabled: false, + unifiedWebSessionEnabled: false, billingControlEnabled: false, legacyBillingMigrationEnabled: false, embeddedCheckoutEnabled: false, diff --git a/src/composables/useFeatureFlags.test.ts b/src/composables/useFeatureFlags.test.ts index b52fd006f73..b2e20d84191 100644 --- a/src/composables/useFeatureFlags.test.ts +++ b/src/composables/useFeatureFlags.test.ts @@ -933,6 +933,105 @@ describe('useFeatureFlags', () => { }) }) + describe('unifiedWebSessionEnabled', () => { + beforeEach(() => { + vi.mocked(distributionTypes).isCloud = true + vi.mocked(api.getServerFeature).mockImplementation( + (_path, defaultValue) => defaultValue + ) + }) + + afterEach(() => { + vi.mocked(distributionTypes).isCloud = false + remoteConfigState.value = 'unloaded' + remoteConfig.value = {} + }) + + it.for([ + { + name: 'is off when /api/features lacks the flag', + state: 'authenticated', + config: {}, + expected: false + }, + { + name: 'is off when remote config failed to load', + state: 'error', + config: {}, + expected: false + }, + { + name: 'is on when the server sends true', + state: 'authenticated', + config: { unified_web_session: true }, + expected: true + } + ] as const)('$name', ({ state, config, expected }) => { + remoteConfigState.value = state + remoteConfig.value = config + + expect(useFeatureFlags().flags.unifiedWebSessionEnabled).toBe(expected) + }) + + it('honours the ff: localStorage dev override', () => { + remoteConfigState.value = 'authenticated' + localStorage.setItem( + `ff:${ServerFeatureFlag.UNIFIED_WEB_SESSION}`, + 'true' + ) + + expect(useFeatureFlags().flags.unifiedWebSessionEnabled).toBe(true) + }) + + it('honours the ?ff= session override', () => { + remoteConfigState.value = 'authenticated' + vi.mocked(getSessionOverride).mockImplementation((flagKey) => + flagKey === ServerFeatureFlag.UNIFIED_WEB_SESSION ? true : undefined + ) + + expect(useFeatureFlags().flags.unifiedWebSessionEnabled).toBe(true) + }) + + it('is off outside the cloud distribution', () => { + vi.mocked(distributionTypes).isCloud = false + remoteConfigState.value = 'authenticated' + remoteConfig.value = { unified_web_session: true } + + expect(useFeatureFlags().flags.unifiedWebSessionEnabled).toBe(false) + }) + + it.for([ + { + name: 'unified_cloud_auth on does not turn it on', + config: { unified_cloud_auth: true }, + expected: { + unifiedCloudAuthEnabled: true, + unifiedWebSessionEnabled: false + } + }, + { + name: 'it turns on without unified_cloud_auth and leaves that flag off', + config: { unified_cloud_auth: false, unified_web_session: true }, + expected: { + unifiedCloudAuthEnabled: false, + unifiedWebSessionEnabled: true + } + } + ])( + 'is independent of unified_cloud_auth: $name', + ({ config, expected }) => { + remoteConfigState.value = 'authenticated' + remoteConfig.value = config + + const { flags } = useFeatureFlags() + expect({ + unifiedCloudAuthEnabled: flags.unifiedCloudAuthEnabled, + unifiedWebSessionEnabled: flags.unifiedWebSessionEnabled + }).toEqual(expected) + } + ) + }) + describe('session override precedence', () => { afterEach(() => { vi.mocked(getSessionOverride).mockReset() diff --git a/src/composables/useFeatureFlags.ts b/src/composables/useFeatureFlags.ts index 57a6fba748a..05026ecff55 100644 --- a/src/composables/useFeatureFlags.ts +++ b/src/composables/useFeatureFlags.ts @@ -39,6 +39,7 @@ export enum ServerFeatureFlag { HOSTED_BILLING_DESTINATION = 'hosted_billing_destination', SHOW_SIGNIN_BUTTON = 'show_signin_button', UNIFIED_CLOUD_AUTH = 'unified_cloud_auth', + UNIFIED_WEB_SESSION = 'unified_web_session', BILLING_CONTROL_ENABLED = 'billing_control_enabled', LEGACY_BILLING_MIGRATION_ENABLED = 'legacy_billing_migration_enabled', EMBEDDED_CHECKOUT_ENABLED = 'embedded_checked_enabled', @@ -257,6 +258,15 @@ export function useFeatureFlags() { false ) }, + get unifiedWebSessionEnabled() { + if (!isCloud) return false + + return resolveFlag( + ServerFeatureFlag.UNIFIED_WEB_SESSION, + remoteConfig.value.unified_web_session, + false + ) + }, get billingControlEnabled() { return resolveAuthGatedFlag( ServerFeatureFlag.BILLING_CONTROL_ENABLED, diff --git a/src/platform/auth/webSessionFlagOff.test.ts b/src/platform/auth/webSessionFlagOff.test.ts new file mode 100644 index 00000000000..3eb2cdf0b9f --- /dev/null +++ b/src/platform/auth/webSessionFlagOff.test.ts @@ -0,0 +1,373 @@ +import { fromPartial } from '@total-typescript/shoehorn' +import type { User } from 'firebase/auth' +import { afterEach, assert, beforeEach, describe, expect, it, vi } from 'vitest' +import { effectScope } from 'vue' + +import type { ExchangeTokenResponse } from '@comfyorg/ingest-types' + +import { useCurrentUser } from '@/composables/auth/useCurrentUser' +import type { FirebaseIdentity } from '@comfyorg/account-core/firebase' +import { refreshRemoteConfig } from '@/platform/remoteConfig/refreshRemoteConfig' +import { remoteConfig } from '@/platform/remoteConfig/remoteConfig' +import { TOKEN_REFRESH_BUFFER_MS } from '@/platform/workspace/workspaceConstants' +import { api } from '@/scripts/api' +import type { useExtensionService } from '@/services/extensionService' +import type { ComfyApp } from '@/scripts/app' +import type { ComfyExtension } from '@/types/comfy' + +type IdentityObserver = (user: User | null) => void + +const identity = vi.hoisted(() => { + const userObservers = new Set() + const tokenObservers = new Set() + const state: { user: User | null } = { user: null } + return { + userObservers, + tokenObservers, + state, + reset() { + userObservers.clear() + tokenObservers.clear() + state.user = null + }, + signIn(user: User) { + state.user = user + userObservers.forEach((observer) => observer(user)) + tokenObservers.forEach((observer) => observer(user)) + }, + refreshIdToken() { + tokenObservers.forEach((observer) => observer(state.user)) + } + } +}) + +const registeredExtensions = vi.hoisted((): ComfyExtension[] => []) + +vi.mock(import('@/platform/distribution/types'), () => ({ + DISTRIBUTION: 'cloud' as const, + isCloud: true, + isDesktop: false, + isNightly: false +})) + +vi.mock(import('@/platform/auth/firebaseIdentity'), () => ({ + firebaseIdentity: fromPartial({ + onUserChanged: (observer: IdentityObserver) => { + identity.userObservers.add(observer) + return () => identity.userObservers.delete(observer) + }, + onTokenChanged: (observer: IdentityObserver) => { + identity.tokenObservers.add(observer) + return () => identity.tokenObservers.delete(observer) + }, + currentUser: () => identity.state.user + }) +})) + +vi.mock(import('@/services/extensionService'), () => ({ + useExtensionService: () => + fromPartial>({ + registerExtension: (extension: ComfyExtension) => { + registeredExtensions.push(extension) + } + }) +})) + +vi.mock(import('@/platform/telemetry')) + +vi.mock(import('@/platform/telemetry/reportError'), () => ({ + reportError: vi.fn() +})) + +await import('@/extensions/core/cloudSessionCookie') + +const ONE_HOUR_MS = 60 * 60 * 1000 + +const FIREBASE_USER = fromPartial({ + uid: 'user-a', + getIdToken: async () => 'firebase-id-token' +}) + +interface RecordedRequest { + method: string + path: string + headers: Record + credentials: RequestCredentials | null +} + +function requestUrl(input: RequestInfo | URL): string { + if (typeof input === 'string') return input + return input instanceof URL ? input.href : input.url +} + +function recordRequest( + input: RequestInfo | URL, + init: RequestInit | undefined +): RecordedRequest { + const url = new URL(requestUrl(input), location.href) + return { + method: (init?.method ?? 'GET').toUpperCase(), + path: url.origin === location.origin ? url.pathname + url.search : url.href, + headers: Object.fromEntries( + [...new Headers(init?.headers).entries()].map(([key, value]) => [ + key.toLowerCase(), + value + ]) + ), + credentials: init?.credentials ?? null + } +} + +function jsonResponse(body: unknown, status = 200): Response { + return new Response(JSON.stringify(body), { + status, + headers: { 'Content-Type': 'application/json' } + }) +} + +function installFetchRecorder(features: Record) { + const all: RecordedRequest[] = [] + let pending: RecordedRequest[] = [] + let mintCount = 0 + + const mintResponse = (): ExchangeTokenResponse => { + mintCount += 1 + return { + token: `cloud-jwt-${mintCount}`, + expires_at: new Date(Date.now() + ONE_HOUR_MS).toISOString(), + workspace: { id: 'ws-personal', name: 'Personal', type: 'personal' }, + role: 'owner', + permissions: ['owner:*'] + } + } + + const respond = (request: RecordedRequest): Response => { + switch (`${request.method} ${request.path}`) { + case 'GET /api/features': + return jsonResponse(features) + case 'POST /api/auth/session': + return jsonResponse({ success: true }) + case 'POST /api/auth/token': + return jsonResponse(mintResponse()) + case 'GET /api/queue': + return jsonResponse({ queue_running: [], queue_pending: [] }) + case 'POST /api/prompt': + return jsonResponse({ prompt_id: 'prompt-1', number: 1 }) + default: + return jsonResponse({ message: 'unexpected request' }, 404) + } + } + + vi.stubGlobal( + 'fetch', + vi.fn(async (input, init) => { + const request = recordRequest(input, init) + all.push(request) + pending.push(request) + return respond(request) + }) + ) + + return { + all, + get pending() { + return pending + }, + take() { + const taken = pending + pending = [] + return taken + } + } +} + +function wireSessionCookieExtension() { + const extension = registeredExtensions.find( + ({ name }) => name === 'Comfy.Cloud.SessionCookie' + ) + assert.exists(extension) + const scope = effectScope() + scope.run(() => { + const { onUserResolved, onTokenRefreshed } = useCurrentUser() + onUserResolved( + (user) => + void extension.onAuthUserResolved?.(user, fromPartial({})) + ) + onTokenRefreshed(() => void extension.onAuthTokenRefreshed?.()) + }) + return scope +} + +const FEATURES_BOOTSTRAP: RecordedRequest[] = [ + { method: 'GET', path: '/api/features', headers: {}, credentials: null } +] + +const SESSION_POST: RecordedRequest = { + method: 'POST', + path: '/api/auth/session', + headers: { + authorization: 'Bearer firebase-id-token', + 'content-type': 'application/json' + }, + credentials: 'include' +} + +const TOKEN_MINT: RecordedRequest = { + method: 'POST', + path: '/api/auth/token', + headers: { + authorization: 'Bearer firebase-id-token', + 'content-type': 'application/json' + }, + credentials: null +} + +interface FlowGolden { + signIn: RecordedRequest[] + tokenRefresh: RecordedRequest[] + apiCall: RecordedRequest[] +} + +const UNIFIED_CLOUD_AUTH_OFF: FlowGolden = { + signIn: [SESSION_POST], + tokenRefresh: [SESSION_POST], + apiCall: [ + { + method: 'GET', + path: '/api/queue', + headers: { authorization: 'Bearer firebase-id-token', 'comfy-user': '' }, + credentials: null + }, + { + method: 'POST', + path: '/api/prompt', + headers: { + authorization: 'Bearer firebase-id-token', + 'comfy-user': '', + 'content-type': 'application/json' + }, + credentials: null + } + ] +} + +const UNIFIED_CLOUD_AUTH_ON: FlowGolden = { + signIn: [TOKEN_MINT, SESSION_POST], + tokenRefresh: [TOKEN_MINT, SESSION_POST], + apiCall: [ + { + method: 'GET', + path: '/api/queue', + headers: { authorization: 'Bearer cloud-jwt-2', 'comfy-user': '' }, + credentials: null + }, + { + method: 'POST', + path: '/api/prompt', + headers: { + authorization: 'Bearer cloud-jwt-2', + 'comfy-user': '', + 'content-type': 'application/json' + }, + credentials: null + } + ] +} + +const FORBIDDEN_HEADERS = [ + 'x-comfy-workspace-id', + 'x-csrf-token', + 'x-comfy-client' +] + +const isSessionMutation = ({ method, path }: RecordedRequest) => + path === '/api/auth/session' && (method === 'POST' || method === 'DELETE') + +describe('cloud auth requests with unified_web_session off', () => { + let hooks: ReturnType | undefined + + beforeEach(() => { + identity.reset() + }) + + afterEach(() => { + hooks?.stop() + hooks = undefined + remoteConfig.value = {} + }) + + it.for([ + { + name: 'unified_cloud_auth off, unified_web_session absent', + features: { unified_cloud_auth: false }, + golden: UNIFIED_CLOUD_AUTH_OFF + }, + { + name: 'unified_cloud_auth off, unified_web_session false', + features: { unified_cloud_auth: false, unified_web_session: false }, + golden: UNIFIED_CLOUD_AUTH_OFF + }, + { + name: 'unified_cloud_auth on, unified_web_session absent', + features: { unified_cloud_auth: true }, + golden: UNIFIED_CLOUD_AUTH_ON + }, + { + name: 'unified_cloud_auth on, unified_web_session false', + features: { unified_cloud_auth: true, unified_web_session: false }, + golden: UNIFIED_CLOUD_AUTH_ON + } + ])( + 'sends the pinned sign-in, refresh and API requests ($name)', + async ({ features, golden }) => { + const recorder = installFetchRecorder(features) + + await refreshRemoteConfig({ useAuth: false }) + expect(recorder.take()).toEqual(FEATURES_BOOTSTRAP) + + hooks = wireSessionCookieExtension() + identity.signIn(FIREBASE_USER) + await vi.waitFor(() => expect(recorder.pending).toEqual(golden.signIn)) + recorder.take() + + identity.refreshIdToken() + await vi.advanceTimersByTimeAsync(ONE_HOUR_MS - TOKEN_REFRESH_BUFFER_MS) + await vi.waitFor(() => + expect(recorder.pending).toEqual(golden.tokenRefresh) + ) + recorder.take() + + await api.fetchApi('/queue') + await api.fetchApi('/prompt', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ prompt: {} }) + }) + expect(recorder.take()).toEqual(golden.apiCall) + + expect(recorder.all).toEqual([ + ...FEATURES_BOOTSTRAP, + ...golden.signIn, + ...golden.tokenRefresh, + ...golden.apiCall + ]) + + expect( + recorder.all.flatMap(({ headers }) => + Object.keys(headers).filter((key) => FORBIDDEN_HEADERS.includes(key)) + ) + ).toEqual([]) + expect( + recorder.all.filter( + (request) => + request.credentials === 'include' && !isSessionMutation(request) + ) + ).toEqual([]) + expect( + recorder.all.filter( + ({ method, path }) => method === 'GET' && path === '/api/auth/session' + ) + ).toEqual([]) + } + ) +}) diff --git a/src/platform/remoteConfig/types.ts b/src/platform/remoteConfig/types.ts index 5fba3273227..76b3b5feb7c 100644 --- a/src/platform/remoteConfig/types.ts +++ b/src/platform/remoteConfig/types.ts @@ -131,6 +131,7 @@ export type RemoteConfig = { // funnel it through normalizeHostedBillingDestination before trusting it. hosted_billing_destination?: string unified_cloud_auth?: boolean + unified_web_session?: boolean // Wire key carries the server's own spelling; see ServerFeatureFlag. embedded_checked_enabled?: boolean billing_sdk_topup_enabled?: boolean diff --git a/src/storybook/mocks/useFeatureFlags.ts b/src/storybook/mocks/useFeatureFlags.ts index c06e2896319..6af2604e2cd 100644 --- a/src/storybook/mocks/useFeatureFlags.ts +++ b/src/storybook/mocks/useFeatureFlags.ts @@ -25,6 +25,7 @@ export enum ServerFeatureFlag { HOSTED_BILLING_DESTINATION = 'hosted_billing_destination', SHOW_SIGNIN_BUTTON = 'show_signin_button', UNIFIED_CLOUD_AUTH = 'unified_cloud_auth', + UNIFIED_WEB_SESSION = 'unified_web_session', BILLING_CONTROL_ENABLED = 'billing_control_enabled', V1_PAYMENT_RECOVERY = 'v1_payment_recovery' } From ba804d85a468b919e5576caf046332a2ba2a03e6 Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Thu, 24 Sep 2026 09:29:35 -0400 Subject: [PATCH 2/7] fix(flags): require a literal true for unified_web_session --- src/composables/useFeatureFlags.test.ts | 15 +++++++++++++++ src/composables/useFeatureFlags.ts | 5 ++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/composables/useFeatureFlags.test.ts b/src/composables/useFeatureFlags.test.ts index b2e20d84191..f7d30199bc8 100644 --- a/src/composables/useFeatureFlags.test.ts +++ b/src/composables/useFeatureFlags.test.ts @@ -973,6 +973,21 @@ describe('useFeatureFlags', () => { expect(useFeatureFlags().flags.unifiedWebSessionEnabled).toBe(expected) }) + it.for(['true', 'false', 1])( + 'is off when the server sends the malformed value %j', + (wireValue) => { + remoteConfigState.value = 'authenticated' + vi.mocked(api.getServerFeature).mockImplementation( + (path, defaultValue) => + path === ServerFeatureFlag.UNIFIED_WEB_SESSION + ? wireValue + : defaultValue + ) + + expect(useFeatureFlags().flags.unifiedWebSessionEnabled).toBe(false) + } + ) + it('honours the ff: localStorage dev override', () => { remoteConfigState.value = 'authenticated' localStorage.setItem( diff --git a/src/composables/useFeatureFlags.ts b/src/composables/useFeatureFlags.ts index 05026ecff55..cc399abf1d3 100644 --- a/src/composables/useFeatureFlags.ts +++ b/src/composables/useFeatureFlags.ts @@ -261,10 +261,9 @@ export function useFeatureFlags() { get unifiedWebSessionEnabled() { if (!isCloud) return false - return resolveFlag( + return resolveStrictBooleanFlag( ServerFeatureFlag.UNIFIED_WEB_SESSION, - remoteConfig.value.unified_web_session, - false + remoteConfig.value.unified_web_session ) }, get billingControlEnabled() { From 47557d915abe35703da8bdf7ee0f9d459e3a9910 Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Thu, 24 Sep 2026 12:34:40 -0400 Subject: [PATCH 3/7] test: pin sign-out and the WebSocket URL with unified_web_session off --- src/platform/auth/webSessionFlagOff.test.ts | 81 +++++++++++++++++++-- 1 file changed, 76 insertions(+), 5 deletions(-) diff --git a/src/platform/auth/webSessionFlagOff.test.ts b/src/platform/auth/webSessionFlagOff.test.ts index 3eb2cdf0b9f..9bf485493a4 100644 --- a/src/platform/auth/webSessionFlagOff.test.ts +++ b/src/platform/auth/webSessionFlagOff.test.ts @@ -37,6 +37,11 @@ const identity = vi.hoisted(() => { }, refreshIdToken() { tokenObservers.forEach((observer) => observer(state.user)) + }, + signOut() { + state.user = null + userObservers.forEach((observer) => observer(null)) + tokenObservers.forEach((observer) => observer(null)) } } }) @@ -158,6 +163,32 @@ function installFetchRecorder(features: Record) { } } + class RecordingWebSocket extends EventTarget { + static readonly CONNECTING = 0 + static readonly OPEN = 1 + static readonly CLOSING = 2 + static readonly CLOSED = 3 + readyState = RecordingWebSocket.CONNECTING + binaryType = 'blob' + constructor(url: string | URL) { + super() + const parsed = new URL(String(url)) + const request: RecordedRequest = { + method: 'WEBSOCKET', + path: parsed.pathname + parsed.search, + headers: {}, + credentials: null + } + all.push(request) + pending.push(request) + } + send() {} + close() { + this.readyState = RecordingWebSocket.CLOSED + } + } + vi.stubGlobal('WebSocket', RecordingWebSocket) + vi.stubGlobal( 'fetch', vi.fn(async (input, init) => { @@ -188,12 +219,13 @@ function wireSessionCookieExtension() { assert.exists(extension) const scope = effectScope() scope.run(() => { - const { onUserResolved, onTokenRefreshed } = useCurrentUser() + const { onUserResolved, onTokenRefreshed, onUserLogout } = useCurrentUser() onUserResolved( (user) => void extension.onAuthUserResolved?.(user, fromPartial({})) ) onTokenRefreshed(() => void extension.onAuthTokenRefreshed?.()) + onUserLogout(() => void extension.onAuthUserLogout?.()) }) return scope } @@ -222,10 +254,22 @@ const TOKEN_MINT: RecordedRequest = { credentials: null } +const SIGN_OUT: RecordedRequest[] = [ + { + method: 'DELETE', + path: '/api/auth/session', + headers: {}, + credentials: 'include' + }, + { method: 'WEBSOCKET', path: '/ws', headers: {}, credentials: null } +] + interface FlowGolden { signIn: RecordedRequest[] tokenRefresh: RecordedRequest[] apiCall: RecordedRequest[] + socket: RecordedRequest[] + signOut: RecordedRequest[] } const UNIFIED_CLOUD_AUTH_OFF: FlowGolden = { @@ -248,7 +292,16 @@ const UNIFIED_CLOUD_AUTH_OFF: FlowGolden = { }, credentials: null } - ] + ], + socket: [ + { + method: 'WEBSOCKET', + path: '/ws?token=firebase-id-token', + headers: {}, + credentials: null + } + ], + signOut: SIGN_OUT } const UNIFIED_CLOUD_AUTH_ON: FlowGolden = { @@ -271,7 +324,16 @@ const UNIFIED_CLOUD_AUTH_ON: FlowGolden = { }, credentials: null } - ] + ], + socket: [ + { + method: 'WEBSOCKET', + path: '/ws?token=cloud-jwt-2', + headers: {}, + credentials: null + } + ], + signOut: SIGN_OUT } const FORBIDDEN_HEADERS = [ @@ -318,7 +380,7 @@ describe('cloud auth requests with unified_web_session off', () => { golden: UNIFIED_CLOUD_AUTH_ON } ])( - 'sends the pinned sign-in, refresh and API requests ($name)', + 'sends the pinned sign-in, refresh, API, socket and sign-out requests ($name)', async ({ features, golden }) => { const recorder = installFetchRecorder(features) @@ -345,11 +407,20 @@ describe('cloud auth requests with unified_web_session off', () => { }) expect(recorder.take()).toEqual(golden.apiCall) + await api.resetSocket() + expect(recorder.take()).toEqual(golden.socket) + + identity.signOut() + await vi.advanceTimersByTimeAsync(1_000) + expect(recorder.take()).toEqual(golden.signOut) + expect(recorder.all).toEqual([ ...FEATURES_BOOTSTRAP, ...golden.signIn, ...golden.tokenRefresh, - ...golden.apiCall + ...golden.apiCall, + ...golden.socket, + ...golden.signOut ]) expect( From ad44d5e9a511497cbf7fa47b5221d1e7b75fe8a3 Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Thu, 24 Sep 2026 13:15:48 -0400 Subject: [PATCH 4/7] test: pin customer provisioning and media URLs with unified_web_session off --- src/platform/auth/webSessionFlagOff.test.ts | 50 ++++++++++++++++++--- 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/src/platform/auth/webSessionFlagOff.test.ts b/src/platform/auth/webSessionFlagOff.test.ts index 9bf485493a4..4488542c89e 100644 --- a/src/platform/auth/webSessionFlagOff.test.ts +++ b/src/platform/auth/webSessionFlagOff.test.ts @@ -1,16 +1,19 @@ import { fromPartial } from '@total-typescript/shoehorn' -import type { User } from 'firebase/auth' +import type { User, UserCredential } from 'firebase/auth' import { afterEach, assert, beforeEach, describe, expect, it, vi } from 'vitest' import { effectScope } from 'vue' import type { ExchangeTokenResponse } from '@comfyorg/ingest-types' import { useCurrentUser } from '@/composables/auth/useCurrentUser' +import { getComfyApiBaseUrl } from '@/config/comfyApi' import type { FirebaseIdentity } from '@comfyorg/account-core/firebase' import { refreshRemoteConfig } from '@/platform/remoteConfig/refreshRemoteConfig' import { remoteConfig } from '@/platform/remoteConfig/remoteConfig' import { TOKEN_REFRESH_BUFFER_MS } from '@/platform/workspace/workspaceConstants' import { api } from '@/scripts/api' +import { useAuthStore } from '@/stores/authStore' +import { resultItemPreviewUrl, resultItemUrl } from '@/utils/resultItemUrl' import type { useExtensionService } from '@/services/extensionService' import type { ComfyApp } from '@/scripts/app' import type { ComfyExtension } from '@/types/comfy' @@ -65,7 +68,11 @@ vi.mock(import('@/platform/auth/firebaseIdentity'), () => ({ identity.tokenObservers.add(observer) return () => identity.tokenObservers.delete(observer) }, - currentUser: () => identity.state.user + currentUser: () => identity.state.user, + signInWithEmail: async () => { + identity.signIn(FIREBASE_USER) + return fromPartial({ user: FIREBASE_USER }) + } }) })) @@ -147,6 +154,9 @@ function installFetchRecorder(features: Record) { } const respond = (request: RecordedRequest): Response => { + if (request.path === `${getComfyApiBaseUrl()}/customers`) { + return jsonResponse({ id: 'customer-1' }, 201) + } switch (`${request.method} ${request.path}`) { case 'GET /api/features': return jsonResponse(features) @@ -254,6 +264,19 @@ const TOKEN_MINT: RecordedRequest = { credentials: null } +const MEDIA_ITEM = { + filename: 'output.png', + subfolder: '', + type: 'output', + nodeId: '1', + mediaType: 'images' +} as const + +const MEDIA_URLS = [ + '/api/view?filename=output.png&type=output&subfolder=', + '/api/view?filename=output.png&type=output&subfolder=&res=512' +] + const SIGN_OUT: RecordedRequest[] = [ { method: 'DELETE', @@ -264,6 +287,16 @@ const SIGN_OUT: RecordedRequest[] = [ { method: 'WEBSOCKET', path: '/ws', headers: {}, credentials: null } ] +const CUSTOMER_PROVISIONING: RecordedRequest = { + method: 'POST', + path: `${getComfyApiBaseUrl()}/customers`, + headers: { + authorization: 'Bearer firebase-id-token', + 'content-type': 'application/json' + }, + credentials: null +} + interface FlowGolden { signIn: RecordedRequest[] tokenRefresh: RecordedRequest[] @@ -273,7 +306,7 @@ interface FlowGolden { } const UNIFIED_CLOUD_AUTH_OFF: FlowGolden = { - signIn: [SESSION_POST], + signIn: [SESSION_POST, CUSTOMER_PROVISIONING], tokenRefresh: [SESSION_POST], apiCall: [ { @@ -305,7 +338,7 @@ const UNIFIED_CLOUD_AUTH_OFF: FlowGolden = { } const UNIFIED_CLOUD_AUTH_ON: FlowGolden = { - signIn: [TOKEN_MINT, SESSION_POST], + signIn: [TOKEN_MINT, SESSION_POST, CUSTOMER_PROVISIONING], tokenRefresh: [TOKEN_MINT, SESSION_POST], apiCall: [ { @@ -380,7 +413,7 @@ describe('cloud auth requests with unified_web_session off', () => { golden: UNIFIED_CLOUD_AUTH_ON } ])( - 'sends the pinned sign-in, refresh, API, socket and sign-out requests ($name)', + 'pins sign-in, refresh, API, socket, media and sign-out traffic ($name)', async ({ features, golden }) => { const recorder = installFetchRecorder(features) @@ -388,7 +421,7 @@ describe('cloud auth requests with unified_web_session off', () => { expect(recorder.take()).toEqual(FEATURES_BOOTSTRAP) hooks = wireSessionCookieExtension() - identity.signIn(FIREBASE_USER) + await useAuthStore().login('user-a@example.com', 'password') await vi.waitFor(() => expect(recorder.pending).toEqual(golden.signIn)) recorder.take() @@ -410,6 +443,11 @@ describe('cloud auth requests with unified_web_session off', () => { await api.resetSocket() expect(recorder.take()).toEqual(golden.socket) + expect([ + resultItemUrl(MEDIA_ITEM), + resultItemPreviewUrl(MEDIA_ITEM) + ]).toEqual(MEDIA_URLS) + identity.signOut() await vi.advanceTimersByTimeAsync(1_000) expect(recorder.take()).toEqual(golden.signOut) From 6ad9c5a0eb11968050970189d1efce6da070e499 Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Thu, 24 Sep 2026 17:00:28 -0400 Subject: [PATCH 5/7] fix(flags): read unified_web_session only from /api/features and the overrides --- src/composables/useFeatureFlags.test.ts | 24 ++++++++++++++++-------- src/composables/useFeatureFlags.ts | 8 +++++--- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/composables/useFeatureFlags.test.ts b/src/composables/useFeatureFlags.test.ts index f7d30199bc8..8870042fd6d 100644 --- a/src/composables/useFeatureFlags.test.ts +++ b/src/composables/useFeatureFlags.test.ts @@ -973,15 +973,23 @@ describe('useFeatureFlags', () => { expect(useFeatureFlags().flags.unifiedWebSessionEnabled).toBe(expected) }) - it.for(['true', 'false', 1])( - 'is off when the server sends the malformed value %j', - (wireValue) => { + it('ignores the server-feature fallback when /api/features lacks the flag', () => { + remoteConfigState.value = 'authenticated' + vi.mocked(api.getServerFeature).mockImplementation( + (path, defaultValue) => + path === ServerFeatureFlag.UNIFIED_WEB_SESSION ? true : defaultValue + ) + + expect(useFeatureFlags().flags.unifiedWebSessionEnabled).toBe(false) + }) + + it.for(['"true"', '"false"', '1'])( + 'is off for the malformed override value %s', + (rawValue) => { remoteConfigState.value = 'authenticated' - vi.mocked(api.getServerFeature).mockImplementation( - (path, defaultValue) => - path === ServerFeatureFlag.UNIFIED_WEB_SESSION - ? wireValue - : defaultValue + localStorage.setItem( + `ff:${ServerFeatureFlag.UNIFIED_WEB_SESSION}`, + rawValue ) expect(useFeatureFlags().flags.unifiedWebSessionEnabled).toBe(false) diff --git a/src/composables/useFeatureFlags.ts b/src/composables/useFeatureFlags.ts index cc399abf1d3..11a467d182d 100644 --- a/src/composables/useFeatureFlags.ts +++ b/src/composables/useFeatureFlags.ts @@ -261,10 +261,12 @@ export function useFeatureFlags() { get unifiedWebSessionEnabled() { if (!isCloud) return false - return resolveStrictBooleanFlag( - ServerFeatureFlag.UNIFIED_WEB_SESSION, + const key = ServerFeatureFlag.UNIFIED_WEB_SESSION + const value = + getSessionOverride(key) ?? + getDevOverride(key) ?? remoteConfig.value.unified_web_session - ) + return value === true }, get billingControlEnabled() { return resolveAuthGatedFlag( From 895667c9b45845d6a80e0fa1d139ac715fbc3b1a Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Thu, 24 Sep 2026 17:00:59 -0400 Subject: [PATCH 6/7] test: assert sign-out closes the previous WebSocket --- src/platform/auth/webSessionFlagOff.test.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/platform/auth/webSessionFlagOff.test.ts b/src/platform/auth/webSessionFlagOff.test.ts index 4488542c89e..2e225321e61 100644 --- a/src/platform/auth/webSessionFlagOff.test.ts +++ b/src/platform/auth/webSessionFlagOff.test.ts @@ -140,6 +140,7 @@ function jsonResponse(body: unknown, status = 200): Response { function installFetchRecorder(features: Record) { const all: RecordedRequest[] = [] let pending: RecordedRequest[] = [] + let socketCloses = 0 let mintCount = 0 const mintResponse = (): ExchangeTokenResponse => { @@ -194,6 +195,7 @@ function installFetchRecorder(features: Record) { } send() {} close() { + socketCloses += 1 this.readyState = RecordingWebSocket.CLOSED } } @@ -214,6 +216,9 @@ function installFetchRecorder(features: Record) { get pending() { return pending }, + get socketCloses() { + return socketCloses + }, take() { const taken = pending pending = [] @@ -448,9 +453,11 @@ describe('cloud auth requests with unified_web_session off', () => { resultItemPreviewUrl(MEDIA_ITEM) ]).toEqual(MEDIA_URLS) + expect(recorder.socketCloses).toBe(0) identity.signOut() await vi.advanceTimersByTimeAsync(1_000) expect(recorder.take()).toEqual(golden.signOut) + expect(recorder.socketCloses).toBe(1) expect(recorder.all).toEqual([ ...FEATURES_BOOTSTRAP, From f4a40396c04b73e3ac756a0f951b3aae464f0ead Mon Sep 17 00:00:00 2001 From: dante01yoon Date: Thu, 24 Sep 2026 17:44:59 -0400 Subject: [PATCH 7/7] test(flags): a false override turns off a server true --- src/composables/useFeatureFlags.test.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/composables/useFeatureFlags.test.ts b/src/composables/useFeatureFlags.test.ts index 8870042fd6d..d95f7a17980 100644 --- a/src/composables/useFeatureFlags.test.ts +++ b/src/composables/useFeatureFlags.test.ts @@ -996,6 +996,31 @@ describe('useFeatureFlags', () => { } ) + it('lets a false ff: override turn off a server true', () => { + remoteConfigState.value = 'authenticated' + remoteConfig.value = { unified_web_session: true } + localStorage.setItem( + `ff:${ServerFeatureFlag.UNIFIED_WEB_SESSION}`, + 'false' + ) + + expect(useFeatureFlags().flags.unifiedWebSessionEnabled).toBe(false) + }) + + it('lets a false ?ff= override beat a true ff: override and server value', () => { + remoteConfigState.value = 'authenticated' + remoteConfig.value = { unified_web_session: true } + localStorage.setItem( + `ff:${ServerFeatureFlag.UNIFIED_WEB_SESSION}`, + 'true' + ) + vi.mocked(getSessionOverride).mockImplementation((flagKey) => + flagKey === ServerFeatureFlag.UNIFIED_WEB_SESSION ? false : undefined + ) + + expect(useFeatureFlags().flags.unifiedWebSessionEnabled).toBe(false) + }) + it('honours the ff: localStorage dev override', () => { remoteConfigState.value = 'authenticated' localStorage.setItem(