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): 清理旧伙伴导航栈并补足菜单交互验证

nana95a9a9d2d5a5
diff --git a/apps/mobile/src/__tests__/homeChromeMenusAndroid.test.ts b/apps/mobile/src/__tests__/homeChromeMenusAndroid.test.ts
index 16e6c6589e..f533c21691 100644
--- a/apps/mobile/src/__tests__/homeChromeMenusAndroid.test.ts
+++ b/apps/mobile/src/__tests__/homeChromeMenusAndroid.test.ts
@@ -17,6 +17,8 @@ import { buildPullDownMenuSections, resolvePullDownSubmenu } from "@/platform/ch
import {
buildHomeDisplayPullDownActions,
buildHomeScopePullDownActions,
+ openHomeRemoteCollection,
+ parseHomeScopePullDownAction,
} from "@/session/homeChromeMenus";
const readSource = (path: string) =>
@@ -137,16 +139,26 @@ describe("Android home chrome menus follow the iOS pull-down", () => {
});
it("opens Teammates from both scope menus through the teammate home path", () => {
- const home = readSource("src/session/HomeSurface.tsx");
- const sharedHandler = home.slice(
- home.indexOf("const openRemoteCollection"),
- home.indexOf("const homeScopePullDownActions"),
- );
- expect(sharedHandler).toContain("collection.id === TEAMMATE_COLLECTION_ID");
- expect(sharedHandler).toContain("void homeNavigation.setMode('teammates')");
- expect(sharedHandler).toContain("guardedPush('/devices')");
- expect(sharedHandler).toContain("onModeChange?.('teammates')");
- expect(home.match(/openRemoteCollection\(collection\)/g)).toHaveLength(2);
+ const collections = [
+ { id: "teammates", title: "Teammates", resourceKind: "bot", targets: [{ deviceId: "mac", deviceName: "Mac" }] },
+ { id: "tools", title: "Tools", resourceKind: "tool", targets: [{ deviceId: "mac", deviceName: "Mac" }] },
+ ];
+ const actions = buildHomeScopePullDownActions([], "All", collections);
+ const teammateAction = actions.find(action => action.id === "scope.collection:teammates")!;
+ const genericAction = actions.find(action => action.id === "scope.collection:tools")!;
+ const setMode = vi.fn(); const push = vi.fn(); const onModeChange = vi.fn();
+ for (const action of [teammateAction, genericAction]) {
+ const parsed = parseHomeScopePullDownAction(action.id);
+ const collection = parsed.kind === "collection" ? collections.find(item => item.id === parsed.collectionId) : undefined;
+ expect(collection).toBeDefined();
+ openHomeRemoteCollection({ collection: collection!, teammateCollectionId: "teammates", embedded: true, setMode, push, onModeChange });
+ }
+ expect(setMode).toHaveBeenCalledExactlyOnceWith("teammates");
+ expect(push).toHaveBeenNthCalledWith(1, "/devices");
+ expect(push).toHaveBeenNthCalledWith(2, expect.objectContaining({ pathname: "/resources/[collectionId]", params: expect.objectContaining({ collectionId: "tools", targets: JSON.stringify(collections[1].targets) }) }));
+ expect(onModeChange).not.toHaveBeenCalled();
+ openHomeRemoteCollection({ collection: collections[0], teammateCollectionId: "teammates", embedded: false, setMode, push, onModeChange });
+ expect(onModeChange).toHaveBeenCalledExactlyOnceWith("teammates");
});
it("routes settings pickers and local-log options through the pull-down on Android", () => {
diff --git a/apps/mobile/src/__tests__/useTeammateNavigation.test.tsx b/apps/mobile/src/__tests__/useTeammateNavigation.test.tsx
index a3347d7d13..2fbb68e5ab 100644
--- a/apps/mobile/src/__tests__/useTeammateNavigation.test.tsx
+++ b/apps/mobile/src/__tests__/useTeammateNavigation.test.tsx
@@ -4,11 +4,11 @@ import { createRoot, type Root } from 'react-dom/client';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import type { MobileUser } from '@/auth/AuthContext';
const h = vi.hoisted(() => ({
- disk: new Map<string, string>(), push: vi.fn(), dismissTo: vi.fn(), dismiss: vi.fn(), accountGeneration: 1,
+ disk: new Map<string, string>(), push: vi.fn(), dismissTo: vi.fn(), dismiss: vi.fn(), reset: vi.fn(), accountGeneration: 1,
user: { id: 'account', passportId: 'passport', membershipKind: 'personal', orgId: null } as MobileUser,
get: vi.fn(), set: vi.fn(), routes: [] as Array<{ name: string; params?: unknown }>,
}));
-vi.mock('expo-router', () => ({ useRouter: () => ({ dismissTo: h.dismissTo, dismiss: h.dismiss }), useNavigation: () => ({ getState: () => ({ routes: h.routes, index: h.routes.length - 1 }) }) }));
+vi.mock('expo-router', () => ({ useRouter: () => ({ dismissTo: h.dismissTo, dismiss: h.dismiss }), useNavigation: () => ({ reset: h.reset, getState: () => ({ routes: h.routes, index: h.routes.length - 1 }) }) }));
vi.mock('react-native', () => ({ Keyboard: { dismiss() {} } }));
vi.mock('react-i18next', () => ({ useTranslation: () => ({ i18n: { language: 'en' } }) }));
vi.mock('@/auth/AuthContext', () => ({ useAuth: () => ({ user: h.user, accountGeneration: h.accountGeneration }) }));
@@ -18,6 +18,7 @@ import { useTeammateNavigation } from '@/session/useTeammateNavigation';
import { homeNavigationOwner } from '@/session/useHomeMode';
import { readHomeNavigationPreferences, saveHomeNavigationPreferences } from '@/session/homeViewPreferenceStore';
import { teammateIdentity } from '@/session/teammateNavigation';
+import { StackRouter, StackActions } from 'expo-router/build/react-navigation/routers/StackRouter';
const teammate = { key: 'mac:writer', host: { deviceId: 'mac', deviceName: 'My Mac' }, item: {
ref: { collectionId: 'teammates', kind: 'bot', id: 'writer' }, revision: '1', display: { title: 'Writer' }, links: [],
} };
@@ -141,16 +142,35 @@ it.each([
await act(async () => result.chooseMode('teammates'));
expect(h.dismiss).toHaveBeenCalledExactlyOnceWith(1);
expect(h.dismissTo).not.toHaveBeenCalled();
+ expect(h.reset).not.toHaveBeenCalled();
});
-it('replaces a retired teammate collection entry instead of returning to it', async () => {
- h.routes = [
+it.each(['tasks', 'teammates'] as const)('removes the retired stack before returning to %s', async mode => {
+ const router = StackRouter({ initialRouteName: 'resources/[collectionId]' });
+ const options = { routeNames: ['resources/[collectionId]', 'sessions/[sessionId]', 'devices/index'],
+ routeParamList: { 'resources/[collectionId]': { collectionId: 'teammates' } }, routeGetIdList: {}, routeKeyChanges: [] };
+ let state = router.getInitialState(options);
+ state = router.getStateForAction(state, StackActions.push('sessions/[sessionId]'), options)! as typeof state;
+ h.routes = state.routes;
+ h.reset.mockImplementationOnce(next => { state = router.getRehydratedState({ ...next, stale: true }, options) as typeof state; });
+ await render();
+ await act(async () => result.chooseMode(mode));
+ expect(h.dismiss).not.toHaveBeenCalled();
+ expect(h.dismissTo).not.toHaveBeenCalled();
+ expect(state.routes.map(route => route.name)).toEqual(['devices/index']);
+ expect(router.getStateForAction(state, StackActions.pop(), options)).toBeNull();
+});
+it('preserves earlier unrelated routes when removing multiple retired entries', async () => {
+ const prior = { name: 'resources/[collectionId]', params: { collectionId: 'tools', targets: 'hosts' } };
+ h.routes = [prior,
+ { name: 'resources/[collectionId]', params: { collectionId: 'teammates' } },
+ { name: 'sessions/[sessionId]' },
{ name: 'resources/[collectionId]', params: { collectionId: 'teammates' } },
{ name: 'sessions/[sessionId]' },
];
await render();
- await act(async () => result.chooseMode('teammates'));
- expect(h.dismiss).not.toHaveBeenCalled();
- expect(h.dismissTo).toHaveBeenCalledWith('/devices');
+ await act(async () => result.chooseMode('tasks'));
+ expect(h.reset).toHaveBeenCalledExactlyOnceWith({ index: 1, routes: [prior, { name: 'devices/index' }] });
+ expect(h.dismissTo).not.toHaveBeenCalled();
});
it('opens a known conversation directly and seeds its first-frame identity', async () => {
const { readRemoteCollectionCache } = await import('@/device-link/remoteResourceAvailability');
diff --git a/apps/mobile/src/session/HomeSurface.tsx b/apps/mobile/src/session/HomeSurface.tsx
index cf22e9b1d5..cc27a3f2ef 100644
--- a/apps/mobile/src/session/HomeSurface.tsx
+++ b/apps/mobile/src/session/HomeSurface.tsx
@@ -105,6 +105,7 @@ import {
type HomeDisplayPatch,
buildHomeScopePullDownActions,
parseHomeScopePullDownAction,
+ openHomeRemoteCollection,
} from '@/session/homeChromeMenus';
import { useConversationSearchFilterMenu } from '@/session/useConversationSearchFilterMenu';
import { AnchoredPullDownMenu } from '@/platform/chrome/AnchoredPullDownMenu';
@@ -2781,22 +2782,9 @@ function HomeScreenContent({ active = true, onModeChange, width, newSessionInSys
const nativeHomeMenus = usesNativePullDownMenu();
const openRemoteCollection = useCallback((collection: RemoteHomeCollection) => {
- if (collection.id === TEAMMATE_COLLECTION_ID) {
- if (embedded) {
- void homeNavigation.setMode('teammates');
- guardedPush('/devices');
- } else {
- onModeChange?.('teammates');
- }
- return;
- }
- guardedPush({
- pathname: '/resources/[collectionId]',
- params: {
- collectionId: collection.id,
- title: collection.title,
- targets: serializeRemoteResourceTargets(collection.targets),
- },
+ openHomeRemoteCollection({
+ collection, teammateCollectionId: TEAMMATE_COLLECTION_ID, embedded,
+ setMode: homeNavigation.setMode, push: guardedPush, onModeChange,
});
}, [embedded, guardedPush, homeNavigation.setMode, onModeChange]);
const homeScopePullDownActions = useMemo(
diff --git a/apps/mobile/src/session/TeammateHomeScreen.tsx b/apps/mobile/src/session/TeammateHomeScreen.tsx
index 88b9033899..a3cdd84b3a 100644
--- a/apps/mobile/src/session/TeammateHomeScreen.tsx
+++ b/apps/mobile/src/session/TeammateHomeScreen.tsx
@@ -149,7 +149,7 @@ const makeStyles = (colors: ThemeColors) => StyleSheet.create({
navDot: { position: 'absolute', top: 6, right: 6, width: 8, height: 8, borderRadius: radius.pill, backgroundColor: colors.textPrimary,
borderWidth: 2, borderColor: colors.surface },
titleMenu: { flex: 1 },
- titleTrigger: { alignItems: 'center', flex: 1, justifyContent: 'center' },
+ titleTrigger: { alignItems: 'center', flex: 1, justifyContent: 'center', minHeight: navigationChrome.target, minWidth: navigationChrome.target },
titleCluster: { alignItems: 'center', flexDirection: 'row', gap: spacing.xs },
title: { color: colors.textPrimary, fontSize: typeScale.title, lineHeight: lineHeight.title, fontWeight: fontWeight.semibold },
pressed: { opacity: 0.72 },
diff --git a/apps/mobile/src/session/homeChromeMenus.ts b/apps/mobile/src/session/homeChromeMenus.ts
index 2a170df7e7..c502516a27 100644
--- a/apps/mobile/src/session/homeChromeMenus.ts
+++ b/apps/mobile/src/session/homeChromeMenus.ts
@@ -22,6 +22,7 @@ import {
type HomeTaskInfoField,
type HomeVendorFilter,
} from "@/session/homeDisplaySettings";
+import { serializeRemoteResourceTargets, type RemoteHomeCollection } from "@/device-link/remoteResources";
export type { HomeListSortBy, HomeProjectOrder, HomeStatusFilter };
@@ -69,6 +70,40 @@ function checkable(
export const HOME_SCOPE_COLLECTION_PREFIX = "scope.collection:";
+export function openHomeRemoteCollection({
+ collection,
+ teammateCollectionId,
+ embedded,
+ setMode,
+ push,
+ onModeChange,
+}: {
+ collection: RemoteHomeCollection;
+ teammateCollectionId: string;
+ embedded: boolean;
+ setMode(mode: "teammates"): void | Promise<void>;
+ push(href: string | { pathname: string; params: Record<string, string> }): void;
+ onModeChange?: (mode: "teammates") => void;
+}) {
+ if (collection.id === teammateCollectionId) {
+ if (embedded) {
+ void setMode("teammates");
+ push("/devices");
+ } else {
+ onModeChange?.("teammates");
+ }
+ return;
+ }
+ push({
+ pathname: "/resources/[collectionId]",
+ params: {
+ collectionId: collection.id,
+ title: collection.title,
+ targets: serializeRemoteResourceTargets(collection.targets),
+ },
+ });
+}
+
export function parseHomeScopePullDownAction(
id: string,
): { kind: "select"; filterId: string } | { kind: "collection"; collectionId: string } {
diff --git a/apps/mobile/src/session/useTeammateNavigation.ts b/apps/mobile/src/session/useTeammateNavigation.ts
index f0ba880896..7cfb370a53 100644
--- a/apps/mobile/src/session/useTeammateNavigation.ts
+++ b/apps/mobile/src/session/useTeammateNavigation.ts
@@ -31,9 +31,19 @@ export function useTeammateNavigation() {
await preferences.setMode(mode);
if (mounted.current && current.current === accountGeneration) {
const state = navigation.getState();
- const count = homeDismissCount(state?.routes.slice(0, state.index + 1) ?? []);
+ const routes = state?.routes.slice(0, state.index + 1) ?? [];
+ const count = homeDismissCount(routes);
// Pop by position to preserve the mounted home's state.
- if (count === null) router.dismissTo('/devices');
+ if (count === null) {
+ const legacyIndex = routes.findIndex(route => route.name === 'resources/[collectionId]'
+ && (route.params as { collectionId?: string } | undefined)?.collectionId === 'teammates');
+ // POP_TO only replaces the top when home is absent, leaving the retired route underneath.
+ if (legacyIndex >= 0) navigation.reset({
+ index: legacyIndex,
+ routes: [...routes.slice(0, legacyIndex), { name: 'devices/index' }],
+ } as never);
+ else router.dismissTo('/devices');
+ }
else if (count > 0) router.dismiss(count);
}
} finally { if (pendingAccount.current === accountGeneration) pendingAccount.current = null; }