Open History to see the last commit
1import { act, useEffect, useSyncExternalStore } from "react";
2import { createRoot } from "react-dom/client";
3import { expect, it, vi } from "vite-plus/test";
4
5const harness = vi.hoisted(() => ({
6 session: null as import("./voiceInputSession").VoiceInputSession | null,
7}));
8
9vi.mock("@react-navigation/native", () => ({
10 useFocusEffect: (callback: () => void | (() => void)) => useEffect(callback, [callback]),
11}));
12vi.mock("./VoiceInputProvider", () => ({
13 useGlobalVoiceInput: () => {
14 const session = harness.session!;
15 const snapshot = useSyncExternalStore(session.subscribe, session.getSnapshot);
16 return { ...snapshot, isAvailable: true, isBusy: false, session };
17 },
18}));
19
20import { useVoiceInputController } from "./useVoiceInputController";
21import { VoiceInputSession } from "./voiceInputSession";
22
23function Composer(props: { readonly hidden: boolean }) {
24 useVoiceInputController({
25 ownerKey: "thread-1",
26 label: "Thread",
27 readDraftMessage: () => "",
28 selection: { start: 0, end: 0 },
29 hidden: props.hidden,
30 onChangeDraftMessage: () => {},
31 onChangeSelection: () => {},
32 });
33 return null;
34}
35
36function setup() {
37 const session = new VoiceInputSession({
38 recorder: {
39 uri: "file:///voice.m4a",
40 prepareToRecordAsync: async () => {},
41 record: () => {},
42 stop: async () => {},
43 },
44 getTranscriber: () => ({
45 prepare: async () => ({ locale: "en-US", transcribe: async () => "" }),
46 }),
47 requestPermission: async () => ({ granted: true, canAskAgain: true }),
48 configureRecording: async () => {},
49 releaseRecording: async () => {},
50 deleteRecording: () => {},
51 sampleRecording: () => null,
52 publishAudioLevels: () => {},
53 keepAwake: () => () => {},
54 });
55 harness.session = session;
56 const root = createRoot(document.createElement("div"));
57 return {
58 isFocused: () => session.getSnapshot().focusedOwners.has("thread-1"),
59 render: (hidden: boolean) => act(() => root.render(<Composer hidden={hidden} />)),
60 [Symbol.dispose]: () => {
61 act(() => root.unmount());
62 session.dispose();
63 },
64 };
65}
66
67// GlobalVoiceInputControl hides its dictation pill while the recording owner is focused, so a
68// composer that stays mounted under display: none (e.g. behind an agent question) must unfocus.
69it("unfocuses a hidden composer so the global dictation controls can show", () => {
70 using voice = setup();
71
72 voice.render(false);
73 expect(voice.isFocused()).toBe(true);
74
75 voice.render(true);
76 expect(voice.isFocused()).toBe(false);
77
78 voice.render(false);
79 expect(voice.isFocused()).toBe(true);
80});