Open History to see the last commit
1import { act, useLayoutEffect } from "react";
2import { createRoot, type Root } from "react-dom/client";
3import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
4
5import { shouldUseRestingComposerLayout } from "../composerFooterLayout";
6import { useComposerFocusState } from "./useComposerFocusState";
7
8let root: Root;
9let composer: ReturnType<typeof useComposerFocusState>;
10let isResting: boolean;
11
12function ComposerProbe({ isMobileViewport = false }: { isMobileViewport?: boolean }) {
13 const state = useComposerFocusState(isMobileViewport);
14 useLayoutEffect(() => {
15 composer = state;
16 isResting = shouldUseRestingComposerLayout({
17 isExistingThread: true,
18 isMobileViewport,
19 isFocused: state.isComposerFocused,
20 isScrollCollapsed: state.isComposerScrollCollapsed,
21 hasExpandedChrome: false,
22 collapseOnBlur: true,
23 timelineOverflows: true,
24 });
25 });
26 return null;
27}
28
29beforeEach(async () => {
30 // The probe has no DOM output, but ReactDOM needs an event target.
31 const document = {
32 nodeType: 9,
33 addEventListener() {},
34 removeEventListener() {},
35 };
36 const container = {
37 nodeType: 1,
38 tagName: "DIV",
39 namespaceURI: "http://www.w3.org/1999/xhtml",
40 ownerDocument: document,
41 addEventListener() {},
42 removeEventListener() {},
43 };
44 vi.stubGlobal("document", document);
45 vi.stubGlobal("window", { document, HTMLIFrameElement: EventTarget });
46 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
47 root = createRoot(container as unknown as HTMLElement);
48 await act(() => root.render(<ComposerProbe />));
49});
50
51afterEach(async () => {
52 await act(() => root.unmount());
53 vi.unstubAllGlobals();
54});
55
56describe("composer focus state", () => {
57 it("expands at the timeline end after a tool call takes focus", async () => {
58 await act(() => composer.setIsComposerFocused(true));
59 expect(isResting).toBe(false);
60
61 // A tool disclosure takes focus before the user scrolls through its output.
62 await act(() => composer.setIsComposerFocused(false));
63 expect(isResting).toBe(true);
64
65 await act(() => composer.restoreAfterTimelineReachedEnd());
66 expect(isResting).toBe(false);
67
68 await act(() => composer.setIsComposerFocused(false));
69 expect(isResting).toBe(true);
70 });
71
72 it("can collapse again on the next scroll after returning to the end", async () => {
73 await act(() => composer.setIsComposerScrollCollapsed(true));
74 expect(isResting).toBe(true);
75
76 await act(() => composer.restoreAfterTimelineReachedEnd());
77 expect(isResting).toBe(false);
78
79 await act(() => composer.setIsComposerScrollCollapsed(true));
80 expect(isResting).toBe(true);
81 });
82
83 it("does not expand the phone composer when the timeline reaches the end", async () => {
84 await act(() => root.render(<ComposerProbe isMobileViewport />));
85 await act(() => composer.restoreAfterTimelineReachedEnd());
86 expect(composer.isComposerFocused).toBe(false);
87 });
88});