Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests #133

Merged
merged 18 commits into from
Dec 30, 2024
22 changes: 5 additions & 17 deletions internal/analysis/analysis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,12 @@ import (
"context"
"testing"

"github.com/capymind/internal/mocks"
"github.com/capymind/internal/translator"
)

type validServiceMock struct{}

func (service validServiceMock) Request(name string, description string, systemPrompt string, userPrompt string, ctx *context.Context) *string {
response := "valid response"
return &response
}

type invalidServiceMock struct{}

func (service invalidServiceMock) Request(name string, description string, systemPrompt string, userPrompt string, ctx *context.Context) *string {
return nil
}

func TestValidAnalysis(t *testing.T) {
service := validServiceMock{}
service := mocks.ValidAIServiceMock{}
notes := []string{"note1", "note2", "note3"}
ctx := context.Background()
response := AnalyzeQuickly(service, notes, translator.EN, &ctx)
Expand All @@ -31,7 +19,7 @@ func TestValidAnalysis(t *testing.T) {
}

func TestInvalidAnalysis(t *testing.T) {
service := invalidServiceMock{}
service := mocks.InvalidAIServiceMock{}
notes := []string{"note1", "note2", "note3"}
ctx := context.Background()
response := AnalyzeQuickly(service, notes, translator.EN, &ctx)
Expand All @@ -41,7 +29,7 @@ func TestInvalidAnalysis(t *testing.T) {
}

func TestAnalysisWithHeader(t *testing.T) {
service := validServiceMock{}
service := mocks.ValidAIServiceMock{}
notes := []string{"note1", "note2", "note3"}
ctx := context.Background()
response := AnalyzeLastWeek(service, notes, translator.EN, &ctx)
Expand All @@ -51,7 +39,7 @@ func TestAnalysisWithHeader(t *testing.T) {
}

func TestAnalyzeSleep(t *testing.T) {
service := validServiceMock{}
service := mocks.ValidAIServiceMock{}
text := "I slept well last night"
ctx := context.Background()
response := AnalyzeSleep(service, text, translator.EN, &ctx)
Expand Down
8 changes: 3 additions & 5 deletions internal/app/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ import (

"github.com/capymind/internal/botservice"
"github.com/capymind/internal/database"
"github.com/capymind/third_party/googledrive"
"github.com/capymind/internal/filestorage"
)

var fileStorage googledrive.GoogleDrive

func handleDownloadData(session *Session) {
func handleDownloadData(session *Session, noteStorage database.NoteStorage, fileStorage filestorage.FileStorage) {
sendMessage("download_all_notes_waiting", session)

userID := session.User.ID
Expand Down Expand Up @@ -88,7 +86,7 @@ func handleDeleteAccount(session *Session) {
setOutputTextWithButtons("delete_account_are_you_sure", []botservice.BotResultTextButton{deleteButton}, session)
}

func handleForceDeleteAccount(session *Session) {
func handleForceDeleteAccount(session *Session, noteStorage database.NoteStorage, userStorage database.UserStorage) {
sendMessage("delete_account_waiting", session)

// Delete all notes
Expand Down
77 changes: 77 additions & 0 deletions internal/app/account_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package app

import (
"os"
"testing"

"github.com/capymind/internal/database"
"github.com/capymind/internal/mocks"
)

func TestCreateZipFile(t *testing.T) {
userID := "123"

note1 := database.Note{
Text: "Test note 1",
}
note2 := database.Note{
Text: "Test note 2",
}
notes := []database.Note{note1, note2}

zipFile, err := createZipFile(userID, notes)

if err != nil {
t.Error("Expected nil error, got", err)
}

if zipFile == nil {
t.Error("Expected zip file, got nil")
}
// Check if the file name starts with notes_123
if zipFile.Name()[:9] != "notes_123" {
t.Error("Expected file name notes_123, got", zipFile.Name())
}

os.Remove(zipFile.Name())
zipFile.Close()
}

func TestDownloadDataHandler(t *testing.T) {
session := createSession(&Job{Command: "/download"}, &database.User{}, nil)
noteStorage := mocks.NoteStorageMock{}
fileStorage := mocks.ValidFileStorageMock{}

handleDownloadData(session, noteStorage, fileStorage)

if session.Job.Output[0].TextID != "link" {
t.Error("Expected link, got nil")
}
}

func TestDeleteAccountHandler(t *testing.T) {
session := createSession(&Job{Command: "/delete"}, &database.User{}, nil)
handleDeleteAccount(session)

if session.Job.Output[0].TextID != "delete_account_are_you_sure" {
t.Error("Expected delete_account_confirm, got nil")
}
if session.Job.Output[0].Buttons[0].TextID != "delete_account_confirm" {
t.Error("Expected delete_account_are_you_sure, got nil")
}
}

func TestForceDeleteAccountHandler(t *testing.T) {
session := createSession(&Job{Command: "/force_delete"}, &database.User{}, nil)
userStorage := mocks.UserStorageMock{}
noteStorage := mocks.NoteStorageMock{}

handleForceDeleteAccount(session, noteStorage, userStorage)

if session.Job.Output[0].TextID != "delete_account_success" {
t.Error("Expected delete_account_success, got nil")
}
if session.Job.Output[1].TextID != "delete_account_telegram_tip" {
t.Error("Expected delete_account_telegram_tip, got nil")
}
}
11 changes: 7 additions & 4 deletions internal/app/admin.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
package app

import "github.com/capymind/internal/helpers"
import (
"github.com/capymind/internal/database"
"github.com/capymind/internal/helpers"
)

func handleTotalUserCount(session *Session) {
func handleTotalUserCount(session *Session, adminStorage database.AdminStorage) {
message := helpers.GetTotalUserCount(session.Context, session.Locale(), adminStorage)
if message != nil {
setOutputText(*message, session)
}
}

func handleTotalActiveUserCount(session *Session) {
func handleTotalActiveUserCount(session *Session, adminStorage database.AdminStorage) {
message := helpers.GetTotalActiveUserCount(session.Context, session.Locale(), adminStorage)
if message != nil {
setOutputText(*message, session)
}
}

func handleTotalNoteCount(session *Session) {
func handleTotalNoteCount(session *Session, adminStorage database.AdminStorage) {
message := helpers.GetTotalNoteCount(session.Context, session.Locale(), adminStorage)
if message != nil {
setOutputText(*message, session)
Expand Down
41 changes: 41 additions & 0 deletions internal/app/admin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package app

import (
"testing"

"github.com/capymind/internal/database"
"github.com/capymind/internal/mocks"
)

func TestTotalUserCountHandler(t *testing.T) {
session := createSession(&Job{Command: "/total_user_count"}, &database.User{}, nil)
adminStorage := mocks.AdminStorageMock{}

handleTotalUserCount(session, adminStorage)

if session.Job.Output[0].TextID != "The total number of users is 100" {
t.Errorf("Expected The total number of users is 100, got %s", session.Job.Output[0].TextID)
}
}

func TestTotalActiveUserCountHandler(t *testing.T) {
session := createSession(&Job{Command: "/total_active_user_count"}, &database.User{}, nil)
adminStorage := mocks.AdminStorageMock{}

handleTotalActiveUserCount(session, adminStorage)

if session.Job.Output[0].TextID != "The total number of active users is 75" {
t.Errorf("Expected The total number of active users is 75, got %s", session.Job.Output[0].TextID)
}
}

func TestTotalNoteCountHandler(t *testing.T) {
session := createSession(&Job{Command: "/total_note_count"}, &database.User{}, nil)
adminStorage := mocks.AdminStorageMock{}

handleTotalNoteCount(session, adminStorage)

if session.Job.Output[0].TextID != "The total number of notes is 999" {
t.Errorf("Expected The total number of notes is 999, got %s", session.Job.Output[0].TextID)
}
}
6 changes: 4 additions & 2 deletions internal/app/analysis.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package app

import (
"github.com/capymind/internal/aiservice"
"github.com/capymind/internal/analysis"
"github.com/capymind/internal/botservice"
"github.com/capymind/internal/database"
)

// Handle the analysis command
func handleAnalysis(session *Session) {
func handleAnalysis(session *Session, noteStorage database.NoteStorage, aiService aiservice.AIService) {
// Get the user's notes
notes := getNotes(session, 5)
notes := getNotes(session, noteStorage, 5)
if len(notes) > 0 {
// Prepare the strings for analysis
var strings []string
Expand Down
44 changes: 44 additions & 0 deletions internal/app/analysis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package app

import (
"testing"

"github.com/capymind/internal/database"
"github.com/capymind/internal/mocks"
)

func TestAnalysisHandler(t *testing.T) {
session := createSession(&Job{Command: "/analysis"}, &database.User{}, nil)
noteStorage := mocks.NoteStorageMock{}
aiService := mocks.ValidAIServiceMock{}

handleAnalysis(session, noteStorage, aiService)

if session.Job.Output[0].TextID != "valid response" {
t.Errorf("Expected valid response, got %s", session.Job.Output[0].TextID)
}
}

func TestAnalysisHandlerNoNotes(t *testing.T) {
session := createSession(&Job{Command: "/analysis"}, &database.User{}, nil)
noteStorage := mocks.EmptyNoteStorageMock{}
aiService := mocks.ValidAIServiceMock{}

handleAnalysis(session, noteStorage, aiService)

if session.Job.Output[0].TextID != "no_analysis" {
t.Errorf("Expected no_analysis, got %s", session.Job.Output[0].TextID)
}
}

func TestAnalysisHandlerNoAIService(t *testing.T) {
session := createSession(&Job{Command: "/analysis"}, &database.User{}, nil)
noteStorage := mocks.NoteStorageMock{}
aiService := mocks.InvalidAIServiceMock{}

handleAnalysis(session, noteStorage, aiService)

if session.Job.Output[0].TextID != "no_analysis" {
t.Errorf("Expected no_analysis, got %s", session.Job.Output[0].TextID)
}
}
7 changes: 5 additions & 2 deletions internal/app/feedback.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package app

import "github.com/capymind/internal/helpers"
import (
"github.com/capymind/internal/database"
"github.com/capymind/internal/helpers"
)

func handleFeedbackLastWeek(session *Session) {
func handleFeedbackLastWeek(session *Session, feedbackStorage database.FeedbackStorage) {
array := helpers.PrepareFeedback(session.Context, session.Locale(), feedbackStorage)
for _, item := range array {
setOutputText(item, session)
Expand Down
25 changes: 25 additions & 0 deletions internal/app/feedback_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package app

import (
"testing"

"github.com/capymind/internal/database"
"github.com/capymind/internal/mocks"
)

func TestFeedbackHandler(t *testing.T) {
session := createSession(&Job{Command: "/feedback"}, &database.User{}, nil)
feedbackStorage := mocks.FeedbackStorageMock{}

handleFeedbackLastWeek(session, feedbackStorage)

if len(session.Job.Output) != 11 {
t.Errorf("Expected 11 feedback items, got %d", len(session.Job.Output))
}
if session.Job.Output[6].TextID != "\nTest feedback\n" {
t.Errorf("Expected Test feedback, got %s", session.Job.Output[0].TextID)
}
if session.Job.Output[10].TextID != "\nTest feedback 2\n" {
t.Errorf("Expected Test feedback 2, got %s", session.Job.Output[0].TextID)
}
}
57 changes: 57 additions & 0 deletions internal/app/language_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package app

import (
"testing"

"github.com/capymind/internal/database"
)

func TestLanguageHandler(t *testing.T) {
session := createSession(&Job{Command: "/language"}, &database.User{}, nil)
handleLanguage(session)

if session.Job.Output[0].TextID != "language_select" {
t.Errorf("Expected language_select, got %s", session.Job.Output[0].TextID)
}
if session.Job.Output[0].Buttons[0].TextID != "English 🇺🇸" {
t.Errorf("Expected French, got %s", session.Job.Output[0].Buttons[0].TextID)
}
if session.Job.Output[0].Buttons[1].TextID != "Українська 🇺🇦" {
t.Errorf("Expected Українська 🇺🇦, got %s", session.Job.Output[0].Buttons[1].TextID)
}
}

func TestLanguageHandlerWithParameters(t *testing.T) {
session := createSession(&Job{Command: "/language", Parameters: []string{"uk"}}, &database.User{}, nil)
handleLanguage(session)

if session.Job.Parameters[0] != "uk" {
t.Errorf("Expected uk, got %s", session.Job.Parameters[0])
}
if *session.User.Locale != "uk" {
t.Errorf("Expected uk, got %s", *session.User.Locale)
}

if session.Job.Output[0].TextID != "locale_set" {
t.Errorf("Expected locale_set, got %s", session.Job.Output[0].TextID)
}
if session.Job.Output[1].TextID != "timezone_select" {
t.Errorf("Expected timezone_select, got %d", len(session.Job.Output))
}
}

func TestLanguageHandlerWithParametersAndTimezone(t *testing.T) {
time := 123456789
session := createSession(&Job{Command: "/language", Parameters: []string{"en"}}, &database.User{SecondsFromUTC: &time}, nil)
handleLanguage(session)

if session.Job.Parameters[0] != "en" {
t.Errorf("Expected en, got %s", session.Job.Parameters[0])
}
if *session.User.Locale != "en" {
t.Errorf("Expected en, got %s", *session.User.Locale)
}
if session.Job.Output[0].TextID != "locale_set" {
t.Errorf("Expected locale_set, got %s", session.Job.Output[0].TextID)
}
}
Loading
Loading