-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
131 lines (109 loc) · 3.46 KB
/
main_test.go
File metadata and controls
131 lines (109 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"context"
"os"
"strings"
"testing"
"github.com/zalando/go-keyring"
)
func TestConfigure_EmptyToken(t *testing.T) {
// Note: This test will fail in non-terminal environments (like CI)
// because term.ReadPassword requires a real terminal
t.Skip("Skipping test that requires terminal for password input")
}
func TestConfigure_ValidToken(t *testing.T) {
// Note: This test will fail in non-terminal environments (like CI)
// because term.ReadPassword requires a real terminal
t.Skip("Skipping test that requires terminal for password input")
}
func TestConfigure_WhitespaceToken(t *testing.T) {
// Note: This test will fail in non-terminal environments (like CI)
// because term.ReadPassword requires a real terminal
t.Skip("Skipping test that requires terminal for password input")
}
func TestRun_MissingSubCommand(t *testing.T) {
ctx := context.Background()
err := run(ctx, []string{})
if err == nil {
t.Error("Expected error for missing sub-command, got nil")
}
if !strings.Contains(err.Error(), "missing sub-command") {
t.Errorf("Expected 'missing sub-command' error, got: %v", err)
}
}
func TestRun_UnknownSubCommand(t *testing.T) {
// Set SLACK_TOKEN env var to get past token check
oldToken := os.Getenv("SLACK_TOKEN")
os.Setenv("SLACK_TOKEN", "test-token")
defer func() {
if oldToken == "" {
os.Unsetenv("SLACK_TOKEN")
} else {
os.Setenv("SLACK_TOKEN", oldToken)
}
}()
ctx := context.Background()
err := run(ctx, []string{"unknown-command"})
if err == nil {
t.Error("Expected error for unknown sub-command, got nil")
}
if !strings.Contains(err.Error(), "unknown sub-command") {
t.Errorf("Expected 'unknown sub-command' error, got: %v", err)
}
}
func TestConvertMarkdownToMrkdwn_Integration(t *testing.T) {
// This is a simple integration test to ensure the function is accessible
result := convertMarkdownToMrkdwn("**bold**")
if result != "*bold*" {
t.Errorf("Expected '*bold*', got '%s'", result)
}
}
func TestRun_SendMessageMissingArgs(t *testing.T) {
// Set SLACK_TOKEN env var to get past token check
oldToken := os.Getenv("SLACK_TOKEN")
os.Setenv("SLACK_TOKEN", "test-token")
defer func() {
if oldToken == "" {
os.Unsetenv("SLACK_TOKEN")
} else {
os.Setenv("SLACK_TOKEN", oldToken)
}
}()
ctx := context.Background()
// Test with no arguments
err := run(ctx, []string{"send-message"})
if err == nil {
t.Error("Expected error for missing arguments, got nil")
}
if !strings.Contains(err.Error(), "usage:") {
t.Errorf("Expected usage error, got: %v", err)
}
// Test with only channel
err = run(ctx, []string{"send-message", "C1234567890"})
if err == nil {
t.Error("Expected error for missing arguments, got nil")
}
if !strings.Contains(err.Error(), "usage:") {
t.Errorf("Expected usage error, got: %v", err)
}
}
func TestRun_SendMessageMissingToken(t *testing.T) {
// Ensure SLACK_TOKEN env var is not set and keyring is cleared
oldToken := os.Getenv("SLACK_TOKEN")
os.Unsetenv("SLACK_TOKEN")
defer func() {
if oldToken != "" {
os.Setenv("SLACK_TOKEN", oldToken)
}
}()
// Clear keyring to ensure no token is stored
_ = keyring.Delete(keyringService, keyringUser)
ctx := context.Background()
err := run(ctx, []string{"send-message", "C1234567890", "test message"})
if err == nil {
t.Error("Expected error for missing token, got nil")
}
if !strings.Contains(err.Error(), "Slack token must be set") {
t.Errorf("Expected 'Slack token must be set' error, got: %v", err)
}
}