@@ -5,9 +5,12 @@ package doc
55
66import (
77 "context"
8+ "encoding/json"
89 "strings"
910 "testing"
1011
12+ "github.com/larksuite/cli/internal/cmdutil"
13+ "github.com/larksuite/cli/internal/httpmock"
1114 "github.com/larksuite/cli/shortcuts/common"
1215 "github.com/spf13/cobra"
1316)
@@ -96,6 +99,126 @@ func TestDocsFetchAPIVersionV1StillUsesV2Endpoint(t *testing.T) {
9699 }
97100}
98101
102+ func TestDocsFetchMarkdownDetailDowngradesToSimple (t * testing.T ) {
103+ t .Parallel ()
104+
105+ for _ , detail := range []string {"with-ids" , "full" } {
106+ t .Run (detail , func (t * testing.T ) {
107+ t .Parallel ()
108+
109+ runtime := newFetchShortcutTestRuntime (t , "" , map [string ]string {
110+ "doc-format" : "markdown" ,
111+ "detail" : detail ,
112+ })
113+ if err := validateFetchV2 (context .Background (), runtime ); err != nil {
114+ t .Fatalf ("validateFetchV2() error = %v" , err )
115+ }
116+
117+ dry := decodeDocDryRun (t , DocsFetch .DryRun (context .Background (), runtime ))
118+ exportOption , _ := dry .API [0 ].Body ["export_option" ].(map [string ]interface {})
119+ if exportOption == nil {
120+ t .Fatalf ("missing export_option: %#v" , dry .API [0 ].Body )
121+ }
122+ if got := exportOption ["export_block_id" ]; got != false {
123+ t .Fatalf ("export_block_id = %#v, want false after markdown detail downgrade" , got )
124+ }
125+ if got := exportOption ["export_style_attrs" ]; got != false {
126+ t .Fatalf ("export_style_attrs = %#v, want false after markdown detail downgrade" , got )
127+ }
128+ if got := exportOption ["export_cite_extra_data" ]; got != false {
129+ t .Fatalf ("export_cite_extra_data = %#v, want false after markdown detail downgrade" , got )
130+ }
131+ })
132+ }
133+ }
134+
135+ func TestDocsFetchMarkdownDetailDowngradeWarnsInOutput (t * testing.T ) {
136+ t .Setenv ("LARKSUITE_CLI_CONFIG_DIR" , t .TempDir ())
137+
138+ f , stdout , _ , reg := cmdutil .TestFactory (t , docsTestConfigWithAppID ("docs-fetch-detail-warning" ))
139+ reg .Register (& httpmock.Stub {
140+ Method : "POST" ,
141+ URL : "/open-apis/docs_ai/v1/documents/doxcnFetchWarning/fetch" ,
142+ Body : map [string ]interface {}{
143+ "code" : 0 ,
144+ "msg" : "ok" ,
145+ "data" : map [string ]interface {}{
146+ "document" : map [string ]interface {}{
147+ "document_id" : "doxcnFetchWarning" ,
148+ "revision_id" : float64 (1 ),
149+ "content" : "# hello" ,
150+ },
151+ },
152+ },
153+ })
154+
155+ err := mountAndRunDocs (t , DocsFetch , []string {
156+ "+fetch" ,
157+ "--doc" , "doxcnFetchWarning" ,
158+ "--doc-format" , "markdown" ,
159+ "--detail" , "with-ids" ,
160+ "--as" , "bot" ,
161+ }, f , stdout )
162+ if err != nil {
163+ t .Fatalf ("unexpected error: %v" , err )
164+ }
165+
166+ var envelope map [string ]interface {}
167+ if err := json .Unmarshal (stdout .Bytes (), & envelope ); err != nil {
168+ t .Fatalf ("decode output: %v\n raw=%s" , err , stdout .String ())
169+ }
170+ data , _ := envelope ["data" ].(map [string ]interface {})
171+ warnings , _ := data ["warnings" ].([]interface {})
172+ if len (warnings ) != 1 {
173+ t .Fatalf ("warnings = %#v, want one downgrade warning" , data ["warnings" ])
174+ }
175+ if got , _ := warnings [0 ].(string ); ! strings .Contains (got , "returning markdown output" ) || ! strings .Contains (got , "ignoring the unsupported detail option" ) {
176+ t .Fatalf ("unexpected warning: %q" , got )
177+ }
178+ }
179+
180+ func TestDocsFetchMarkdownDetailDowngradeWarnsInPrettyOutput (t * testing.T ) {
181+ t .Setenv ("LARKSUITE_CLI_CONFIG_DIR" , t .TempDir ())
182+
183+ f , stdout , stderr , reg := cmdutil .TestFactory (t , docsTestConfigWithAppID ("docs-fetch-detail-pretty-warning" ))
184+ reg .Register (& httpmock.Stub {
185+ Method : "POST" ,
186+ URL : "/open-apis/docs_ai/v1/documents/doxcnFetchPrettyWarning/fetch" ,
187+ Body : map [string ]interface {}{
188+ "code" : 0 ,
189+ "msg" : "ok" ,
190+ "data" : map [string ]interface {}{
191+ "document" : map [string ]interface {}{
192+ "document_id" : "doxcnFetchPrettyWarning" ,
193+ "revision_id" : float64 (1 ),
194+ "content" : "# hello" ,
195+ },
196+ },
197+ },
198+ })
199+
200+ err := mountAndRunDocs (t , DocsFetch , []string {
201+ "+fetch" ,
202+ "--doc" , "doxcnFetchPrettyWarning" ,
203+ "--doc-format" , "markdown" ,
204+ "--detail" , "full" ,
205+ "--format" , "pretty" ,
206+ "--as" , "bot" ,
207+ }, f , stdout )
208+ if err != nil {
209+ t .Fatalf ("unexpected error: %v" , err )
210+ }
211+
212+ if got := stdout .String (); got != "# hello\n " {
213+ t .Fatalf ("stdout = %q, want markdown content only" , got )
214+ }
215+ if got := stderr .String (); ! strings .Contains (got , "warning: --detail full is only supported with --doc-format xml" ) ||
216+ ! strings .Contains (got , "returning markdown output" ) ||
217+ ! strings .Contains (got , "ignoring the unsupported detail option" ) {
218+ t .Fatalf ("stderr missing downgrade warning: %q" , got )
219+ }
220+ }
221+
99222func TestDocsFetchRejectsLegacyFlags (t * testing.T ) {
100223 tests := []struct {
101224 name string
0 commit comments