✅ Add tests for cmd/activity

This commit is contained in:
OpenWaygate committed 2026-01-22 23:06:54 +08:00
1 parent 03dce65e27
commit 110b494038
6 files changed
+85 -10

No files matched your search

+1
View File
@@ -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",
+13 -1
View File
@@ -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",
],
)
+4 -3
View File
@@ -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)
}
+57
View File
@@ -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")
}
+3
View File
@@ -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
+7 -6
View File
@@ -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
}
}