From 110b494038d35c42aff27711739b8e047f0f2134 Mon Sep 17 00:00:00 2001 From: OpenWaygate <27287694+OpenWaygate@users.noreply.github.com> Date: Thu, 22 Jan 2026 22:02:12 +0800 Subject: [PATCH] :white_check_mark: Add tests for cmd/activity --- MODULE.bazel | 1 + cmd/activity/BUILD.bazel | 14 +++++++++- cmd/activity/list.go | 7 ++--- cmd/activity/list_test.go | 57 +++++++++++++++++++++++++++++++++++++++ go.mod | 3 +++ pkg/activity/activity.go | 13 ++++----- 6 files changed, 85 insertions(+), 10 deletions(-) create mode 100644 cmd/activity/list_test.go diff --git a/MODULE.bazel b/MODULE.bazel index 79d8de5..1197072 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -25,6 +25,7 @@ use_repo( "com_github_spf13_cobra", "com_github_spf13_pflag", "com_github_spf13_viper", + "com_github_stretchr_testify", "in_gopkg_yaml_v3", "org_golang_google_adk", "org_golang_google_api", diff --git a/cmd/activity/BUILD.bazel b/cmd/activity/BUILD.bazel index 9520e9d..93db0e1 100644 --- a/cmd/activity/BUILD.bazel +++ b/cmd/activity/BUILD.bazel @@ -1,4 +1,4 @@ -load("@rules_go//go:def.bzl", "go_library") +load("@rules_go//go:def.bzl", "go_library", "go_test") go_library( name = "activity", @@ -18,3 +18,15 @@ go_library( "@com_github_spf13_cobra//:cobra", ], ) + +go_test( + name = "activity_test", + srcs = ["list_test.go"], + embed = [":activity"], + deps = [ + "//pkg/activity", + "@com_github_stretchr_testify//assert", + "@org_golang_google_api//option", + "@org_golang_google_api//youtube/v3:youtube", + ], +) diff --git a/cmd/activity/list.go b/cmd/activity/list.go index 39d716d..f4e8ef2 100644 --- a/cmd/activity/list.go +++ b/cmd/activity/list.go @@ -155,8 +155,8 @@ func listHandler( return &mcp.CallToolResult{Content: []mcp.Content{&mcp.TextContent{Text: writer.String()}}}, nil, nil } -func list(writer io.Writer) error { - a := activity.NewActivity( +func list(writer io.Writer, opts ...activity.Option) error { + defaultOpts := []activity.Option{ activity.WithChannelId(channelId), activity.WithHome(home), activity.WithMaxResults(maxResults), @@ -165,7 +165,8 @@ func list(writer io.Writer) error { activity.WithPublishedBefore(publishedBefore), activity.WithRegionCode(regionCode), activity.WithService(nil), - ) + } + a := activity.NewActivity(append(defaultOpts, opts...)...) return a.List(parts, output, jpath, writer) } diff --git a/cmd/activity/list_test.go b/cmd/activity/list_test.go new file mode 100644 index 0000000..850b882 --- /dev/null +++ b/cmd/activity/list_test.go @@ -0,0 +1,57 @@ +// Copyright 2026 eat-pray-ai & OpenWaygate +// SPDX-License-Identifier: Apache-2.0 + +package activity + +import ( + "bytes" + "context" + "net/http" + "net/http/httptest" + "testing" + + "github.com/eat-pray-ai/yutu/pkg/activity" + "github.com/stretchr/testify/assert" + "google.golang.org/api/option" + "google.golang.org/api/youtube/v3" +) + +func TestList(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + w.Write([]byte(`{ + "items": [ + { + "id": "test-activity-id", + "snippet": { + "title": "Test Activity", + "type": "upload", + "publishedAt": "2024-01-01T00:00:00Z" + }, + "contentDetails": { + "upload": { + "videoId": "test-video-id" + } + } + } + ], + "nextPageToken": "" + }`)) + })) + defer ts.Close() + + svc, err := youtube.NewService(context.Background(), option.WithEndpoint(ts.URL), option.WithAPIKey("test-key")) + if err != nil { + t.Fatalf("failed to create service: %v", err) + } + + var buf bytes.Buffer + + err = list(&buf, activity.WithService(svc)) + + assert.NoError(t, err) + output := buf.String() + assert.Contains(t, output, "test-activity-id") + assert.Contains(t, output, "Test Activity") + assert.Contains(t, output, "upload") +} diff --git a/go.mod b/go.mod index 446e636..61f5622 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/savioxavier/termlink v1.4.3 github.com/spf13/cobra v1.10.2 github.com/spf13/viper v1.21.0 + github.com/stretchr/testify v1.11.1 golang.org/x/oauth2 v0.34.0 ) @@ -20,6 +21,7 @@ require ( github.com/clipperhouse/stringish v0.1.1 // indirect github.com/clipperhouse/uax29/v2 v2.3.0 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.7 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/felixge/httpsnoop v1.0.4 // indirect github.com/go-logr/logr v1.4.3 // indirect github.com/go-logr/stdr v1.2.2 // indirect @@ -34,6 +36,7 @@ require ( github.com/gorilla/websocket v1.5.3 // indirect github.com/mattn/go-runewidth v0.0.19 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/yosida95/uritemplate/v3 v3.0.2 // indirect go.opentelemetry.io/auto/sdk v1.2.1 // indirect diff --git a/pkg/activity/activity.go b/pkg/activity/activity.go index 45dd22a..6ac4cda 100644 --- a/pkg/activity/activity.go +++ b/pkg/activity/activity.go @@ -44,6 +44,13 @@ func NewActivity(opts ...Option) Activity[youtube.Activity] { opt(a) } + if service == nil { + service = auth.NewY2BService( + auth.WithCredential("", pkg.Root.FS()), + auth.WithCacheToken("", pkg.Root.FS()), + ).GetService() + } + return a } @@ -181,12 +188,6 @@ func WithRegionCode(regionCode string) Option { func WithService(svc *youtube.Service) Option { return func(_ *activity) { - if svc == nil { - svc = auth.NewY2BService( - auth.WithCredential("", pkg.Root.FS()), - auth.WithCacheToken("", pkg.Root.FS()), - ).GetService() - } service = svc } }