package storage import ( "os" "path/filepath" "testing" ) func setupTestStorage(t *testing.T) *Storage { tempDir := t.TempDir() return NewStorage(tempDir) } func TestListFiles(t *testing.T) { storage := setupTestStorage(t) // Create test files content := "Test content" testFiles := []string{"test1.md", "test2.md", "notes.md"} for _, filename := range testFiles { path := filepath.Join(storage.dataDir, filename) if err := os.WriteFile(path, []byte(content), 0644); err != nil { t.Fatalf("Failed to create test file: %v", err) } } files, err := storage.ListFiles() if err != nil { t.Fatalf("Failed to list files: %v", err) } if len(files) != 3 { t.Errorf("Expected 3 files, got %d", len(files)) } expected := map[string]bool{"test1.md": true, "test2.md": true, "notes.md": true} for _, file := range files { if !expected[file] { t.Errorf("Unexpected file: %s", file) } } } func TestGetFile(t *testing.T) { storage := setupTestStorage(t) filename := "testfile.md" content := "# Test Heading\n\nTest content." path := filepath.Join(storage.dataDir, filename) os.WriteFile(path, []byte(content), 0644) fileContent, err := storage.GetFile(filename) if err != nil { t.Fatalf("Failed to get file: %v", err) } if fileContent != content { t.Errorf("Expected content %q, got %q", content, fileContent) } } func TestGetFileNotFound(t *testing.T) { storage := setupTestStorage(t) _, err := storage.GetFile("nonexistent.md") if err == nil { t.Error("Expected error for non-existent file") } } func TestSaveFile(t *testing.T) { storage := setupTestStorage(t) filename := "newfile.md" content := "# New File\n\nContent here." err := storage.SaveFile(filename, content) if err != nil { t.Fatalf("Failed to save file: %v", err) } // Verify file was saved path := filepath.Join(storage.dataDir, filename) if _, err := os.Stat(path); os.IsNotExist(err) { t.Error("File was not saved") } // Verify content storedContent, err := os.ReadFile(path) if err != nil { t.Fatalf("Failed to read file: %v", err) } if string(storedContent) != content { t.Error("File content does not match") } } func TestDeleteFile(t *testing.T) { storage := setupTestStorage(t) filename := "todelete.md" content := "To be deleted." path := filepath.Join(storage.dataDir, filename) os.WriteFile(path, []byte(content), 0644) err := storage.DeleteFile(filename) if err != nil { t.Fatalf("Failed to delete file: %v", err) } // Verify file was deleted if _, err := os.Stat(path); !os.IsNotExist(err) { t.Error("File was not deleted") } } func TestDeleteFileNotFound(t *testing.T) { storage := setupTestStorage(t) err := storage.DeleteFile("nonexistent.md") if err == nil { t.Error("Expected error for non-existent file") } } func TestExists(t *testing.T) { storage := setupTestStorage(t) filename := "exists.md" path := filepath.Join(storage.dataDir, filename) os.WriteFile(path, []byte("content"), 0644) if !storage.Exists(filename) { t.Error("File should exist") } if storage.Exists("nonexistent.md") { t.Error("Non-existent file should not exist") } }