Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(mobile): 兼容旧版电脑的任务已读操作

Dashc651e6d588da
diff --git a/apps/mobile/src/__tests__/useSessionListActions.test.tsx b/apps/mobile/src/__tests__/useSessionListActions.test.tsx
new file mode 100644
index 0000000000..552e17c75f
--- /dev/null
+++ b/apps/mobile/src/__tests__/useSessionListActions.test.tsx
@@ -0,0 +1,114 @@
+// @vitest-environment jsdom
+import { act, createElement } from 'react';
+import { createRoot, type Root } from 'react-dom/client';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import { Alert } from 'react-native';
+import { useSessionListActions } from '@/session/useSessionListActions';
+import { loadSharedSessionScheduleIndex, resetScheduleIndexThrottleForTesting } from '@/session/scheduleIndex';
+import { createMobileMakerTransport, type RemoteInvoke } from '@/device-link/mobileMakerTransport';
+import type { RemoteSession } from '@/session/types';
+
+const mocks = vi.hoisted(() => ({ invoke: vi.fn() }));
+vi.mock('react-native', () => ({ Alert: { alert: vi.fn() } }));
+vi.mock('react-i18next', async (original) => ({
+ ...await original<typeof import('react-i18next')>(),
+ useTranslation: () => ({ t: (key: string) => key }),
+}));
+vi.mock('@/device-link/DeviceLinkContext', () => ({ useDeviceLink: () => ({ invoke: mocks.invoke }) }));
+vi.mock('@/device-link/remoteStatus', () => ({ humanizeRemoteError: (error: Error) => error.message }));
+vi.mock('@/session/newSessionCreation', () => ({ withNewSessionCreationRemoval: vi.fn() }));
+
+// Unit, PR #6980 review: invoke the real hook, transport, device resolver and
+// schedule-read orchestration. Gesture tests stub the callback and cannot catch
+// old-host alerts or requests sent to a stale shard. Minimum regression: a Host
+// rejects clear-attention with CHANNEL_NOT_ALLOWED; no failure alert is shown.
+Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true });
+let root: Root;
+let actions: ReturnType<typeof useSessionListActions>;
+const task = { id: 'task', deviceLinkDeviceId: 'old-shard', canonicalDeviceId: 'host' } as RemoteSession;
+function mount(preferredRpcDeviceId?: string) {
+ function Probe() {
+ actions = useSessionListActions({ preferredRpcDeviceId });
+ return null;
+ }
+ act(() => root.render(createElement(Probe)));
+}
+async function markRead(session = task) {
+ await act(async () => { actions.sessionSwipeControls.onMarkRead(session); });
+}
+beforeEach(() => {
+ vi.clearAllMocks();
+ resetScheduleIndexThrottleForTesting();
+ mocks.invoke.mockImplementation(async (_device: string, channel: string) => {
+ if (channel === 'maker:schedule:list') return [];
+ if (channel === 'maker:schedule:list-sidebar-index-runs') return { runs: [] };
+ return undefined;
+ });
+ root = createRoot(document.createElement('div'));
+});
+afterEach(() => act(() => root.unmount()));
+
+describe('task list mark-read', () => {
+ it.each([undefined, 'route-host'])('writes ordinary task attention to the resolved Host (%s)', async (preferred) => {
+ const host = preferred ?? 'host';
+ await loadSharedSessionScheduleIndex(host, createMobileMakerTransport({ deviceId: host, invoke: mocks.invoke as RemoteInvoke }));
+ mocks.invoke.mockClear();
+ mount(preferred);
+ await markRead();
+ expect(mocks.invoke.mock.calls).toEqual([[host, 'notification:clear-session-attention', ['task', 'explicit']]]);
+ expect(Alert.alert).not.toHaveBeenCalled();
+ });
+
+ it.each([false, true])('also marks only the task’s unread automation runs (legacy attention: %s)', async (legacyAttention) => {
+ mocks.invoke.mockImplementation(async (_device: string, channel: string) => {
+ if (legacyAttention && channel === 'notification:clear-session-attention') {
+ throw Object.assign(new Error('old Host'), { code: 'CHANNEL_NOT_ALLOWED' });
+ }
+ if (channel === 'maker:schedule:list') return [{ id: 'schedule', name: 'Daily', status: 'active' }];
+ if (channel === 'maker:schedule:list-sidebar-index-runs') throw new Error('[CHANNEL_NOT_ALLOWED] old index');
+ if (channel === 'maker:schedule:list-runs') return [
+ { id: 'unread', scheduleId: 'schedule', sessionId: 'task', status: 'success', firedAt: 1 },
+ { id: 'other', scheduleId: 'schedule', sessionId: 'another', status: 'success', firedAt: 2 },
+ ];
+ });
+ mount();
+ await markRead({ ...task, source: 'scheduler' });
+ expect(mocks.invoke).toHaveBeenCalledWith('host', 'notification:clear-session-attention', ['task', 'explicit']);
+ expect(mocks.invoke.mock.calls.filter((call) => call[1] === 'maker:schedule:mark-run-read'))
+ .toEqual([['host', 'maker:schedule:mark-run-read', ['unread']]]);
+ expect(Alert.alert).not.toHaveBeenCalled();
+ });
+
+ it.each([
+ Object.assign(new Error('channel unavailable'), { code: 'CHANNEL_NOT_ALLOWED' }),
+ new Error('[DEVICE_LINK_CHANNEL_NOT_ALLOWED] old Host'),
+ ])('downgrades an unsupported attention channel without an alert (%s)', async (error) => {
+ mocks.invoke.mockImplementation(async (_device: string, channel: string) => {
+ if (channel === 'notification:clear-session-attention') throw error;
+ if (channel === 'maker:schedule:list-sidebar-index-runs') return { runs: [] };
+ return [];
+ });
+ mount();
+ await markRead();
+ expect(Alert.alert).not.toHaveBeenCalled();
+ expect(mocks.invoke).toHaveBeenCalledWith('host', 'maker:schedule:list', []);
+ });
+
+ it('reports a missing Host before sending any request', async () => {
+ mount();
+ await markRead({ id: 'unknown' } as RemoteSession);
+ expect(mocks.invoke).not.toHaveBeenCalled();
+ expect(Alert.alert).toHaveBeenCalledWith('devices.list.alert.actionFailed', 'devices.list.error.sessionDeviceNotFound');
+ });
+
+ it('still reports real attention write failures', async () => {
+ mocks.invoke.mockImplementation(async (_device: string, channel: string) => {
+ if (channel === 'notification:clear-session-attention') throw new Error('permission denied');
+ if (channel === 'maker:schedule:list-sidebar-index-runs') return { runs: [] };
+ return [];
+ });
+ mount();
+ await markRead();
+ expect(Alert.alert).toHaveBeenCalledWith('devices.list.alert.actionFailed', 'permission denied');
+ });
+});
diff --git a/apps/mobile/src/session/useSessionListActions.ts b/apps/mobile/src/session/useSessionListActions.ts
index 6f3cd1ec18..23ee2c56e9 100644
--- a/apps/mobile/src/session/useSessionListActions.ts
+++ b/apps/mobile/src/session/useSessionListActions.ts
@@ -4,7 +4,7 @@ import { useTranslation } from 'react-i18next';
import { projectDraftSessionTitle } from '@cindy/maker-shared/session-title';
import { useDeviceLink } from '@/device-link/DeviceLinkContext';
import { humanizeRemoteError } from '@/device-link/remoteStatus';
-import { isExplicitRemoteNotFoundError } from '@/session/newSessionWorktree';
+import { isExplicitRemoteNotFoundError, isWorktreeChannelNotAllowedError } from '@/session/newSessionWorktree';
import { createMobileMakerTransport } from '@/device-link/mobileMakerTransport';
import { markSessionScheduleRunsRead } from '@/session/scheduleRunRead';
import { sessionMayHaveScheduleRuns } from '@/session/scheduleIndex';
@@ -265,7 +265,10 @@ export function useSessionListActions(options?: {
return;
}
const maker = createMobileMakerTransport({ deviceId: devices.rpcDeviceId, invoke });
- const writes: Promise<unknown>[] = [maker.clearSessionAttention(session.id, 'explicit')];
+ const writes: Promise<unknown>[] = [maker.clearSessionAttention(session.id, 'explicit').catch((err: unknown) => {
+ // Older Hosts lack this channel. Preserve schedule-read below and surface real failures.
+ if (!isWorktreeChannelNotAllowedError(err)) throw err;
+ })];
if (sessionMayHaveScheduleRuns(devices.rpcDeviceId, session.id, session.source)) {
writes.push(markSessionScheduleRunsRead(maker, session.id, devices.rpcDeviceId));
}