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
38 changes: 21 additions & 17 deletions src/stores/bootstrapStore.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useAuthStore } from '@/stores/authStore'
import { useUserStore } from '@/stores/userStore'
import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
import { fromPartial } from '@total-typescript/shoehorn'
import type { AxiosResponse } from 'axios'
import { AxiosError } from 'axios'
import { beforeEach, describe, expect, it, vi } from 'vitest'
Expand Down Expand Up @@ -41,7 +42,7 @@ describe('bootstrapStore', () => {
vi.spyOn(i18nModule, 'mergeCustomNodesI18n').mockImplementation(() => {})
useSettingStore().isReady = false
useAuthStore().isInitialized = false
Object.assign(useAuthStore(), { isAuthenticated: false })
Object.assign(useAuthStore(), { currentUser: null })
Object.assign(useUserStore(), { needsLogin: false })
mockDistributionTypes.isCloud = false
vi.mocked(useSettingStore().load).mockImplementation(() => {
Expand Down Expand Up @@ -129,32 +130,38 @@ describe('bootstrapStore', () => {
mockReportError.mockReset()
})

it('waits for Firebase init before loading stores, then proceeds regardless of auth state', async () => {
it('waits for Firebase init before loading stores, then resumes after sign-in', async () => {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
const store = useBootstrapStore()
const settingStore = useSettingStore()
const bootstrapPromise = store.startStoreBootstrap()

expect(store.isI18nReady).toBe(false)
expect(settingStore.isReady).toBe(false)

// Firebase resolves with no user (signed-out) — bootstrap must unblock.
// Previously it also waited for isAuthenticated, which made every
// signed-out load wait 35s and fire a false Sentry timeout.
await vi.waitFor(() => {
expect(store.isI18nReady).toBe(true)
})
expect(useAuthStore().isInitialized).toBe(false)

useAuthStore().isInitialized = true
await bootstrapPromise

await vi.waitFor(() => {
expect(store.isI18nReady).toBe(true)
expect(settingStore.isReady).toBe(true)
})
expect(settingStore.isReady).toBe(false)
expect(useWorkflowStore().loadWorkflows).not.toHaveBeenCalled()

useAuthStore().currentUser = fromPartial({ uid: 'user-123' })
await bootstrapPromise

expect(settingStore.isReady).toBe(true)
})

it('retries once and proceeds if Firebase init resolves during the backoff', async () => {
it('retries once and keeps authenticated stores gated if Firebase resolves signed out', async () => {
vi.useFakeTimers()
try {
const store = useBootstrapStore()
const settingStore = useSettingStore()
const bootstrapPromise = store.startStoreBootstrap()
void store.startStoreBootstrap()

// First wait times out with Firebase still not initialized.
await vi.advanceTimersByTimeAsync(16_001)
Expand All @@ -163,36 +170,33 @@ describe('bootstrapStore', () => {
// Firebase resolves during the retry backoff.
useAuthStore().isInitialized = true
await vi.advanceTimersByTimeAsync(3_001)
await bootstrapPromise

expect(settingStore.isReady).toBe(true)
expect(settingStore.isReady).toBe(false)
expect(mockReportError).not.toHaveBeenCalled()
} finally {
vi.useRealTimers()
}
})

it('gives up after a second timeout, reports it, and continues bootstrap unauthenticated', async () => {
it('gives up after a second timeout, reports it, and keeps authenticated stores gated', async () => {
vi.useFakeTimers()
const consoleError = vi
.spyOn(console, 'error')
.mockImplementation(() => {})
try {
const store = useBootstrapStore()
const settingStore = useSettingStore()
const bootstrapPromise = store.startStoreBootstrap()
void store.startStoreBootstrap()

// Firebase never resolves through the initial wait, the backoff, or the retry.
await vi.advanceTimersByTimeAsync(16_000 + 3_000 + 16_001)
await bootstrapPromise

expect(mockReportError).toHaveBeenCalledOnce()
expect(mockReportError).toHaveBeenCalledWith(expect.anything(), {
errorType: 'bootstrap_auth_wait_timeout'
})
expect(consoleError).not.toHaveBeenCalled()
// Bootstrap must not stay stuck: stores load even when Firebase never fires.
expect(settingStore.isReady).toBe(true)
expect(settingStore.isReady).toBe(false)
} finally {
vi.useRealTimers()
}
Expand Down
17 changes: 11 additions & 6 deletions src/stores/bootstrapStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ const AUTH_WAIT_RETRY_DELAY_MS = 3_000
* token or a broken auth response can never hang bootstrap forever.
*
* Only isInitialized is awaited — onAuthStateChanged fires with null for
* signed-out users, which sets isInitialized but not isAuthenticated.
* Awaiting isAuthenticated here would make every signed-out page load wait
* 35s and fire a false Sentry timeout. The router guard handles the
* login redirect for unauthenticated users separately.
* signed-out users, so a signed-out load resolves this wait immediately
* instead of burning the timeout and firing a false Sentry report. Waiting
* for an actual user is a separate, unbounded wait in the caller.
*
* Retries once after a short delay; if auth is still unresolved, reports it
* to every observability sink and lets bootstrap continue rather than leaving
Expand Down Expand Up @@ -103,8 +102,16 @@ export const useBootstrapStore = defineStore('bootstrap', () => {
}

async function startStoreBootstrap() {
void loadI18n()

if (isCloud) {
await bootstrapTracer.settle('auth-gate/initialized', waitForCloudAuth)

// Signed-out cloud pages (/cloud/login) must issue no authenticated
// request, so bootstrap parks here until the user signs in rather than
// loading stores that can only answer 401.
const { isAuthenticated } = storeToRefs(useAuthStore())
await until(isAuthenticated).toBe(true)
Comment on lines 108 to +114

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@christian-byrne FYI I attempted to integrate isAuthenticated check into waitForCloudAuth, but it breaks current assumptions about cloud / core separation in it (where cloud requires auth token but core doesn't). Thought it might be an opportunity that we can simplify this drastically in a separate PR

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tracked as FE-2625. The PR merged with this thread unresolved, so the finding is carried in Linear rather than lost here. Filed automatically; close the ticket if the finding is no longer true at main.

}

const userStore = useUserStore()
Expand All @@ -117,9 +124,7 @@ export const useBootstrapStore = defineStore('bootstrap', () => {
until(needsLogin).toBe(false)
)

void loadI18n()
const storeLoads = loadAuthenticatedStores()

void Promise.allSettled(storeLoads).then(() => {
bootstrapTracer.milestone('stores-ready')
})
Expand Down
Loading