|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "google.golang.org/api/docs/v1" |
| 11 | +) |
| 12 | + |
| 13 | +// TestDocsInsertCmd_MarkdownAtIndex verifies that `docs insert --markdown |
| 14 | +// --index N` converts the markdown and places the converted block at the |
| 15 | +// resolved index: a body InsertText at N plus the converter's heading |
| 16 | +// paragraph-style request, rather than a single plain InsertText. |
| 17 | +func TestDocsInsertCmd_MarkdownAtIndex(t *testing.T) { |
| 18 | + t.Parallel() |
| 19 | + |
| 20 | + var batchRequests [][]*docs.Request |
| 21 | + var getCalls int |
| 22 | + |
| 23 | + docSvc, cleanup := newDocsServiceForTest(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 24 | + switch { |
| 25 | + case r.Method == http.MethodGet && strings.HasPrefix(r.URL.Path, "/v1/documents/"): |
| 26 | + getCalls++ |
| 27 | + w.Header().Set("Content-Type", "application/json") |
| 28 | + _ = json.NewEncoder(w).Encode(docBodyWithEndIndex(42)) |
| 29 | + case r.Method == http.MethodPost && strings.Contains(r.URL.Path, ":batchUpdate"): |
| 30 | + var req docs.BatchUpdateDocumentRequest |
| 31 | + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { |
| 32 | + t.Fatalf("decode: %v", err) |
| 33 | + } |
| 34 | + batchRequests = append(batchRequests, req.Requests) |
| 35 | + w.Header().Set("Content-Type", "application/json") |
| 36 | + _ = json.NewEncoder(w).Encode(map[string]any{"documentId": "doc1"}) |
| 37 | + default: |
| 38 | + http.NotFound(w, r) |
| 39 | + } |
| 40 | + })) |
| 41 | + defer cleanup() |
| 42 | + |
| 43 | + flags := &RootFlags{ Account: "[email protected]"} |
| 44 | + ctx := withDocsTestService(newCmdRuntimeOutputContext(t, io.Discard, io.Discard), docSvc) |
| 45 | + |
| 46 | + markdown := "## Heading\n\n```\ncode\n```" |
| 47 | + if err := runKong(t, &DocsInsertCmd{}, []string{"doc1", markdown, "--markdown", "--index", "7"}, ctx, flags); err != nil { |
| 48 | + t.Fatalf("insert --markdown: %v", err) |
| 49 | + } |
| 50 | + |
| 51 | + // Explicit --index means no GET is needed to resolve placement, and the |
| 52 | + // markdown has no heading links so no rewrite GET is issued either. |
| 53 | + if getCalls != 0 { |
| 54 | + t.Fatalf("explicit --index with no heading links should not GET the doc, got %d", getCalls) |
| 55 | + } |
| 56 | + if len(batchRequests) != 1 { |
| 57 | + t.Fatalf("expected one batchUpdate, got %d", len(batchRequests)) |
| 58 | + } |
| 59 | + |
| 60 | + var insertAtIndex *docs.InsertTextRequest |
| 61 | + var sawHeadingStyle bool |
| 62 | + var fencedCodeStyle *docs.UpdateTextStyleRequest |
| 63 | + for _, req := range batchRequests[0] { |
| 64 | + if req.InsertText != nil && req.InsertText.Location != nil && req.InsertText.Location.Index == 7 { |
| 65 | + insertAtIndex = req.InsertText |
| 66 | + } |
| 67 | + if req.UpdateParagraphStyle != nil && |
| 68 | + req.UpdateParagraphStyle.ParagraphStyle != nil && |
| 69 | + strings.HasPrefix(req.UpdateParagraphStyle.ParagraphStyle.NamedStyleType, "HEADING") { |
| 70 | + sawHeadingStyle = true |
| 71 | + } |
| 72 | + if req.UpdateTextStyle != nil && strings.Contains(req.UpdateTextStyle.Fields, "weightedFontFamily") { |
| 73 | + fencedCodeStyle = req.UpdateTextStyle |
| 74 | + } |
| 75 | + } |
| 76 | + if insertAtIndex == nil { |
| 77 | + t.Fatalf("expected an InsertText at index 7, requests: %#v", batchRequests[0]) |
| 78 | + } |
| 79 | + if !strings.Contains(insertAtIndex.Text, "Heading") { |
| 80 | + t.Fatalf("expected inserted text to contain the heading text, got %q", insertAtIndex.Text) |
| 81 | + } |
| 82 | + if !sawHeadingStyle { |
| 83 | + t.Fatalf("expected a HEADING paragraph-style request from the markdown converter, requests: %#v", batchRequests[0]) |
| 84 | + } |
| 85 | + assertFencedCodeTextStyle(t, fencedCodeStyle) |
| 86 | +} |
| 87 | + |
| 88 | +func TestDocsInsertCmd_MarkdownAtAnchorUsesRevisionControl(t *testing.T) { |
| 89 | + t.Parallel() |
| 90 | + |
| 91 | + rec := &docsAtAnchorRecorder{} |
| 92 | + doc := docsFindRangeDoc(docsFindRangeParagraph(1, "Before anchor after\n")) |
| 93 | + doc.RevisionId = "rev-markdown-anchor" |
| 94 | + svc := setupDocsAtAnchorTestService(t, doc, rec) |
| 95 | + |
| 96 | + flags := &RootFlags{ Account: "[email protected]"} |
| 97 | + ctx := withDocsTestService(newCmdRuntimeOutputContext(t, io.Discard, io.Discard), svc) |
| 98 | + if err := runKong(t, &DocsInsertCmd{}, []string{"doc1", "## Heading", "--markdown", "--at", "anchor"}, ctx, flags); err != nil { |
| 99 | + t.Fatalf("insert --markdown --at: %v", err) |
| 100 | + } |
| 101 | + if len(rec.batchRequests) != 1 { |
| 102 | + t.Fatalf("batch calls = %d, want 1", len(rec.batchRequests)) |
| 103 | + } |
| 104 | + assertDocsAtAnchorWriteControl(t, rec, 0, "rev-markdown-anchor") |
| 105 | +} |
| 106 | + |
| 107 | +// TestDocsInsertCmd_MarkdownRejectsBatch verifies the --markdown/--batch combo |
| 108 | +// is rejected up front (the markdown path issues its own batchUpdate calls and |
| 109 | +// cannot be queued into a persisted batch). |
| 110 | +func TestDocsInsertCmd_MarkdownRejectsBatch(t *testing.T) { |
| 111 | + t.Parallel() |
| 112 | + |
| 113 | + docSvc, cleanup := newDocsServiceForTest(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 114 | + http.NotFound(w, r) |
| 115 | + })) |
| 116 | + defer cleanup() |
| 117 | + |
| 118 | + flags := &RootFlags{ Account: "[email protected]"} |
| 119 | + ctx := withDocsTestService(newCmdRuntimeOutputContext(t, io.Discard, io.Discard), docSvc) |
| 120 | + |
| 121 | + err := runKong(t, &DocsInsertCmd{}, []string{"doc1", "## H", "--markdown", "--batch", "b1"}, ctx, flags) |
| 122 | + if err == nil || !strings.Contains(err.Error(), "--markdown cannot be combined with --batch") { |
| 123 | + t.Fatalf("expected --markdown/--batch rejection, got %v", err) |
| 124 | + } |
| 125 | +} |
0 commit comments