import { act, cleanup, fireEvent, render, screen, waitFor, within } from "@testing-library/react"; import { useEffect, useState } from "react"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { RemoteInstances, useRemoteConnections } from "@/components/remote/RemoteInstances"; import { RemoteConnectionsPage } from "@/components/remote/RemoteConnectionsPage"; import { Sidebar } from "@/components/Sidebar"; import { HOST_BRIDGE } from "@/components/remote/host-bridge"; import { readSelectedRemote, readRecentRemotes, rememberSelectedRemote, validateRemoteConnection, type RemoteConnection } from "@/lib/remote-instances"; import i18n from "@/i18n"; import { clearPairReturn, initializePairReturn } from "@/lib/remote-pair-return"; import type { Window as HappyWindow } from "happy-dom"; (window as unknown as HappyWindow).happyDOM.settings.disableIframePageLoading = true; const mocks = vi.hoisted(() => { const request = vi.fn(); const discover = vi.fn(); const onStatus = vi.fn(); const inspect = vi.fn(); return { read: vi.fn(), request, discover, inspect, onStatus, client: { status: "open", requestMutation: (action: string, payload: unknown, timeout: number) => { // Quick pairing prepares a local-only invitation when Add server opens; // these tests track SSH/config mutations separately from that lifecycle. if (action === "remote.pair_start") return Promise.resolve({ id: "pending-pair", command: "nanobot remote pair nbpr1.public", expires: Date.now() / 1000 + 600 }); if (action === "remote.pair_cancel") return Promise.resolve({ cancelled: true }); return action === "remote.discover" ? discover(payload) : action === "remote.inspect" ? inspect(payload) : request(action, payload, timeout); }, onStatus }, token: () => "local-token" }; }); vi.mock("@/providers/ClientProvider", () => ({ useClient: () => ({ client: mocks.client, getToken: mocks.token }) })); vi.mock("@/lib/remote-instances", async (original) => ({ ...await original(), readRemoteInstances: mocks.read, })); const profile = { id: "3b968d52-081d-4898-9970-ff0a1fc93817", name: "Team server", host: "ubuntu@example.test", port: 22, config_path: "~/.nanobot/config.json", ssh_config: "", identity_file: "", runtime_user: "", connected: false }; const connection: RemoteConnection = { ...profile, hostname: "team-host", gateway_id: "gateway-1", url: "http://127.0.0.1:23456/#/?bootstrapSecret=private-secret" }; const noop = () => {}; function LocalShell({ collapsed = false, initialRemote = false }) { const connections = useRemoteConnections(); const [remotePage, setRemotePage] = useState(initialRemote); useEffect(() => { const route = () => setRemotePage(window.location.hash === "#/remote"); window.addEventListener("hashchange", route); return () => window.removeEventListener("hashchange", route); }, []); return <> { if (connections?.managing) connections.selectLocal(); window.location.hash = "/new"; setRemotePage(false); }} onSelect={noop} onRequestDelete={noop} onTogglePin={noop} onRequestRename={noop} onToggleArchive={noop} onToggleGroup={noop} onRequestRenameProject={noop} onNewChatInProject={noop} onOpenSettings={noop} onOpenApps={noop} onOpenSkills={noop} onOpenAutomations={noop} onOpenChannels={noop} onOpenSearch={noop} onToggleArchived={noop} />