diff --git a/.github/workflows/goreleaser.yml b/.github/workflows/goreleaser.yml index f3f3b1a..083685b 100644 --- a/.github/workflows/goreleaser.yml +++ b/.github/workflows/goreleaser.yml @@ -1,5 +1,4 @@ name: goreleaser - on: push: tags: diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..4c39eda --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,27 @@ +name: go-lint +on: + push: + branches: + - master + pull_request: + branches: + - master + +permissions: + contents: read + # Optional: allow read access to pull request. Use with `only-new-issues` option. + # pull-requests: read + +jobs: + golangci: + name: lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + with: + go-version: stable + - name: golangci-lint + uses: golangci/golangci-lint-action@v6 + with: + version: v1.60 \ No newline at end of file diff --git a/.golangci.yml b/.golangci.yml index 89c31ed..d6cff44 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -280,7 +280,7 @@ linters: - tenv # detects using os.Setenv instead of t.Setenv since Go1.17 - testableexamples # checks if examples are testable (have an expected output) - testifylint # checks usage of github.com/stretchr/testify - - testpackage # makes you use a separate _test package + - tparallel # detects inappropriate usage of t.Parallel() method in your Go test codes - unconvert # removes unnecessary type conversions - unparam # reports unused function parameters @@ -289,6 +289,7 @@ linters: - whitespace # detects leading and trailing whitespace ## you may want to enable + #- testpackage # makes you use a separate _test package #- decorder # checks declaration order and count of types, constants, variables and functions #- exhaustruct # [highly recommend to enable] checks if all structure fields are initialized #- gci # controls golang package import order and makes it always deterministic @@ -339,7 +340,7 @@ issues: - source: "(noinspection|TODO)" linters: [ godot ] - source: "//noinspection" - linters: [ gocritic ] + linters: [ gocritic, gosec ] - path: "_test\\.go" linters: - bodyclose @@ -351,3 +352,6 @@ issues: - wrapcheck - text: 'shadow: declaration of "(ctx|err)" shadows declaration at' linters: [ govet ] + # avoid false positives for tainted input on command execution, user input is only a number + - path: "internal/adapters/converter/magick.go" + linters: [ gosec ] \ No newline at end of file diff --git a/internal/adapters/converter/magick.go b/internal/adapters/converter/magick.go index 218bb84..460dbb2 100644 --- a/internal/adapters/converter/magick.go +++ b/internal/adapters/converter/magick.go @@ -4,11 +4,12 @@ import ( "context" "errors" "fmt" - "github.com/rs/zerolog/log" "hsbot/internal/adapters/file" "os/exec" "path/filepath" "strings" + + "github.com/rs/zerolog/log" ) const MaxPower = 100 @@ -53,22 +54,19 @@ func (m *MagickConverter) Scale(ctx context.Context, imageURL string, power floa return nil, err } - size := MaxPower - (power / PowerFactor) outFile := fmt.Sprintf("%sliq%s", strings.TrimSuffix(path, extension), extension) - dimensions := fmt.Sprintf("%d%%x%d%%", int(size), int(size)) + command := createCommand(m.magickBinary, power, path, outFile) defer file.RemoveTempFile(path) defer file.RemoveTempFile(outFile) - args := append(m.magickBinary, path, "-liquid-rescale", dimensions, outFile) - - log.Debug().Strs("args", args). - Str("dimensions", dimensions). + log.Debug(). + Strs("command", command). Str("outFile", outFile). Str("path", path). Msg("scaling image") - cmd := exec.Command(args[0], args[1:]...) + cmd := exec.Command(command[0], command[1:]...) out, err := cmd.Output() if err != nil { log.Error().Bytes("magickStderr", out).Msg("magick commands failed") @@ -79,3 +77,9 @@ func (m *MagickConverter) Scale(ctx context.Context, imageURL string, power floa return file.GetTempFile(outFile) } + +func createCommand(baseCommands []string, power float32, inFile, outFile string) []string { + size := MaxPower - (power / PowerFactor) + dimensions := fmt.Sprintf("%d%%x%d%%", int(size), int(size)) + return append(baseCommands, inFile, "-liquid-rescale", dimensions, outFile) +} diff --git a/internal/adapters/file/file_test.go b/internal/adapters/file/file_test.go index 1a4e405..1758ec7 100644 --- a/internal/adapters/file/file_test.go +++ b/internal/adapters/file/file_test.go @@ -2,47 +2,50 @@ package file import ( "context" - "github.com/stretchr/testify/assert" "os" "testing" + + "github.com/stretchr/testify/require" + + "github.com/stretchr/testify/assert" ) func TestDownloadFile(t *testing.T) { res, err := DownloadFile(context.Background(), "https://kore.cc/test.txt") - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, []byte("test\n"), res) } func TestSaveTemp(t *testing.T) { res, err := DownloadFile(context.Background(), "https://kore.cc/test.txt") - assert.NoError(t, err) + require.NoError(t, err) path, err := SaveTempFile(res, "txt") - assert.NoError(t, err) + require.NoError(t, err) defer RemoveTempFile(path) stat, err := os.Stat(path) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, int64(5), stat.Size()) } func TestGetTemp(t *testing.T) { res, err := DownloadFile(context.Background(), "https://kore.cc/test.txt") - assert.NoError(t, err) + require.NoError(t, err) path, err := SaveTempFile(res, "txt") - assert.NoError(t, err) + require.NoError(t, err) defer RemoveTempFile(path) stat, err := os.Stat(path) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, int64(5), stat.Size()) file, err := GetTempFile(path) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, []byte("test\n"), file) } diff --git a/internal/adapters/handler/command.go b/internal/adapters/handler/command.go index f149ac9..e9a20df 100644 --- a/internal/adapters/handler/command.go +++ b/internal/adapters/handler/command.go @@ -44,14 +44,19 @@ func (h *CommandHandler) Handle(ctx context.Context, b *bot.Bot, update *models. go getOptionalImage(ctx, b, update, imageURL) go getOptionalAudio(ctx, b, update, audioURL) - go commandHandler.Respond(ctx, h.timeout, &domain.Message{ - ID: update.Message.ID, - ChatID: update.Message.Chat.ID, - Text: update.Message.Text, - ReplyToMessageID: replyToMessageID, - ImageURL: <-imageURL, - AudioURL: <-audioURL, - }) + go func() { + err := commandHandler.Respond(ctx, h.timeout, &domain.Message{ + ID: update.Message.ID, + ChatID: update.Message.Chat.ID, + Text: update.Message.Text, + ReplyToMessageID: replyToMessageID, + ImageURL: <-imageURL, + AudioURL: <-audioURL, + }) + if err != nil { + log.Err(err).Str("command", cmd).Msg("failed to respond to command") + } + }() } func getOptionalImage(ctx context.Context, b *bot.Bot, update *models.Update, url chan string) { diff --git a/internal/core/domain/command_test.go b/internal/core/domain/command_test.go index 8511b38..d5f8d98 100644 --- a/internal/core/domain/command_test.go +++ b/internal/core/domain/command_test.go @@ -3,15 +3,17 @@ package domain import ( "context" "testing" + "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) type MockResponder struct { command string } -func (m *MockResponder) Respond(ctx context.Context, message *Message) error { +func (m *MockResponder) Respond(_ context.Context, _ time.Duration, _ *Message) error { return nil } @@ -24,14 +26,14 @@ func TestRegister(t *testing.T) { mr := &MockResponder{command: "/test"} cr.Register(mr) - assert.Equal(t, 1, len(cr.commands)) + assert.Len(t, cr.commands, 1) } func TestGetNotRegistered(t *testing.T) { cr := &CommandRegistry{} _, err := cr.Get("test") - assert.Errorf(t, err, "can't fetch commands, registry not initialized") + require.Errorf(t, err, "can't fetch commands, registry not initialized") } func TestGetCommandNotFound(t *testing.T) { @@ -39,10 +41,10 @@ func TestGetCommandNotFound(t *testing.T) { mr := &MockResponder{command: "/test"} cr.Register(mr) - assert.Equal(t, 1, len(cr.commands)) + assert.Len(t, cr.commands, 1) _, err := cr.Get("/foo") - assert.Errorf(t, err, "command not found") + require.Errorf(t, err, "command not found") } func TestGetCommandFound(t *testing.T) { @@ -50,10 +52,10 @@ func TestGetCommandFound(t *testing.T) { mr := &MockResponder{command: "/test"} cr.Register(mr) - assert.Equal(t, 1, len(cr.commands)) + assert.Len(t, cr.commands, 1) cmd, err := cr.Get("/test") - assert.NoError(t, err) + require.NoError(t, err) assert.NotNil(t, cmd) assert.Equal(t, "/test", cmd.GetCommand()) @@ -66,11 +68,11 @@ func TestListServices(t *testing.T) { cr.Register(mr1) cr.Register(mr2) - assert.Equal(t, 2, len(cr.commands)) + assert.Len(t, cr.commands, 2) list := cr.ListCommands() - assert.Equal(t, 2, len(list)) + assert.Len(t, list, 2) assert.Equal(t, "/foo", list[0]) assert.Equal(t, "/bar", list[1]) } diff --git a/internal/core/domain/commands/chat_test.go b/internal/core/domain/commands/chat_test.go index 6ec29b2..42752cb 100644 --- a/internal/core/domain/commands/chat_test.go +++ b/internal/core/domain/commands/chat_test.go @@ -3,10 +3,12 @@ package commands import ( "context" "errors" - "github.com/stretchr/testify/assert" "hsbot/internal/core/domain" "testing" "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) type MockTextGenerator struct { @@ -15,7 +17,7 @@ type MockTextGenerator struct { Message string } -func (m *MockTextGenerator) GenerateFromPrompt(ctx context.Context, prompts []domain.Prompt) (string, error) { +func (m *MockTextGenerator) GenerateFromPrompt(_ context.Context, _ []domain.Prompt) (string, error) { return m.response, m.err } @@ -24,12 +26,12 @@ type MockTextSender struct { Message string } -func (m *MockTextSender) SendMessageReply(ctx context.Context, chatID int64, messageID int, message string) error { +func (m *MockTextSender) SendMessageReply(_ context.Context, _ int64, _ int, message string) error { m.Message = message return m.err } -func (m *MockTextSender) SendChatAction(ctx context.Context, chatID int64, action domain.Action) {} +func (m *MockTextSender) SendChatAction(_ context.Context, _ int64, _ domain.Action) {} func TestChatHandlerClearingCache(t *testing.T) { mg := &MockTextGenerator{response: "mock response"} @@ -40,15 +42,15 @@ func TestChatHandlerClearingCache(t *testing.T) { assert.NotNil(t, chatHandler) - err := chatHandler.Respond(context.Background(), &domain.Message{ChatID: 1, ID: 1, Text: "/chat prompt"}) + err := chatHandler.Respond(context.Background(), time.Minute, &domain.Message{ChatID: 1, ID: 1, Text: "/chat prompt"}) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, "mock response", ms.Message) - assert.Equal(t, len(chatHandler.cache), 1) + assert.Len(t, chatHandler.cache, 1) time.Sleep(time.Second * 4) - assert.Equal(t, len(chatHandler.cache), 0) + assert.Empty(t, chatHandler.cache) } func TestChatHandlerCache(t *testing.T) { @@ -60,22 +62,22 @@ func TestChatHandlerCache(t *testing.T) { assert.NotNil(t, chatHandler) - err := chatHandler.Respond(context.Background(), &domain.Message{ChatID: 1, ID: 1, Text: "/chat prompt"}) + err := chatHandler.Respond(context.Background(), time.Minute, &domain.Message{ChatID: 1, ID: 1, Text: "/chat prompt"}) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, "mock response", ms.Message) - assert.Equal(t, len(chatHandler.cache), 1) + assert.Len(t, chatHandler.cache, 1) - err = chatHandler.Respond(context.Background(), &domain.Message{ChatID: 1, ID: 2, Text: "/chat prompt2"}) - assert.NoError(t, err) - assert.Equal(t, len(chatHandler.cache), 1) + err = chatHandler.Respond(context.Background(), time.Minute, &domain.Message{ChatID: 1, ID: 2, Text: "/chat prompt2"}) + require.NoError(t, err) + assert.Len(t, chatHandler.cache, 1) - assert.Equal(t, len(chatHandler.cache[1].messages), 4) + assert.Len(t, chatHandler.cache[1].messages, 4) - assert.Equal(t, chatHandler.cache[1].messages[0].Prompt, "prompt") - assert.Equal(t, chatHandler.cache[1].messages[1].Prompt, "mock response") - assert.Equal(t, chatHandler.cache[1].messages[2].Prompt, "prompt2") - assert.Equal(t, chatHandler.cache[1].messages[3].Prompt, "mock response") + assert.Equal(t, "prompt", chatHandler.cache[1].messages[0].Prompt) + assert.Equal(t, "mock response", chatHandler.cache[1].messages[1].Prompt) + assert.Equal(t, "prompt2", chatHandler.cache[1].messages[2].Prompt) + assert.Equal(t, "mock response", chatHandler.cache[1].messages[3].Prompt) } func TestGeneratorError(t *testing.T) { @@ -87,7 +89,8 @@ func TestGeneratorError(t *testing.T) { assert.NotNil(t, chatHandler) - chatHandler.Respond(context.Background(), &domain.Message{ChatID: 1, ID: 1, Text: "/chat prompt"}) + err := chatHandler.Respond(context.Background(), time.Minute, &domain.Message{ChatID: 1, ID: 1, Text: "/chat prompt"}) + require.NoError(t, err) assert.Equal(t, "failed to generate reply: mock error", ms.Message) } @@ -101,7 +104,8 @@ func TestEmptyPromptError(t *testing.T) { assert.NotNil(t, chatHandler) - chatHandler.Respond(context.Background(), &domain.Message{ChatID: 1, ID: 1, Text: "/chat"}) + err := chatHandler.Respond(context.Background(), time.Minute, &domain.Message{ChatID: 1, ID: 1, Text: "/chat"}) + require.NoError(t, err) assert.Equal(t, "please input a prompt", ms.Message) } @@ -115,10 +119,10 @@ func TestSendMessageError(t *testing.T) { assert.NotNil(t, chatHandler) - err := chatHandler.Respond(context.Background(), &domain.Message{ChatID: 1, ID: 1, Text: "/chat prompt"}) + err := chatHandler.Respond(context.Background(), time.Minute, &domain.Message{ChatID: 1, ID: 1, Text: "/chat prompt"}) assert.Equal(t, "mock response", ms.Message) - assert.Errorf(t, err, "failed to send reply") + require.Errorf(t, err, "failed to send reply") } func TestSendGenerateErrorAndMessageError(t *testing.T) { @@ -130,8 +134,8 @@ func TestSendGenerateErrorAndMessageError(t *testing.T) { assert.NotNil(t, chatHandler) - err := chatHandler.Respond(context.Background(), &domain.Message{ChatID: 1, ID: 1, Text: "/chat prompt"}) + err := chatHandler.Respond(context.Background(), time.Minute, &domain.Message{ChatID: 1, ID: 1, Text: "/chat prompt"}) assert.Equal(t, "failed to generate reply: mock error", ms.Message) - assert.Errorf(t, err, "failed to send reply") + require.Errorf(t, err, "failed to send reply") } diff --git a/internal/core/domain/commands/image_test.go b/internal/core/domain/commands/image_test.go index 6c493aa..d395c58 100644 --- a/internal/core/domain/commands/image_test.go +++ b/internal/core/domain/commands/image_test.go @@ -3,9 +3,12 @@ package commands import ( "context" "errors" - "github.com/stretchr/testify/assert" "hsbot/internal/core/domain" "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) type MockImageGenerator struct { @@ -14,7 +17,7 @@ type MockImageGenerator struct { Message string } -func (m *MockImageGenerator) GenerateFromPrompt(ctx context.Context, prompt string) (string, error) { +func (m *MockImageGenerator) GenerateFromPrompt(_ context.Context, prompt string) (string, error) { m.Message = prompt return m.response, m.err } @@ -24,12 +27,12 @@ type MockImageSender struct { Message string } -func (m *MockImageSender) SendImageURLReply(ctx context.Context, chatID int64, messageID int, url string) error { +func (m *MockImageSender) SendImageURLReply(_ context.Context, _ int64, _ int, url string) error { m.Message = url return m.err } -func (m *MockImageSender) SendImageFileReply(ctx context.Context, chatID int64, messageID int, file []byte) error { +func (m *MockImageSender) SendImageFileReply(_ context.Context, _ int64, _ int, file []byte) error { m.Message = string(file) return m.err } @@ -52,8 +55,9 @@ func TestImageRepondSuccessful(t *testing.T) { imageHandler := NewImageHandler(mg, mi, mt, "/image") - err := imageHandler.Respond(context.Background(), &domain.Message{ChatID: 1, ID: 1, Text: "/image prompt"}) - assert.NoError(t, err) + err := imageHandler.Respond(context.Background(), time.Minute, + &domain.Message{ChatID: 1, ID: 1, Text: "/image prompt"}) + require.NoError(t, err) assert.Equal(t, "https://example.org/image.png", mi.Message) } @@ -65,9 +69,9 @@ func TestImageRepondSendFailed(t *testing.T) { imageHandler := NewImageHandler(mg, mi, mt, "/image") - err := imageHandler.Respond(context.Background(), &domain.Message{ChatID: 1, ID: 1, Text: "/image prompt"}) - assert.Errorf(t, err, "mock error") - + err := imageHandler.Respond(context.Background(), time.Minute, + &domain.Message{ChatID: 1, ID: 1, Text: "/image prompt"}) + require.Errorf(t, err, "mock error") } func TestImageRepondErrorEmptyPrompt(t *testing.T) { @@ -77,8 +81,9 @@ func TestImageRepondErrorEmptyPrompt(t *testing.T) { imageHandler := NewImageHandler(mg, mi, mt, "/image") - err := imageHandler.Respond(context.Background(), &domain.Message{ChatID: 1, ID: 1, Text: "/image"}) - assert.NoError(t, err) + err := imageHandler.Respond(context.Background(), time.Minute, + &domain.Message{ChatID: 1, ID: 1, Text: "/image"}) + require.NoError(t, err) assert.Equal(t, "missing image prompt", mt.Message) } @@ -90,8 +95,9 @@ func TestImageRepondErrorGenerating(t *testing.T) { imageHandler := NewImageHandler(mg, mi, mt, "/image") - err := imageHandler.Respond(context.Background(), &domain.Message{ChatID: 1, ID: 1, Text: "/image prompt"}) - assert.NoError(t, err) + err := imageHandler.Respond(context.Background(), time.Minute, + &domain.Message{ChatID: 1, ID: 1, Text: "/image prompt"}) + require.NoError(t, err) assert.Equal(t, "error getting FAL response: mock error", mt.Message) } @@ -103,8 +109,9 @@ func TestImageRepondErrorGeneratingAndSending(t *testing.T) { imageHandler := NewImageHandler(mg, mi, mt, "/image") - err := imageHandler.Respond(context.Background(), &domain.Message{ChatID: 1, ID: 1, Text: "/image prompt"}) - assert.Errorf(t, err, "mock error") + err := imageHandler.Respond(context.Background(), time.Minute, + &domain.Message{ChatID: 1, ID: 1, Text: "/image prompt"}) + require.Errorf(t, err, "mock error") assert.Equal(t, "error getting FAL response: mock error", mt.Message) } @@ -116,8 +123,9 @@ func TestImageRepondErrorEmptyPromptAndErrorSending(t *testing.T) { imageHandler := NewImageHandler(mg, mi, mt, "/image") - err := imageHandler.Respond(context.Background(), &domain.Message{ChatID: 1, ID: 1, Text: "/image"}) - assert.Errorf(t, err, "mock error") + err := imageHandler.Respond(context.Background(), time.Minute, + &domain.Message{ChatID: 1, ID: 1, Text: "/image"}) + require.Errorf(t, err, "mock error") assert.Equal(t, "missing image prompt", mt.Message) } diff --git a/internal/core/domain/commands/scale_test.go b/internal/core/domain/commands/scale_test.go index 10afff3..412b34a 100644 --- a/internal/core/domain/commands/scale_test.go +++ b/internal/core/domain/commands/scale_test.go @@ -3,9 +3,12 @@ package commands import ( "context" "errors" - "github.com/stretchr/testify/assert" "hsbot/internal/core/domain" "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) type MockImageConverter struct { @@ -13,7 +16,7 @@ type MockImageConverter struct { response []byte } -func (m *MockImageConverter) Scale(ctx context.Context, imageURL string, power float32) ([]byte, error) { +func (m *MockImageConverter) Scale(_ context.Context, _ string, _ float32) ([]byte, error) { return m.response, m.err } @@ -37,8 +40,8 @@ func TestScaleRespondSuccessful(t *testing.T) { id := new(int) *id = 1 - err := scaleHandler.Respond(context.Background(), &domain.Message{ImageURL: "foo", ReplyToMessageID: id}) - assert.NoError(t, err) + err := scaleHandler.Respond(context.Background(), time.Minute, &domain.Message{ImageURL: "foo", ReplyToMessageID: id}) + require.NoError(t, err) assert.Equal(t, "success", ms.Message) } @@ -52,8 +55,8 @@ func TestScaleRespondErrorNoReply(t *testing.T) { id := new(int) *id = 1 - err := scaleHandler.Respond(context.Background(), &domain.Message{ImageURL: "foo", Text: "/scale 80"}) - assert.NoError(t, err) + err := scaleHandler.Respond(context.Background(), time.Minute, &domain.Message{ImageURL: "foo", Text: "/scale 80"}) + require.NoError(t, err) assert.Equal(t, "reply to an image", ts.Message) } @@ -67,8 +70,8 @@ func TestScaleRespondErrorNoReplyAndErrorSending(t *testing.T) { id := new(int) *id = 1 - err := scaleHandler.Respond(context.Background(), &domain.Message{ImageURL: "foo", Text: "/scale 80"}) - assert.Errorf(t, err, "mock error") + err := scaleHandler.Respond(context.Background(), time.Minute, &domain.Message{ImageURL: "foo", Text: "/scale 80"}) + require.Errorf(t, err, "mock error") assert.Equal(t, "reply to an image", ts.Message) } @@ -82,9 +85,9 @@ func TestScaleRespondInvalidParam(t *testing.T) { id := new(int) *id = 1 - err := scaleHandler.Respond(context.Background(), &domain.Message{ImageURL: "foo", ReplyToMessageID: id, + err := scaleHandler.Respond(context.Background(), time.Minute, &domain.Message{ImageURL: "foo", ReplyToMessageID: id, Text: "/scale foo"}) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, "usage: /scale or /scale , 1-100", ts.Message) } @@ -98,9 +101,9 @@ func TestScaleRespondInvalidParamAndErrorSending(t *testing.T) { id := new(int) *id = 1 - err := scaleHandler.Respond(context.Background(), &domain.Message{ImageURL: "foo", ReplyToMessageID: id, + err := scaleHandler.Respond(context.Background(), time.Minute, &domain.Message{ImageURL: "foo", ReplyToMessageID: id, Text: "/scale foo"}) - assert.Errorf(t, err, "mock error") + require.Errorf(t, err, "mock error") assert.Equal(t, "usage: /scale or /scale , 1-100", ts.Message) } @@ -114,9 +117,9 @@ func TestScaleRespondErrorScaleFailed(t *testing.T) { id := new(int) *id = 1 - err := scaleHandler.Respond(context.Background(), &domain.Message{ImageURL: "foo", ReplyToMessageID: id, + err := scaleHandler.Respond(context.Background(), time.Minute, &domain.Message{ImageURL: "foo", ReplyToMessageID: id, Text: "/scale 80"}) - assert.NoError(t, err) + require.NoError(t, err) assert.Equal(t, "failed to scale image: mock error", ts.Message) } @@ -130,9 +133,9 @@ func TestScaleRespondErrorScaleFailedAndErrorSending(t *testing.T) { id := new(int) *id = 1 - err := scaleHandler.Respond(context.Background(), &domain.Message{ImageURL: "foo", ReplyToMessageID: id, + err := scaleHandler.Respond(context.Background(), time.Minute, &domain.Message{ImageURL: "foo", ReplyToMessageID: id, Text: "/scale 80"}) - assert.Errorf(t, err, "mock error") + require.Errorf(t, err, "mock error") assert.Equal(t, "failed to scale image: mock error", ts.Message) } @@ -146,6 +149,6 @@ func TestScaleRespondSendImageFailed(t *testing.T) { id := new(int) *id = 1 - err := scaleHandler.Respond(context.Background(), &domain.Message{ImageURL: "foo", ReplyToMessageID: id}) - assert.Errorf(t, err, "mock error") + err := scaleHandler.Respond(context.Background(), time.Minute, &domain.Message{ImageURL: "foo", ReplyToMessageID: id}) + require.Errorf(t, err, "mock error") } diff --git a/internal/core/domain/commands/transcribe_test.go b/internal/core/domain/commands/transcribe_test.go index c53749e..4c98b74 100644 --- a/internal/core/domain/commands/transcribe_test.go +++ b/internal/core/domain/commands/transcribe_test.go @@ -3,16 +3,19 @@ package commands import ( "context" "errors" - "github.com/stretchr/testify/assert" "hsbot/internal/core/domain" "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) type MockTranscriber struct { err error } -func (m *MockTranscriber) GenerateFromAudio(ctx context.Context, url string) (string, error) { +func (m *MockTranscriber) GenerateFromAudio(_ context.Context, url string) (string, error) { return url, m.err } @@ -32,8 +35,8 @@ func TestTranscribeRespondSuccessful(t *testing.T) { transcribeHandler := NewTranscribeHandler(mt, ts, "/transcribe") - err := transcribeHandler.Respond(context.Background(), &domain.Message{AudioURL: "mock"}) - assert.NoError(t, err) + err := transcribeHandler.Respond(context.Background(), time.Minute, &domain.Message{AudioURL: "mock"}) + require.NoError(t, err) assert.Equal(t, "mock", ts.Message) } @@ -44,8 +47,8 @@ func TestTranscribeRespondErrorGenerating(t *testing.T) { transcribeHandler := NewTranscribeHandler(mt, ts, "/transcribe") - err := transcribeHandler.Respond(context.Background(), &domain.Message{AudioURL: "mock"}) - assert.NoError(t, err) + err := transcribeHandler.Respond(context.Background(), time.Minute, &domain.Message{AudioURL: "mock"}) + require.NoError(t, err) assert.Equal(t, "transcription failed: mock error", ts.Message) } @@ -56,8 +59,8 @@ func TestTranscribeRespondErrorGeneratingAndSending(t *testing.T) { transcribeHandler := NewTranscribeHandler(mt, ts, "/transcribe") - err := transcribeHandler.Respond(context.Background(), &domain.Message{AudioURL: "mock"}) - assert.Errorf(t, err, "mock error") + err := transcribeHandler.Respond(context.Background(), time.Minute, &domain.Message{AudioURL: "mock"}) + require.Errorf(t, err, "mock error") assert.Equal(t, "transcription failed: mock error", ts.Message) } @@ -68,8 +71,8 @@ func TestTranscribeRespondErrorSending(t *testing.T) { transcribeHandler := NewTranscribeHandler(mt, ts, "/transcribe") - err := transcribeHandler.Respond(context.Background(), &domain.Message{AudioURL: "mock"}) - assert.Errorf(t, err, "mock error") + err := transcribeHandler.Respond(context.Background(), time.Minute, &domain.Message{AudioURL: "mock"}) + require.Errorf(t, err, "mock error") assert.Equal(t, "mock", ts.Message) } @@ -80,8 +83,8 @@ func TestTranscribeRespondErrorEmptyURLAndSending(t *testing.T) { transcribeHandler := NewTranscribeHandler(mt, ts, "/transcribe") - err := transcribeHandler.Respond(context.Background(), &domain.Message{}) - assert.Errorf(t, err, "mock error") + err := transcribeHandler.Respond(context.Background(), time.Minute, &domain.Message{}) + require.Errorf(t, err, "mock error") assert.Equal(t, "reply to an audio", ts.Message) }