diff --git a/lib/__tests__/plugin-store.test.ts b/lib/__tests__/plugin-store.test.ts index 92ec8f130..ea4ee26ac 100644 --- a/lib/__tests__/plugin-store.test.ts +++ b/lib/__tests__/plugin-store.test.ts @@ -287,6 +287,20 @@ describe('usePluginStore', () => { expect(usePluginStore.getState().initialized).toBe(true); }); + it('starts the plugins when a call made after the sign-in joined the signed-out one', async () => { + const { loadPlugin } = await import('@/lib/plugin-loader'); + let answer!: (r: unknown) => void; + mocks.apiFetch.mockImplementationOnce(() => new Promise((r) => { answer = r; })); + + const beforeSignIn = usePluginStore.getState().initializePlugins(); + const afterSignIn = usePluginStore.getState().initializePlugins(); + answer({ ok: false, status: 401, json: async () => ({}) }); + await Promise.all([beforeSignIn, afterSignIn]); + + expect(loadPlugin).toHaveBeenCalledWith(expect.objectContaining({ id: 'managed-plugin' })); + expect(usePluginStore.getState().initialized).toBe(true); + }); + it('keeps the record when the download fails so the error can surface at load time', async () => { usePluginStore.setState({ plugins: [syncedRecord()] }); mocks.downloadManagedBundle.mockRejectedValue(new Error('Could not download the bundle (HTTP 503)')); diff --git a/stores/plugin-store.ts b/stores/plugin-store.ts index 4e9f3fad1..9a4a527c3 100644 --- a/stores/plugin-store.ts +++ b/stores/plugin-store.ts @@ -317,7 +317,11 @@ export const usePluginStore = create()( if (get().initialized) return; if (pluginInitializationPromise) { - await pluginInitializationPromise; + const joined = pluginInitializationPromise; + await joined; + // The run this joined may have ended signed out (it started before + // the sign-in); then this call starts the plugins itself. + if (!get().initialized && pluginInitializationPromise !== joined) await get().initializePlugins(); return; }