@@ -7,11 +7,16 @@ import (
77 "fmt"
88 "io/fs"
99 "net"
10+ "net/http"
11+ "net/http/httptest"
1012 "net/url"
1113 "os"
1214 "path/filepath"
15+ "strconv"
16+ "strings"
1317 "syscall"
1418 "testing"
19+ "time"
1520
1621 "github.com/charmbracelet/huh"
1722 "github.com/databricks/cli/libs/aitools/installer"
@@ -42,6 +47,66 @@ func TestClassifyInstallError(t *testing.T) {
4247 err : & installer.BlockedError {Agent : "codex" , Reason : installer .ReasonInstallFailed },
4348 want : protos .AitoolsErrorCategoryPluginInstallFailed ,
4449 },
50+ {
51+ name : "blocked timeout wins over its reason" ,
52+ err : & installer.BlockedError {Agent : "claude-code" , Reason : installer .ReasonCLINotOnPath , TimedOut : true },
53+ want : protos .AitoolsErrorCategoryPluginCLITimeout ,
54+ },
55+ {
56+ name : "release lookup failed" ,
57+ err : fmt .Errorf ("resolve latest skills release: %w" , & installer.FetchError {Stage : installer .FetchStageRelease , StatusCode : 500 , Err : errors .New ("HTTP 500 fetching latest skills release" )}),
58+ want : protos .AitoolsErrorCategoryReleaseLookupFailed ,
59+ },
60+ {
61+ name : "release lookup with no response is NETWORK" ,
62+ err : fmt .Errorf ("resolve latest skills release: %w" , & installer.FetchError {Stage : installer .FetchStageRelease , Err : & url.Error {Op : "Get" , URL : "https://example.test" , Err : & net.DNSError {Err : "no such host" }}}),
63+ want : protos .AitoolsErrorCategoryNetwork ,
64+ },
65+ {
66+ name : "release payload that does not decode is not an invalid manifest" ,
67+ err : & installer.FetchError {Stage : installer .FetchStageRelease , StatusCode : 200 , Err : fmt .Errorf ("parse latest skills release: %w" , & json.SyntaxError {})},
68+ want : protos .AitoolsErrorCategoryReleaseLookupFailed ,
69+ },
70+ {
71+ name : "release lookup rate limited" ,
72+ err : & installer.FetchError {Stage : installer .FetchStageRelease , StatusCode : 403 , RateLimited : true , Err : errors .New ("HTTP 403" )},
73+ want : protos .AitoolsErrorCategoryRateLimited ,
74+ },
75+ {
76+ name : "release lookup denied without rate-limit evidence" ,
77+ err : & installer.FetchError {Stage : installer .FetchStageRelease , StatusCode : 403 , Err : errors .New ("HTTP 403" )},
78+ want : protos .AitoolsErrorCategoryReleaseLookupFailed ,
79+ },
80+ {
81+ name : "manifest fetch rate limited" ,
82+ err : & installer.FetchError {Stage : installer .FetchStageManifest , StatusCode : 429 , RateLimited : true , Err : errors .New ("HTTP 429" )},
83+ want : protos .AitoolsErrorCategoryRateLimited ,
84+ },
85+ {
86+ name : "manifest fetch with no response is NETWORK" ,
87+ err : & installer.FetchError {Stage : installer .FetchStageManifest , Err : fmt .Errorf ("failed to fetch manifest: %w" , & url.Error {Op : "Get" , URL : "https://example.test" , Err : & net.OpError {Op : "dial" , Net : "tcp" , Err : os .NewSyscallError ("connect" , syscall .ECONNREFUSED )}})},
88+ want : protos .AitoolsErrorCategoryNetwork ,
89+ },
90+ {
91+ name : "skill file fetch that timed out is TIMEOUT" ,
92+ err : & installer.FetchError {Stage : installer .FetchStageFile , Err : & url.Error {Op : "Get" , URL : "https://example.test" , Err : os .ErrDeadlineExceeded }},
93+ want : protos .AitoolsErrorCategoryTimeout ,
94+ },
95+ {
96+ name : "skill file fetch failed" ,
97+ err : fmt .Errorf ("failed to fetch skills/x/SKILL.md: %w" , & installer.FetchError {Stage : installer .FetchStageFile , StatusCode : 500 , Err : errors .New ("HTTP 500" )}),
98+ want : protos .AitoolsErrorCategorySkillsFetchFailed ,
99+ },
100+ {
101+ name : "manifest missing at the pinned ref" ,
102+ err : & installer.FetchError {Stage : installer .FetchStageManifest , StatusCode : 404 , Err : fmt .Errorf ("skills manifest at x@v9: %w" , clicompat .ErrNotFound )},
103+ want : protos .AitoolsErrorCategoryManifestNotFound ,
104+ },
105+ {
106+ name : "state file invalid" ,
107+ err : fmt .Errorf ("failed to load install state: %w" , & installer.StateError {Path : "/s/.state.json" , Err : errors .New ("failed to parse state file" )}),
108+ want : protos .AitoolsErrorCategoryStateInvalid ,
109+ },
45110 {
46111 name : "blocked error with unknown reason is uncategorized" ,
47112 err : & installer.BlockedError {Agent : "codex" , Reason : "some-future-reason" },
@@ -102,12 +167,12 @@ func TestClassifyInstallErrorGenericCauses(t *testing.T) {
102167 err error
103168 want protos.AitoolsErrorCategory
104169 }{
105- {"network" , fmt .Errorf ("failed to fetch manifest : %w" , urlErr ), protos .AitoolsErrorCategoryNetwork },
170+ {"network" , fmt .Errorf ("fetch: %w" , urlErr ), protos .AitoolsErrorCategoryNetwork },
106171 // The shape net/http returns for a refused connection: the Errno sits under the network types.
107- {"network errno" , fmt .Errorf ("failed to fetch manifest : %w" , & url.Error {Op : "Get" , URL : "https://example.test" , Err : & net.OpError {Op : "dial" , Net : "tcp" , Err : os .NewSyscallError ("connect" , syscall .ECONNREFUSED )}}), protos .AitoolsErrorCategoryNetwork },
172+ {"network errno" , fmt .Errorf ("fetch: %w" , & url.Error {Op : "Get" , URL : "https://example.test" , Err : & net.OpError {Op : "dial" , Net : "tcp" , Err : os .NewSyscallError ("connect" , syscall .ECONNREFUSED )}}), protos .AitoolsErrorCategoryNetwork },
108173 // A sandbox that blocks outbound sockets: a network failure, even though the Errno is EACCES.
109- {"network permission denied" , fmt .Errorf ("failed to fetch manifest : %w" , & url.Error {Op : "Get" , URL : "https://example.test" , Err : & net.OpError {Op : "dial" , Net : "tcp" , Err : os .NewSyscallError ("connect" , syscall .EACCES )}}), protos .AitoolsErrorCategoryNetwork },
110- {"network timeout" , fmt .Errorf ("failed to fetch manifest : %w" , & url.Error {Op : "Get" , URL : "https://example.test" , Err : os .ErrDeadlineExceeded }), protos .AitoolsErrorCategoryTimeout },
174+ {"network permission denied" , fmt .Errorf ("fetch: %w" , & url.Error {Op : "Get" , URL : "https://example.test" , Err : & net.OpError {Op : "dial" , Net : "tcp" , Err : os .NewSyscallError ("connect" , syscall .EACCES )}}), protos .AitoolsErrorCategoryNetwork },
175+ {"network timeout" , fmt .Errorf ("fetch: %w" , & url.Error {Op : "Get" , URL : "https://example.test" , Err : os .ErrDeadlineExceeded }), protos .AitoolsErrorCategoryTimeout },
111176 {"deadline" , fmt .Errorf ("fetch: %w" , context .DeadlineExceeded ), protos .AitoolsErrorCategoryTimeout },
112177 {"cancelled" , fmt .Errorf ("fetch: %w" , context .Canceled ), protos .AitoolsErrorCategoryCommandInterrupted },
113178 {"cancelled request" , & url.Error {Op : "Get" , URL : "https://example.test" , Err : context .Canceled }, protos .AitoolsErrorCategoryCommandInterrupted },
@@ -144,15 +209,14 @@ func TestWithCategoryKeepsMessage(t *testing.T) {
144209 assert .ErrorIs (t , err , cause )
145210}
146211
147- // A corrupt install state must not be blamed on the remote manifest. It stays
148- // UNCATEGORIZED until the installer returns a typed state error, which should
149- // change this to STATE_INVALID.
150- func TestClassifyInstallErrorCorruptStateIsUncategorized (t * testing.T ) {
212+ // A corrupt install state must not be blamed on the remote manifest: the
213+ // installer's typed StateError makes it STATE_INVALID.
214+ func TestClassifyInstallErrorCorruptStateIsStateInvalid (t * testing.T ) {
151215 dir := t .TempDir ()
152216 require .NoError (t , os .WriteFile (filepath .Join (dir , ".state.json" ), []byte ("{not json" ), 0o600 ))
153217 _ , err := installer .LoadState (dir )
154218 require .Error (t , err )
155- assert .Equal (t , protos .AitoolsErrorCategoryUncategorized , classifyInstallError (fmt .Errorf ("failed to load install state: %w" , err )))
219+ assert .Equal (t , protos .AitoolsErrorCategoryStateInvalid , classifyInstallError (fmt .Errorf ("failed to load install state: %w" , err )))
156220}
157221
158222// readMissingFile returns the error the OS gives for a missing file, so the
@@ -163,3 +227,96 @@ func readMissingFile(t *testing.T) error {
163227 require .Error (t , err )
164228 return err
165229}
230+
231+ func TestClassifyCancelledManifestFetchIsCommandInterrupted (t * testing.T ) {
232+ ctx , cancel := context .WithCancel (t .Context ())
233+ server := httptest .NewServer (http .HandlerFunc (func (_ http.ResponseWriter , r * http.Request ) {
234+ // Cancel while the request is in flight, then wait for the client to give up.
235+ cancel ()
236+ <- r .Context ().Done ()
237+ }))
238+ t .Cleanup (server .Close )
239+ t .Setenv ("DATABRICKS_SKILLS_BASE_URL" , server .URL )
240+
241+ _ , err := (& installer.GitHubManifestSource {}).FetchManifest (ctx , "v1" )
242+ require .ErrorIs (t , err , context .Canceled )
243+ assert .Equal (t , protos .AitoolsErrorCategoryCommandInterrupted , classifyInstallError (err ))
244+ }
245+
246+ // A response whose body stalls or is cut off by a reset is a transport
247+ // failure, not a failed fetch: it keeps TIMEOUT or NETWORK.
248+ func TestClassifyManifestBodyTransportFailure (t * testing.T ) {
249+ t .Run ("stalled body" , func (t * testing.T ) {
250+ release := make (chan struct {})
251+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
252+ w .Header ().Set ("Content-Length" , "100" )
253+ w .WriteHeader (http .StatusOK )
254+ _ , _ = w .Write ([]byte (`{"skills":` ))
255+ w .(http.Flusher ).Flush ()
256+ select {
257+ case <- release :
258+ case <- r .Context ().Done ():
259+ }
260+ }))
261+ t .Cleanup (server .Close )
262+ t .Cleanup (func () { close (release ) })
263+ t .Setenv ("DATABRICKS_SKILLS_BASE_URL" , server .URL )
264+
265+ ctx , cancel := context .WithTimeout (t .Context (), 200 * time .Millisecond )
266+ defer cancel ()
267+ _ , err := (& installer.GitHubManifestSource {}).FetchManifest (ctx , "v1" )
268+ require .Error (t , err )
269+ assert .Equal (t , protos .AitoolsErrorCategoryTimeout , classifyInstallError (err ))
270+ })
271+
272+ t .Run ("reset mid-body" , func (t * testing.T ) {
273+ err := & installer.FetchError {Stage : installer .FetchStageManifest , StatusCode : http .StatusOK , Err : fmt .Errorf ("failed to read manifest: %w" , & net.OpError {Op : "read" , Net : "tcp" , Err : os .NewSyscallError ("read" , syscall .ECONNRESET )})}
274+ assert .Equal (t , protos .AitoolsErrorCategoryNetwork , classifyInstallError (err ))
275+ })
276+
277+ t .Run ("release body timeout" , func (t * testing.T ) {
278+ err := & installer.FetchError {Stage : installer .FetchStageRelease , StatusCode : http .StatusOK , Err : fmt .Errorf ("parse latest skills release: %w" , os .ErrDeadlineExceeded )}
279+ assert .Equal (t , protos .AitoolsErrorCategoryTimeout , classifyInstallError (err ))
280+ })
281+ }
282+
283+ func TestClassifyRealManifestFetchErrors (t * testing.T ) {
284+ tests := []struct {
285+ name string
286+ status int
287+ body string
288+ // cutOff announces a longer body than is sent, so the connection
289+ // closes mid-body.
290+ cutOff bool
291+ want protos.AitoolsErrorCategory
292+ }{
293+ // A captive portal or broken mirror answers 200 with something else.
294+ {name : "200 with HTML" , status : http .StatusOK , body : "<html>login</html>" , want : protos .AitoolsErrorCategoryManifestInvalid },
295+ {name : "200 with the wrong JSON shape" , status : http .StatusOK , body : `{"skills": []}` , want : protos .AitoolsErrorCategoryManifestInvalid },
296+ {name : "200 with an empty body" , status : http .StatusOK , body : "" , want : protos .AitoolsErrorCategoryManifestInvalid },
297+ {name : "200 with incomplete JSON" , status : http .StatusOK , body : `{"skills":` , want : protos .AitoolsErrorCategoryManifestInvalid },
298+ // The body is read up to 1 MiB: past that the manifest is cut off, so the
299+ // valid object after the padding is never parsed.
300+ {name : "200 with a body over the size cap" , status : http .StatusOK , body : strings .Repeat (" " , 1 << 20 ) + "{}" , want : protos .AitoolsErrorCategoryManifestInvalid },
301+ {name : "connection closed mid-body" , status : http .StatusOK , body : `{"skills":` , cutOff : true , want : protos .AitoolsErrorCategorySkillsFetchFailed },
302+ {name : "404" , status : http .StatusNotFound , want : protos .AitoolsErrorCategoryManifestNotFound },
303+ {name : "503" , status : http .StatusServiceUnavailable , want : protos .AitoolsErrorCategorySkillsFetchFailed },
304+ }
305+ for _ , tc := range tests {
306+ t .Run (tc .name , func (t * testing.T ) {
307+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
308+ if tc .cutOff {
309+ w .Header ().Set ("Content-Length" , strconv .Itoa (len (tc .body )+ 100 ))
310+ }
311+ w .WriteHeader (tc .status )
312+ _ , _ = w .Write ([]byte (tc .body ))
313+ }))
314+ t .Cleanup (server .Close )
315+ t .Setenv ("DATABRICKS_SKILLS_BASE_URL" , server .URL )
316+
317+ _ , err := (& installer.GitHubManifestSource {}).FetchManifest (t .Context (), "v1" )
318+ require .Error (t , err )
319+ assert .Equal (t , tc .want , classifyInstallError (err ))
320+ })
321+ }
322+ }
0 commit comments