Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
57 changes: 57 additions & 0 deletions apps/mobile/__tests__/components/chat/chat-input-bar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ vi.mock('lucide-react-native', () => {
Image: icon('Image'),
Lock: icon('Lock'),
Mic: icon('Mic'),
Paperclip: icon('Paperclip'),
Square: icon('Square'),
ArrowUp: icon('ArrowUp'),
}
Expand Down Expand Up @@ -42,13 +43,15 @@ function buildProps(overrides: Record<string, unknown> = {}) {
atMessageLimit: false,
limitLocked: false,
selectedImagePresent: false,
selectedTextFilePresent: false,
transcript: '',
composerResetSignal: 0,
recordingTime: '0:00',
speechSupported: true,
onSend: vi.fn(),
onToggleRecording: vi.fn(),
onOpenFilePicker: vi.fn(),
onOpenTextFilePicker: vi.fn(),
...overrides,
}
}
Expand Down Expand Up @@ -87,3 +90,57 @@ describe('ChatInputBar voice transcript (mobile)', () => {
expect(findInputValue(tree!.root)).toBe('buy milk')
})
})

describe('ChatInputBar text-file attachment (mobile)', () => {
it('invokes the text-file picker from the attach-file control', async () => {
const onOpenTextFilePicker = vi.fn()
let tree: ReturnType<typeof TestRenderer.create>

await TestRenderer.act(async () => {
tree = TestRenderer.create(
<ChatInputBar {...buildProps({ onOpenTextFilePicker })} />,
)
await Promise.resolve()
})

const attachButton = tree!.root.findAll(
(node: { props?: Record<string, unknown> }) =>
!!node.props &&
node.props.accessibilityLabel === 'chat.attachFile' &&
typeof node.props.onPress === 'function',
)[0] as { props: { onPress: () => void } } | undefined

TestRenderer.act(() => {
attachButton?.props.onPress()
})

expect(onOpenTextFilePicker).toHaveBeenCalled()
})

it('allows sending when only a text file is attached', async () => {
const onSend = vi.fn()
let tree: ReturnType<typeof TestRenderer.create>

await TestRenderer.act(async () => {
tree = TestRenderer.create(
<ChatInputBar
{...buildProps({ selectedTextFilePresent: true, onSend })}
/>,
)
await Promise.resolve()
})

const sendButton = tree!.root.findAll(
(node: { props?: Record<string, unknown> }) =>
!!node.props &&
node.props.accessibilityLabel === 'chat.send' &&
typeof node.props.onPress === 'function',
)[0] as { props: { onPress: () => void } } | undefined

TestRenderer.act(() => {
sendButton?.props.onPress()
})

expect(onSend).toHaveBeenCalled()
})
})
52 changes: 52 additions & 0 deletions apps/mobile/__tests__/hooks/use-chat-composer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const mocks = vi.hoisted(() => {
queryClient,
apiClient: vi.fn(),
openChatStream: vi.fn(),
getDocumentAsync: vi.fn(),
routerPush: vi.fn(),
useQueryClient: vi.fn(() => queryClient),
}
Expand All @@ -51,6 +52,10 @@ vi.mock('expo-image-picker', () => ({
launchImageLibraryAsync: vi.fn(),
}))

vi.mock('expo-document-picker', () => ({
getDocumentAsync: mocks.getDocumentAsync,
}))

vi.mock('@/hooks/use-profile', () => ({
useProfile: () => ({ profile: mocks.state.profile }),
}))
Expand Down Expand Up @@ -142,6 +147,7 @@ describe('mobile useChatComposer', () => {
mocks.state.profile = undefined
mocks.apiClient.mockReset()
mocks.openChatStream.mockReset()
mocks.getDocumentAsync.mockReset()
mocks.routerPush.mockReset()
mocks.queryClient.invalidateQueries.mockClear()
mocks.queryClient.setQueryData.mockClear()
Expand Down Expand Up @@ -262,6 +268,52 @@ describe('mobile useChatComposer', () => {
expect(composer.current.sendError).toBe('You are offline')
})

it('reads an attached text file and folds it into the sent message', async () => {
mocks.getDocumentAsync.mockResolvedValue({
canceled: false,
assets: [
{ name: 'habits.csv', uri: 'file:///tmp/habits.csv', size: 12, mimeType: 'text/csv' },
],
})
mocks.openChatStream.mockResolvedValue(
sseStreamResponse(finalFrame(makeChatResponse({ aiMessage: 'Imported' }))),
)
const composer = await renderComposer()

await TestRenderer.act(async () => {
await composer.current.openTextFilePicker()
})
expect(composer.current.selectedTextFile?.name).toBe('habits.csv')

await TestRenderer.act(async () => {
await composer.current.sendMessage()
})

const userMessage = useChatStore
.getState()
.messages.find((message) => message.role === 'user')
expect(userMessage?.content).toContain('mock-file-content')
expect(userMessage?.content).toContain('chat.fileAttached')
expect(composer.current.selectedTextFile).toBeNull()
})

it('surfaces the i18n error for an unsupported attachment type', async () => {
mocks.getDocumentAsync.mockResolvedValue({
canceled: false,
assets: [
{ name: 'photo.png', uri: 'file:///tmp/photo.png', size: 12, mimeType: 'image/png' },
],
})
const composer = await renderComposer()

await TestRenderer.act(async () => {
await composer.current.openTextFilePicker()
})

expect(composer.current.sendError).toBe('chat.fileError')
expect(composer.current.selectedTextFile).toBeNull()
})

it('aborts an idle stream at the watchdog and arms retry with the timeout copy', async () => {
vi.useFakeTimers()
try {
Expand Down
32 changes: 32 additions & 0 deletions apps/mobile/app/chat.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,38 @@ export function createStyles(tokens: Tokens) {
alignItems: "center",
justifyContent: "center",
},
textFileChipRow: {
paddingBottom: 8,
},
textFileChip: {
flexDirection: "row",
alignItems: "center",
alignSelf: "flex-start",
maxWidth: "100%",
gap: 8,
paddingVertical: 8,
paddingHorizontal: 12,
borderRadius: 12,
backgroundColor: tokens.bgElev,
borderWidth: StyleSheet.hairlineWidth,
borderColor: tokens.hairline,
},
textFileChipText: {
flexShrink: 1,
fontFamily: 'Rubik_400Regular',
fontSize: 13,
color: tokens.fg2,
},
textFileChipRemove: {
width: 20,
height: 20,
borderRadius: 10,
borderWidth: StyleSheet.hairlineWidth,
borderColor: tokens.hairlineStrong,
backgroundColor: tokens.bgElev,
alignItems: "center",
justifyContent: "center",
},
quickChipsScroll: {
marginBottom: 12,
},
Expand Down
9 changes: 9 additions & 0 deletions apps/mobile/app/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ export default function ChatScreen() {
showSuggestions,
openFilePicker,
removeImage,
selectedTextFile,
openTextFilePicker,
removeTextFile,
sendMessage,
scrollToBottom,
handleBreakdownConfirmed,
Expand Down Expand Up @@ -246,6 +249,8 @@ export default function ChatScreen() {
isTranscribing={isTranscribing}
isTyping={isTyping}
selectedImagePresent={selectedImage !== null}
selectedTextFileName={selectedTextFile?.name ?? null}
selectedTextFilePresent={selectedTextFile !== null}
transcript={transcript}
composerResetSignal={composerResetSignal}
recordingTime={recordingTime}
Expand All @@ -262,6 +267,7 @@ export default function ChatScreen() {
},
}}
onRemoveImage={removeImage}
onRemoveTextFile={removeTextFile}
onRetry={() => {
void retryLastSend();
}}
Expand All @@ -275,6 +281,9 @@ export default function ChatScreen() {
onOpenFilePicker={() => {
void openFilePicker();
}}
onOpenTextFilePicker={() => {
void openTextFilePicker();
}}
onUpgrade={() => router.push("/upgrade")}
/>
</KeyboardAvoidingView>
Expand Down
36 changes: 35 additions & 1 deletion apps/mobile/components/chat/chat-input-area.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { forwardRef } from "react";
import { View, Text, TouchableOpacity, Pressable, ScrollView, Image, Linking } from "react-native";
import { Crown, X, WifiOff } from "lucide-react-native";
import { Crown, FileText, X, WifiOff } from "lucide-react-native";
import { useTranslation } from "react-i18next";
import Animated, { FadeInLeft, ReduceMotion } from "react-native-reanimated";
import { InfoCard } from "@/components/ui/info-card";
Expand Down Expand Up @@ -43,16 +43,20 @@ interface ChatInputAreaProps {
isTranscribing: boolean;
isTyping: boolean;
selectedImagePresent: boolean;
selectedTextFileName: string | null;
selectedTextFilePresent: boolean;
transcript: string;
composerResetSignal: number;
recordingTime: string;
speechSupported: boolean;
onRemoveImage: () => void;
onRemoveTextFile: () => void;
onRetry: () => void;
onSendChip: (chip: string) => void;
onSend: (message: string) => void;
onToggleRecording: () => void;
onOpenFilePicker: () => void;
onOpenTextFilePicker: () => void;
onUpgrade: () => void;
}

Expand All @@ -67,8 +71,10 @@ interface ChatInputNoticesProps {
canRetry: boolean;
speechError: string | null;
imagePreview: string | null;
selectedTextFileName: string | null;
onRetry: () => void;
onRemoveImage: () => void;
onRemoveTextFile: () => void;
}

function ChatInputNotices({
Expand All @@ -82,8 +88,10 @@ function ChatInputNotices({
canRetry,
speechError,
imagePreview,
selectedTextFileName,
onRetry,
onRemoveImage,
onRemoveTextFile,
}: Readonly<ChatInputNoticesProps>) {
const { t } = useTranslation();
return (
Expand Down Expand Up @@ -157,6 +165,26 @@ function ChatInputNotices({
</View>
)}

{selectedTextFileName && (
<View style={styles.textFileChipRow}>
<View style={styles.textFileChip}>
<FileText size={16} color={tokens.primary} strokeWidth={1.8} />
<Text style={styles.textFileChipText} numberOfLines={1}>
{selectedTextFileName}
</Text>
<TouchableOpacity
accessibilityRole="button"
accessibilityLabel={t("chat.removeFile")}
activeOpacity={0.8}
onPress={onRemoveTextFile}
style={styles.textFileChipRemove}
>
<X size={12} color={tokens.fg1} />
</TouchableOpacity>
</View>
</View>
)}

{hasMessages && !isOnline ? (
<View style={styles.offlineNotice}>
<WifiOff size={15} color={tokens.fg3} strokeWidth={1.6} />
Expand Down Expand Up @@ -272,6 +300,7 @@ export const ChatInputArea = forwardRef<View, Readonly<ChatInputAreaProps>>(
canRetry,
speechError,
imagePreview,
selectedTextFileName,
starterChips,
hasProAccess,
aiMessagesUsed,
Expand All @@ -280,6 +309,7 @@ export const ChatInputArea = forwardRef<View, Readonly<ChatInputAreaProps>>(
reward,
voiceRef,
onRemoveImage,
onRemoveTextFile,
onRetry,
onSendChip,
onUpgrade,
Expand Down Expand Up @@ -309,8 +339,10 @@ export const ChatInputArea = forwardRef<View, Readonly<ChatInputAreaProps>>(
canRetry={canRetry}
speechError={speechError}
imagePreview={imagePreview}
selectedTextFileName={selectedTextFileName}
onRetry={onRetry}
onRemoveImage={onRemoveImage}
onRemoveTextFile={onRemoveTextFile}
/>

{hasMessages && (
Expand All @@ -332,13 +364,15 @@ export const ChatInputArea = forwardRef<View, Readonly<ChatInputAreaProps>>(
atMessageLimit={atMessageLimit}
limitLocked={!hasProAccess && atMessageLimit}
selectedImagePresent={props.selectedImagePresent}
selectedTextFilePresent={props.selectedTextFilePresent}
transcript={props.transcript}
composerResetSignal={props.composerResetSignal}
recordingTime={props.recordingTime}
speechSupported={props.speechSupported}
onSend={props.onSend}
onToggleRecording={props.onToggleRecording}
onOpenFilePicker={props.onOpenFilePicker}
onOpenTextFilePicker={props.onOpenTextFilePicker}
/>

{!hasProAccess && atMessageLimit && (
Expand Down
Loading