@@ -5,6 +5,7 @@ import { withEnvAsync } from "openclaw/plugin-sdk/test-env";
55import { beforeEach , describe , expect , it , vi } from "vitest" ;
66import { createStreamingResponse } from "../../test-support/streaming-error-response.js" ;
77import { createOllamaWebSearchProvider as createContractOllamaWebSearchProvider } from "../web-search-contract-api.js" ;
8+ import { createLazyOllamaWebSearchProvider } from "./web-search-provider-registration.js" ;
89import { createOllamaWebSearchProvider } from "./web-search-provider.js" ;
910
1011const { fetchWithSsrFGuardMock } = vi . hoisted ( ( ) => ( {
@@ -40,8 +41,10 @@ function createOllamaConfig(provider: OllamaProviderConfigOverride = {}): OpenCl
4041 } ;
4142}
4243
43- async function runOllamaWebSearchSetup ( config : OpenClawConfig ) {
44- const provider = createOllamaWebSearchProvider ( ) ;
44+ async function runOllamaWebSearchSetup (
45+ config : OpenClawConfig ,
46+ provider = createOllamaWebSearchProvider ( ) ,
47+ ) {
4548 if ( ! provider . runSetup ) {
4649 throw new Error ( "Expected Ollama web search setup" ) ;
4750 }
@@ -460,6 +463,14 @@ describe("ollama web search provider", () => {
460463 ) ;
461464 } ) ;
462465
466+ it ( "surfaces API-key guidance for hosted Ollama 401 responses" , async ( ) => {
467+ fetchWithSsrFGuardMock . mockResolvedValue ( guardedResponse ( "" , { status : 401 } ) ) ;
468+
469+ await expect (
470+ runOllamaWebSearch ( createOllamaConfig ( { baseUrl : "https://ollama.com" } ) ) ,
471+ ) . rejects . toThrow ( "Set OLLAMA_API_KEY or configure models.providers.ollama.apiKey" ) ;
472+ } ) ;
473+
463474 it ( "reports malformed Ollama web search JSON with a stable provider error" , async ( ) => {
464475 fetchWithSsrFGuardMock . mockResolvedValueOnce ( guardedResponse ( "{ nope" ) ) ;
465476
@@ -503,6 +514,89 @@ describe("ollama web search provider", () => {
503514 ) ;
504515 } ) ;
505516
517+ it . each ( [
518+ {
519+ name : "the configured model provider" ,
520+ config : createOllamaConfig ( { baseUrl : "https://ollama.com" , apiKey : "hosted-test-key" } ) ,
521+ } ,
522+ {
523+ name : "the plugin's higher-priority web search override" ,
524+ config : {
525+ ...createOllamaConfig ( { apiKey : "hosted-test-key" } ) ,
526+ plugins : {
527+ entries : {
528+ ollama : { config : { webSearch : { baseUrl : "https://ollama.com/v1" } } } ,
529+ } ,
530+ } ,
531+ } ,
532+ } ,
533+ ] ) ( "does not require a local daemon when $name selects hosted search" , async ( { config } ) => {
534+ fetchWithSsrFGuardMock
535+ . mockResolvedValueOnce ( guardedResponse ( { models : [ ] } ) )
536+ . mockResolvedValueOnce ( guardedResponse ( { error : "not signed in" } , { status : 401 } ) ) ;
537+
538+ const { next, notes } = await runOllamaWebSearchSetup (
539+ config ,
540+ createLazyOllamaWebSearchProvider ( ) ,
541+ ) ;
542+
543+ expect ( next ) . toBe ( config ) ;
544+ expect ( notes ) . toEqual ( [ ] ) ;
545+ expect ( fetchWithSsrFGuardMock ) . not . toHaveBeenCalled ( ) ;
546+ } ) ;
547+
548+ it . each ( [
549+ {
550+ name : "the configured model provider" ,
551+ config : createOllamaConfig ( { baseUrl : "https://ollama.com" } ) ,
552+ } ,
553+ {
554+ name : "the plugin's higher-priority web search override" ,
555+ config : {
556+ ...createOllamaConfig ( ) ,
557+ plugins : {
558+ entries : {
559+ ollama : { config : { webSearch : { baseUrl : "https://ollama.com/v1" } } } ,
560+ } ,
561+ } ,
562+ } ,
563+ } ,
564+ ] ) ( "warns when $name selects hosted search without an API key" , async ( { config } ) => {
565+ await withEnvAsync ( { OLLAMA_API_KEY : undefined } , async ( ) => {
566+ const { next, notes } = await runOllamaWebSearchSetup (
567+ config ,
568+ createLazyOllamaWebSearchProvider ( ) ,
569+ ) ;
570+
571+ expect ( next ) . toBe ( config ) ;
572+ expect ( notes ) . toEqual ( [
573+ {
574+ title : "Ollama Web Search" ,
575+ message :
576+ "Hosted Ollama Web Search requires an API key. Set OLLAMA_API_KEY or configure models.providers.ollama.apiKey." ,
577+ } ,
578+ ] ) ;
579+ expect ( fetchWithSsrFGuardMock ) . not . toHaveBeenCalled ( ) ;
580+ } ) ;
581+ } ) ;
582+
583+ it ( "accepts the ambient API key for hosted Ollama search setup" , async ( ) => {
584+ await withEnvAsync ( { OLLAMA_API_KEY : "hosted-env-key" } , async ( ) => {
585+ const config = createOllamaConfig ( {
586+ baseUrl : "https://ollama.com" ,
587+ apiKey : "OLLAMA_API_KEY" ,
588+ } ) ;
589+ const { next, notes } = await runOllamaWebSearchSetup (
590+ config ,
591+ createLazyOllamaWebSearchProvider ( ) ,
592+ ) ;
593+
594+ expect ( next ) . toBe ( config ) ;
595+ expect ( notes ) . toEqual ( [ ] ) ;
596+ expect ( fetchWithSsrFGuardMock ) . not . toHaveBeenCalled ( ) ;
597+ } ) ;
598+ } ) ;
599+
506600 it ( "resolves env var when config apiKey is a marker string" , async ( ) => {
507601 await withEnvAsync ( { OLLAMA_API_KEY : "real-secret-from-env" } , async ( ) => {
508602 mockSuccessfulSearchResponse ( ) ;
0 commit comments