Repository navigation
fix(web): keep device results in the thread that started them #16040
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
saphid
wants to merge
13
commits into
pingdotgg:main
Choose a base branch
from
saphid:stack/04a-device-late-result
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+313
−6
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
920770b
refactor(web): register the Diff side panel
saphid e1ae808
refactor(web): register the Preview side panel
github-actions[bot] 4851ae8
perf(web): keep the panel host stable across chat view renders
github-actions[bot] 30972c6
fix(web): send a late annotation only to the thread it was picked in
github-actions[bot] 52a78e7
fix(web): lend the annotation sender only after the chat view commits
github-actions[bot] 7be7fb5
refactor(web): move the thread terminal panel and drawer out of ChatView
github-actions[bot] a0ea164
refactor(web): open the right-panel terminal through the panel host
github-actions[bot] 02d7fe8
fix(web): keep a local-checkout terminal launch off the thread's work…
github-actions[bot] ca03345
fix(web): keep a terminal the server opened on the checkout off the t…
github-actions[bot] 4b7aa2e
fix(web): keep device results in the thread that started them
github-actions[bot] 6d97acc
Merge upstream main into stack/04a-device-late-result
github-actions[bot] 579ca53
chore: drop the stacked panel-host commits from the device fix
github-actions[bot] 24adce2
test(web): cover same-environment switches and overlapping device picks
github-actions[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,268 @@ | ||
| import { | ||
| EnvironmentId, | ||
| ThreadId, | ||
| type DeviceServiceState, | ||
| type ScopedThreadRef, | ||
| } from "@t3tools/contracts"; | ||
| import { act, type ReactNode } from "react"; | ||
| import { create, type ReactTestRenderer } from "react-test-renderer"; | ||
| import { beforeEach, describe, expect, it, vi } from "vite-plus/test"; | ||
|
|
||
| import type { RightPanelSurface } from "~/rightPanelStore"; | ||
|
|
||
| import { DevicePanel } from "./DevicePanel"; | ||
|
|
||
| type CommandResult = | ||
| | { _tag: "Success"; value: { hostId: string; deviceId: string } } | ||
| | { _tag: "Failure"; cause: unknown }; | ||
| const mocks = vi.hoisted(() => ({ | ||
| open: vi.fn<(request: unknown) => Promise<CommandResult>>(), | ||
| close: vi.fn<(request: unknown) => Promise<CommandResult>>(), | ||
| openDevice: vi.fn(), | ||
| closeSurface: vi.fn(), | ||
| })); | ||
| const phone = { | ||
| hostId: "local", | ||
| id: "phone", | ||
| name: "Phone", | ||
| platform: "ios", | ||
| version: "iOS 19", | ||
| booted: false, | ||
| physical: false, | ||
| } as const; | ||
| const deviceState: DeviceServiceState = { | ||
| hosts: [ | ||
| { | ||
| id: "local", | ||
| kind: "local", | ||
| label: "This Mac", | ||
| platforms: [{ platform: "ios", available: true }], | ||
| hubInstalled: true, | ||
| agentDeviceInstalled: true, | ||
| }, | ||
| ], | ||
| hostStatus: "ready", | ||
| hostStatuses: { local: { status: "ready" } }, | ||
| devices: [phone], | ||
| sessions: [ | ||
| { | ||
| threadId: ThreadId.make("thread-1"), | ||
| hostId: "local", | ||
| deviceId: "phone", | ||
| platform: "ios", | ||
| openedAt: "2026-01-01T00:00:00.000Z", | ||
| }, | ||
| ], | ||
| onboardingCompleted: true, | ||
| agentAccessEnabled: true, | ||
| hubBasePath: "/api/device-hub", | ||
| revision: 1, | ||
| }; | ||
| vi.mock("~/state/device", () => ({ | ||
| deviceEnvironment: { list: "list", open: "open", close: "close" }, | ||
| useDeviceState: () => ({ state: deviceState, loaded: true }), | ||
| })); | ||
| vi.mock("~/state/use-atom-command", () => ({ | ||
| useAtomCommand: (command: string) => | ||
| command === "open" | ||
| ? mocks.open | ||
| : command === "close" | ||
| ? mocks.close | ||
| : () => Promise.resolve({ _tag: "Success", value: {} }), | ||
| })); | ||
| vi.mock("~/state/query", () => ({ formatEnvironmentQueryError: () => "Device failed" })); | ||
| vi.mock("~/rightPanelStore", () => ({ | ||
| useRightPanelStore: { | ||
| getState: () => ({ openDevice: mocks.openDevice, closeSurface: mocks.closeSurface }), | ||
| }, | ||
| })); | ||
| vi.mock("~/components/device/DeviceHostUpdates", () => ({ DeviceHostUpdates: () => null })); | ||
| vi.mock("~/components/device/DeviceLoadingView", () => ({ DeviceLoadingView: () => null })); | ||
| vi.mock("~/components/device/DeviceWorkspace", () => ({ DeviceWorkspace: () => null })); | ||
| vi.mock("~/components/preview/PreviewPanelShell", () => ({ | ||
| PreviewPanelShell: ({ children }: { children: ReactNode }) => children, | ||
| })); | ||
|
|
||
| // Same thread id in another environment: the surface id collides, so the panel | ||
| // stays mounted across the switch, as it does under ChatView's surface key. | ||
| const picking = { | ||
| environmentId: EnvironmentId.make("environment-a"), | ||
| threadId: ThreadId.make("thread-1"), | ||
| }; | ||
| const next = { environmentId: EnvironmentId.make("environment-b"), threadId: picking.threadId }; | ||
| // Another thread in the same environment, as when ChatView keeps the panel for a shared surface. | ||
| const sibling = { environmentId: picking.environmentId, threadId: ThreadId.make("thread-2") }; | ||
| const switches = [ | ||
| ["the same thread id in another environment", next], | ||
| ["another thread in the same environment", sibling], | ||
| ] as const; | ||
| const picker: Extract<RightPanelSurface, { kind: "device" }> = { id: "device", kind: "device" }; | ||
| const streaming: Extract<RightPanelSurface, { kind: "device" }> = { | ||
| id: "device:phone", | ||
| kind: "device", | ||
| target: { hostId: "local", deviceId: "phone", platform: "ios", name: "Phone" }, | ||
| }; | ||
|
|
||
| const render = (threadRef: ScopedThreadRef, surface = picker) => ( | ||
| <DevicePanel | ||
| key={surface.id} | ||
| mode="embedded" | ||
| threadRef={threadRef} | ||
| surface={surface} | ||
| visible | ||
| onDismissSetup={() => undefined} | ||
| /> | ||
| ); | ||
| const deferred = () => { | ||
| let settle!: (result: CommandResult) => void; | ||
| const promise = new Promise<CommandResult>((resolve) => (settle = resolve)); | ||
| return { promise, settle }; | ||
| }; | ||
| const startPhone = (renderer: ReactTestRenderer) => | ||
| act(async () => { | ||
| renderer.root.findByProps({ "aria-label": "Start Phone" }).props.onClick(); | ||
| }); | ||
| const errors = (renderer: ReactTestRenderer) => renderer.root.findAllByProps({ role: "alert" }); | ||
| const startButtons = (renderer: ReactTestRenderer) => | ||
| renderer.root.findAllByProps({ "aria-label": "Start Phone" }); | ||
| const pressPowerOff = (renderer: ReactTestRenderer) => | ||
| act(async () => { | ||
| renderer.root.findByProps({ hostLabel: "This Mac" }).props.onPowerOff(); | ||
| }); | ||
| const opened = { _tag: "Success", value: { hostId: "local", deviceId: "phone" } } as const; | ||
|
|
||
| beforeEach(() => { | ||
| vi.resetAllMocks(); | ||
| }); | ||
|
|
||
| describe("DevicePanel", () => { | ||
| it("opens a picked device when the pick settles in the same thread", async () => { | ||
| const pick = deferred(); | ||
| mocks.open.mockReturnValue(pick.promise); | ||
| const renderer = await act(async () => create(render(picking))); | ||
|
|
||
| await startPhone(renderer); | ||
| expect(mocks.open).toHaveBeenCalledWith({ | ||
| environmentId: picking.environmentId, | ||
| input: { threadId: picking.threadId, hostId: "local", deviceId: "phone", platform: "ios" }, | ||
| }); | ||
| await act(async () => pick.settle(opened)); | ||
|
|
||
| expect(mocks.openDevice).toHaveBeenCalledExactlyOnceWith(picking, { | ||
| hostId: "local", | ||
| deviceId: "phone", | ||
| platform: "ios", | ||
| name: "Phone", | ||
| }); | ||
| }); | ||
|
|
||
| it.each(switches)( | ||
| "keeps the picker usable after switching to %s while a pick is pending", | ||
| async (_, target) => { | ||
| mocks.open.mockReturnValue(deferred().promise); | ||
| const renderer = await act(async () => create(render(picking))); | ||
|
|
||
| await startPhone(renderer); | ||
| await act(async () => renderer.update(render(target))); | ||
|
|
||
| expect(renderer.root.findByProps({ "aria-label": "Start Phone" }).props.disabled).toBe(false); | ||
| }, | ||
| ); | ||
|
|
||
| it("keeps the next thread's own pick pending when an earlier pick settles", async () => { | ||
| const first = deferred(); | ||
| const second = deferred(); | ||
| mocks.open.mockReturnValueOnce(first.promise).mockReturnValueOnce(second.promise); | ||
| const renderer = await act(async () => create(render(picking))); | ||
|
|
||
| await startPhone(renderer); | ||
| await act(async () => renderer.update(render(sibling))); | ||
| await startPhone(renderer); | ||
| await act(async () => first.settle(opened)); | ||
|
|
||
| expect(startButtons(renderer)).toHaveLength(0); | ||
| await act(async () => second.settle(opened)); | ||
| expect(startButtons(renderer).length).toBeGreaterThan(0); | ||
| expect(mocks.openDevice.mock.calls.map(([threadRef]) => threadRef)).toEqual([picking, sibling]); | ||
| }); | ||
|
|
||
| it("opens a pick that settles after a switch in the thread it started in", async () => { | ||
| const pick = deferred(); | ||
| mocks.open.mockReturnValue(pick.promise); | ||
| const renderer = await act(async () => create(render(picking))); | ||
|
|
||
| await startPhone(renderer); | ||
| await act(async () => renderer.update(render(next))); | ||
| await act(async () => pick.settle(opened)); | ||
|
|
||
| expect(mocks.openDevice).toHaveBeenCalledExactlyOnceWith(picking, { | ||
| hostId: "local", | ||
| deviceId: "phone", | ||
| platform: "ios", | ||
| name: "Phone", | ||
| }); | ||
| expect(renderer.root.findByProps({ "aria-label": "Start Phone" }).props.disabled).toBe(false); | ||
| }); | ||
|
|
||
| it.each(switches)( | ||
| "hides a failed pick that settles after switching to %s and back", | ||
| async (_, target) => { | ||
| const pick = deferred(); | ||
| mocks.open.mockReturnValue(pick.promise); | ||
| const renderer = await act(async () => create(render(picking))); | ||
|
|
||
| await startPhone(renderer); | ||
| await act(async () => renderer.update(render(target))); | ||
| await act(async () => renderer.update(render(picking))); | ||
| await act(async () => pick.settle({ _tag: "Failure", cause: new Error("boom") })); | ||
|
|
||
| expect(errors(renderer)).toHaveLength(0); | ||
| expect(mocks.openDevice).not.toHaveBeenCalled(); | ||
| }, | ||
| ); | ||
|
|
||
| it.each(switches)("does not carry an operation error to %s", async (_, target) => { | ||
| mocks.open.mockResolvedValue({ _tag: "Failure", cause: new Error("boom") }); | ||
| const renderer = await act(async () => create(render(picking))); | ||
|
|
||
| await startPhone(renderer); | ||
| expect(errors(renderer)).toHaveLength(1); | ||
| await act(async () => renderer.update(render(target))); | ||
|
|
||
| expect(errors(renderer)).toHaveLength(0); | ||
| }); | ||
|
|
||
| it("closes a powered-off surface in its own thread after the panel moved", async () => { | ||
| const powerOff = deferred(); | ||
| mocks.close.mockReturnValue(powerOff.promise); | ||
| const renderer = await act(async () => create(render(picking, streaming))); | ||
|
|
||
| await pressPowerOff(renderer); | ||
| expect(mocks.close).toHaveBeenCalledWith({ | ||
| environmentId: picking.environmentId, | ||
| input: { threadId: picking.threadId, hostId: "local", deviceId: "phone", shutdown: true }, | ||
| }); | ||
| await act(async () => renderer.update(render(next, streaming))); | ||
| await act(async () => powerOff.settle(opened)); | ||
|
|
||
| expect(mocks.closeSurface).toHaveBeenCalledExactlyOnceWith(picking, streaming.id); | ||
| }); | ||
|
|
||
| it("shows a failed power-off only in the thread that started it", async () => { | ||
| const late = deferred(); | ||
| mocks.close | ||
| .mockResolvedValueOnce({ _tag: "Failure", cause: new Error("boom") }) | ||
| .mockReturnValueOnce(late.promise); | ||
| const renderer = await act(async () => create(render(picking, streaming))); | ||
|
|
||
| await pressPowerOff(renderer); | ||
| expect(errors(renderer)).toHaveLength(1); | ||
|
|
||
| await pressPowerOff(renderer); | ||
| await act(async () => renderer.update(render(sibling, streaming))); | ||
| await act(async () => late.settle({ _tag: "Failure", cause: new Error("boom") })); | ||
|
|
||
| expect(errors(renderer)).toHaveLength(0); | ||
| expect(mocks.closeSurface).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.