|
7 | 7 | "net/http" |
8 | 8 | "net/http/httptest" |
9 | 9 | "os" |
| 10 | + "reflect" |
10 | 11 | "strings" |
11 | 12 | "testing" |
12 | 13 |
|
@@ -285,3 +286,122 @@ func TestSheetsLinksCmd_NoLinks(t *testing.T) { |
285 | 286 | t.Errorf("expected 'No links found' on stderr: %q", errOut) |
286 | 287 | } |
287 | 288 | } |
| 289 | + |
| 290 | +func TestSheetsLinksCmd_RichTextRunsAndCellLevelLinks(t *testing.T) { |
| 291 | + origNew := newSheetsService |
| 292 | + t.Cleanup(func() { newSheetsService = origNew }) |
| 293 | + |
| 294 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 295 | + w.Header().Set("Content-Type", "application/json") |
| 296 | + _ = json.NewEncoder(w).Encode(map[string]any{ |
| 297 | + "sheets": []map[string]any{ |
| 298 | + { |
| 299 | + "properties": map[string]any{ |
| 300 | + "title": "Sheet1", |
| 301 | + }, |
| 302 | + "data": []map[string]any{ |
| 303 | + { |
| 304 | + "startRow": 2, |
| 305 | + "startColumn": 2, |
| 306 | + "rowData": []map[string]any{ |
| 307 | + { |
| 308 | + "values": []map[string]any{ |
| 309 | + { |
| 310 | + "formattedValue": "Rich links", |
| 311 | + "userEnteredFormat": map[string]any{ |
| 312 | + "textFormat": map[string]any{ |
| 313 | + "link": map[string]any{"uri": "https://cell.example"}, |
| 314 | + }, |
| 315 | + }, |
| 316 | + "textFormatRuns": []map[string]any{ |
| 317 | + {"startIndex": 0, "format": map[string]any{"link": map[string]any{"uri": "https://cell.example"}}}, |
| 318 | + {"startIndex": 4, "format": map[string]any{"link": map[string]any{"uri": "https://run1.example"}}}, |
| 319 | + {"startIndex": 8, "format": map[string]any{"link": map[string]any{"uri": " https://run2.example "}}}, |
| 320 | + }, |
| 321 | + }, |
| 322 | + }, |
| 323 | + }, |
| 324 | + }, |
| 325 | + }, |
| 326 | + }, |
| 327 | + }, |
| 328 | + }, |
| 329 | + }) |
| 330 | + })) |
| 331 | + defer srv.Close() |
| 332 | + |
| 333 | + svc, err := sheets.NewService(context.Background(), |
| 334 | + option.WithoutAuthentication(), |
| 335 | + option.WithHTTPClient(srv.Client()), |
| 336 | + option.WithEndpoint(srv.URL+"/"), |
| 337 | + ) |
| 338 | + if err != nil { |
| 339 | + t.Fatalf("NewService: %v", err) |
| 340 | + } |
| 341 | + newSheetsService = func(context.Context, string) (*sheets.Service, error) { return svc, nil } |
| 342 | + |
| 343 | + flags := &RootFlags{Account: "a@b.com"} |
| 344 | + u, uiErr := ui.New(ui.Options{Stdout: io.Discard, Stderr: io.Discard, Color: "never"}) |
| 345 | + if uiErr != nil { |
| 346 | + t.Fatalf("ui.New: %v", uiErr) |
| 347 | + } |
| 348 | + ctx := ui.WithUI(context.Background(), u) |
| 349 | + ctx = outfmt.WithMode(ctx, outfmt.Mode{JSON: true}) |
| 350 | + |
| 351 | + out := captureStdout(t, func() { |
| 352 | + if err := runKong(t, &SheetsLinksCmd{}, []string{"s1", "Sheet1!C3"}, ctx, flags); err != nil { |
| 353 | + t.Fatalf("links: %v", err) |
| 354 | + } |
| 355 | + }) |
| 356 | + |
| 357 | + var result map[string]any |
| 358 | + if err := json.Unmarshal([]byte(out), &result); err != nil { |
| 359 | + t.Fatalf("unmarshal: %v (output: %q)", err, out) |
| 360 | + } |
| 361 | + |
| 362 | + linksAny, ok := result["links"].([]any) |
| 363 | + if !ok { |
| 364 | + t.Fatalf("expected links array, got %T", result["links"]) |
| 365 | + } |
| 366 | + if len(linksAny) != 3 { |
| 367 | + t.Fatalf("expected 3 deduped links, got %d", len(linksAny)) |
| 368 | + } |
| 369 | + |
| 370 | + got := make([]string, 0, len(linksAny)) |
| 371 | + for _, entry := range linksAny { |
| 372 | + row := entry.(map[string]any) |
| 373 | + if row["a1"] != "Sheet1!C3" { |
| 374 | + t.Fatalf("unexpected a1: %v", row["a1"]) |
| 375 | + } |
| 376 | + got = append(got, row["link"].(string)) |
| 377 | + } |
| 378 | + |
| 379 | + want := []string{"https://cell.example", "https://run1.example", "https://run2.example"} |
| 380 | + if !reflect.DeepEqual(got, want) { |
| 381 | + t.Fatalf("unexpected links: got %v want %v", got, want) |
| 382 | + } |
| 383 | +} |
| 384 | + |
| 385 | +func TestExtractCellLinks(t *testing.T) { |
| 386 | + cell := &sheets.CellData{ |
| 387 | + Hyperlink: " https://a.example ", |
| 388 | + UserEnteredFormat: &sheets.CellFormat{ |
| 389 | + TextFormat: &sheets.TextFormat{ |
| 390 | + Link: &sheets.Link{Uri: "https://a.example"}, |
| 391 | + }, |
| 392 | + }, |
| 393 | + TextFormatRuns: []*sheets.TextFormatRun{ |
| 394 | + {Format: &sheets.TextFormat{Link: &sheets.Link{Uri: "https://b.example"}}}, |
| 395 | + {Format: &sheets.TextFormat{Link: &sheets.Link{Uri: "https://a.example"}}}, |
| 396 | + nil, |
| 397 | + {Format: nil}, |
| 398 | + {Format: &sheets.TextFormat{Link: &sheets.Link{Uri: " "}}}, |
| 399 | + }, |
| 400 | + } |
| 401 | + |
| 402 | + got := extractCellLinks(cell) |
| 403 | + want := []string{"https://a.example", "https://b.example"} |
| 404 | + if !reflect.DeepEqual(got, want) { |
| 405 | + t.Fatalf("unexpected links: got %v want %v", got, want) |
| 406 | + } |
| 407 | +} |
0 commit comments