The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { InfoIcon } from "lucide-react"; |
| 2 | import type { ReactNode } from "react"; |
| 3 | import { expect, it, vi } from "vite-plus/test"; |
| 4 | import { page, userEvent } from "vite-plus/test/browser"; |
| 5 | |
| 6 | import "~/index.css"; |
| 7 | |
| 8 | import { render } from "../../../test/render"; |
| 9 | import { ComposerBanner } from "./ComposerBanner"; |
| 10 | import { |
| 11 | ComposerBannerStack, |
| 12 | NoticeText, |
| 13 | type ComposerBannerStackItem, |
| 14 | } from "./ComposerBannerStack"; |
| 15 | |
| 16 | function notice(title: string, group?: ComposerBannerStackItem["group"]): ComposerBannerStackItem { |
| 17 | return { id: title, variant: "default", icon: <InfoIcon />, title, ...(group ? { group } : {}) }; |
| 18 | } |
| 19 | |
| 20 | it("puts machine notices first, whatever order the notices arrive in", async () => { |
| 21 | await using screen = await render( |
| 22 | <ComposerBannerStack items={[notice("Branch changed"), notice("Offline", "environment")]} />, |
| 23 | ); |
| 24 | |
| 25 | expect(screen.container.textContent).toBe("OfflineBranch changed"); |
| 26 | }); |
| 27 | |
| 28 | it("leaves the next attachment its own top edge when there are no notices", async () => { |
| 29 | await using screen = await render( |
| 30 | <ComposerBanner.Column> |
| 31 | <ComposerBannerStack items={[]} /> |
| 32 | <ComposerBanner.Attachment> |
| 33 | <ComposerBanner.Root data-testid="drawer">Approval</ComposerBanner.Root> |
| 34 | </ComposerBanner.Attachment> |
| 35 | </ComposerBanner.Column>, |
| 36 | ); |
| 37 | |
| 38 | const surface = getComputedStyle(screen.getByTestId("drawer").element(), "::before"); |
| 39 | expect(surface.borderTopWidth).toBe("1px"); |
| 40 | expect(surface.borderTopLeftRadius).not.toBe("0px"); |
| 41 | }); |
| 42 | |
| 43 | function noticeRow(width: string, children: ReactNode) { |
| 44 | return ( |
| 45 | <div data-testid="row" className="flex items-center gap-1.5" style={{ width }}> |
| 46 | <NoticeText detailsLabel="Show details" className="min-w-0 truncate"> |
| 47 | {children} |
| 48 | </NoticeText> |
| 49 | </div> |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | it("leaves out the details button when only a link's hit area reaches past the text", async () => { |
| 54 | // Phones lay out in device pixels, so a range can measure one float32 step (2^-17 px at |
| 55 | // this size) wider than its element. Headless Chromium's rects agree exactly. |
| 56 | const rangeRect = Range.prototype.getBoundingClientRect; |
| 57 | using _rangeRect = vi |
| 58 | .spyOn(Range.prototype, "getBoundingClientRect") |
| 59 | .mockImplementation(function (this: Range) { |
| 60 | const rect = rangeRect.call(this); |
| 61 | return new DOMRect(rect.x, rect.y, rect.width + 2 ** -17, rect.height); |
| 62 | }); |
| 63 | // The flicker is a details button that mounts and then unmounts, so record every insertion. |
| 64 | const inserted: Node[] = []; |
| 65 | using observing = new DisposableStack(); |
| 66 | const mutations = new MutationObserver((records) => |
| 67 | inserted.push(...records.flatMap((record) => [...record.addedNodes])), |
| 68 | ); |
| 69 | mutations.observe(document.body, { childList: true, subtree: true }); |
| 70 | observing.defer(() => mutations.disconnect()); |
| 71 | await using screen = await render( |
| 72 | noticeRow( |
| 73 | "20rem", |
| 74 | <> |
| 75 | Needs input{" "} |
| 76 | <button type="button" className="relative after:absolute after:-inset-x-10 after:inset-y-0"> |
| 77 | Review |
| 78 | </button> |
| 79 | </>, |
| 80 | ), |
| 81 | ); |
| 82 | |
| 83 | await expect.element(screen.getByText("Review")).toBeVisible(); |
| 84 | const details = [ |
| 85 | ...inserted, |
| 86 | ...mutations.takeRecords().flatMap((record) => [...record.addedNodes]), |
| 87 | ].filter( |
| 88 | (node) => |
| 89 | node instanceof Element && |
| 90 | (node.matches('[aria-label="Show details"]') || |
| 91 | node.querySelector('[aria-label="Show details"]')), |
| 92 | ); |
| 93 | expect(details).toEqual([]); |
| 94 | }); |
| 95 | |
| 96 | it("shows the details button while text or nested text is cut, until it fits again", async () => { |
| 97 | await using screen = await render(noticeRow("4rem", "Waiting on a subagent with a long name")); |
| 98 | const details = screen.getByRole("button", { name: "Show details" }); |
| 99 | await expect.element(details).toBeInTheDocument(); |
| 100 | |
| 101 | await screen.rerender(noticeRow("4rem", "Ready")); |
| 102 | await expect.element(details).not.toBeInTheDocument(); |
| 103 | |
| 104 | await screen.rerender(noticeRow("4rem", "Waiting on a subagent with a long name")); |
| 105 | await expect.element(details).toBeInTheDocument(); |
| 106 | |
| 107 | screen.getByTestId("row").element().style.width = "40rem"; |
| 108 | await expect.element(details).not.toBeInTheDocument(); |
| 109 | |
| 110 | await screen.rerender( |
| 111 | noticeRow( |
| 112 | "40rem", |
| 113 | <span className="inline-block max-w-16 truncate align-bottom">A long nested name</span>, |
| 114 | ), |
| 115 | ); |
| 116 | await expect.element(details).toBeInTheDocument(); |
| 117 | |
| 118 | await screen.rerender( |
| 119 | <ComposerBanner.Root> |
| 120 | {noticeRow("4rem", "Waiting on a subagent with a long name")} |
| 121 | </ComposerBanner.Root>, |
| 122 | ); |
| 123 | await expect.element(details).toBeInTheDocument(); |
| 124 | await screen.rerender(<ComposerBanner.Root>{noticeRow("4rem", "Ready")}</ComposerBanner.Root>); |
| 125 | await expect.element(details).not.toBeInTheDocument(); |
| 126 | }); |
| 127 | |
| 128 | it("opens the details of a long notice right above its button", async () => { |
| 129 | const viewport = { width: window.innerWidth, height: window.innerHeight }; |
| 130 | await page.viewport(1280, 800); |
| 131 | await using _viewport = { |
| 132 | [Symbol.asyncDispose]: () => page.viewport(viewport.width, viewport.height), |
| 133 | }; |
| 134 | const script = Array.from({ length: 400 }, (_, line) => `echo "line ${line}"`).join("\n"); |
| 135 | const container = document.createElement("div"); |
| 136 | container.style.cssText = "position: fixed; bottom: 4rem; right: 4rem;"; |
| 137 | await using screen = await render(noticeRow("40rem", script), { container }); |
| 138 | const details = screen.getByRole("button", { name: "Show details" }); |
| 139 | await userEvent.hover(details); |
| 140 | const popup = page.getByLabelText("Notice details"); |
| 141 | await expect.element(popup).toBeVisible(); |
| 142 | |
| 143 | // A positioner sized to the unscrolled text flips beside the button and covers it. |
| 144 | await vi.waitFor(() => { |
| 145 | const gap = |
| 146 | details.element().getBoundingClientRect().top - |
| 147 | popup.element().getBoundingClientRect().bottom; |
| 148 | expect(gap).toBeGreaterThanOrEqual(0); |
| 149 | expect(gap).toBeLessThan(8); |
| 150 | }); |
| 151 | }); |