From 8a9b0d54ce047026d2f33cd758185e6002e8fd08 Mon Sep 17 00:00:00 2001 From: H0llyW00dzZ Date: Sun, 10 Dec 2023 18:12:33 +0700 Subject: [PATCH 1/3] Chore [Golang] [Package] File System Mock - [+] chore(file_system_mock.go): add initialization of Files map in NewMockFileSystem function --- filesystem/file_system_mock.go | 1 + 1 file changed, 1 insertion(+) diff --git a/filesystem/file_system_mock.go b/filesystem/file_system_mock.go index 04b40c3..9001757 100644 --- a/filesystem/file_system_mock.go +++ b/filesystem/file_system_mock.go @@ -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), } } From 4a206a3a542bea27fc320ea0cd344d96ec30bcec Mon Sep 17 00:00:00 2001 From: H0llyW00dzZ Date: Sun, 10 Dec 2023 18:13:22 +0700 Subject: [PATCH 2/3] Feat [Golang] [Module] Main Command Testing - [+] feat(main_test.go): add unit test for ConfirmOverwrite function in interactivity package --- main_test.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/main_test.go b/main_test.go index 6017d18..2067762 100644 --- a/main_test.go +++ b/main_test.go @@ -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. @@ -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() @@ -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 is not actually operated on I/O Disk +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) + } + }) + } +} From 65545e5c097c540e2b3cafeff18ccfa4a5a81be1 Mon Sep 17 00:00:00 2001 From: H0llyW00dzZ Date: Sun, 10 Dec 2023 18:28:16 +0700 Subject: [PATCH 3/3] Chore [Golang] Docs - [+] chore(file_system_mock.go): fix copyright year in file header - [+] chore(interactivity.go): update package description and comments - [+] test(main_test.go): update test case comment --- filesystem/file_system_mock.go | 2 +- interactivity/interactivity.go | 10 ++++------ main_test.go | 2 +- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/filesystem/file_system_mock.go b/filesystem/file_system_mock.go index 9001757..9d7521b 100644 --- a/filesystem/file_system_mock.go +++ b/filesystem/file_system_mock.go @@ -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 ( diff --git a/interactivity/interactivity.go b/interactivity/interactivity.go index 0ec9419..8a4421d 100644 --- a/interactivity/interactivity.go +++ b/interactivity/interactivity.go @@ -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 ( diff --git a/main_test.go b/main_test.go index 2067762..511079b 100644 --- a/main_test.go +++ b/main_test.go @@ -239,7 +239,7 @@ func TestWriteContentToFile(t *testing.T) { } // TestConfirmOverwrite tests the ConfirmOverwrite function from the interactivity package. -// Note: This test is not actually operated on I/O Disk +// Note: This test does not perform operations on the actual disk I/O. func TestConfirmOverwrite(t *testing.T) { // Define test cases. tests := []struct {