|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { describe, expect, it } from "vitest"; |
| 5 | +import fs from "node:fs"; |
| 6 | +import os from "node:os"; |
| 7 | +import path from "node:path"; |
| 8 | + |
| 9 | +const { |
| 10 | + isHijackedDockerInternalUrl, |
| 11 | +} = require("../../../dist/lib/inference/onboard-host-docker-internal"); |
| 12 | +const { isSandboxInternalUrl, probeOpenAiLikeEndpoint } = require("../../../dist/lib/inference/onboard-probes"); |
| 13 | + |
| 14 | +describe("host.docker.internal onboarding inference policy", () => { |
| 15 | + it("does not treat host.docker.internal as a usable sandbox URL", () => { |
| 16 | + expect(isSandboxInternalUrl("http://host.docker.internal:11434/v1")).toBe(false); |
| 17 | + expect(isHijackedDockerInternalUrl("http://host.docker.internal:11434/v1")).toBe(true); |
| 18 | + expect(isHijackedDockerInternalUrl("http://host.openshell.internal:11435/v1")).toBe(false); |
| 19 | + expect(isHijackedDockerInternalUrl("https://api.openai.com/v1")).toBe(false); |
| 20 | + }); |
| 21 | + |
| 22 | + it("rejects host.docker.internal URLs with an actionable proxy hint (#3136)", () => { |
| 23 | + const result = probeOpenAiLikeEndpoint( |
| 24 | + "http://host.docker.internal:11434/v1", |
| 25 | + "openai/nemotron-mini", |
| 26 | + "", |
| 27 | + ); |
| 28 | + expect(result.ok).toBe(false); |
| 29 | + expect(result.message).toMatch(/host\.docker\.internal/); |
| 30 | + expect(result.message).toMatch(/host\.openshell\.internal:11435/); |
| 31 | + expect(result.failures).toEqual([ |
| 32 | + expect.objectContaining({ name: "host.docker.internal reachability" }), |
| 33 | + ]); |
| 34 | + }); |
| 35 | + |
| 36 | + it("rejects host.docker.internal even when strict chat-completions tool calling is required", () => { |
| 37 | + const result = probeOpenAiLikeEndpoint( |
| 38 | + "http://host.docker.internal:11434/v1", |
| 39 | + "openai/nemotron-mini", |
| 40 | + "", |
| 41 | + { skipResponsesProbe: true, requireChatCompletionsToolCalling: true }, |
| 42 | + ); |
| 43 | + |
| 44 | + expect(result).toMatchObject({ ok: false }); |
| 45 | + expect(result.message).toMatch(/host\.docker\.internal/); |
| 46 | + expect(result.message).toMatch(/host\.openshell\.internal:11435/); |
| 47 | + }); |
| 48 | + |
| 49 | + it("allows explicit Windows-host Ollama validation to probe host.docker.internal", () => { |
| 50 | + const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-host-docker-probe-")); |
| 51 | + const fakeBin = path.join(tmpDir, "bin"); |
| 52 | + const seenUrl = path.join(tmpDir, "url"); |
| 53 | + fs.mkdirSync(fakeBin, { recursive: true }); |
| 54 | + fs.writeFileSync( |
| 55 | + path.join(fakeBin, "curl"), |
| 56 | + `#!/usr/bin/env bash |
| 57 | +outfile="" |
| 58 | +url="" |
| 59 | +while [ "$#" -gt 0 ]; do |
| 60 | + case "$1" in |
| 61 | + -o) outfile="$2"; shift 2 ;; |
| 62 | + -w) shift 2 ;; |
| 63 | + *) url="$1"; shift ;; |
| 64 | + esac |
| 65 | +done |
| 66 | +printf '%s' "$url" > "${seenUrl}" |
| 67 | +if [ -n "$outfile" ]; then |
| 68 | + cat <<'JSON' > "$outfile" |
| 69 | +{"choices":[{"message":{"tool_calls":[{"id":"call_1","type":"function","function":{"name":"sessions_send","arguments":"{\\"message\\":\\"hello\\"}"}}]}}]} |
| 70 | +JSON |
| 71 | +fi |
| 72 | +printf '200' |
| 73 | +exit 0 |
| 74 | +`, |
| 75 | + { mode: 0o755 }, |
| 76 | + ); |
| 77 | + |
| 78 | + const originalPath = process.env.PATH; |
| 79 | + process.env.PATH = `${fakeBin}:${originalPath || ""}`; |
| 80 | + try { |
| 81 | + const result = probeOpenAiLikeEndpoint( |
| 82 | + "http://host.docker.internal:11434/v1", |
| 83 | + "openai/nemotron-mini", |
| 84 | + "", |
| 85 | + { |
| 86 | + skipResponsesProbe: true, |
| 87 | + requireChatCompletionsToolCalling: true, |
| 88 | + allowHostDockerInternal: true, |
| 89 | + }, |
| 90 | + ); |
| 91 | + |
| 92 | + expect(result).toMatchObject({ |
| 93 | + ok: true, |
| 94 | + api: "openai-completions", |
| 95 | + label: "Chat Completions API", |
| 96 | + }); |
| 97 | + expect(fs.readFileSync(seenUrl, "utf8")).toBe( |
| 98 | + "http://host.docker.internal:11434/v1/chat/completions", |
| 99 | + ); |
| 100 | + } finally { |
| 101 | + process.env.PATH = originalPath; |
| 102 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 103 | + } |
| 104 | + }); |
| 105 | +}); |
0 commit comments