cli + tui to publish to leaflet (wip) & manage tasks, notes & watch/read lists ๐Ÿƒ
charm leaflet readability golang

refactor(wip): use helpers in note handler tests

+39 -116
+39 -116
internal/handlers/notes_test.go
··· 59 t.Run("New", func(t *testing.T) { 60 t.Run("creates handler successfully", func(t *testing.T) { 61 testHandler, err := NewNoteHandler() 62 - if err != nil { 63 - t.Fatalf("NewNoteHandler failed: %v", err) 64 - } 65 if testHandler == nil { 66 t.Fatal("Handler should not be nil") 67 } ··· 79 }) 80 81 t.Run("handles database initialization error", func(t *testing.T) { 82 - originalXDG := os.Getenv("XDG_CONFIG_HOME") 83 - originalHome := os.Getenv("HOME") 84 85 if runtime.GOOS == "windows" { 86 - originalAppData := os.Getenv("APPDATA") 87 - os.Unsetenv("APPDATA") 88 - defer os.Setenv("APPDATA", originalAppData) 89 } else { 90 - os.Unsetenv("XDG_CONFIG_HOME") 91 - os.Unsetenv("HOME") 92 } 93 94 - defer func() { 95 - os.Setenv("XDG_CONFIG_HOME", originalXDG) 96 - os.Setenv("HOME", originalHome) 97 - }() 98 - 99 _, err := NewNoteHandler() 100 - if err == nil { 101 - t.Error("NewNoteHandler should fail when database initialization fails") 102 - } 103 - if !strings.Contains(err.Error(), "failed to initialize database") { 104 - t.Errorf("Expected database error, got: %v", err) 105 - } 106 }) 107 }) 108 ··· 111 112 t.Run("creates note from title only", func(t *testing.T) { 113 err := handler.Create(ctx, "Test Note 1", "", "", false) 114 - if err != nil { 115 - t.Errorf("Create failed: %v", err) 116 - } 117 }) 118 119 t.Run("creates note from title and content", func(t *testing.T) { 120 err := handler.Create(ctx, "Test Note 2", "This is test content", "", false) 121 - if err != nil { 122 - t.Errorf("Create failed: %v", err) 123 - } 124 }) 125 126 t.Run("creates note from markdown file", func(t *testing.T) { ··· 131 filePath := createTestMarkdownFile(t, tempDir, "test.md", content) 132 133 err := handler.Create(ctx, "", "", filePath, false) 134 - if err != nil { 135 - t.Errorf("Create from file failed: %v", err) 136 - } 137 }) 138 139 t.Run("handles non-existent file", func(t *testing.T) { 140 err := handler.Create(ctx, "", "", "/non/existent/file.md", false) 141 - if err == nil { 142 - t.Error("Create should fail with non-existent file") 143 - } 144 }) 145 }) 146 ··· 149 150 t.Run("handles non-existent note", func(t *testing.T) { 151 err := handler.Edit(ctx, 999) 152 - if err == nil { 153 - t.Error("Edit should fail with non-existent note ID") 154 - } 155 - if !strings.Contains(err.Error(), "failed to get note") && !strings.Contains(err.Error(), "failed to find note") { 156 - t.Errorf("Expected note not found error, got: %v", err) 157 - } 158 }) 159 160 t.Run("handles no editor configured", func(t *testing.T) { 161 - originalEditor := os.Getenv("EDITOR") 162 - originalPath := os.Getenv("PATH") 163 - os.Setenv("EDITOR", "") 164 - os.Setenv("PATH", "") 165 - defer func() { 166 - os.Setenv("EDITOR", originalEditor) 167 - os.Setenv("PATH", originalPath) 168 - }() 169 170 err := handler.Edit(ctx, 1) 171 - if err == nil { 172 - t.Error("Edit should fail when no editor is configured") 173 - } 174 - if !strings.Contains(err.Error(), "no editor configured") && !strings.Contains(err.Error(), "failed to open editor") { 175 - t.Errorf("Expected no editor error, got: %v", err) 176 - } 177 }) 178 179 t.Run("handles database connection error", func(t *testing.T) { ··· 181 defer func() { 182 var err error 183 handler.db, err = store.NewDatabase() 184 - if err != nil { 185 - t.Fatalf("Failed to reconnect to database: %v", err) 186 - } 187 }() 188 189 err := handler.Edit(ctx, 1) 190 - if err == nil { 191 - t.Error("Edit should fail when database is closed") 192 - } 193 - if !strings.Contains(err.Error(), "failed to get note") { 194 - t.Errorf("Expected database error, got: %v", err) 195 - } 196 }) 197 198 t.Run("handles temp file creation error", func(t *testing.T) { 199 testHandler, err := NewNoteHandler() 200 - if err != nil { 201 - t.Fatalf("Failed to create test handler: %v", err) 202 - } 203 defer testHandler.Close() 204 205 err = testHandler.Create(ctx, "Temp File Test Note", "Test content", "", false) 206 - if err != nil { 207 - t.Fatalf("Failed to create test note: %v", err) 208 - } 209 210 - originalTempDir := os.Getenv("TMPDIR") 211 - os.Setenv("TMPDIR", "/non/existent/path") 212 - defer os.Setenv("TMPDIR", originalTempDir) 213 214 err = testHandler.Edit(ctx, 1) 215 - if err == nil { 216 - t.Error("Edit should fail when temp file creation fails") 217 - } 218 - if !strings.Contains(err.Error(), "failed to create temporary file") { 219 - t.Errorf("Expected temp file error, got: %v", err) 220 - } 221 }) 222 223 t.Run("handles editor failure", func(t *testing.T) { 224 testHandler, err := NewNoteHandler() 225 - if err != nil { 226 - t.Fatalf("Failed to create test handler: %v", err) 227 - } 228 defer testHandler.Close() 229 230 err = testHandler.Create(ctx, "Editor Failure Test Note", "Test content", "", false) 231 - if err != nil { 232 - t.Fatalf("Failed to create test note: %v", err) 233 - } 234 235 - mockEditor := func(editor, filePath string) error { 236 - return fmt.Errorf("editor process failed") 237 - } 238 - testHandler.openInEditorFunc = mockEditor 239 240 err = testHandler.Edit(ctx, 1) 241 - if err == nil { 242 - t.Error("Edit should fail when editor fails") 243 - } 244 - if !strings.Contains(err.Error(), "failed to open editor") { 245 - t.Errorf("Expected editor error, got: %v", err) 246 - } 247 }) 248 249 t.Run("handles temp file write error", func(t *testing.T) { 250 originalHandler := handler.openInEditorFunc 251 defer func() { handler.openInEditorFunc = originalHandler }() 252 253 - mockEditor := func(editor, filePath string) error { 254 - return os.Chmod(filePath, 0444) 255 - } 256 - handler.openInEditorFunc = mockEditor 257 258 err := handler.Edit(ctx, 1) 259 - if err == nil { 260 - t.Error("Edit should handle temp file write issues") 261 - } 262 }) 263 264 t.Run("handles file read error after editing", func(t *testing.T) { 265 testHandler, err := NewNoteHandler() 266 - if err != nil { 267 - t.Fatalf("Failed to create test handler: %v", err) 268 - } 269 defer testHandler.Close() 270 271 err = testHandler.Create(ctx, "File Read Error Test Note", "Test content", "", false) 272 - if err != nil { 273 - t.Fatalf("Failed to create test note: %v", err) 274 - } 275 276 - mockEditor := func(editor, filePath string) error { 277 - return os.Remove(filePath) 278 - } 279 - testHandler.openInEditorFunc = mockEditor 280 281 err = testHandler.Edit(ctx, 1) 282 - if err == nil { 283 - t.Error("Edit should fail when temp file is deleted") 284 - } 285 - if !strings.Contains(err.Error(), "failed to read edited content") { 286 - t.Errorf("Expected file read error, got: %v", err) 287 - } 288 }) 289 290 t.Run("handles database update error", func(t *testing.T) {
··· 59 t.Run("New", func(t *testing.T) { 60 t.Run("creates handler successfully", func(t *testing.T) { 61 testHandler, err := NewNoteHandler() 62 + Expect.AssertNoError(t, err, "NewNoteHandler should succeed") 63 if testHandler == nil { 64 t.Fatal("Handler should not be nil") 65 } ··· 77 }) 78 79 t.Run("handles database initialization error", func(t *testing.T) { 80 + envHelper := NewEnvironmentTestHelper() 81 + defer envHelper.RestoreEnv() 82 83 if runtime.GOOS == "windows" { 84 + envHelper.UnsetEnv("APPDATA") 85 } else { 86 + envHelper.UnsetEnv("XDG_CONFIG_HOME") 87 + envHelper.UnsetEnv("HOME") 88 } 89 90 _, err := NewNoteHandler() 91 + Expect.AssertError(t, err, "failed to initialize database", "NewNoteHandler should fail when database initialization fails") 92 }) 93 }) 94 ··· 97 98 t.Run("creates note from title only", func(t *testing.T) { 99 err := handler.Create(ctx, "Test Note 1", "", "", false) 100 + Expect.AssertNoError(t, err, "Create should succeed") 101 }) 102 103 t.Run("creates note from title and content", func(t *testing.T) { 104 err := handler.Create(ctx, "Test Note 2", "This is test content", "", false) 105 + Expect.AssertNoError(t, err, "Create should succeed") 106 }) 107 108 t.Run("creates note from markdown file", func(t *testing.T) { ··· 113 filePath := createTestMarkdownFile(t, tempDir, "test.md", content) 114 115 err := handler.Create(ctx, "", "", filePath, false) 116 + Expect.AssertNoError(t, err, "Create from file should succeed") 117 }) 118 119 t.Run("handles non-existent file", func(t *testing.T) { 120 err := handler.Create(ctx, "", "", "/non/existent/file.md", false) 121 + Expect.AssertError(t, err, "", "Create should fail with non-existent file") 122 }) 123 }) 124 ··· 127 128 t.Run("handles non-existent note", func(t *testing.T) { 129 err := handler.Edit(ctx, 999) 130 + Expect.AssertError(t, err, "failed to get note", "Edit should fail with non-existent note ID") 131 }) 132 133 t.Run("handles no editor configured", func(t *testing.T) { 134 + envHelper := NewEnvironmentTestHelper() 135 + defer envHelper.RestoreEnv() 136 + 137 + envHelper.SetEnv("EDITOR", "") 138 + envHelper.SetEnv("PATH", "") 139 140 err := handler.Edit(ctx, 1) 141 + Expect.AssertError(t, err, "failed to open editor", "Edit should fail when no editor is configured") 142 }) 143 144 t.Run("handles database connection error", func(t *testing.T) { ··· 146 defer func() { 147 var err error 148 handler.db, err = store.NewDatabase() 149 + Expect.AssertNoError(t, err, "Failed to reconnect to database") 150 }() 151 152 err := handler.Edit(ctx, 1) 153 + Expect.AssertError(t, err, "failed to get note", "Edit should fail when database is closed") 154 }) 155 156 t.Run("handles temp file creation error", func(t *testing.T) { 157 testHandler, err := NewNoteHandler() 158 + Expect.AssertNoError(t, err, "Failed to create test handler") 159 defer testHandler.Close() 160 161 err = testHandler.Create(ctx, "Temp File Test Note", "Test content", "", false) 162 + Expect.AssertNoError(t, err, "Failed to create test note") 163 164 + envHelper := NewEnvironmentTestHelper() 165 + defer envHelper.RestoreEnv() 166 + envHelper.SetEnv("TMPDIR", "/non/existent/path") 167 168 err = testHandler.Edit(ctx, 1) 169 + Expect.AssertError(t, err, "failed to create temporary file", "Edit should fail when temp file creation fails") 170 }) 171 172 t.Run("handles editor failure", func(t *testing.T) { 173 testHandler, err := NewNoteHandler() 174 + Expect.AssertNoError(t, err, "Failed to create test handler") 175 defer testHandler.Close() 176 177 err = testHandler.Create(ctx, "Editor Failure Test Note", "Test content", "", false) 178 + Expect.AssertNoError(t, err, "Failed to create test note") 179 180 + mockEditor := NewMockEditor().WithFailure("editor process failed") 181 + testHandler.openInEditorFunc = mockEditor.GetEditorFunc() 182 183 err = testHandler.Edit(ctx, 1) 184 + Expect.AssertError(t, err, "failed to open editor", "Edit should fail when editor fails") 185 }) 186 187 t.Run("handles temp file write error", func(t *testing.T) { 188 originalHandler := handler.openInEditorFunc 189 defer func() { handler.openInEditorFunc = originalHandler }() 190 191 + mockEditor := NewMockEditor().WithReadOnly() 192 + handler.openInEditorFunc = mockEditor.GetEditorFunc() 193 194 err := handler.Edit(ctx, 1) 195 + Expect.AssertError(t, err, "", "Edit should handle temp file write issues") 196 }) 197 198 t.Run("handles file read error after editing", func(t *testing.T) { 199 testHandler, err := NewNoteHandler() 200 + Expect.AssertNoError(t, err, "Failed to create test handler") 201 defer testHandler.Close() 202 203 err = testHandler.Create(ctx, "File Read Error Test Note", "Test content", "", false) 204 + Expect.AssertNoError(t, err, "Failed to create test note") 205 206 + mockEditor := NewMockEditor().WithFileDeleted() 207 + testHandler.openInEditorFunc = mockEditor.GetEditorFunc() 208 209 err = testHandler.Edit(ctx, 1) 210 + Expect.AssertError(t, err, "failed to read edited content", "Edit should fail when temp file is deleted") 211 }) 212 213 t.Run("handles database update error", func(t *testing.T) {