|
| 1 | +import { afterEach, beforeEach, describe, expect, test } from "bun:test"; |
| 2 | +import { act, cleanup, renderHook, waitFor } from "@testing-library/react"; |
| 3 | +import { GlobalWindow } from "happy-dom"; |
| 4 | +import React from "react"; |
| 5 | +import { APIProvider, type APIClient } from "@/browser/contexts/API"; |
| 6 | +import { ModeProvider } from "@/browser/contexts/ModeContext"; |
| 7 | +import { ThinkingProvider } from "@/browser/contexts/ThinkingContext"; |
| 8 | +import { updatePersistedState } from "@/browser/hooks/usePersistedState"; |
| 9 | +import { getLastRuntimeConfigKey } from "@/common/constants/storage"; |
| 10 | +import { useDraftWorkspaceSettings } from "./useDraftWorkspaceSettings"; |
| 11 | + |
| 12 | +function createStubApiClient(): APIClient { |
| 13 | + // useModelLRU() only needs providers.getConfig + providers.onConfigChanged. |
| 14 | + // Provide a minimal stub so tests can run without spinning up a real oRPC client. |
| 15 | + async function* empty() { |
| 16 | + // no-op |
| 17 | + } |
| 18 | + |
| 19 | + return { |
| 20 | + providers: { |
| 21 | + getConfig: () => Promise.resolve({}), |
| 22 | + onConfigChanged: () => Promise.resolve(empty()), |
| 23 | + }, |
| 24 | + } as unknown as APIClient; |
| 25 | +} |
| 26 | + |
| 27 | +describe("useDraftWorkspaceSettings", () => { |
| 28 | + beforeEach(() => { |
| 29 | + globalThis.window = new GlobalWindow() as unknown as Window & typeof globalThis; |
| 30 | + globalThis.document = globalThis.window.document; |
| 31 | + globalThis.localStorage = globalThis.window.localStorage; |
| 32 | + globalThis.localStorage.clear(); |
| 33 | + }); |
| 34 | + |
| 35 | + afterEach(() => { |
| 36 | + cleanup(); |
| 37 | + globalThis.window = undefined as unknown as Window & typeof globalThis; |
| 38 | + globalThis.document = undefined as unknown as Document; |
| 39 | + }); |
| 40 | + |
| 41 | + test("does not reset selected runtime to the default while editing SSH host", async () => { |
| 42 | + const projectPath = "/tmp/project"; |
| 43 | + |
| 44 | + const wrapper: React.FC<{ children: React.ReactNode }> = (props) => ( |
| 45 | + <APIProvider client={createStubApiClient()}> |
| 46 | + <ModeProvider projectPath={projectPath}> |
| 47 | + <ThinkingProvider projectPath={projectPath}>{props.children}</ThinkingProvider> |
| 48 | + </ModeProvider> |
| 49 | + </APIProvider> |
| 50 | + ); |
| 51 | + |
| 52 | + const { result } = renderHook(() => useDraftWorkspaceSettings(projectPath, ["main"], "main"), { |
| 53 | + wrapper, |
| 54 | + }); |
| 55 | + |
| 56 | + act(() => { |
| 57 | + result.current.setSelectedRuntime({ mode: "ssh", host: "dev@host" }); |
| 58 | + }); |
| 59 | + |
| 60 | + await waitFor(() => { |
| 61 | + expect(result.current.settings.selectedRuntime).toEqual({ mode: "ssh", host: "dev@host" }); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + test("seeds SSH host from the remembered value when switching modes", async () => { |
| 66 | + const projectPath = "/tmp/project"; |
| 67 | + |
| 68 | + updatePersistedState(getLastRuntimeConfigKey(projectPath), { |
| 69 | + ssh: { host: "remembered@host" }, |
| 70 | + }); |
| 71 | + |
| 72 | + const wrapper: React.FC<{ children: React.ReactNode }> = (props) => ( |
| 73 | + <APIProvider client={createStubApiClient()}> |
| 74 | + <ModeProvider projectPath={projectPath}> |
| 75 | + <ThinkingProvider projectPath={projectPath}>{props.children}</ThinkingProvider> |
| 76 | + </ModeProvider> |
| 77 | + </APIProvider> |
| 78 | + ); |
| 79 | + |
| 80 | + const { result } = renderHook(() => useDraftWorkspaceSettings(projectPath, ["main"], "main"), { |
| 81 | + wrapper, |
| 82 | + }); |
| 83 | + |
| 84 | + act(() => { |
| 85 | + // Simulate UI switching into ssh mode with an empty field. |
| 86 | + result.current.setSelectedRuntime({ mode: "ssh", host: "" }); |
| 87 | + }); |
| 88 | + |
| 89 | + await waitFor(() => { |
| 90 | + expect(result.current.settings.selectedRuntime).toEqual({ |
| 91 | + mode: "ssh", |
| 92 | + host: "remembered@host", |
| 93 | + }); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + test("seeds Docker image from the remembered value when switching modes", async () => { |
| 98 | + const projectPath = "/tmp/project"; |
| 99 | + |
| 100 | + updatePersistedState(getLastRuntimeConfigKey(projectPath), { |
| 101 | + docker: { image: "ubuntu:22.04" }, |
| 102 | + }); |
| 103 | + |
| 104 | + const wrapper: React.FC<{ children: React.ReactNode }> = (props) => ( |
| 105 | + <APIProvider client={createStubApiClient()}> |
| 106 | + <ModeProvider projectPath={projectPath}> |
| 107 | + <ThinkingProvider projectPath={projectPath}>{props.children}</ThinkingProvider> |
| 108 | + </ModeProvider> |
| 109 | + </APIProvider> |
| 110 | + ); |
| 111 | + |
| 112 | + const { result } = renderHook(() => useDraftWorkspaceSettings(projectPath, ["main"], "main"), { |
| 113 | + wrapper, |
| 114 | + }); |
| 115 | + |
| 116 | + act(() => { |
| 117 | + // Simulate UI switching into docker mode with an empty field. |
| 118 | + result.current.setSelectedRuntime({ mode: "docker", image: "" }); |
| 119 | + }); |
| 120 | + |
| 121 | + await waitFor(() => { |
| 122 | + expect(result.current.settings.selectedRuntime).toEqual({ |
| 123 | + mode: "docker", |
| 124 | + image: "ubuntu:22.04", |
| 125 | + }); |
| 126 | + }); |
| 127 | + }); |
| 128 | +}); |
0 commit comments