diff --git a/cli/cmd/auth/status_jq_precision_test.go b/cli/cmd/auth/status_jq_precision_test.go new file mode 100644 index 00000000..4f164805 --- /dev/null +++ b/cli/cmd/auth/status_jq_precision_test.go @@ -0,0 +1,32 @@ +package auth + +import ( + "context" + "net/http" + "net/http/httptest" + "testing" + + "github.com/Tencent/WeKnora/cli/internal/cmdutil" + "github.com/Tencent/WeKnora/cli/internal/config" + "github.com/Tencent/WeKnora/cli/internal/iostreams" + sdk "github.com/Tencent/WeKnora/client" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestStatusJQPreservesTenantID(t *testing.T) { + out, _ := iostreams.SetForTest(t) + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/api/v1/auth/me", r.URL.Path) + _, _ = w.Write([]byte(`{"success":true,"data":{"user":{"id":"user-test","tenant_id":9007199254740993}}}`)) + })) + defer server.Close() + f := &cmdutil.Factory{Config: func() (*config.Config, error) { + return &config.Config{CurrentProfile: "test", Profiles: map[string]config.Profile{ + "test": {Host: server.URL}, + }}, nil + }} + require.NoError(t, runStatus(context.Background(), + &cmdutil.FormatOptions{Mode: cmdutil.FormatJSON, JQ: ".data.tenant_id"}, f, sdk.NewClient(server.URL))) + assert.Equal(t, "9007199254740993\n", out.String()) +} diff --git a/cli/internal/format/filter.go b/cli/internal/format/filter.go index 1a70b7e7..cd4c0633 100644 --- a/cli/internal/format/filter.go +++ b/cli/internal/format/filter.go @@ -69,7 +69,9 @@ func writeJQ(w io.Writer, raw []byte, expr string) error { return &JQError{Err: fmt.Errorf("jq parse: %w", err)} } var input any - if err := json.Unmarshal(raw, &input); err != nil { + decoder := json.NewDecoder(bytes.NewReader(raw)) + decoder.UseNumber() + if err := decoder.Decode(&input); err != nil { return fmt.Errorf("jq input parse: %w", err) } iter := query.Run(input)