|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "google.golang.org/api/option" |
| 13 | + "google.golang.org/api/sheets/v4" |
| 14 | + |
| 15 | + "github.com/steipete/gogcli/internal/ui" |
| 16 | +) |
| 17 | + |
| 18 | +func TestSheetsFormatCmd(t *testing.T) { |
| 19 | + origNew := newSheetsService |
| 20 | + t.Cleanup(func() { newSheetsService = origNew }) |
| 21 | + |
| 22 | + var gotRepeat *sheets.RepeatCellRequest |
| 23 | + |
| 24 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 25 | + path := strings.TrimPrefix(r.URL.Path, "/sheets/v4") |
| 26 | + path = strings.TrimPrefix(path, "/v4") |
| 27 | + switch { |
| 28 | + case strings.HasPrefix(path, "/spreadsheets/s1") && r.Method == http.MethodGet: |
| 29 | + w.Header().Set("Content-Type", "application/json") |
| 30 | + _ = json.NewEncoder(w).Encode(map[string]any{ |
| 31 | + "spreadsheetId": "s1", |
| 32 | + "sheets": []map[string]any{ |
| 33 | + {"properties": map[string]any{"sheetId": 42, "title": "Sheet1"}}, |
| 34 | + }, |
| 35 | + }) |
| 36 | + return |
| 37 | + case strings.Contains(path, "/spreadsheets/s1:batchUpdate") && r.Method == http.MethodPost: |
| 38 | + var req sheets.BatchUpdateSpreadsheetRequest |
| 39 | + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { |
| 40 | + t.Fatalf("decode batchUpdate: %v", err) |
| 41 | + } |
| 42 | + if len(req.Requests) != 1 || req.Requests[0].RepeatCell == nil { |
| 43 | + t.Fatalf("expected repeatCell request, got %#v", req.Requests) |
| 44 | + } |
| 45 | + gotRepeat = req.Requests[0].RepeatCell |
| 46 | + w.Header().Set("Content-Type", "application/json") |
| 47 | + _ = json.NewEncoder(w).Encode(map[string]any{}) |
| 48 | + return |
| 49 | + default: |
| 50 | + http.NotFound(w, r) |
| 51 | + return |
| 52 | + } |
| 53 | + })) |
| 54 | + defer srv.Close() |
| 55 | + |
| 56 | + svc, err := sheets.NewService(context.Background(), |
| 57 | + option.WithoutAuthentication(), |
| 58 | + option.WithHTTPClient(srv.Client()), |
| 59 | + option.WithEndpoint(srv.URL+"/"), |
| 60 | + ) |
| 61 | + if err != nil { |
| 62 | + t.Fatalf("NewService: %v", err) |
| 63 | + } |
| 64 | + newSheetsService = func(context.Context, string) (*sheets.Service, error) { return svc, nil } |
| 65 | + |
| 66 | + flags := &RootFlags{Account: "a@b.com"} |
| 67 | + u, uiErr := ui.New(ui.Options{Stdout: io.Discard, Stderr: io.Discard, Color: "never"}) |
| 68 | + if uiErr != nil { |
| 69 | + t.Fatalf("ui.New: %v", uiErr) |
| 70 | + } |
| 71 | + ctx := ui.WithUI(context.Background(), u) |
| 72 | + cmd := &SheetsFormatCmd{} |
| 73 | + if err := runKong(t, cmd, []string{ |
| 74 | + "s1", |
| 75 | + "Sheet1!B2:C3", |
| 76 | + "--format-json", `{"textFormat":{"bold":true}}`, |
| 77 | + "--format-fields", "userEnteredFormat.textFormat.bold", |
| 78 | + }, ctx, flags); err != nil { |
| 79 | + t.Fatalf("format: %v", err) |
| 80 | + } |
| 81 | + |
| 82 | + if gotRepeat == nil { |
| 83 | + t.Fatal("expected repeatCell request") |
| 84 | + } |
| 85 | + if gotRepeat.Fields != "userEnteredFormat.textFormat.bold" { |
| 86 | + t.Fatalf("unexpected fields: %s", gotRepeat.Fields) |
| 87 | + } |
| 88 | + if gotRepeat.Range == nil { |
| 89 | + t.Fatalf("missing range") |
| 90 | + } |
| 91 | + if gotRepeat.Range.SheetId != 42 { |
| 92 | + t.Fatalf("unexpected sheet id: %d", gotRepeat.Range.SheetId) |
| 93 | + } |
| 94 | + if gotRepeat.Range.StartRowIndex != 1 || gotRepeat.Range.EndRowIndex != 3 { |
| 95 | + t.Fatalf("unexpected row range: %#v", gotRepeat.Range) |
| 96 | + } |
| 97 | + if gotRepeat.Range.StartColumnIndex != 1 || gotRepeat.Range.EndColumnIndex != 3 { |
| 98 | + t.Fatalf("unexpected column range: %#v", gotRepeat.Range) |
| 99 | + } |
| 100 | + if gotRepeat.Cell == nil || gotRepeat.Cell.UserEnteredFormat == nil || gotRepeat.Cell.UserEnteredFormat.TextFormat == nil { |
| 101 | + t.Fatalf("missing format data: %#v", gotRepeat.Cell) |
| 102 | + } |
| 103 | + if !gotRepeat.Cell.UserEnteredFormat.TextFormat.Bold { |
| 104 | + t.Fatalf("expected bold text format, got %#v", gotRepeat.Cell.UserEnteredFormat.TextFormat) |
| 105 | + } |
| 106 | +} |
0 commit comments