Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(desktop): 登出后设备目录停在终态,本地模式不再卡加载中 (#987)
DavidShenXD81a74e87a27d
diff --git a/apps/desktop/src/renderer/__tests__/deviceLinkDeviceListLoading.test.tsx b/apps/desktop/src/renderer/__tests__/deviceLinkDeviceListLoading.test.tsxindex e85aafba83..cb6cf8f498 100644--- a/apps/desktop/src/renderer/__tests__/deviceLinkDeviceListLoading.test.tsx+++ b/apps/desktop/src/renderer/__tests__/deviceLinkDeviceListLoading.test.tsx@@ -1,7 +1,13 @@// @vitest-environment jsdomimport { act, renderHook, waitFor } from '@testing-library/react';-import { afterEach, describe, expect, it, vi } from 'vitest';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';++// useDeviceLinkDeviceList 是模块级共享单例(devices / started / initialRequestSettled 都在模块作用域),+// 每个用例必须拿一份全新模块状态,否则上一个用例的 started=true 会让后面的用例不再拉取。+beforeEach(() => {+ vi.resetModules();+});afterEach(() => {vi.unstubAllGlobals();@@ -51,4 +57,102 @@ describe('useDeviceLinkDeviceList initial request', () => {await act(async () => resolveRetry?.({ devices: [] }));await waitFor(() => expect(result.current).toEqual({ devices: [], settled: true }));});++ // 回归 #797:云端登录 → 登出(relay 'stopped')→ 进入本地模式后不会再有 'online',+ // 设备目录必须停在终态,否则 shouldWaitForRemoteSessionBootstrap 恒为 true,+ // 侧栏「对话」分区会一直显示「加载中…」直到冷重启。+ it('settles the directory on relay stop and stays settled without a later online', async () => {+ let statusChanged:+ ((payload: { status: 'stopped' | 'connecting' | 'online' }) => void) | undefined;+ const listDevices = vi.fn().mockResolvedValue({+ devices: [+ {+ deviceId: 'dev-a',+ name: 'Mac A',+ platform: 'darwin',+ online: true,+ remoteControlEnabled: true,+ controlEnabled: true,+ isSelf: false,+ } as unknown as DeviceLinkDeviceView,+ ],+ });+ vi.stubGlobal('electronAPI', {+ deviceLink: {+ listDevices,+ onPresenceChanged: vi.fn(),+ onStatusChanged: vi.fn(+ (callback: (payload: { status: 'stopped' | 'connecting' | 'online' }) => void) => {+ statusChanged = callback;+ },+ ),+ onControlTargetChanged: vi.fn(),+ },+ });++ const { useDeviceLinkDeviceList, useDeviceLinkDeviceListSettled } =+ await import('@/features/device-link/useDeviceLinkDeviceList');+ const useProbe = (): { devices: DeviceLinkDeviceView[] | null; settled: boolean } => ({+ devices: useDeviceLinkDeviceList(),+ settled: useDeviceLinkDeviceListSettled(),+ });+ const { result, unmount } = renderHook(useProbe);+ await waitFor(() => expect(result.current.settled).toBe(true));+ expect(result.current.devices).toHaveLength(1);++ // 登出:清掉上一账号的远程机器(devices 回 null → 切换栏隐藏),但目录仍是终态。+ act(() => statusChanged?.({ status: 'stopped' }));+ expect(result.current).toEqual({ devices: null, settled: true });+ expect(listDevices).toHaveBeenCalledTimes(1);++ // 本地模式重挂侧栏(共享单例已 started)也不得退回未结算态。+ unmount();+ const local = renderHook(useProbe);+ expect(local.result.current).toEqual({ devices: null, settled: true });+ expect(listDevices).toHaveBeenCalledTimes(1);+ });++ it('re-enters loading when a cloud account logs back in after a relay stop', async () => {+ let statusChanged:+ ((payload: { status: 'stopped' | 'connecting' | 'online' }) => void) | undefined;+ let resolveRelogin: ((value: { devices: DeviceLinkDeviceView[] }) => void) | undefined;+ const relogin = new Promise<{ devices: DeviceLinkDeviceView[] }>((resolve) => {+ resolveRelogin = resolve;+ });+ const listDevices = vi+ .fn()+ .mockResolvedValueOnce({ devices: [] })+ .mockReturnValueOnce(relogin);+ vi.stubGlobal('electronAPI', {+ deviceLink: {+ listDevices,+ onPresenceChanged: vi.fn(),+ onStatusChanged: vi.fn(+ (callback: (payload: { status: 'stopped' | 'connecting' | 'online' }) => void) => {+ statusChanged = callback;+ },+ ),+ onControlTargetChanged: vi.fn(),+ },+ });++ const { useDeviceLinkDeviceList, useDeviceLinkDeviceListSettled } =+ await import('@/features/device-link/useDeviceLinkDeviceList');+ const { result } = renderHook(() => ({+ devices: useDeviceLinkDeviceList(),+ settled: useDeviceLinkDeviceListSettled(),+ }));+ await waitFor(() => expect(result.current).toEqual({ devices: [], settled: true }));++ act(() => statusChanged?.({ status: 'stopped' }));+ expect(result.current).toEqual({ devices: null, settled: true });++ // 重新登录:relay 'online' 重新拉取,首快照落地前照旧回到 loading。+ act(() => statusChanged?.({ status: 'online' }));+ expect(listDevices).toHaveBeenCalledTimes(2);+ expect(result.current).toEqual({ devices: null, settled: false });++ await act(async () => resolveRelogin?.({ devices: [] }));+ await waitFor(() => expect(result.current).toEqual({ devices: [], settled: true }));+ });});diff --git a/apps/desktop/src/renderer/features/device-link/useDeviceLinkDeviceList.ts b/apps/desktop/src/renderer/features/device-link/useDeviceLinkDeviceList.tsindex 1c11853032..10f9176cd8 100644--- a/apps/desktop/src/renderer/features/device-link/useDeviceLinkDeviceList.ts+++ b/apps/desktop/src/renderer/features/device-link/useDeviceLinkDeviceList.ts@@ -19,7 +19,11 @@ import { useSyncExternalStore } from 'react';let devices: DeviceLinkDeviceView[] | null = null;const subs = new Set<() => void>();let started = false;-/** 当前账号 / relay 生命周期内,至少一次最新的 listDevices 请求已经 resolve 或 reject。 */+/**+ * 设备目录是否已进入终态 —— 上层(shouldWaitForRemoteSessionBootstrap)据此决定还要不要等。+ * 两种终态:最新一次 listDevices 已 resolve / reject(见 setDevices / markInitialRequestSettled),+ * 或 relay 已停(登出 / 本地模式,见 clearDevices)。false 必须意味着「还有结果会来」。+ */let initialRequestSettled = false;/*** 加载代次:**每次 refresh 发起**与**每次清空(登出 / relay stop)**都自增,作废所有更早的在途@@ -84,13 +88,23 @@ function setDevices(next: DeviceLinkDeviceView[]): void {subs.forEach((fn) => fn());}-/** 清空缓存设备,回到「未加载」态(null → 切换栏隐藏)。登出 / relay 停止时调用。 */+/**+ * 清空缓存设备(null → 切换栏隐藏)。登出 / relay 停止时调用。+ *+ * `initialRequestSettled` **置 true**:link 已停 = 此刻确定没有可用的远端设备目录,而且在途+ * 请求刚被 loadGeneration 作废、不会再有结果落地 —— 这与 refresh 失败兜底同一个终态+ * (devices=null + settled=true)。若像早先那样置回 false,登出后进入本地模式 / 保持未登录+ * 就再也收不到 relay 'online' 来重新结算,shouldWaitForRemoteSessionBootstrap 恒为 true,+ * 侧栏「对话」分区卡在「加载中…」直到冷重启(#797)。+ * 重新登录不受影响:relay 'online' → refresh() 会因 `devices === null && initialRequestSettled`+ * 主动退回 loading,远程设备首快照的等待行为保持原样。+ */function clearDevices(): void {loadGeneration += 1; // 作废所有在途 listDevices 响应(见 loadGeneration 注释)。- const changed = devices !== null || initialRequestSettled;+ const changed = devices !== null || !initialRequestSettled;if (!changed) return;devices = null;- initialRequestSettled = false;+ initialRequestSettled = true;subs.forEach((fn) => fn());}