@@ -12,6 +12,7 @@ import (
1212 "github.com/spf13/cobra"
1313
1414 "github.com/larksuite/cli/errs"
15+ "github.com/larksuite/cli/internal/client"
1516 "github.com/larksuite/cli/internal/cmdutil"
1617 "github.com/larksuite/cli/internal/core"
1718 "github.com/larksuite/cli/internal/httpmock"
@@ -198,3 +199,58 @@ func TestCallAPITyped_NonObjectJSON(t *testing.T) {
198199 t .Errorf ("subtype = %q, want %q" , intErr .Subtype , errs .SubtypeInvalidResponse )
199200 }
200201}
202+
203+ // TestDoAPIJSONTyped_Success returns the data object on code 0, confirming the
204+ // typed DoAPIJSON replacement preserves the success contract of DoAPIJSON.
205+ func TestDoAPIJSONTyped_Success (t * testing.T ) {
206+ rt , reg := newCallAPITypedRuntime (t )
207+ reg .Register (& httpmock.Stub {
208+ Method : "GET" ,
209+ URL : "/open-apis/x/z" ,
210+ Body : map [string ]interface {}{"code" : float64 (0 ), "data" : map [string ]interface {}{"id" : "z1" }},
211+ })
212+
213+ data , err := rt .DoAPIJSONTyped ("GET" , "/open-apis/x/z" , nil , nil )
214+ if err != nil {
215+ t .Fatalf ("unexpected error: %v" , err )
216+ }
217+ if data ["id" ] != "z1" {
218+ t .Errorf ("data[id] = %v, want z1" , data ["id" ])
219+ }
220+ }
221+
222+ func TestDoAPIJSONTyped_RawClientErrorBecomesTypedInternal (t * testing.T ) {
223+ rt := TestNewRuntimeContextForAPI (context .Background (), & cobra.Command {Use : "+x" }, & core.CliConfig {}, nil , core .AsUser )
224+ rt .apiClientFunc = func () (* client.APIClient , error ) {
225+ return nil , errors .New ("raw client construction error" )
226+ }
227+
228+ _ , err := rt .DoAPIJSONTyped ("GET" , "/open-apis/x/z" , nil , nil )
229+ var internalErr * errs.InternalError
230+ if ! errors .As (err , & internalErr ) {
231+ t .Fatalf ("expected raw client errors to be lifted to typed internal errors, got %T: %v" , err , err )
232+ }
233+ if internalErr .Subtype != errs .SubtypeUnknown {
234+ t .Errorf ("subtype = %q, want %q" , internalErr .Subtype , errs .SubtypeUnknown )
235+ }
236+ }
237+
238+ // TestDoAPIJSONTyped_NonZeroCode classifies a non-zero API code into a typed
239+ // errs.* error (carrying log_id), never a legacy output.ExitError envelope.
240+ func TestDoAPIJSONTyped_NonZeroCode (t * testing.T ) {
241+ rt , reg := newCallAPITypedRuntime (t )
242+ reg .Register (& httpmock.Stub {
243+ Method : "POST" ,
244+ URL : "/open-apis/x/z" ,
245+ Body : map [string ]interface {}{"code" : float64 (1061044 ), "msg" : "boom" , "log_id" : "lz" },
246+ })
247+
248+ _ , err := rt .DoAPIJSONTyped ("POST" , "/open-apis/x/z" , nil , map [string ]any {})
249+ p , ok := errs .ProblemOf (err )
250+ if ! ok {
251+ t .Fatalf ("expected a typed errs.* error, got %T: %v" , err , err )
252+ }
253+ if p .LogID != "lz" {
254+ t .Errorf ("LogID = %q, want lz" , p .LogID )
255+ }
256+ }
0 commit comments