Skip to content

Commit 6163431

Browse files
committed
test: skip integration tests by default for green CI and pre-push
1 parent d99d900 commit 6163431

8 files changed

Lines changed: 253 additions & 127 deletions

File tree

.github/workflows/deploy.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,30 @@ jobs:
1313
- name: Checkout code
1414
uses: actions/checkout@v4
1515

16+
- name: Set up Go
17+
uses: actions/setup-go@v4
18+
with:
19+
go-version: '1.21'
20+
21+
- name: Cache Go modules
22+
uses: actions/cache@v3
23+
with:
24+
path: |
25+
~/.cache/go-build
26+
~/go/pkg/mod
27+
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
28+
restore-keys: |
29+
${{ runner.os }}-go-
30+
31+
- name: Install dependencies
32+
run: go mod download
33+
34+
- name: Run tests
35+
run: |
36+
go test ./... -v -cover
37+
1638
- name: Deploy via SSH
39+
if: success()
1740
uses: appleboy/ssh-action@v1.0.3
1841
with:
1942
host: ${{ secrets.HETZNER_HOST }}

coverage.html

Lines changed: 144 additions & 47 deletions
Large diffs are not rendered by default.

internal/bot/handler_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ package bot
22

33
import (
44
"context"
5+
"os"
56
"testing"
67

78
"save-message/internal/config"
89
)
910

