The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { StrictMode } from "react"; |
| 2 | import { EnvironmentId, ThreadId, type ScopedThreadRef } from "@t3tools/contracts"; |
| 3 | import { expect, it } from "vite-plus/test"; |
| 4 | import { page } from "vite-plus/test/browser"; |
| 5 | import { act, render } from "../../../test/render"; |
| 6 | import { selectThreadPanelOpen, useRightPanelStore } from "../../rightPanelStore"; |
| 7 | import { PopoverCreateHandle } from "../ui/popover"; |
| 8 | import { ChatCanvasContext } from "./ChatCanvasContext"; |
| 9 | import { resolveChatCanvasLayout } from "./chatCanvasLayout"; |
| 10 | import { ThreadDetailsCard } from "./ThreadDetailsCard"; |
| 11 | |
| 12 | const threadA: ScopedThreadRef = { |
| 13 | environmentId: EnvironmentId.make("environment"), |
| 14 | threadId: ThreadId.make("thread-a"), |
| 15 | }; |
| 16 | const threadB: ScopedThreadRef = { |
| 17 | environmentId: EnvironmentId.make("environment"), |
| 18 | threadId: ThreadId.make("thread-b"), |
| 19 | }; |
| 20 | const container = { width: 1200, height: 800 }; |
| 21 | const placement = { x: 908, y: 0, right: 12, width: 280, height: 400 } as const; |
| 22 | |
| 23 | function setup() { |
| 24 | useRightPanelStore.setState({ threadPanelVisibilityByThreadKey: {} }); |
| 25 | const anchor = document.createElement("div"); |
| 26 | document.body.append(anchor); |
| 27 | return { |
| 28 | anchor: { current: anchor }, |
| 29 | handle: PopoverCreateHandle(), |
| 30 | [Symbol.dispose]: () => anchor.remove(), |
| 31 | }; |
| 32 | } |
| 33 | |
| 34 | const popoverOpen = (threadRef: ScopedThreadRef) => |
| 35 | selectThreadPanelOpen( |
| 36 | useRightPanelStore.getState().threadPanelVisibilityByThreadKey, |
| 37 | threadRef, |
| 38 | "popover", |
| 39 | ); |
| 40 | |
| 41 | function Card({ |
| 42 | threadRef, |
| 43 | inline, |
| 44 | fixture, |
| 45 | rows = 1, |
| 46 | automationRows = 0, |
| 47 | serverOverflowRows = 0, |
| 48 | }: { |
| 49 | threadRef: ScopedThreadRef; |
| 50 | inline: boolean; |
| 51 | fixture: ReturnType<typeof setup>; |
| 52 | /** Rows of 100px shown at full density. Compact always takes 50px. */ |
| 53 | rows?: number; |
| 54 | /** Rows of 100px in an automations list shown at full density. */ |
| 55 | automationRows?: number; |
| 56 | /** Rows of 100px that expanded server lists add, which density ignores. */ |
| 57 | serverOverflowRows?: number; |
| 58 | }) { |
| 59 | return ( |
| 60 | // The web root renders in StrictMode, which re-runs ref callbacks on every mount. |
| 61 | <StrictMode> |
| 62 | <ChatCanvasContext |
| 63 | value={{ |
| 64 | container, |
| 65 | lane: { padding: 48, minChatWidth: 640 }, |
| 66 | layout: resolveChatCanvasLayout({ container, preview: null }), |
| 67 | previewKey: null, |
| 68 | registerTimeline: () => {}, |
| 69 | measureDetailsCard: () => {}, |
| 70 | detailsCardTopInset: 0, |
| 71 | preferredDetailsCard: placement, |
| 72 | detailsCardPlacement: inline ? placement : null, |
| 73 | }} |
| 74 | > |
| 75 | <ThreadDetailsCard |
| 76 | threadRef={threadRef} |
| 77 | anchor={fixture.anchor} |
| 78 | handle={fixture.handle} |
| 79 | contentKey={String(rows)} |
| 80 | > |
| 81 | {(density) => ( |
| 82 | <> |
| 83 | <p style={{ height: density === "full" ? rows * 100 : 50 }}> |
| 84 | Details for {threadRef.threadId} |
| 85 | </p> |
| 86 | {density === "full" && automationRows > 0 ? ( |
| 87 | <section data-thread-automations-panel style={{ height: automationRows * 100 }} /> |
| 88 | ) : null} |
| 89 | {density === "full" && serverOverflowRows > 0 ? ( |
| 90 | <div |
| 91 | data-thread-details-server-overflow |
| 92 | style={{ height: serverOverflowRows * 100 }} |
| 93 | /> |
| 94 | ) : null} |
| 95 | </> |
| 96 | )} |
| 97 | </ThreadDetailsCard> |
| 98 | </ChatCanvasContext> |
| 99 | </StrictMode> |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | it("stays open once opened", async () => { |
| 104 | using fixture = setup(); |
| 105 | await using _screen = await render(<Card threadRef={threadB} inline={false} fixture={fixture} />); |
| 106 | await act(() => useRightPanelStore.getState().setThreadPanelOpen(threadB, "popover", true)); |
| 107 | await expect.element(page.getByText("Details for thread-b")).toBeVisible(); |
| 108 | expect(popoverOpen(threadB)).toBe(true); |
| 109 | }); |
| 110 | |
| 111 | it("closes a popup left open when the card switches inline, and reopens from the flag", async () => { |
| 112 | using fixture = setup(); |
| 113 | await using screen = await render(<Card threadRef={threadB} inline={false} fixture={fixture} />); |
| 114 | const setOpen = (open: boolean) => |
| 115 | act(() => useRightPanelStore.getState().setThreadPanelOpen(threadB, "popover", open)); |
| 116 | await setOpen(true); |
| 117 | await expect.element(page.getByText("Details for thread-b")).toBeVisible(); |
| 118 | |
| 119 | useRightPanelStore.getState().setThreadPanelOpen(threadB, "inline", false); |
| 120 | await screen.rerender(<Card threadRef={threadB} inline fixture={fixture} />); |
| 121 | await expect.element(page.getByText("Details for thread-b")).not.toBeInTheDocument(); |
| 122 | await setOpen(false); |
| 123 | |
| 124 | await screen.rerender(<Card threadRef={threadB} inline={false} fixture={fixture} />); |
| 125 | await expect.element(page.getByText("Details for thread-b")).not.toBeInTheDocument(); |
| 126 | await setOpen(true); |
| 127 | await expect.element(page.getByText("Details for thread-b")).toBeVisible(); |
| 128 | }); |
| 129 | |
| 130 | it("does not reopen the popover when the card returns from inline mode", async () => { |
| 131 | using fixture = setup(); |
| 132 | await using screen = await render(<Card threadRef={threadB} inline={false} fixture={fixture} />); |
| 133 | await act(() => useRightPanelStore.getState().setThreadPanelOpen(threadB, "popover", true)); |
| 134 | await expect.element(page.getByText("Details for thread-b")).toBeVisible(); |
| 135 | |
| 136 | useRightPanelStore.getState().setThreadPanelOpen(threadB, "inline", false); |
| 137 | await screen.rerender(<Card threadRef={threadB} inline fixture={fixture} />); |
| 138 | await expect.element(page.getByText("Details for thread-b")).not.toBeInTheDocument(); |
| 139 | |
| 140 | await screen.rerender(<Card threadRef={threadB} inline={false} fixture={fixture} />); |
| 141 | await expect.element(page.getByText("Details for thread-b")).not.toBeInTheDocument(); |
| 142 | expect(popoverOpen(threadB)).toBe(false); |
| 143 | }); |
| 144 | |
| 145 | it("removes the leaving thread's popup on a thread switch", async () => { |
| 146 | using fixture = setup(); |
| 147 | await using screen = await render(<Card threadRef={threadB} inline={false} fixture={fixture} />); |
| 148 | await act(() => useRightPanelStore.getState().setThreadPanelOpen(threadB, "popover", true)); |
| 149 | await expect.element(page.getByText("Details for thread-b")).toBeVisible(); |
| 150 | |
| 151 | await screen.rerender(<Card threadRef={threadA} inline={false} fixture={fixture} />); |
| 152 | await expect.element(page.getByText("Details for thread-b")).not.toBeInTheDocument(); |
| 153 | await expect.element(page.getByText("Details for thread-a")).not.toBeInTheDocument(); |
| 154 | }); |
| 155 | |
| 156 | it("reopens after a normal close", async () => { |
| 157 | using fixture = setup(); |
| 158 | await using _screen = await render(<Card threadRef={threadB} inline={false} fixture={fixture} />); |
| 159 | const setOpen = (open: boolean) => |
| 160 | act(() => useRightPanelStore.getState().setThreadPanelOpen(threadB, "popover", open)); |
| 161 | await setOpen(true); |
| 162 | await expect.element(page.getByText("Details for thread-b")).toBeVisible(); |
| 163 | await setOpen(false); |
| 164 | await expect.element(page.getByText("Details for thread-b")).not.toBeInTheDocument(); |
| 165 | await setOpen(true); |
| 166 | await expect.element(page.getByText("Details for thread-b")).toBeVisible(); |
| 167 | }); |
| 168 | |
| 169 | it("returns to full density when hidden items make the content fit", async () => { |
| 170 | using fixture = setup(); |
| 171 | await using screen = await render(<Card threadRef={threadB} inline fixture={fixture} rows={6} />); |
| 172 | const card = page.getByRole("region", { name: "Thread details" }); |
| 173 | await expect.element(card).toHaveAttribute("data-density", "compact"); |
| 174 | |
| 175 | await screen.rerender(<Card threadRef={threadB} inline fixture={fixture} rows={2} />); |
| 176 | await expect.element(card).toHaveAttribute("data-density", "full"); |
| 177 | }); |
| 178 | |
| 179 | it("keeps full density when many automations make the content tall", async () => { |
| 180 | using fixture = setup(); |
| 181 | await using _screen = await render( |
| 182 | <Card threadRef={threadB} inline fixture={fixture} rows={2} automationRows={6} />, |
| 183 | ); |
| 184 | await expect.element(page.getByText("Details for thread-b")).toBeVisible(); |
| 185 | await expect |
| 186 | .element(page.getByRole("region", { name: "Thread details" })) |
| 187 | .toHaveAttribute("data-density", "full"); |
| 188 | }); |
| 189 | |
| 190 | it("stays full while expanded server rows overflow", async () => { |
| 191 | using fixture = setup(); |
| 192 | await using _screen = await render( |
| 193 | <Card threadRef={threadB} inline fixture={fixture} rows={2} serverOverflowRows={4} />, |
| 194 | ); |
| 195 | await expect |
| 196 | .element(page.getByRole("region", { name: "Thread details" })) |
| 197 | .toHaveAttribute("data-density", "full"); |
| 198 | }); |