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

Binary Interactivity Testing Testing *Ghoper #28

Merged
merged 3 commits into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion filesystem/file_system_mock.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Package filesystem provides a mock implementation of the FileSystem interface for testing purposes.
// It allows for tracking file operations and simulating file system interactions without actual disk I/O (Magic Golang 🎩 🪄).
//
// Copyright 2023 H0llyW00dzZ
// Copyright (c) 2023 H0llyW00dzZ
package filesystem

import (
Expand Down Expand Up @@ -55,6 +55,7 @@ func (m *MockExporter) ConvertSessionsToCSV(ctx context.Context, sessions []expo
func NewMockFileSystem() *MockFileSystem {
return &MockFileSystem{
FilesCreated: make(map[string]*bytes.Buffer),
Files: make(map[string][]byte),
}
}

Expand Down
10 changes: 4 additions & 6 deletions interactivity/interactivity.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// Package interactivity provides functions to handle interactive command-line
// user interfaces. It includes utilities for prompting the user with questions
// and processing their responses. This package is specifically designed to work
// with the filesystem provided by the github.com/H0llyW00dzZ/ChatGPT-Next-Web-Session-Exporter/filesystem
// package to confirm potential file overwrites and to collect user input in a
// context-aware manner, allowing for graceful cancellation of input requests.
// Package interactivity provides functions to handle interactive command-line interfaces.
//
// It includes utilities for prompting users for confirmation and reading their input in a context-aware manner, which allows for graceful
// cancellation of input requests. The package is designed to integrate with a filesystem interface to check for file existence and handle potential file overwrites.
package interactivity

import (
Expand Down
53 changes: 52 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

"github.com/H0llyW00dzZ/ChatGPT-Next-Web-Session-Exporter/exporter"
"github.com/H0llyW00dzZ/ChatGPT-Next-Web-Session-Exporter/filesystem"
"github.com/H0llyW00dzZ/ChatGPT-Next-Web-Session-Exporter/interactivity"
)

// loadTestSessions is a helper function that loads test session data from a JSON file.
Expand Down Expand Up @@ -212,7 +213,7 @@ func TestWriteContentToFile(t *testing.T) {
reader := bufio.NewReader(&userInput)

// Define the content to be written to the file.
content := "Test content"
content := `{"message": "Hello Machine ? This is a Gopher unit test."}`

// Create a mock file system.
mockFS := filesystem.NewMockFileSystem()
Expand All @@ -236,3 +237,53 @@ func TestWriteContentToFile(t *testing.T) {
t.Errorf("WriteFile was called with the wrong content: got %v, want %v", string(mockFS.FilesCreated[expectedFileName].Bytes()), content)
}
}

// TestConfirmOverwrite tests the ConfirmOverwrite function from the interactivity package.
// Note: This test does not perform operations on the actual disk I/O.
func TestConfirmOverwrite(t *testing.T) {
// Define test cases.
tests := []struct {
name string
fileExists bool
userInput string
expectedResult bool
expectError bool
}{
{"FileDoesNotExist", false, "", true, false},
{"UserConfirmsOverwrite", true, "yes\n", true, false},
{"UserDeniesOverwrite", true, "no\n", false, false},
{"UserInputError", true, "", false, true},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Create a new mock file system.
mockFS := filesystem.NewMockFileSystem()

// Set the file existence state in the mock file system according to the test case.
if tc.fileExists {
// Simulate an existing file by adding it to the Files map.
mockFS.Files["testing.json"] = []byte(`{"message": "Hello Machine ? This is a Gopher unit test."}`)
} else {
// Ensure the file does not exist in the Files map.
delete(mockFS.Files, "testing.json")
}

// Simulate user input.
reader := bufio.NewReader(strings.NewReader(tc.userInput))

// Call the ConfirmOverwrite function.
result, err := interactivity.ConfirmOverwrite(mockFS, context.Background(), reader, "testing.json")

// Verify the result.
if result != tc.expectedResult {
t.Errorf("ConfirmOverwrite() = %v, want %v", result, tc.expectedResult)
}

// Verify the error.
if (err != nil) != tc.expectError {
t.Errorf("ConfirmOverwrite() error = %v, wantErr %v", err, tc.expectError)
}
})
}
}
Loading