Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
37 changes: 20 additions & 17 deletions src/renderer/src/components/mobile/MobilePage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ vi.mock('./MobilePageContent', () => ({
Continue
</button>
<button type="button" onClick={() => props.handleConnectionModeChange('automatic')}>
Anywhere
Orca Relay
</button>
<button type="button" onClick={() => props.handleConnectionModeChange('local-only')}>
Local only
Local network
</button>
</div>
)
Expand Down Expand Up @@ -110,46 +110,49 @@ describe('MobilePage pairing connection mode', () => {
await user.click(screen.getByRole('button', { name: 'Continue' }))
}

it('defaults signed-in pairing to local-only and rotates when Relay is selected', async () => {
it('defaults signed-in pairing to Anywhere and rotates when same-network is selected', async () => {
const user = userEvent.setup()
await openPairingStep()

await waitFor(() => expect(getPairingQR).toHaveBeenCalledWith({ connectionMode: 'local-only' }))
await waitFor(() => expect(getPairingQR).toHaveBeenCalledWith({ connectionMode: 'automatic' }))
await waitFor(() => expect(screen.getByTestId('pairing-qr')).toHaveTextContent('base64,qr'))
expect(screen.getByTestId('mode')).toHaveTextContent('local-only')
expect(screen.getByTestId('mode')).toHaveTextContent('automatic')

let resolveRelayQr: ((value: Record<string, unknown>) => void) | undefined
let resolveLocalQr: ((value: Record<string, unknown>) => void) | undefined
getPairingQR.mockImplementationOnce(
() =>
new Promise((resolve) => {
resolveRelayQr = resolve
resolveLocalQr = resolve
})
)
await user.click(screen.getByRole('button', { name: 'Anywhere' }))
await user.click(screen.getByRole('button', { name: 'Local network' }))
await waitFor(() =>
expect(getPairingQR).toHaveBeenLastCalledWith({
connectionMode: 'automatic',
connectionMode: 'local-only',
rotate: true
})
)
expect(screen.getByTestId('mode')).toHaveTextContent('automatic')
expect(screen.getByTestId('mode')).toHaveTextContent('local-only')
expect(screen.getByTestId('pairing-qr')).toHaveTextContent('base64,qr')
expect(screen.getByTestId('pairing-url')).toHaveTextContent('none')
expect(mocks.storeState.updateSettings).toHaveBeenCalledWith({
mobilePairingConnectionMode: 'local-only'
})

resolveRelayQr?.({
resolveLocalQr?.({
available: true,
qrDataUrl: 'data:image/png;base64,relay-qr',
pairingUrl: 'orca://pair#relay'
qrDataUrl: 'data:image/png;base64,local-qr',
pairingUrl: 'orca://pair#local'
})
await waitFor(() => expect(screen.getByTestId('pairing-qr')).toHaveTextContent('relay-qr'))
await waitFor(() => expect(screen.getByTestId('pairing-qr')).toHaveTextContent('local-qr'))
})

it('defaults signed-out pairing to local-only', async () => {
it('defaults signed-out UI to Orca Relay but only issues a local-only QR', async () => {
mocks.storeState.orcaProfileAuthStatus = { state: 'local' }
await openPairingStep()

await waitFor(() => expect(getPairingQR).toHaveBeenCalledWith({ connectionMode: 'local-only' }))
expect(screen.getByTestId('mode')).toHaveTextContent('local-only')
expect(screen.getByTestId('mode')).toHaveTextContent('automatic')
})

it('removes the old QR if policy rotation fails', async () => {
Expand All @@ -158,7 +161,7 @@ describe('MobilePage pairing connection mode', () => {
await waitFor(() => expect(screen.getByTestId('pairing-qr')).toHaveTextContent('base64,qr'))

getPairingQR.mockRejectedValueOnce(new Error('rotation failed'))
await user.click(screen.getByRole('button', { name: 'Anywhere' }))
await user.click(screen.getByRole('button', { name: 'Local network' }))

await waitFor(() => expect(screen.getByTestId('pairing-qr')).toHaveTextContent('none'))
expect(screen.getByTestId('pairing-url')).toHaveTextContent('none')
Expand Down
35 changes: 21 additions & 14 deletions src/renderer/src/components/mobile/MobilePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ import { translate } from '@/i18n/i18n'
import { useMobilePageEscape } from './use-mobile-page-escape'
import { MobilePageContent } from './MobilePageContent'
import { useMobileInstallQr } from './use-mobile-install-qr'
import type { MobilePairingConnectionMode } from '../../../../shared/mobile-pairing-connection-mode'
import {
effectiveMobilePairingConnectionMode,
resolveMobilePairingConnectionMode,
type MobilePairingConnectionMode
} from '../../../../shared/mobile-pairing-connection-mode'
import { useMobileInstallActions } from './use-mobile-install-actions'

export default function MobilePage(): React.JSX.Element {
Expand All @@ -33,9 +37,12 @@ export default function MobilePage(): React.JSX.Element {
const [pairingUrl, setPairingUrl] = useState<string | null>(null)
const [pairLoading, setPairLoading] = useState(false)
const signedIn = useAppStore((state) => state.orcaProfileAuthStatus?.state === 'connected')
// Why: Relay is opt-in while compatible mobile builds are limited to the
// TestFlight preview and Android APK.
const [connectionMode, setConnectionMode] = useState<MobilePairingConnectionMode>('local-only')
const savedConnectionMode = useAppStore((s) => s.settings?.mobilePairingConnectionMode)
// Why: Anywhere is the default; an explicit saved `local-only` is the
// "user already set it up otherwise" signal.
const [connectionMode, setConnectionMode] = useState<MobilePairingConnectionMode>(() =>
resolveMobilePairingConnectionMode(savedConnectionMode)
)
const [networkInterfaces, setNetworkInterfaces] = useState<MobileNetworkInterface[]>([])
const [selectedAddress, setSelectedAddress] = useState<string | undefined>(undefined)
// Why: tracks whether `selectedAddress` came from the user typing a
Expand All @@ -48,7 +55,6 @@ export default function MobilePage(): React.JSX.Element {
const [deviceCountAtPairStart, setDeviceCountAtPairStart] = useState<number | null>(null)
const hasGeneratedRef = useRef(false)
const pairingRequestIdRef = useRef(0)
const wasSignedInRef = useRef(signedIn)
const mountedRef = useMountedRef()
const stageRef = useRef<FlowStage | null>(null)
const deviceCountAtPairStartRef = useRef<number | null>(null)
Expand Down Expand Up @@ -186,7 +192,11 @@ export default function MobilePage(): React.JSX.Element {
}
try {
const address = addressOverride ?? selectedAddress
const nextConnectionMode = connectionModeOverride ?? connectionMode
const preferredMode = connectionModeOverride ?? connectionMode
const nextConnectionMode = effectiveMobilePairingConnectionMode({
preferred: preferredMode,
signedIn
})
const result = await window.api.mobile.getPairingQR({
...(address ? { address } : {}),
connectionMode: nextConnectionMode,
Expand Down Expand Up @@ -231,7 +241,7 @@ export default function MobilePage(): React.JSX.Element {
}
}
},
[connectionMode, mountedRef, selectedAddress]
[connectionMode, mountedRef, selectedAddress, signedIn]
)

const handleConnectionModeChange = useCallback(
Expand All @@ -240,6 +250,7 @@ export default function MobilePage(): React.JSX.Element {
return
}
setConnectionMode(nextMode)
void updateSettings({ mobilePairingConnectionMode: nextMode })
// Why: an offer encodes its connection policy. Invalidate the prior
// request before rotating so a late response cannot restore a stale QR.
pairingRequestIdRef.current += 1
Expand All @@ -250,16 +261,12 @@ export default function MobilePage(): React.JSX.Element {
void generatePairing(true, undefined, nextMode)
}
},
[connectionMode, generatePairing, pairLoading]
[connectionMode, generatePairing, pairLoading, updateSettings]
)

useEffect(() => {
const wasSignedIn = wasSignedInRef.current
wasSignedInRef.current = signedIn
if (wasSignedIn && !signedIn) {
handleConnectionModeChange('local-only')
}
}, [handleConnectionModeChange, signedIn])
setConnectionMode(resolveMobilePairingConnectionMode(savedConnectionMode))
}, [savedConnectionMode])

const loadNetworkInterfaces = useCallback(async () => {
if (mountedRef.current) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type MobileRelayStoreState = {
orcaProfileAuthStatus: OrcaProfileAuthStatus | null
orcaProfileConnecting: boolean
connectCurrentOrcaProfile: () => Promise<null>
fetchOrcaProfileAuthStatus: () => Promise<OrcaProfileAuthStatus | null>
}

const mocks = vi.hoisted(() => ({
Expand All @@ -30,10 +31,12 @@ vi.mock('../../i18n/i18n', () => ({
describe('MobilePairingConnectionOptions', () => {
let statusListener: ((status: MobileRelayStatus) => void) | null
const connect = vi.fn().mockResolvedValue(null)
const fetchAuthStatus = vi.fn().mockResolvedValue(null)

beforeEach(() => {
statusListener = null
connect.mockClear()
fetchAuthStatus.mockClear()
Object.defineProperty(window, 'api', {
configurable: true,
value: {
Expand All @@ -55,64 +58,60 @@ describe('MobilePairingConnectionOptions', () => {
persistence: 'none'
},
orcaProfileConnecting: false,
connectCurrentOrcaProfile: connect
connectCurrentOrcaProfile: connect,
fetchOrcaProfileAuthStatus: fetchAuthStatus
}
})

afterEach(() => cleanup())

it('offers local-only pairing while Relay requires sign-in', async () => {
it('shows a compact Sign in row when Orca Relay is selected and signed out', async () => {
const user = userEvent.setup()
render(<MobilePairingConnectionOptions value="local-only" onChange={vi.fn()} />)
const onChange = vi.fn()
render(<MobilePairingConnectionOptions value="automatic" onChange={onChange} />)

expect(screen.getByTestId('anywhere-sign-in-panel')).toBeVisible()
expect(screen.getByText('Sign in to use Orca Mobile Relay.')).toBeVisible()
// Why: do not surface build-setup diagnostics in the pairing flow.
expect(screen.queryByText(/not configured for this build/i)).toBeNull()

expect(screen.getByRole('switch', { name: /connect with Orca Relay/i })).toBeDisabled()
expect(screen.getByRole('switch', { name: /connect with Orca Relay/i })).not.toBeChecked()
await user.click(screen.getByRole('button', { name: 'Sign in' }))
expect(onChange).toHaveBeenCalledWith('automatic')
expect(connect).toHaveBeenCalledOnce()
})

it('selects either automatic fallback or local-only pairing when signed in', async () => {
mocks.state = {
orcaProfileAuthStatus: {
activeProfileId: 'profile-1',
configured: true,
state: 'connected',
persistence: 'encrypted'
},
orcaProfileConnecting: false,
connectCurrentOrcaProfile: connect
}
const onChange = vi.fn()
const user = userEvent.setup()
render(<MobilePairingConnectionOptions value="automatic" onChange={onChange} />)

await waitFor(() => expect(screen.getByText('Ready')).toBeVisible())
expect(screen.getByRole('switch', { name: /connect with Orca Relay/i })).toBeChecked()
await user.click(screen.getByRole('switch', { name: /connect with Orca Relay/i }))
expect(onChange).toHaveBeenCalledWith('local-only')
statusListener?.('standby')
await waitFor(() => expect(screen.getByText('Available')).toBeVisible())
it('hides Sign in when local network is selected', () => {
render(<MobilePairingConnectionOptions value="local-only" onChange={vi.fn()} />)
expect(screen.queryByTestId('anywhere-sign-in-panel')).toBeNull()
})

it('shows the Relay beta availability inline and opens both compatible mobile builds', async () => {
it('selects a path from the compact list', async () => {
const user = userEvent.setup()
render(<MobilePairingConnectionOptions value="local-only" onChange={vi.fn()} />)
const onChange = vi.fn()
render(<MobilePairingConnectionOptions value="local-only" onChange={onChange} />)

expect(screen.getByText('Beta')).toBeVisible()
expect(screen.getByText('Available on')).toBeVisible()
expect(
screen.getByText('Phone can be on cellular or any Wi‑Fi. Sign-in required.')
).toBeVisible()
expect(
screen.getByText('Phone must be on this Wi‑Fi or your Tailscale. No sign-in.')
).toBeVisible()

await user.click(screen.getByRole('button', { name: 'TestFlight' }))
expect(window.api.shell.openUrl).toHaveBeenCalledWith(
'https://testflight.apple.com/join/YjeGMQBA'
)
await user.click(screen.getByRole('radio', { name: /Orca Relay/i }))
expect(onChange).toHaveBeenCalledWith('automatic')
})

await user.click(screen.getByRole('button', { name: 'Android APK' }))
expect(window.api.shell.openUrl).toHaveBeenCalledWith(
'https://github.com/stablyai/orca/releases/download/mobile-android-v0.0.31/app-release.apk'
)
it('refreshes auth status when it is missing on mount', () => {
mocks.state = {
...mocks.state,
orcaProfileAuthStatus: null
}
render(<MobilePairingConnectionOptions value="automatic" onChange={vi.fn()} />)
expect(fetchAuthStatus).toHaveBeenCalledOnce()
expect(screen.getByTestId('anywhere-sign-in-panel')).toBeVisible()
})

it('keeps the compact onboarding choices structurally stable across modes', async () => {
it('shows relay status when signed in on Orca Relay', async () => {
mocks.state = {
orcaProfileAuthStatus: {
activeProfileId: 'profile-1',
Expand All @@ -121,18 +120,18 @@ describe('MobilePairingConnectionOptions', () => {
persistence: 'encrypted'
},
orcaProfileConnecting: false,
connectCurrentOrcaProfile: connect
connectCurrentOrcaProfile: connect,
fetchOrcaProfileAuthStatus: fetchAuthStatus
}
const props = { compact: true, onChange: vi.fn() }
const { rerender } = render(<MobilePairingConnectionOptions {...props} value="automatic" />)
const onChange = vi.fn()
const user = userEvent.setup()
render(<MobilePairingConnectionOptions value="automatic" onChange={onChange} />)

expect(screen.getByRole('switch', { name: /connect with Orca Relay/i })).toBeChecked()
expect(screen.getByText(/direct connection when available/i)).toBeVisible()
expect(screen.queryByText('Ready')).toBeNull()
await waitFor(() => expect(screen.getByText('Ready')).toBeVisible())
expect(screen.queryByTestId('anywhere-sign-in-panel')).toBeNull()

rerender(<MobilePairingConnectionOptions {...props} value="local-only" />)
expect(screen.getByRole('switch', { name: /connect with Orca Relay/i })).not.toBeChecked()
expect(screen.getByText(/direct connection when available/i)).toBeVisible()
expect(screen.queryByText(/without connecting this phone through Orca Relay/i)).toBeNull()
await user.click(screen.getByRole('radio', { name: /Local network/i }))
expect(onChange).toHaveBeenCalledWith('local-only')
statusListener?.('standby')
})
})
Loading
Loading