|
| 1 | +import React from 'react'; |
| 2 | +import renderer, { act } from 'react-test-renderer'; |
| 3 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 4 | + |
| 5 | +import { AndroidCaptureIntentSection } from './android-capture-intent-section'; |
| 6 | + |
| 7 | +const nativeMocks = vi.hoisted(() => ({ |
| 8 | + getCaptureIntentConfig: vi.fn(), |
| 9 | + isSupported: vi.fn(() => true), |
| 10 | + setCaptureIntentEnabled: vi.fn(), |
| 11 | +})); |
| 12 | +const clipboardMocks = vi.hoisted(() => ({ setStringAsync: vi.fn() })); |
| 13 | +const showToast = vi.hoisted(() => vi.fn()); |
| 14 | + |
| 15 | +vi.mock('@/modules/android-widget', () => nativeMocks); |
| 16 | +vi.mock('expo-clipboard', () => clipboardMocks); |
| 17 | +vi.mock('@/hooks/use-theme-colors', () => ({ |
| 18 | + useThemeColors: () => ({ |
| 19 | + bg: '#0f172a', |
| 20 | + cardBg: '#111827', |
| 21 | + border: '#334155', |
| 22 | + text: '#f8fafc', |
| 23 | + secondaryText: '#94a3b8', |
| 24 | + tint: '#3b82f6', |
| 25 | + }), |
| 26 | +})); |
| 27 | +vi.mock('@/contexts/toast-context', () => ({ |
| 28 | + useToast: () => ({ showToast }), |
| 29 | +})); |
| 30 | +vi.mock('./settings.hooks', () => ({ |
| 31 | + useSettingsLocalization: () => ({ |
| 32 | + tr: (key: string) => ({ |
| 33 | + 'settings.automationCapture': 'Automation capture', |
| 34 | + 'settings.automationCaptureDesc': 'Allow trusted automation apps with your token to queue text to Inbox. Captures appear the next time Mindwtr opens.', |
| 35 | + 'settings.automationCaptureToken': 'Capture token', |
| 36 | + 'settings.automationCaptureCopyToken': 'Copy token', |
| 37 | + 'settings.automationCaptureCopied': 'Capture token copied.', |
| 38 | + 'settings.automationCaptureCopyFailed': "Couldn't copy the capture token.", |
| 39 | + 'settings.automationCaptureLoadFailed': "Couldn't load automation capture settings.", |
| 40 | + 'settings.automationCaptureUpdateFailed': "Couldn't update automation capture settings.", |
| 41 | + }[key] ?? key), |
| 42 | + }), |
| 43 | +})); |
| 44 | + |
| 45 | +const TOKEN = 'ab'.repeat(32); |
| 46 | +const settle = async () => { |
| 47 | + await Promise.resolve(); |
| 48 | + await Promise.resolve(); |
| 49 | +}; |
| 50 | + |
| 51 | +async function renderSection() { |
| 52 | + let tree!: renderer.ReactTestRenderer; |
| 53 | + await act(async () => { |
| 54 | + tree = renderer.create(<AndroidCaptureIntentSection />); |
| 55 | + await settle(); |
| 56 | + }); |
| 57 | + return tree; |
| 58 | +} |
| 59 | + |
| 60 | +describe('AndroidCaptureIntentSection', () => { |
| 61 | + beforeEach(() => { |
| 62 | + vi.clearAllMocks(); |
| 63 | + nativeMocks.isSupported.mockReturnValue(true); |
| 64 | + nativeMocks.getCaptureIntentConfig.mockResolvedValue({ enabled: false, token: null }); |
| 65 | + nativeMocks.setCaptureIntentEnabled.mockImplementation(async (enabled: boolean) => ( |
| 66 | + enabled ? { enabled: true, token: TOKEN } : { enabled: false, token: null } |
| 67 | + )); |
| 68 | + clipboardMocks.setStringAsync.mockResolvedValue(true); |
| 69 | + }); |
| 70 | + |
| 71 | + it('loads off, enables, copies the selectable token, and disables with native-confirmed state', async () => { |
| 72 | + const tree = await renderSection(); |
| 73 | + expect(tree.root.findByProps({ testID: 'android-capture-intent-section' })).toBeTruthy(); |
| 74 | + expect(tree.root.findByProps({ testID: 'android-capture-intent-switch' }).props).toMatchObject({ |
| 75 | + value: false, |
| 76 | + disabled: false, |
| 77 | + }); |
| 78 | + |
| 79 | + await act(async () => { |
| 80 | + tree.root.findByProps({ testID: 'android-capture-intent-switch' }).props.onValueChange(true); |
| 81 | + await settle(); |
| 82 | + }); |
| 83 | + expect(nativeMocks.setCaptureIntentEnabled).toHaveBeenCalledWith(true); |
| 84 | + expect(tree.root.findByProps({ testID: 'android-capture-intent-token' }).props).toMatchObject({ |
| 85 | + selectable: true, |
| 86 | + children: TOKEN, |
| 87 | + }); |
| 88 | + |
| 89 | + await act(async () => { |
| 90 | + tree.root.findByProps({ testID: 'android-capture-intent-copy' }).props.onPress(); |
| 91 | + await settle(); |
| 92 | + }); |
| 93 | + expect(clipboardMocks.setStringAsync).toHaveBeenCalledWith(TOKEN); |
| 94 | + expect(showToast).toHaveBeenCalledWith({ message: 'Capture token copied.', tone: 'info' }); |
| 95 | + |
| 96 | + await act(async () => { |
| 97 | + tree.root.findByProps({ testID: 'android-capture-intent-switch' }).props.onValueChange(false); |
| 98 | + await settle(); |
| 99 | + }); |
| 100 | + expect(nativeMocks.setCaptureIntentEnabled).toHaveBeenLastCalledWith(false); |
| 101 | + expect(tree.root.findByProps({ testID: 'android-capture-intent-switch' }).props.value).toBe(false); |
| 102 | + expect(tree.root.findAllByProps({ testID: 'android-capture-intent-token' })).toHaveLength(0); |
| 103 | + }); |
| 104 | + |
| 105 | + it('keeps confirmed state when a native mutation fails and surfaces the error', async () => { |
| 106 | + nativeMocks.getCaptureIntentConfig.mockResolvedValue({ enabled: true, token: TOKEN }); |
| 107 | + nativeMocks.setCaptureIntentEnabled.mockRejectedValue(new Error('disk full')); |
| 108 | + const tree = await renderSection(); |
| 109 | + |
| 110 | + await act(async () => { |
| 111 | + tree.root.findByProps({ testID: 'android-capture-intent-switch' }).props.onValueChange(false); |
| 112 | + await settle(); |
| 113 | + }); |
| 114 | + |
| 115 | + expect(tree.root.findByProps({ testID: 'android-capture-intent-switch' }).props.value).toBe(true); |
| 116 | + expect(showToast).toHaveBeenCalledWith({ |
| 117 | + message: "Couldn't update automation capture settings.", |
| 118 | + tone: 'error', |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + it('disables unknown state and surfaces a config load failure', async () => { |
| 123 | + nativeMocks.getCaptureIntentConfig.mockRejectedValue(new Error('corrupt')); |
| 124 | + const tree = await renderSection(); |
| 125 | + |
| 126 | + expect(tree.root.findByProps({ testID: 'android-capture-intent-switch' }).props.disabled).toBe(true); |
| 127 | + expect(showToast).toHaveBeenCalledWith({ |
| 128 | + message: "Couldn't load automation capture settings.", |
| 129 | + tone: 'error', |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + it('omits the section when the optional Android native module is unavailable', async () => { |
| 134 | + nativeMocks.isSupported.mockReturnValue(false); |
| 135 | + const tree = await renderSection(); |
| 136 | + |
| 137 | + expect(tree.toJSON()).toBeNull(); |
| 138 | + expect(nativeMocks.getCaptureIntentConfig).not.toHaveBeenCalled(); |
| 139 | + }); |
| 140 | +}); |
0 commit comments