|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "google.golang.org/api/docs/v1" |
| 12 | +) |
| 13 | + |
| 14 | +func TestDocsPageLayoutCmd_PagelessDefault(t *testing.T) { |
| 15 | + origDocs := newDocsService |
| 16 | + t.Cleanup(func() { newDocsService = origDocs }) |
| 17 | + |
| 18 | + var batchRequests [][]*docs.Request |
| 19 | + var targetDocID string |
| 20 | + |
| 21 | + docSvc, cleanup := newDocsServiceForTest(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 22 | + switch { |
| 23 | + case r.Method == http.MethodPost && strings.Contains(r.URL.Path, ":batchUpdate"): |
| 24 | + // Capture the doc ID from the path: /v1/documents/{id}:batchUpdate |
| 25 | + path := strings.TrimPrefix(r.URL.Path, "/v1/documents/") |
| 26 | + targetDocID = strings.TrimSuffix(path, ":batchUpdate") |
| 27 | + var req docs.BatchUpdateDocumentRequest |
| 28 | + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { |
| 29 | + t.Fatalf("decode: %v", err) |
| 30 | + } |
| 31 | + batchRequests = append(batchRequests, req.Requests) |
| 32 | + w.Header().Set("Content-Type", "application/json") |
| 33 | + _ = json.NewEncoder(w).Encode(map[string]any{"documentId": "doc1"}) |
| 34 | + default: |
| 35 | + http.NotFound(w, r) |
| 36 | + } |
| 37 | + })) |
| 38 | + defer cleanup() |
| 39 | + newDocsService = func(context.Context, string) (*docs.Service, error) { return docSvc, nil } |
| 40 | + |
| 41 | + flags := &RootFlags{ Account: "[email protected]"} |
| 42 | + ctx := newDocsCmdContext(t) |
| 43 | + |
| 44 | + if err := runKong(t, &DocsPageLayoutCmd{}, []string{"doc1"}, ctx, flags); err != nil { |
| 45 | + t.Fatalf("page-layout: %v", err) |
| 46 | + } |
| 47 | + |
| 48 | + if targetDocID != "doc1" { |
| 49 | + t.Fatalf("expected batchUpdate on doc1, got %q", targetDocID) |
| 50 | + } |
| 51 | + if len(batchRequests) != 1 || len(batchRequests[0]) != 1 { |
| 52 | + t.Fatalf("expected 1 batch request with 1 op, got %#v", batchRequests) |
| 53 | + } |
| 54 | + upd := batchRequests[0][0].UpdateDocumentStyle |
| 55 | + if upd == nil { |
| 56 | + t.Fatalf("expected UpdateDocumentStyle, got %#v", batchRequests[0][0]) |
| 57 | + } |
| 58 | + if upd.Fields != "documentFormat" { |
| 59 | + t.Fatalf("expected fields=documentFormat, got %q", upd.Fields) |
| 60 | + } |
| 61 | + if upd.DocumentStyle == nil || upd.DocumentStyle.DocumentFormat == nil { |
| 62 | + t.Fatalf("expected DocumentStyle.DocumentFormat, got %#v", upd.DocumentStyle) |
| 63 | + } |
| 64 | + if upd.DocumentStyle.DocumentFormat.DocumentMode != docsDocumentModePageless { |
| 65 | + t.Fatalf("expected documentMode=PAGELESS, got %q", upd.DocumentStyle.DocumentFormat.DocumentMode) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func TestDocsPageLayoutCmd_Pages(t *testing.T) { |
| 70 | + origDocs := newDocsService |
| 71 | + t.Cleanup(func() { newDocsService = origDocs }) |
| 72 | + |
| 73 | + var batchRequests [][]*docs.Request |
| 74 | + |
| 75 | + docSvc, cleanup := newDocsServiceForTest(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 76 | + switch { |
| 77 | + case r.Method == http.MethodPost && strings.Contains(r.URL.Path, ":batchUpdate"): |
| 78 | + var req docs.BatchUpdateDocumentRequest |
| 79 | + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { |
| 80 | + t.Fatalf("decode: %v", err) |
| 81 | + } |
| 82 | + batchRequests = append(batchRequests, req.Requests) |
| 83 | + w.Header().Set("Content-Type", "application/json") |
| 84 | + _ = json.NewEncoder(w).Encode(map[string]any{"documentId": "doc1"}) |
| 85 | + default: |
| 86 | + http.NotFound(w, r) |
| 87 | + } |
| 88 | + })) |
| 89 | + defer cleanup() |
| 90 | + newDocsService = func(context.Context, string) (*docs.Service, error) { return docSvc, nil } |
| 91 | + |
| 92 | + flags := &RootFlags{ Account: "[email protected]"} |
| 93 | + ctx := newDocsCmdContext(t) |
| 94 | + |
| 95 | + if err := runKong(t, &DocsPageLayoutCmd{}, []string{"doc1", "--layout=pages"}, ctx, flags); err != nil { |
| 96 | + t.Fatalf("page-layout pages: %v", err) |
| 97 | + } |
| 98 | + |
| 99 | + if len(batchRequests) != 1 { |
| 100 | + t.Fatalf("expected 1 batch request, got %d", len(batchRequests)) |
| 101 | + } |
| 102 | + upd := batchRequests[0][0].UpdateDocumentStyle |
| 103 | + if upd == nil || upd.DocumentStyle == nil || upd.DocumentStyle.DocumentFormat == nil { |
| 104 | + t.Fatalf("unexpected request shape: %#v", batchRequests[0][0]) |
| 105 | + } |
| 106 | + if upd.DocumentStyle.DocumentFormat.DocumentMode != docsDocumentModePages { |
| 107 | + t.Fatalf("expected documentMode=PAGES, got %q", upd.DocumentStyle.DocumentFormat.DocumentMode) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func TestDocsPageLayoutCmd_EmptyDocID(t *testing.T) { |
| 112 | + flags := &RootFlags{ Account: "[email protected]"} |
| 113 | + ctx := newDocsCmdContext(t) |
| 114 | + err := runKong(t, &DocsPageLayoutCmd{}, []string{""}, ctx, flags) |
| 115 | + if err == nil || !strings.Contains(err.Error(), "empty docId") { |
| 116 | + t.Fatalf("expected empty docId error, got %v", err) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +func TestDocsPageLayoutCmd_InvalidLayoutRejected(t *testing.T) { |
| 121 | + flags := &RootFlags{ Account: "[email protected]"} |
| 122 | + ctx := newDocsCmdContext(t) |
| 123 | + err := runKong(t, &DocsPageLayoutCmd{}, []string{"doc1", "--layout=portrait"}, ctx, flags) |
| 124 | + if err == nil { |
| 125 | + t.Fatalf("expected enum validation error, got nil") |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func TestNormalizePageLayout(t *testing.T) { |
| 130 | + cases := []struct { |
| 131 | + in string |
| 132 | + want string |
| 133 | + wantErr bool |
| 134 | + }{ |
| 135 | + {"pageless", docsDocumentModePageless, false}, |
| 136 | + {"PAGELESS", docsDocumentModePageless, false}, |
| 137 | + {"paged", docsDocumentModePages, false}, |
| 138 | + {"pages", docsDocumentModePages, false}, |
| 139 | + {" Paged ", docsDocumentModePages, false}, |
| 140 | + {"", "", true}, |
| 141 | + {"weird", "", true}, |
| 142 | + } |
| 143 | + for _, tc := range cases { |
| 144 | + got, err := normalizePageLayout(tc.in) |
| 145 | + if tc.wantErr { |
| 146 | + if err == nil { |
| 147 | + t.Errorf("normalizePageLayout(%q): expected error, got %q", tc.in, got) |
| 148 | + } |
| 149 | + continue |
| 150 | + } |
| 151 | + if err != nil { |
| 152 | + t.Errorf("normalizePageLayout(%q): unexpected error: %v", tc.in, err) |
| 153 | + continue |
| 154 | + } |
| 155 | + if got != tc.want { |
| 156 | + t.Errorf("normalizePageLayout(%q): got %q, want %q", tc.in, got, tc.want) |
| 157 | + } |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +func TestDocsPageLayoutCmd_DryRun(t *testing.T) { |
| 162 | + origDocs := newDocsService |
| 163 | + t.Cleanup(func() { newDocsService = origDocs }) |
| 164 | + |
| 165 | + newDocsService = func(context.Context, string) (*docs.Service, error) { |
| 166 | + t.Fatal("docs service should not be created on dry-run") |
| 167 | + return nil, errors.New("unexpected docs service creation") |
| 168 | + } |
| 169 | + |
| 170 | + flags := &RootFlags{ Account: "[email protected]", DryRun: true} |
| 171 | + ctx := newDocsJSONContext(t) |
| 172 | + |
| 173 | + err := (&DocsPageLayoutCmd{DocID: "doc1", Layout: "pageless"}).Run(ctx, flags) |
| 174 | + var exitErr *ExitError |
| 175 | + if err == nil { |
| 176 | + t.Fatalf("expected dry-run ExitError, got nil") |
| 177 | + } |
| 178 | + if !errors.As(err, &exitErr) || exitErr.Code != 0 { |
| 179 | + t.Fatalf("expected dry-run exit 0, got %v", err) |
| 180 | + } |
| 181 | +} |
0 commit comments