Skip to content

Commit ff2ea12

Browse files
committed
test(webview): isolate the dev-server probe test from a stray .vite-port file and restore env in finally
1 parent 984225e commit ff2ea12

1 file changed

Lines changed: 42 additions & 22 deletions

File tree

‎src/core/webview/__tests__/ClineProvider.spec.ts‎

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// pnpm --filter roo-cline test core/webview/__tests__/ClineProvider.spec.ts
22

3+
import fs from "fs"
34
import * as path from "path"
45
import { TaskRegistry } from "../../task/TaskRegistry"
56

@@ -700,31 +701,50 @@ describe("ClineProvider", () => {
700701
test("resolveWebviewView builds HMR content against the local dev server when it is reachable", async () => {
701702
const originalThemeFixtureProbe = process.env.ROO_CODE_THEME_FIXTURE_PROBE
702703
delete process.env.ROO_CODE_THEME_FIXTURE_PROBE
704+
// getHMRHtmlContent prefers an on-disk .vite-port file when one exists, so a
705+
// stale dev leftover could silently move the probe (and the HMR URLs) to
706+
// another port. Establish the default-port branch for this test by hiding
707+
// exactly that file from the existence check; every other path falls
708+
// through to the real implementation.
709+
const realExistsSync = fs.existsSync
710+
const existsSyncSpy = vi
711+
.spyOn(fs, "existsSync")
712+
.mockImplementation((target) =>
713+
path.basename(String(target)) === ".vite-port" ? false : realExistsSync(target),
714+
)
703715

704-
provider = new ClineProvider(
705-
{ ...mockContext, extensionMode: vscode.ExtensionMode.Development },
706-
mockOutputChannel,
707-
"sidebar",
708-
new ContextProxy({ ...mockContext, extensionMode: vscode.ExtensionMode.Development }),
709-
)
710-
// The default axios mock resolves, so the dev-server probe succeeds and the
711-
// HMR HTML branch (instead of the production fallback) is taken.
712-
vi.mocked(axios.get).mockClear()
713-
await provider.resolveWebviewView(mockWebviewView)
714-
715-
// Pin the health-check URL itself: the mock resolves for any URL, so only
716-
// this assertion keeps the probe from silently drifting back to
717-
// `localhost` (the IPv6 resolution failure this branch fixes).
718-
expect(axios.get).toHaveBeenCalledWith("http://127.0.0.1:5173")
716+
try {
717+
provider = new ClineProvider(
718+
{ ...mockContext, extensionMode: vscode.ExtensionMode.Development },
719+
mockOutputChannel,
720+
"sidebar",
721+
new ContextProxy({ ...mockContext, extensionMode: vscode.ExtensionMode.Development }),
722+
)
723+
// The default axios mock resolves, so the dev-server probe succeeds and the
724+
// HMR HTML branch (instead of the production fallback) is taken.
725+
vi.mocked(axios.get).mockClear()
726+
await provider.resolveWebviewView(mockWebviewView)
719727

720-
const html = mockWebviewView.webview.html
721-
// The dev server URL must be baked into the module script tag and CSP directives.
722-
expect(html).toContain("http://127.0.0.1:5173/src/index.tsx")
723-
expect(html).toContain("ws://127.0.0.1:5173")
724-
expect(html).toContain("http://127.0.0.1:5173/@react-refresh")
728+
// Pin the health-check URL itself: the mock resolves for any URL, so only
729+
// this assertion keeps the probe from silently drifting back to
730+
// `localhost` (the IPv6 resolution failure this branch fixes).
731+
expect(axios.get).toHaveBeenCalledWith("http://127.0.0.1:5173")
725732

726-
if (originalThemeFixtureProbe !== undefined) {
727-
process.env.ROO_CODE_THEME_FIXTURE_PROBE = originalThemeFixtureProbe
733+
const html = mockWebviewView.webview.html
734+
// The dev server URL must be baked into the module script tag and CSP directives.
735+
expect(html).toContain("http://127.0.0.1:5173/src/index.tsx")
736+
expect(html).toContain("ws://127.0.0.1:5173")
737+
expect(html).toContain("http://127.0.0.1:5173/@react-refresh")
738+
} finally {
739+
existsSyncSpy.mockRestore()
740+
// Always restore the probe flag (even when an assertion above throws),
741+
// so later tests outside the fixture-probe describe block cannot observe
742+
// this test's deletion.
743+
if (originalThemeFixtureProbe !== undefined) {
744+
process.env.ROO_CODE_THEME_FIXTURE_PROBE = originalThemeFixtureProbe
745+
} else {
746+
delete process.env.ROO_CODE_THEME_FIXTURE_PROBE
747+
}
728748
}
729749
})
730750

0 commit comments

Comments
 (0)