(t *testing.T)
| 41 | } |
| 42 | |
| 43 | func TestDocsWriteUpdate_WithTab(t *testing.T) { |
| 44 | var batchRequests [][]*docs.Request |
| 45 | var includeTabsCalls int |
| 46 | |
| 47 | docSvc, cleanup := newDocsServiceForTest(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 48 | path := r.URL.Path |
| 49 | switch { |
| 50 | case r.Method == http.MethodPost && strings.Contains(path, ":batchUpdate"): |
| 51 | var req docs.BatchUpdateDocumentRequest |
| 52 | if err := json.NewDecoder(r.Body).Decode(&req); err != nil { |
| 53 | t.Fatalf("decode request: %v", err) |
| 54 | } |
| 55 | batchRequests = append(batchRequests, req.Requests) |
| 56 | id := strings.TrimSuffix(strings.TrimPrefix(path, "/v1/documents/"), ":batchUpdate") |
| 57 | w.Header().Set("Content-Type", "application/json") |
| 58 | _ = json.NewEncoder(w).Encode(map[string]any{"documentId": id}) |
| 59 | return |
| 60 | case r.Method == http.MethodGet && strings.HasPrefix(path, "/v1/documents/"): |
| 61 | if strings.Contains(r.URL.RawQuery, "includeTabsContent=true") { |
| 62 | includeTabsCalls++ |
| 63 | } |
| 64 | w.Header().Set("Content-Type", "application/json") |
| 65 | _ = json.NewEncoder(w).Encode(tabsDocWithEndIndex()) |
| 66 | return |
| 67 | default: |
| 68 | http.NotFound(w, r) |
| 69 | return |
| 70 | } |
| 71 | })) |
| 72 | defer cleanup() |
| 73 | |
| 74 | flags := &RootFlags{Account: "a@b.com"} |
| 75 | ctx := withDocsTestService(newCmdRuntimeOutputContext(t, io.Discard, io.Discard), docSvc) |
| 76 | |
| 77 | if err := runKong(t, &DocsWriteCmd{}, []string{"doc1", "--text", "hello", "--tab", "Second"}, ctx, flags); err != nil { |
| 78 | t.Fatalf("write replace: %v", err) |
| 79 | } |
| 80 | if got := batchRequests[0]; len(got) != 2 || got[0].DeleteContentRange == nil || got[1].InsertText == nil { |
| 81 | t.Fatalf("unexpected write requests: %#v", got) |
| 82 | } |
| 83 | if got := batchRequests[0][0].DeleteContentRange.Range; got.TabId != "t.second" || got.EndIndex != 19 { |
| 84 | t.Fatalf("unexpected delete range: %#v", got) |
| 85 | } |
| 86 | if got := batchRequests[0][1].InsertText.Location; got.TabId != "t.second" || got.Index != 1 { |
| 87 | t.Fatalf("unexpected write insert location: %#v", got) |
| 88 | } |
| 89 | |
| 90 | if err := runKong(t, &DocsWriteCmd{}, []string{"doc1", "--text", "world", "--append", "--tab", "t.second"}, ctx, flags); err != nil { |
| 91 | t.Fatalf("write append: %v", err) |
| 92 | } |
| 93 | if got := batchRequests[1][0].InsertText.Location; got.TabId != "t.second" || got.Index != 19 { |
| 94 | t.Fatalf("unexpected append insert location: %#v", got) |
| 95 | } |
| 96 | |
| 97 | if err := runKong(t, &DocsWriteCmd{}, []string{"doc1", "--text", "**markdown**", "--append", "--markdown", "--tab", "Second"}, ctx, flags); err != nil { |
| 98 | t.Fatalf("write markdown append: %v", err) |
| 99 | } |
| 100 | if got := batchRequests[2][0].InsertText.Location; got.TabId != "t.second" || got.Index != 19 { |
nothing calls this directly
no test coverage detected