gogcli/internal/cmd/slides_read_slide_test.go
Chris Parsons 8b08d11777
feat(slides): add add-slide, list-slides, and delete-slide commands (#214)
* feat(slides): add `slides add-slide` command for full-bleed image slides

Adds a new `gog slides add-slide <presentationId> <image>` command that
appends a slide with a full-bleed image and optional speaker notes using
the native Google Slides API (presentations.batchUpdate).

Workflow: create a deck with `gog slides create`, then add slides one at
a time with this command. Supports --notes for inline text and
--notes-file for multiline markdown speaker notes.

Also registers ServiceSlides in the auth layer with proper scopes and
readonly support.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* feat(slides): add list-slides and delete-slide commands

list-slides shows all slide object IDs in a presentation.
delete-slide removes a slide by its object ID.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* feat(slides): add --before flag to add-slide for insertion ordering

Allow inserting a slide before a specific existing slide ID instead of
always appending to the end of the presentation.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* feat(slides): add read-slide, update-notes, and replace-slide commands

Adds three new commands to support in-place editing of existing slides:

- `slides read-slide` — shows slide content including speaker notes,
  text elements, and image references (supports --json output)
- `slides update-notes` — updates speaker notes on an existing slide
  without deleting/re-adding (--notes or --notes-file)
- `slides replace-slide` — atomically swaps the image on an existing
  slide using the ReplaceImage API, optionally updating notes in the
  same batch operation

These eliminate the error-prone delete+add-before workflow when editing
slides in existing decks.

Closes chrismdp/gogcli#1

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix(slides): rebase PR 214, clear notes semantics, and hard-fail missing placeholders (#214) (thanks @chrismdp)

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: Peter Steinberger <steipete@gmail.com>
2026-02-14 00:21:59 +01:00

301 lines
8.0 KiB
Go

package cmd
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"google.golang.org/api/option"
"google.golang.org/api/slides/v1"
"github.com/steipete/gogcli/internal/outfmt"
"github.com/steipete/gogcli/internal/ui"
)
func readSlidePresResponse() map[string]any {
return map[string]any{
"presentationId": "pres1",
"slides": []any{
map[string]any{
"objectId": "slide_1",
"slideProperties": map[string]any{
"notesPage": map[string]any{
"notesProperties": map[string]any{
"speakerNotesObjectId": "notes_1",
},
"pageElements": []any{
map[string]any{
"objectId": "notes_1",
"shape": map[string]any{
"placeholder": map[string]any{"type": "BODY"},
"text": map[string]any{
"textElements": []any{
map[string]any{
"textRun": map[string]any{
"content": "These are speaker notes",
},
},
},
},
},
},
},
},
},
"pageElements": []any{
map[string]any{
"objectId": "text_el_1",
"shape": map[string]any{
"text": map[string]any{
"textElements": []any{
map[string]any{
"textRun": map[string]any{
"content": "Slide Title",
},
},
},
},
},
},
map[string]any{
"objectId": "img_el_1",
"image": map[string]any{
"contentUrl": "https://example.com/image.png",
},
},
},
},
},
}
}
func TestSlidesReadSlide(t *testing.T) {
origSlides := newSlidesService
t.Cleanup(func() { newSlidesService = origSlides })
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if strings.Contains(r.URL.Path, "/presentations/pres1") && r.Method == http.MethodGet {
_ = json.NewEncoder(w).Encode(readSlidePresResponse())
return
}
http.NotFound(w, r)
}))
defer srv.Close()
svc, err := slides.NewService(context.Background(),
option.WithoutAuthentication(),
option.WithHTTPClient(srv.Client()),
option.WithEndpoint(srv.URL+"/"),
)
if err != nil {
t.Fatalf("slides.NewService: %v", err)
}
newSlidesService = func(context.Context, string) (*slides.Service, error) { return svc, nil }
flags := &RootFlags{Account: "a@b.com"}
out := captureStdout(t, func() {
u, uiErr := ui.New(ui.Options{Stdout: os.Stdout, Stderr: io.Discard, Color: "never"})
if uiErr != nil {
t.Fatalf("ui.New: %v", uiErr)
}
ctx := ui.WithUI(context.Background(), u)
cmd := &SlidesReadSlideCmd{
PresentationID: "pres1",
SlideID: "slide_1",
}
if err := cmd.Run(ctx, flags); err != nil {
t.Fatalf("Run: %v", err)
}
})
if !strings.Contains(out, "Slide 1") {
t.Errorf("expected slide number, got: %q", out)
}
if !strings.Contains(out, "These are speaker notes") {
t.Errorf("expected speaker notes, got: %q", out)
}
if !strings.Contains(out, "Slide Title") {
t.Errorf("expected text element, got: %q", out)
}
if !strings.Contains(out, "img_el_1") {
t.Errorf("expected image element, got: %q", out)
}
}
func TestSlidesReadSlide_JSON(t *testing.T) {
origSlides := newSlidesService
t.Cleanup(func() { newSlidesService = origSlides })
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if strings.Contains(r.URL.Path, "/presentations/pres1") && r.Method == http.MethodGet {
_ = json.NewEncoder(w).Encode(readSlidePresResponse())
return
}
http.NotFound(w, r)
}))
defer srv.Close()
svc, err := slides.NewService(context.Background(),
option.WithoutAuthentication(),
option.WithHTTPClient(srv.Client()),
option.WithEndpoint(srv.URL+"/"),
)
if err != nil {
t.Fatalf("slides.NewService: %v", err)
}
newSlidesService = func(context.Context, string) (*slides.Service, error) { return svc, nil }
flags := &RootFlags{Account: "a@b.com"}
out := captureStdout(t, func() {
u, uiErr := ui.New(ui.Options{Stdout: io.Discard, Stderr: io.Discard, Color: "never"})
if uiErr != nil {
t.Fatalf("ui.New: %v", uiErr)
}
ctx := ui.WithUI(context.Background(), u)
ctx = outfmt.WithMode(ctx, outfmt.Mode{JSON: true})
cmd := &SlidesReadSlideCmd{
PresentationID: "pres1",
SlideID: "slide_1",
}
if err := cmd.Run(ctx, flags); err != nil {
t.Fatalf("Run: %v", err)
}
})
var result map[string]any
if err := json.Unmarshal([]byte(out), &result); err != nil {
t.Fatalf("JSON parse: %v\noutput: %q", err, out)
}
if result["slideNumber"] != float64(1) {
t.Errorf("expected slideNumber=1, got %v", result["slideNumber"])
}
if result["notes"] != "These are speaker notes" {
t.Errorf("expected notes text, got %v", result["notes"])
}
if result["slideObjectId"] != "slide_1" {
t.Errorf("expected slideObjectId=slide_1, got %v", result["slideObjectId"])
}
textEls, ok := result["textElements"].([]any)
if !ok || len(textEls) != 1 {
t.Errorf("expected 1 text element, got %v", result["textElements"])
}
imgs, ok := result["images"].([]any)
if !ok || len(imgs) != 1 {
t.Errorf("expected 1 image, got %v", result["images"])
}
}
func TestSlidesReadSlide_NotFound(t *testing.T) {
origSlides := newSlidesService
t.Cleanup(func() { newSlidesService = origSlides })
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if strings.Contains(r.URL.Path, "/presentations/pres1") && r.Method == http.MethodGet {
_ = json.NewEncoder(w).Encode(readSlidePresResponse())
return
}
http.NotFound(w, r)
}))
defer srv.Close()
svc, err := slides.NewService(context.Background(),
option.WithoutAuthentication(),
option.WithHTTPClient(srv.Client()),
option.WithEndpoint(srv.URL+"/"),
)
if err != nil {
t.Fatalf("slides.NewService: %v", err)
}
newSlidesService = func(context.Context, string) (*slides.Service, error) { return svc, nil }
flags := &RootFlags{Account: "a@b.com"}
u, uiErr := ui.New(ui.Options{Stdout: io.Discard, Stderr: io.Discard, Color: "never"})
if uiErr != nil {
t.Fatalf("ui.New: %v", uiErr)
}
ctx := ui.WithUI(context.Background(), u)
cmd := &SlidesReadSlideCmd{
PresentationID: "pres1",
SlideID: "nonexistent",
}
err = cmd.Run(ctx, flags)
if err == nil || !strings.Contains(err.Error(), `slide "nonexistent" not found`) {
t.Fatalf("expected slide-not-found error, got: %v", err)
}
}
func TestSlidesReadSlide_NoNotes(t *testing.T) {
origSlides := newSlidesService
t.Cleanup(func() { newSlidesService = origSlides })
presResp := map[string]any{
"presentationId": "pres1",
"slides": []any{
map[string]any{
"objectId": "slide_1",
"slideProperties": map[string]any{
"notesPage": map[string]any{},
},
"pageElements": []any{},
},
},
}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if strings.Contains(r.URL.Path, "/presentations/pres1") && r.Method == http.MethodGet {
_ = json.NewEncoder(w).Encode(presResp)
return
}
http.NotFound(w, r)
}))
defer srv.Close()
svc, err := slides.NewService(context.Background(),
option.WithoutAuthentication(),
option.WithHTTPClient(srv.Client()),
option.WithEndpoint(srv.URL+"/"),
)
if err != nil {
t.Fatalf("slides.NewService: %v", err)
}
newSlidesService = func(context.Context, string) (*slides.Service, error) { return svc, nil }
flags := &RootFlags{Account: "a@b.com"}
out := captureStdout(t, func() {
u, uiErr := ui.New(ui.Options{Stdout: os.Stdout, Stderr: io.Discard, Color: "never"})
if uiErr != nil {
t.Fatalf("ui.New: %v", uiErr)
}
ctx := ui.WithUI(context.Background(), u)
cmd := &SlidesReadSlideCmd{
PresentationID: "pres1",
SlideID: "slide_1",
}
if err := cmd.Run(ctx, flags); err != nil {
t.Fatalf("Run: %v", err)
}
})
if !strings.Contains(out, "Speaker Notes: (none)") {
t.Errorf("expected '(none)' for empty notes, got: %q", out)
}
}