Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(mobile): 补齐标签原生页的返回键、读屏播报与交互测试
kirozengaffe6e50e830
diff --git a/apps/mobile/src/__tests__/taskTagsNative.test.tsx b/apps/mobile/src/__tests__/taskTagsNative.test.tsxnew file mode 100644index 0000000000..fb8931c8cd--- /dev/null+++ b/apps/mobile/src/__tests__/taskTagsNative.test.tsx@@ -0,0 +1,218 @@+// @vitest-environment jsdom+import React, { act } from 'react';+import { createRoot } from 'react-dom/client';+import { afterEach, beforeEach, expect, it, vi } from 'vitest';++const h = vi.hoisted(() => ({+ onMove: null as null | ((sources: number[], destination: number) => void),+ textField: null as any,+ announce: vi.fn(),+}));++vi.mock('react-native', () => ({+ AccessibilityInfo: { announceForAccessibility: h.announce },+}));+vi.mock('react-i18next', () => ({+ useTranslation: () => ({ t: (key: string) => key }),+}));+vi.mock('@/theme', () => ({+ iconSize: { xs: 12, sm: 14, lg: 18 },+ useTheme: () => ({ colors: {} }),+}));+vi.mock('@/platform/chrome/nativeGlassButtonStyle.ios', () => ({+ useNativeGlassButtonStyle: () => [],+}));+vi.mock('@/session/ComposerNativeSection', () => ({+ ComposerNativeSection: ({ children }: any) => <section>{children}</section>,+}));+vi.mock('@/session/ComposerNativeRow', () => ({+ ComposerNativeRow: ({ title, onPress, onOptions, testID }: any) => (+ <div>+ <button data-testid={testID} onClick={onPress}>+ {title}+ </button>+ {onOptions ? (+ <button data-testid={`${testID}.options`} onClick={onOptions}>+ options+ </button>+ ) : null}+ </div>+ ),+}));+vi.mock('@expo/ui/swift-ui', () => {+ const Box = ({ children }: any) => <div>{children}</div>;+ const List: any = Box;+ List.ForEach = ({ children, onMove }: any) => {+ h.onMove = onMove;+ return <div>{children}</div>;+ };+ return {+ Button: ({ children, onPress, testID }: any) => (+ <button data-testid={testID} onClick={onPress}>+ {children}+ </button>+ ),+ Circle: () => null,+ Group: Box,+ HStack: Box,+ Image: () => null,+ List,+ ProgressView: () => null,+ Section: Box,+ Text: ({ children }: any) => <span>{children}</span>,+ TextField: (props: any) => {+ h.textField = props;+ return null;+ },+ VStack: Box,+ ZStack: Box,+ useNativeState: (value: string) => ({ get: () => value, set: () => {} }),+ };+});+vi.mock('@expo/ui/swift-ui/modifiers', () => ({+ ...Object.fromEntries(+ [+ 'accessibilityAddTraits',+ 'accessibilityLabel',+ 'buttonStyle',+ 'contentShape',+ 'disabled',+ 'font',+ 'foregroundStyle',+ 'frame',+ 'listRowBackground',+ 'listRowInsets',+ 'listRowSeparator',+ 'moveDisabled',+ 'opacity',+ 'strokeBorder',+ 'tint',+ ].map((name) => [name, (value: unknown) => ({ [name]: value })]),+ ),+ shapes: { rectangle: () => ({}) },+}));++import { NativeTaskTagsExpanded } from '@/session/TaskTagsNative.ios';+import { taskTagMoveTarget } from '@/session/taskTagsNativePage';++const swatch = (id: string) => ({+ id,+ name: id,+ color: '#fff',+ checkColor: '#000',+ outlined: false,+ selected: false,+});+const baseState = (over: Record<string, unknown> = {}): any => ({+ page: 'list',+ tags: [swatch('a'), swatch('b'), swatch('c')],+ disabled: false,+ loading: false,+ canRetry: false,+ canAdd: true,+ onRetry: vi.fn(),+ onToggle: vi.fn(),+ onEdit: vi.fn(),+ onAdd: vi.fn(),+ onMove: vi.fn(),+ form: {+ name: 'Work',+ onNameChange: vi.fn(),+ locked: false,+ colors: ['red', 'blue'].map((value) => ({+ ...swatch(value),+ value,+ available: true,+ })),+ onColor: vi.fn(),+ hint: '',+ submitLabel: 'taskTags.save',+ canSubmit: true,+ onSubmit: vi.fn(),+ canDelete: true,+ onDelete: vi.fn(),+ },+ ...over,+});++let root: ReturnType<typeof createRoot>;+let node: HTMLDivElement;+const render = (state: any) =>+ act(async () => root.render(<NativeTaskTagsExpanded state={state} />));+const press = (testID: string) =>+ act(async () =>+ node.querySelector<HTMLButtonElement>(`[data-testid="${testID}"]`)!.click(),+ );++beforeEach(() => {+ (globalThis as any).IS_REACT_ACT_ENVIRONMENT = true;+ h.onMove = null;+ h.textField = null;+ h.announce.mockClear();+ node = document.createElement('div');+ root = createRoot(node);+});+afterEach(() => act(() => root.unmount()));++it('maps SwiftUI insertion points to the final index in both directions', () => {+ expect(taskTagMoveTarget(0, 3)).toBe(2);+ expect(taskTagMoveTarget(0, 2)).toBe(1);+ expect(taskTagMoveTarget(2, 0)).toBe(0);+ // Dropping a row where it already is must stay a no-op.+ expect(taskTagMoveTarget(1, 1)).toBe(1);+ expect(taskTagMoveTarget(1, 2)).toBe(1);+});++it('routes list drags, taps, edit and add through the shared state', async () => {+ const state = baseState();+ await render(state);+ act(() => h.onMove!([0], 3));+ expect(state.onMove).toHaveBeenLastCalledWith(0, 2);+ act(() => h.onMove!([2], 0));+ expect(state.onMove).toHaveBeenLastCalledWith(2, 0);+ await press('taskTags.tag.b');+ expect(state.onToggle).toHaveBeenCalledWith('b');+ await press('taskTags.tag.b.options');+ expect(state.onEdit).toHaveBeenCalledWith('b');+ await press('taskTags.add');+ expect(state.onAdd).toHaveBeenCalledTimes(1);+});++it('wires the editor name, colour, save and delete callbacks', async () => {+ const state = baseState({ page: 'edit' });+ await render(state);+ act(() => h.textField.onTextChange('Home'));+ expect(state.form.onNameChange).toHaveBeenCalledWith('Home');+ await press('taskTags.form.color.blue');+ expect(state.form.onColor).toHaveBeenCalledWith('blue');+ await press('taskTags.form.submit');+ expect(state.form.onSubmit).toHaveBeenCalledTimes(1);+ await press('taskTags.form.delete');+ expect(state.form.onDelete).toHaveBeenCalledTimes(1);+ expect(state.form.onSubmit).toHaveBeenCalledTimes(1);+});++it('keeps the add page free of the delete action', async () => {+ await render(baseState({ page: 'add', form: { ...baseState().form, canDelete: false } }));+ expect(node.querySelector('[data-testid="taskTags.form.delete"]')).toBeNull();+});++it('confirms deletion only from the confirmation page', async () => {+ const onConfirm = vi.fn();+ await render(baseState({ page: 'delete', deletion: { message: 'sure?', onConfirm } }));+ expect(node.querySelector('[data-testid="taskTags.form.submit"]')).toBeNull();+ await press('taskTags.delete.confirm');+ expect(onConfirm).toHaveBeenCalledTimes(1);+});++it('announces failures to VoiceOver once per message', async () => {+ await render(baseState());+ expect(h.announce).not.toHaveBeenCalled();+ await render(baseState({ message: 'taskTags.failed' }));+ expect(h.announce).toHaveBeenCalledTimes(1);+ expect(h.announce).toHaveBeenCalledWith('taskTags.failed');+ await render(baseState({ message: 'taskTags.failed' }));+ expect(h.announce).toHaveBeenCalledTimes(1);+ await render(baseState({ message: 'taskTags.offline' }));+ expect(h.announce).toHaveBeenLastCalledWith('taskTags.offline');+});diff --git a/apps/mobile/src/__tests__/taskTagsSheet.test.tsx b/apps/mobile/src/__tests__/taskTagsSheet.test.tsxnew file mode 100644index 0000000000..28fab33e6b--- /dev/null+++ b/apps/mobile/src/__tests__/taskTagsSheet.test.tsx@@ -0,0 +1,73 @@+// @vitest-environment jsdom+import React, { act } from 'react';+import { createRoot } from 'react-dom/client';+import { afterEach, beforeEach, expect, it, vi } from 'vitest';++const h = vi.hoisted(() => ({+ sheet: null as any,+ pageChange: null as null | ((page: string | null, back: () => void) => void),+}));++vi.mock('react-native', () => ({+ Platform: { OS: 'ios' },+ View: ({ children }: any) => <div>{children}</div>,+}));+vi.mock('react-i18next', () => ({+ useTranslation: () => ({ t: (key: string) => key }),+}));+vi.mock('react-native-safe-area-context', () => ({+ useSafeAreaInsets: () => ({ top: 0, bottom: 0 }),+}));+vi.mock('@/theme', () => ({+ useTheme: () => ({ colors: {} }),+ radius: {},+ spacing: {},+}));+vi.mock('@/session/SheetModal', () => ({ SheetModal: () => null }));+vi.mock('@/session/TaskTagsNative', () => ({ NativeTaskTagsExpanded: () => null }));+vi.mock('@/session/ComposerSheet', () => ({+ ComposerSheet: (props: any) => {+ h.sheet = props;+ return <div>{props.children}</div>;+ },+}));+vi.mock('@/session/TaskTags', () => ({+ TaskTagsPanel: ({ onPageChange }: any) => {+ h.pageChange = onPageChange;+ return null;+ },+}));++import { TaskTagsSheet } from '@/session/TaskTagsSheet';++let root: ReturnType<typeof createRoot>;+beforeEach(() => {+ (globalThis as any).IS_REACT_ACT_ENVIRONMENT = true;+ h.sheet = null;+ h.pageChange = null;+ root = createRoot(document.createElement('div'));+});+afterEach(() => act(() => root.unmount()));++it('lets the list close the sheet and the editor pages step back inside it', async () => {+ const onClose = vi.fn();+ await act(async () =>+ root.render(+ <TaskTagsSheet+ session={{ id: 's', canonicalDeviceId: 'host' } as any}+ visible+ onClose={onClose}+ onClosed={() => {}}+ />,+ ),+ );+ // The list is the first page the user sees, and it still needs a back arrow.+ expect(h.sheet.onBack).toBe(onClose);++ const back = vi.fn();+ await act(async () => h.pageChange!('edit', back));+ expect(h.sheet.onBack).not.toBe(onClose);+ h.sheet.onBack();+ expect(back).toHaveBeenCalledTimes(1);+ expect(onClose).not.toHaveBeenCalled();+});diff --git a/apps/mobile/src/session/TaskTagsNative.ios.tsx b/apps/mobile/src/session/TaskTagsNative.ios.tsxindex a7f495f3f8..fc1d1d0a88 100644--- a/apps/mobile/src/session/TaskTagsNative.ios.tsx+++ b/apps/mobile/src/session/TaskTagsNative.ios.tsx@@ -1,3 +1,5 @@+import { useEffect } from 'react';+import { AccessibilityInfo } from 'react-native';import { useTranslation } from 'react-i18next';import {Button,@@ -38,7 +40,7 @@ import { ComposerNativeSection as Section } from './ComposerNativeSection';import { ComposerNativeRow } from './ComposerNativeRow';import type { TaskTagsExpandedState } from './TaskTags';import type { NativeTagSwatchProps } from './TaskTagsNative';-import { TAG_SWATCH } from './taskTagsNativePage';+import { TAG_SWATCH, taskTagMoveTarget } from './taskTagsNativePage';/** One drawn swatch for every tag surface; the outline only rescues white tags. */export function NativeTagSwatch({ color, checkColor, outlined, selected, size }: NativeTagSwatchProps) {@@ -245,6 +247,11 @@ function TagForm({ state }: { state: TaskTagsExpandedState }) {/** SwiftUI rendering of the expanded tag pages: list, add / edit, delete confirmation. */export function NativeTaskTagsExpanded({ state }: { state: TaskTagsExpandedState }) {const { t } = useTranslation();+ // The failure text arrives asynchronously and focus does not move to it, so a footer+ // alone is silent in VoiceOver; the React Native panel exposed it as an alert.+ useEffect(() => {+ if (state.message) AccessibilityInfo.announceForAccessibility(state.message);+ }, [state.message]);if (state.page === 'delete' && state.deletion) {return (<>@@ -273,8 +280,7 @@ export function NativeTaskTagsExpanded({ state }: { state: TaskTagsExpandedStateonMove={(sources, destination) => {const from = sources[0];if (from === undefined) return;- // SwiftUI reports the insertion point before removing the moved row.- state.onMove(from, destination > from ? destination - 1 : destination);+ state.onMove(from, taskTagMoveTarget(from, destination));}}>{state.tags.map((tag) => (diff --git a/apps/mobile/src/session/TaskTagsSheet.tsx b/apps/mobile/src/session/TaskTagsSheet.tsxindex 2c017310f0..9717f90bbb 100644--- a/apps/mobile/src/session/TaskTagsSheet.tsx+++ b/apps/mobile/src/session/TaskTagsSheet.tsx@@ -70,8 +70,9 @@ export function TaskTagsSheet({<ComposerSheetvisible={visible}title={taskTagsPageTitle(page, t)}- // The list is the root of this sheet; only the editor pages step back.- onBack={page === 'list' ? undefined : tagPage.back}+ // This sheet opens from task details, so the list's back arrow closes it and lets+ // the parent reopen; the editor pages step back within the sheet.+ onBack={page === 'list' ? onClose : tagPage.back}backLabel={t('taskTags.back')}onClose={onClose}onClosed={onClosed}diff --git a/apps/mobile/src/session/taskTagsNativePage.ts b/apps/mobile/src/session/taskTagsNativePage.tsindex 461186262d..d8ce9464b8 100644--- a/apps/mobile/src/session/taskTagsNativePage.ts+++ b/apps/mobile/src/session/taskTagsNativePage.ts@@ -27,3 +27,11 @@ export function taskTagsPageTitle(page: TaskTagsPage, t: (key: string) => string: 'taskTags.title',);}++/**+ * SwiftUI `onMove` reports the insertion point in the array before the moved row is+ * removed, so moving down lands one slot earlier than the reported destination.+ */+export function taskTagMoveTarget(from: number, destination: number) {+ return destination > from ? destination - 1 : destination;+}