11import type { WizardPrompter } from "openclaw/plugin-sdk/setup" ;
22import { afterEach , describe , expect , it , vi } from "vitest" ;
3- import { pullOllamaModel } from "./setup-pull.js" ;
3+ import { pullOllamaModel , pullOllamaModelNonInteractive } from "./setup-pull.js" ;
44
55const fetchWithSsrFGuardMock = vi . hoisted ( ( ) => vi . fn ( ) ) ;
66
@@ -17,6 +17,81 @@ describe("Ollama onboarding model pulls", () => {
1717 fetchWithSsrFGuardMock . mockReset ( ) ;
1818 } ) ;
1919
20+ it ( "uses the canonical Ollama model request field and requires its success terminal" , async ( ) => {
21+ const release = vi . fn ( async ( ) => { } ) ;
22+ fetchWithSsrFGuardMock . mockResolvedValue ( {
23+ response : new Response ( '{"status":"pulling manifest"}\n{"status":"success"}\n' ) ,
24+ release,
25+ } ) ;
26+ const progress = { update : vi . fn ( ) , stop : vi . fn ( ) } ;
27+ const prompter = { progress : vi . fn ( ( ) => progress ) } as unknown as WizardPrompter ;
28+
29+ await expect ( pullOllamaModel ( "http://127.0.0.1:11434" , "gemma4:e2b" , prompter ) ) . resolves . toBe (
30+ true ,
31+ ) ;
32+
33+ const request = fetchWithSsrFGuardMock . mock . calls [ 0 ] ?. [ 0 ] as { init ?: { body ?: string } } ;
34+ expect ( JSON . parse ( request . init ?. body ?? "null" ) ) . toEqual ( { model : "gemma4:e2b" } ) ;
35+ expect ( progress . stop ) . toHaveBeenCalledWith ( "Downloaded gemma4:e2b" ) ;
36+ expect ( release ) . toHaveBeenCalledOnce ( ) ;
37+ } ) ;
38+
39+ it . each ( [
40+ { label : "empty response" , body : "" } ,
41+ { label : "interrupted manifest download" , body : '{"status":"pulling manifest"}\n' } ,
42+ {
43+ label : "interrupted model layer" ,
44+ body : '{"status":"pulling abcdef123456","total":100,"completed":40}\n' ,
45+ } ,
46+ { label : "malformed stream" , body : "not valid json\n" } ,
47+ { label : "incomplete trailing record" , body : '{"status":"success"' } ,
48+ ] ) ( "does not report a completed model pull for an $label" , async ( { body } ) => {
49+ const release = vi . fn ( async ( ) => { } ) ;
50+ fetchWithSsrFGuardMock . mockResolvedValue ( { response : new Response ( body ) , release } ) ;
51+ const progress = { update : vi . fn ( ) , stop : vi . fn ( ) } ;
52+ const prompter = { progress : vi . fn ( ( ) => progress ) } as unknown as WizardPrompter ;
53+
54+ await expect ( pullOllamaModel ( "http://127.0.0.1:11434" , "gemma4:e2b" , prompter ) ) . resolves . toBe (
55+ false ,
56+ ) ;
57+
58+ expect ( progress . stop ) . toHaveBeenCalledWith (
59+ "Failed to download gemma4:e2b: pull stream ended before success" ,
60+ ) ;
61+ expect ( release ) . toHaveBeenCalledOnce ( ) ;
62+ } ) ;
63+
64+ it ( "accepts a final success record without a trailing newline" , async ( ) => {
65+ fetchWithSsrFGuardMock . mockResolvedValue ( {
66+ response : new Response ( '{"status":"success"}' ) ,
67+ release : vi . fn ( async ( ) => { } ) ,
68+ } ) ;
69+ const progress = { update : vi . fn ( ) , stop : vi . fn ( ) } ;
70+ const prompter = { progress : vi . fn ( ( ) => progress ) } as unknown as WizardPrompter ;
71+
72+ await expect ( pullOllamaModel ( "http://127.0.0.1:11434" , "gemma4:e2b" , prompter ) ) . resolves . toBe (
73+ true ,
74+ ) ;
75+ expect ( progress . stop ) . toHaveBeenCalledWith ( "Downloaded gemma4:e2b" ) ;
76+ } ) ;
77+
78+ it ( "reports interrupted pulls as failures during non-interactive setup" , async ( ) => {
79+ fetchWithSsrFGuardMock . mockResolvedValue ( {
80+ response : new Response ( '{"status":"pulling manifest"}\n' ) ,
81+ release : vi . fn ( async ( ) => { } ) ,
82+ } ) ;
83+ const runtime = { log : vi . fn ( ) , error : vi . fn ( ) } ;
84+
85+ await expect (
86+ pullOllamaModelNonInteractive ( "http://127.0.0.1:11434" , "gemma4:e2b" , runtime as never ) ,
87+ ) . resolves . toBe ( false ) ;
88+
89+ expect ( runtime . error ) . toHaveBeenCalledWith (
90+ "Failed to download gemma4:e2b: pull stream ended before success" ,
91+ ) ;
92+ expect ( runtime . log ) . not . toHaveBeenCalledWith ( "Downloaded gemma4:e2b" ) ;
93+ } ) ;
94+
2095 it ( "coerces non-Error stream failures through the shared error contract" , async ( ) => {
2196 const release = vi . fn ( async ( ) => { } ) ;
2297 const response = new Response (
0 commit comments