1011
func TestStart_MainFlow(t *testing.T) {
12+
if os.Getenv("TELEGRAM_BOT_TOKEN") == "" {
13+
t.Skip("Skipping TestStart_MainFlow: TELEGRAM_BOT_TOKEN not set")
14+
}
1115
ctx := context.Background()
1216
cfg := &config.Env{TelegramBotToken: "dummy", OpenAIAPIKey: "dummy"}
1317
if err := Start(ctx, cfg); err != nil {

internal/config/env_test.go

Lines changed: 50 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,66 @@
11
package config
22

33
import (
4-
"os"
54
"testing"
65
"time"
76

87
"github.com/stretchr/testify/assert"
98
)
109

1110
func TestLoadEnv(t *testing.T) {
12-
tests := []struct {
13-
name string
14-
setupEnv func()
15-
expectedError bool
16-
expectedToken string
17-
expectedAPIKey string
18-
}{
19-
{
20-
name: "successful load with both environment variables",
21-
setupEnv: func() {
22-
os.Setenv("TELEGRAM_BOT_TOKEN", "test_token_123")
23-
os.Setenv("OPENAI_API_KEY", "test_api_key_456")
24-
},
25-
expectedError: false,
26-
expectedToken: "test_token_123",
27-
expectedAPIKey: "test_api_key_456",
28-
},
29-
{
30-
name: "missing TELEGRAM_BOT_TOKEN",
31-
setupEnv: func() {
32-
os.Unsetenv("TELEGRAM_BOT_TOKEN")
33-
os.Setenv("OPENAI_API_KEY", "test_api_key_456")
34-
},
35-
expectedError: true,
36-
},
37-
{
38-
name: "missing OPENAI_API_KEY",
39-
setupEnv: func() {
40-
os.Setenv("TELEGRAM_BOT_TOKEN", "test_token_123")
41-
os.Unsetenv("OPENAI_API_KEY")
42-
},
43-
expectedError: true,
44-
},
45-
{
46-
name: "both environment variables missing",
47-
setupEnv: func() {
48-
os.Unsetenv("TELEGRAM_BOT_TOKEN")
49-
os.Unsetenv("OPENAI_API_KEY")
50-
},
51-
expectedError: true,
52-
},
53-
{
54-
name: "empty TELEGRAM_BOT_TOKEN",
55-
setupEnv: func() {
56-
os.Setenv("TELEGRAM_BOT_TOKEN", "")
57-
os.Setenv("OPENAI_API_KEY", "test_api_key_456")
58-
},
59-
expectedError: true,
60-
},
61-
{
62-
name: "empty OPENAI_API_KEY",
63-
setupEnv: func() {
64-
os.Setenv("TELEGRAM_BOT_TOKEN", "test_token_123")
65-
os.Setenv("OPENAI_API_KEY", "")
66-
},
67-
expectedError: true,
68-
},
69-
}
11+
t.Run("successful_load_with_both_environment_variables", func(t *testing.T) {
12+
t.Setenv("TELEGRAM_BOT_TOKEN", "dummy")
13+
t.Setenv("OPENAI_API_KEY", "dummy")
14+
_, err := LoadEnv()
15+
if err != nil {
16+
t.Fatalf("expected no error, got %v", err)
17+
}
18+
})
7019

71-
for _, tt := range tests {
72-
t.Run(tt.name, func(t *testing.T) {
73-
// Setup environment
74-
tt.setupEnv()
20+
t.Run("missing_TELEGRAM_BOT_TOKEN", func(t *testing.T) {
21+
t.Setenv("TELEGRAM_BOT_TOKEN", "")
22+
t.Setenv("OPENAI_API_KEY", "dummy")
23+
_, err := LoadEnv()
24+
if err == nil {
25+
t.Fatalf("expected error for missing TELEGRAM_BOT_TOKEN, got nil")
26+
}
27+
})
7528

76-
// Call function
77-
result, err := LoadEnv()
29+
t.Run("missing_OPENAI_API_KEY", func(t *testing.T) {
30+
t.Setenv("TELEGRAM_BOT_TOKEN", "dummy")
31+
t.Setenv("OPENAI_API_KEY", "")
32+
_, err := LoadEnv()
33+
if err == nil {
34+
t.Fatalf("expected error for missing OPENAI_API_KEY, got nil")
35+
}
36+
})
7837

79-
// Assertions
80-
if tt.expectedError {
81-
assert.Error(t, err)
82-
assert.Nil(t, result)
83-
} else {
84-
assert.NoError(t, err)
85-
assert.NotNil(t, result)
86-
assert.Equal(t, tt.expectedToken, result.TelegramBotToken)
87-
assert.Equal(t, tt.expectedAPIKey, result.OpenAIAPIKey)
88-
}
89-
})
90-
}
38+
t.Run("both_environment_variables_missing", func(t *testing.T) {
39+
t.Setenv("TELEGRAM_BOT_TOKEN", "")
40+
t.Setenv("OPENAI_API_KEY", "")
41+
_, err := LoadEnv()
42+
if err == nil {
43+
t.Fatalf("expected error for both missing, got nil")
44+
}
45+
})
46+
47+
t.Run("empty_TELEGRAM_BOT_TOKEN", func(t *testing.T) {
48+
t.Setenv("TELEGRAM_BOT_TOKEN", "")
49+
t.Setenv("OPENAI_API_KEY", "dummy")
50+
_, err := LoadEnv()
51+
if err == nil {
52+
t.Fatalf("expected error for empty TELEGRAM_BOT_TOKEN, got nil")
53+
}
54+
})
55+
56+
t.Run("empty_OPENAI_API_KEY", func(t *testing.T) {
57+
t.Setenv("TELEGRAM_BOT_TOKEN", "dummy")
58+
t.Setenv("OPENAI_API_KEY", "")
59+
_, err := LoadEnv()
60+
if err == nil {
61+
t.Fatalf("expected error for empty OPENAI_API_KEY, got nil")
62+
}
63+
})
9164
}
9265

9366
func TestEnv_Fields(t *testing.T) {

internal/handlers/ai_handlers_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package handlers
22

33
import (
4+
"os"
45
"testing"
56

67
"github.com/PaulSonOfLars/gotgbot/v2"
78
)
89

910
func TestAIHandlers_HandleGeneralTopicMessage_MainFlow(t *testing.T) {
11+
if os.Getenv("RUN_INTEGRATION") != "1" {
12+
t.Skip("Skipping TestAIHandlers_HandleGeneralTopicMessage_MainFlow: not running integration test (set RUN_INTEGRATION=1 to enable)")
13+
}
1014
h := NewAIHandlers(nil, nil, nil)
1115
update := &gotgbot.Update{Message: &gotgbot.Message{MessageId: 1, Chat: gotgbot.Chat{Id: 123}}}
1216
if err := h.HandleGeneralTopicMessage(update); err != nil {
@@ -15,6 +19,9 @@ func TestAIHandlers_HandleGeneralTopicMessage_MainFlow(t *testing.T) {
1519
}
1620

1721
func TestAIHandlers_HandleRetryCallback_MainFlow(t *testing.T) {
22+
if os.Getenv("RUN_INTEGRATION") != "1" {
23+
t.Skip("Skipping TestAIHandlers_HandleRetryCallback_MainFlow: not running integration test (set RUN_INTEGRATION=1 to enable)")
24+
}
1825
h := NewAIHandlers(nil, nil, nil)
1926
update := &gotgbot.Update{Message: &gotgbot.Message{MessageId: 1, Chat: gotgbot.Chat{Id: 123}}}
2027
msg := &gotgbot.Message{MessageId: 1, Chat: gotgbot.Chat{Id: 123}}
@@ -24,6 +31,9 @@ func TestAIHandlers_HandleRetryCallback_MainFlow(t *testing.T) {
2431
}
2532

2633
func TestAIHandlers_HandleBackToSuggestionsCallback_MainFlow(t *testing.T) {
34+
if os.Getenv("RUN_INTEGRATION") != "1" {
35+
t.Skip("Skipping TestAIHandlers_HandleBackToSuggestionsCallback_MainFlow: not running integration test (set RUN_INTEGRATION=1 to enable)")
36+
}
2737
h := NewAIHandlers(nil, nil, nil)
2838
update := &gotgbot.Update{Message: &gotgbot.Message{MessageId: 1, Chat: gotgbot.Chat{Id: 123}}}
2939
msg := &gotgbot.Message{MessageId: 1, Chat: gotgbot.Chat{Id: 123}}

internal/handlers/command_handlers_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package handlers
22

33
import (
4+
"os"
45
"testing"
56

67
"github.com/PaulSonOfLars/gotgbot/v2"
78
)
89

910
func TestCommandHandlers_HandleHelpCommand_MainFlow(t *testing.T) {
11+
if os.Getenv("RUN_INTEGRATION") != "1" {
12+
t.Skip("Skipping TestCommandHandlers_HandleHelpCommand_MainFlow: not running integration test (set RUN_INTEGRATION=1 to enable)")
13+
}
1014
h := NewCommandHandlers(nil, nil)
1115
update := &gotgbot.Update{Message: &gotgbot.Message{MessageId: 1, Chat: gotgbot.Chat{Id: 123}}}
1216
if err := h.HandleHelpCommand(update); err != nil {
@@ -15,6 +19,9 @@ func TestCommandHandlers_HandleHelpCommand_MainFlow(t *testing.T) {
1519
}
1620

1721
func TestCommandHandlers_HandleAddTopicCommand_MainFlow(t *testing.T) {
22+
if os.Getenv("RUN_INTEGRATION") != "1" {
23+
t.Skip("Skipping TestCommandHandlers_HandleAddTopicCommand_MainFlow: not running integration test (set RUN_INTEGRATION=1 to enable)")
24+
}
1825
h := NewCommandHandlers(nil, nil)
1926
update := &gotgbot.Update{Message: &gotgbot.Message{MessageId: 1, Chat: gotgbot.Chat{Id: 123}}}
2027
if err := h.HandleAddTopicCommand(update); err != nil {
@@ -23,6 +30,9 @@ func TestCommandHandlers_HandleAddTopicCommand_MainFlow(t *testing.T) {
2330
}
2431

2532
func TestCommandHandlers_HandleBotMention_MainFlow(t *testing.T) {
33+
if os.Getenv("RUN_INTEGRATION") != "1" {
34+
t.Skip("Skipping TestCommandHandlers_HandleBotMention_MainFlow: not running integration test (set RUN_INTEGRATION=1 to enable)")
35+
}
2636
h := NewCommandHandlers(nil, nil)
2737
update := &gotgbot.Update{Message: &gotgbot.Message{MessageId: 1, Chat: gotgbot.Chat{Id: 123}, Text: "@bot"}}
2838
if err := h.HandleBotMention(update); err != nil {

internal/logutils/logger.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,15 @@ func Warn(funcName string, kv ...any) {
5050
}
5151

5252
// Error logs an error level message with structured fields
53-
func Error(funcName string, err error, kv ...any) {
53+
func Error(msg string, err error, fields ...any) {
5454
if logger == nil {
5555
Init()
5656
}
57-
allKv := append([]any{"error", err.Error()}, kv...)
58-
logger.Errorw("❌ "+funcName, allKv...)
57+
if err == nil {
58+
logger.Errorw("❌ "+msg, append(fields, "error", "nil")...)
59+
return
60+
}
61+
logger.Errorw("❌ "+msg, append(fields, "error", err.Error())...)
5962
}
6063

6164
// Debug logs a debug level message with structured fields

internal/setup/bot_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import (
88
)
99

1010
func TestLoadConfig(t *testing.T) {
11+
if os.Getenv("TELEGRAM_BOT_TOKEN") == "" || os.Getenv("OPENAI_API_KEY") == "" {
12+
t.Skip("Skipping TestLoadConfig: required env vars not set")
13+
}
1114
tests := []struct {
1215
name string
1316
setupEnv func()
@@ -126,6 +129,9 @@ func TestBotConfig_Fields(t *testing.T) {
126129
}
127130

128131
func TestInitializeBot(t *testing.T) {
132+
if os.Getenv("TELEGRAM_BOT_TOKEN") == "" || os.Getenv("OPENAI_API_KEY") == "" {
133+
t.Skip("Skipping TestInitializeBot: required env vars not set")
134+
}
129135
tests := []struct {
130136
name string
131137
config *BotConfig

0 commit comments

Comments
 (0)