1+ // Copyright 2022-2026 Salesforce, Inc.
2+ //
3+ // Licensed under the Apache License, Version 2.0 (the "License");
4+ // you may not use this file except in compliance with the License.
5+ // You may obtain a copy of the License at
6+ //
7+ // http://www.apache.org/licenses/LICENSE-2.0
8+ //
9+ // Unless required by applicable law or agreed to in writing, software
10+ // distributed under the License is distributed on an "AS IS" BASIS,
11+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ // See the License for the specific language governing permissions and
13+ // limitations under the License.
14+
15+ package docs
16+
17+ import (
18+ "context"
19+ "testing"
20+
21+ "github.com/slackapi/slack-cli/internal/shared"
22+ "github.com/slackapi/slack-cli/internal/slacktrace"
23+ "github.com/slackapi/slack-cli/test/testutil"
24+ "github.com/spf13/cobra"
25+ "github.com/stretchr/testify/mock"
26+ )
27+
28+ func Test_Docs_DocsCommand (t * testing.T ) {
29+ testutil .TableTestCommand (t , testutil.CommandTests {
30+ "opens docs homepage without search" : {
31+ Setup : func (t * testing.T , ctx context.Context , cm * shared.ClientsMock , cf * shared.ClientFactory ) {
32+ // No special setup needed for basic functionality
33+ },
34+ ExpectedAsserts : func (t * testing.T , ctx context.Context , cm * shared.ClientsMock ) {
35+ expectedURL := "https://docs.slack.dev"
36+ cm .Browser .AssertCalled (t , "OpenURL" , expectedURL )
37+ cm .IO .AssertCalled (t , "PrintTrace" , mock .Anything , slacktrace .DocsSuccess , mock .Anything )
38+ },
39+ ExpectedOutputs : []string {
40+ "Slack developer docs" ,
41+ "https://docs.slack.dev" ,
42+ },
43+ },
44+ "opens docs with basic search query" : {
45+ CmdArgs : []string {"--search" , "Block Kit" },
46+ ExpectedAsserts : func (t * testing.T , ctx context.Context , cm * shared.ClientsMock ) {
47+ expectedURL := "https://docs.slack.dev/search/?q=Block+Kit"
48+ cm .Browser .AssertCalled (t , "OpenURL" , expectedURL )
49+ cm .IO .AssertCalled (t , "PrintTrace" , mock .Anything , slacktrace .DocsSearchSuccess , mock .Anything )
50+ },
51+ ExpectedOutputs : []string {
52+ "Searching Slack developer docs: \" Block Kit\" " ,
53+ "https://docs.slack.dev/search/?q=Block+Kit" ,
54+ },
55+ },
56+ "handles search query with multiple words" : {
57+ CmdArgs : []string {"--search" , "socket mode authentication" },
58+ ExpectedAsserts : func (t * testing.T , ctx context.Context , cm * shared.ClientsMock ) {
59+ expectedURL := "https://docs.slack.dev/search/?q=socket+mode+authentication"
60+ cm .Browser .AssertCalled (t , "OpenURL" , expectedURL )
61+ cm .IO .AssertCalled (t , "PrintTrace" , mock .Anything , slacktrace .DocsSearchSuccess , mock .Anything )
62+ },
63+ ExpectedOutputs : []string {
64+ "Searching Slack developer docs: \" socket mode authentication\" " ,
65+ "https://docs.slack.dev/search/?q=socket+mode+authentication" ,
66+ },
67+ },
68+ "handles special characters in search query" : {
69+ CmdArgs : []string {"--search" , "API & webhooks" },
70+ ExpectedAsserts : func (t * testing.T , ctx context.Context , cm * shared.ClientsMock ) {
71+ expectedURL := "https://docs.slack.dev/search/?q=API+%26+webhooks"
72+ cm .Browser .AssertCalled (t , "OpenURL" , expectedURL )
73+ cm .IO .AssertCalled (t , "PrintTrace" , mock .Anything , slacktrace .DocsSearchSuccess , mock .Anything )
74+ },
75+ ExpectedOutputs : []string {
76+ "Searching Slack developer docs: \" API & webhooks\" " ,
77+ "https://docs.slack.dev/search/?q=API+%26+webhooks" ,
78+ },
79+ },
80+ "handles search query with quotes" : {
81+ CmdArgs : []string {"--search" , "function \" hello world\" " },
82+ ExpectedAsserts : func (t * testing.T , ctx context.Context , cm * shared.ClientsMock ) {
83+ expectedURL := "https://docs.slack.dev/search/?q=function+%22hello+world%22"
84+ cm .Browser .AssertCalled (t , "OpenURL" , expectedURL )
85+ cm .IO .AssertCalled (t , "PrintTrace" , mock .Anything , slacktrace .DocsSearchSuccess , mock .Anything )
86+ },
87+ ExpectedOutputs : []string {
88+ "Searching Slack developer docs: \" function \" hello world\" \" " ,
89+ "https://docs.slack.dev/search/?q=function+%22hello+world%22" ,
90+ },
91+ },
92+ "handles empty search query as homepage" : {
93+ CmdArgs : []string {"--search" , "" },
94+ ExpectedAsserts : func (t * testing.T , ctx context.Context , cm * shared.ClientsMock ) {
95+ expectedURL := "https://docs.slack.dev"
96+ cm .Browser .AssertCalled (t , "OpenURL" , expectedURL )
97+ cm .IO .AssertCalled (t , "PrintTrace" , mock .Anything , slacktrace .DocsSuccess , mock .Anything )
98+ },
99+ ExpectedOutputs : []string {
100+ "Slack developer docs" ,
101+ "https://docs.slack.dev" ,
102+ },
103+ },
104+ "handles the exact user request example" : {
105+ CmdArgs : []string {"--search" , "something example" },
106+ ExpectedAsserts : func (t * testing.T , ctx context.Context , cm * shared.ClientsMock ) {
107+ expectedURL := "https://docs.slack.dev/search/?q=something+example"
108+ cm .Browser .AssertCalled (t , "OpenURL" , expectedURL )
109+ cm .IO .AssertCalled (t , "PrintTrace" , mock .Anything , slacktrace .DocsSearchSuccess , mock .Anything )
110+ },
111+ ExpectedOutputs : []string {
112+ "Searching Slack developer docs: \" something example\" " ,
113+ "https://docs.slack.dev/search/?q=something+example" ,
114+ },
115+ },
116+ }, func (cf * shared.ClientFactory ) * cobra.Command {
117+ return NewCommand (cf )
118+ })
119+ }
0 commit comments