Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
test(mobile): cover WeChat login visibility behavior
xd-bobo29c7c6790a8f
diff --git a/apps/mobile/app/(auth)/login.tsx b/apps/mobile/app/(auth)/login.tsxindex 66f48d6433..313b474379 100644--- a/apps/mobile/app/(auth)/login.tsx+++ b/apps/mobile/app/(auth)/login.tsx@@ -4,7 +4,6 @@ import { useCallback, useEffect, useMemo, useRef, useState } from 'react';import { useTranslation } from 'react-i18next';import {Animated,- AppState,Easing,Keyboard,Linking,@@ -35,14 +34,7 @@ import { authErrorText, getAuthLocale, loginText } from '@/auth/loginMessages';import { canResumePendingConsent, makeConsentStamp, type ConsentStamp } from '@/auth/consentGate';import { acceptPrivacyConsent } from '@/analytics/analyticsConsentStore';import { initMobileTapdb } from '@/analytics/mobileTapdb';-import {- isNativeSocialProviderAvailable,- isNativeSocialProviderSupported,-} from '@/auth/nativeSocial';-import {- resolveMobileSocialLoginMode,- type MobileSocialLoginMode,-} from '@/auth/mobileSocialLoginMode';+import { useMobileSocialProviderModes } from '@/auth/useMobileSocialProviderModes';import { Text, TextInput } from '@/components/AppText';import { useTheme, useThemedStyles, type ThemeColors } from '@/theme';import {@@ -119,6 +111,8 @@ export interface LoginScreenProps {onClose?: () => void;}+const NO_SOCIAL_PROVIDERS: readonly SocialProvider[] = [];+export function LoginScreen({additionalAccount = false,onClose,@@ -291,10 +285,17 @@ export function LoginScreen({const [accountDeletionStatus, setAccountDeletionStatus] =useState<AccountDeletionStatus | null>(null);const [accountSwitcherVisible, setAccountSwitcherVisible] = useState(false);- const [iosWechatAvailable, setIosWechatAvailable] = useState(false);const styles = useThemedStyles(makeStyles);const configIssues = getMobileConfigIssues();const disabled = auth.isBusy || !auth.initialized || configIssues.length > 0;+ const advertisedSocialProviders =+ auth.loginState?.step === 'identifier'+ ? auth.loginState.providers.social+ : NO_SOCIAL_PROVIDERS;+ const socialProviderModes = useMobileSocialProviderModes({+ providers: advertisedSocialProviders,+ region: BUILD_AUTH_REGION,+ });useEffect(() => {let cancelled = false;@@ -306,23 +307,6 @@ export function LoginScreen({};}, []);- useEffect(() => {- if (Platform.OS !== 'ios') return;- let cancelled = false;- const refresh = async () => {- const available = await isNativeSocialProviderAvailable('wechat');- if (!cancelled) setIosWechatAvailable(available);- };- void refresh();- const subscription = AppState.addEventListener('change', (state) => {- if (state === 'active') void refresh();- });- return () => {- cancelled = true;- subscription.remove();- };- }, []);-useEffect(() => {if (!ssoOrgMode || ssoOrgEditedRef.current || ssoOrgHistory.length === 0)return;@@ -490,19 +474,6 @@ export function LoginScreen({const state = auth.loginState;if (state?.step !== 'identifier') return null;const providers = state.providers;- const socialProviderModes = new Map<SocialProvider, MobileSocialLoginMode>();- for (const provider of providers.social) {- const mode = resolveMobileSocialLoginMode({- provider,- region: BUILD_AUTH_REGION,- platform: Platform.OS,- nativeSupported:- provider === 'wechat' && Platform.OS === 'ios'- ? iosWechatAvailable- : isNativeSocialProviderSupported(provider),- });- if (mode) socialProviderModes.set(provider, mode);- }const socialProviders = providers.social.filter((provider) =>socialProviderModes.has(provider),);diff --git a/apps/mobile/src/__tests__/mobileAuthServerLogin.test.ts b/apps/mobile/src/__tests__/mobileAuthServerLogin.test.tsindex 4243e81966..9bca752262 100644--- a/apps/mobile/src/__tests__/mobileAuthServerLogin.test.ts+++ b/apps/mobile/src/__tests__/mobileAuthServerLogin.test.ts@@ -55,12 +55,8 @@ describe('mobile auth-server login', () => {);expect(nativeSource).toContain('GoogleSignin.configure({');expect(nativeSource).toContain("import('xdt-wechat-login')");- expect(loginSource).toContain(- "isNativeSocialProviderAvailable('wechat')",- );- expect(loginSource).toContain(- "provider === 'wechat' && Platform.OS === 'ios'",- );+ expect(loginSource).toContain('useMobileSocialProviderModes({');+ expect(loginSource).toContain('socialProviderModes.has(provider)');expect(nativeSource).toContain('requestWechatAuthCode({');expect(nativeSource).toContain('createNativeWechatLoginTimeout()');expect(nativeSource).toContain('cancelWechatAuthRequest().catch');diff --git a/apps/mobile/src/auth/__tests__/useMobileSocialProviderModes.test.tsx b/apps/mobile/src/auth/__tests__/useMobileSocialProviderModes.test.tsxnew file mode 100644index 0000000000..d0ab5aa2f7--- /dev/null+++ b/apps/mobile/src/auth/__tests__/useMobileSocialProviderModes.test.tsx@@ -0,0 +1,115 @@+// @vitest-environment jsdom+import React, { act } from 'react';+import { createRoot, type Root } from 'react-dom/client';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';+import type { SocialProvider } from '@cindy/auth-client';++const native = vi.hoisted(() => ({+ platform: 'ios',+ listeners: new Set<(state: string) => void>(),+ available: vi.fn(),+ supported: vi.fn(),+}));++vi.mock('react-native', () => ({+ AppState: {+ addEventListener: (+ _event: string,+ listener: (state: string) => void,+ ) => {+ native.listeners.add(listener);+ return { remove: () => native.listeners.delete(listener) };+ },+ },+ Platform: {+ get OS() {+ return native.platform;+ },+ },+}));++vi.mock('@/auth/nativeSocial', () => ({+ isNativeSocialProviderAvailable: native.available,+ isNativeSocialProviderSupported: native.supported,+}));++import { useMobileSocialProviderModes } from '../useMobileSocialProviderModes';++const providers: SocialProvider[] = ['wechat'];++function Probe() {+ const modes = useMobileSocialProviderModes({ providers, region: 'cn' });+ return (+ <div>+ {[...modes.keys()].map((provider) => (+ <button data-testid={`login.${provider}Button`} key={provider} />+ ))}+ </div>+ );+}++let host: HTMLDivElement;+let root: Root;++async function renderProbe() {+ await act(async () => {+ root.render(<Probe />);+ await Promise.resolve();+ });+}++function wechatButton() {+ return host.querySelector('[data-testid="login.wechatButton"]');+}++describe('mobile social provider visibility', () => {+ beforeEach(() => {+ (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT =+ true;+ native.platform = 'ios';+ native.available.mockReset().mockResolvedValue(false);+ native.supported.mockReset().mockReturnValue(true);+ host = document.createElement('div');+ root = createRoot(host);+ });++ afterEach(async () => {+ await act(async () => root.unmount());+ expect(native.listeners.size).toBe(0);+ });++ it('shows WeChat on iOS only after the installation probe succeeds', async () => {+ native.available.mockResolvedValue(true);+ await renderProbe();+ expect(wechatButton()).not.toBeNull();+ expect(native.available).toHaveBeenCalledWith('wechat');+ });++ it('hides WeChat on iOS when the installation probe returns false', async () => {+ await renderProbe();+ expect(wechatButton()).toBeNull();+ });++ it('refreshes iOS visibility when the app returns to the foreground', async () => {+ native.available+ .mockResolvedValueOnce(false)+ .mockResolvedValueOnce(true);+ await renderProbe();+ expect(wechatButton()).toBeNull();++ await act(async () => {+ for (const listener of native.listeners) listener('active');+ await Promise.resolve();+ });++ expect(wechatButton()).not.toBeNull();+ expect(native.available).toHaveBeenCalledTimes(2);+ });++ it('keeps configured WeChat visible on Android without probing installation', async () => {+ native.platform = 'android';+ await renderProbe();+ expect(wechatButton()).not.toBeNull();+ expect(native.available).not.toHaveBeenCalled();+ });+});diff --git a/apps/mobile/src/auth/useMobileSocialProviderModes.ts b/apps/mobile/src/auth/useMobileSocialProviderModes.tsnew file mode 100644index 0000000000..11a97ef471--- /dev/null+++ b/apps/mobile/src/auth/useMobileSocialProviderModes.ts@@ -0,0 +1,57 @@+import { useEffect, useMemo, useState } from 'react';+import { AppState, Platform } from 'react-native';+import type { AuthRegion, SocialProvider } from '@cindy/auth-client';++import {+ isNativeSocialProviderAvailable,+ isNativeSocialProviderSupported,+} from '@/auth/nativeSocial';+import {+ resolveMobileSocialLoginMode,+ type MobileSocialLoginMode,+} from '@/auth/mobileSocialLoginMode';++/** Keeps the rendered social-login set aligned with native device availability. */+export function useMobileSocialProviderModes({+ providers,+ region,+}: {+ providers: readonly SocialProvider[];+ region: AuthRegion;+}): ReadonlyMap<SocialProvider, MobileSocialLoginMode> {+ const [iosWechatAvailable, setIosWechatAvailable] = useState(false);++ useEffect(() => {+ if (Platform.OS !== 'ios') return;+ let cancelled = false;+ const refresh = async () => {+ const available = await isNativeSocialProviderAvailable('wechat');+ if (!cancelled) setIosWechatAvailable(available);+ };+ void refresh();+ const subscription = AppState.addEventListener('change', (state) => {+ if (state === 'active') void refresh();+ });+ return () => {+ cancelled = true;+ subscription.remove();+ };+ }, []);++ return useMemo(() => {+ const modes = new Map<SocialProvider, MobileSocialLoginMode>();+ for (const provider of providers) {+ const mode = resolveMobileSocialLoginMode({+ provider,+ region,+ platform: Platform.OS,+ nativeSupported:+ provider === 'wechat' && Platform.OS === 'ios'+ ? iosWechatAvailable+ : isNativeSocialProviderSupported(provider),+ });+ if (mode) modes.set(provider, mode);+ }+ return modes;+ }, [iosWechatAvailable, providers, region]);+}