Add complete markdown editor with Go backend and React/TypeScript frontend. Backend: - Cobra CLI with configurable host, port, data-dir, static-dir flags - REST API for CRUD operations on markdown files (GET, POST, PUT, DELETE) - File storage with flat .md structure - Comprehensive Logrus logging for all operations - Static asset serving for frontend Frontend: - React 18 + TypeScript + Tailwind CSS - Live markdown editor with GFM preview (react-markdown) - File management UI (list, create, open, save, delete) - Theme system (Light/Dark/System) with localStorage persistence - Responsive design (320px - 1920px+) Testing: - 6 backend tests covering CRUD round-trip, validation, error handling - 19 frontend tests covering API, theme system, and UI components - All tests passing with single 'make test' command Build: - Frontend compiles to optimized assets in dist/ - Backend can serve frontend via --static-dir flag
140 lines
3.1 KiB
Go
140 lines
3.1 KiB
Go
package test
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/evanreichard/markdown-editor/internal/logging"
|
|
"github.com/evanreichard/markdown-editor/internal/storage"
|
|
)
|
|
|
|
func init() {
|
|
logging.Init()
|
|
}
|
|
|
|
func TestStorageCRUD(t *testing.T) {
|
|
// Create temporary directory
|
|
tmpDir, err := os.MkdirTemp("", "markdown-test-*")
|
|
if err != nil {
|
|
t.Fatalf("Failed to create temp dir: %v", err)
|
|
}
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
// Initialize storage
|
|
s, err := storage.New(tmpDir)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create storage: %v", err)
|
|
}
|
|
|
|
// Test Create
|
|
file, err := s.Create("test.md", "# Test\n\nHello, World!")
|
|
if err != nil {
|
|
t.Fatalf("Failed to create file: %v", err)
|
|
}
|
|
if file.Name != "test.md" {
|
|
t.Errorf("Expected name 'test.md', got '%s'", file.Name)
|
|
}
|
|
if file.Content != "# Test\n\nHello, World!" {
|
|
t.Errorf("Content mismatch")
|
|
}
|
|
|
|
// Test Get
|
|
retrieved, err := s.Get("test.md")
|
|
if err != nil {
|
|
t.Fatalf("Failed to get file: %v", err)
|
|
}
|
|
if retrieved.Name != "test.md" {
|
|
t.Errorf("Expected name 'test.md', got '%s'", retrieved.Name)
|
|
}
|
|
|
|
// Test Update
|
|
updated, err := s.Update("test.md", "# Updated\n\nNew content")
|
|
if err != nil {
|
|
t.Fatalf("Failed to update file: %v", err)
|
|
}
|
|
if updated.Content != "# Updated\n\nNew content" {
|
|
t.Errorf("Update failed, content mismatch")
|
|
}
|
|
|
|
// Test List
|
|
files, err := s.List()
|
|
if err != nil {
|
|
t.Fatalf("Failed to list files: %v", err)
|
|
}
|
|
if len(files) != 1 {
|
|
t.Errorf("Expected 1 file, got %d", len(files))
|
|
}
|
|
|
|
// Test Delete
|
|
err = s.Delete("test.md")
|
|
if err != nil {
|
|
t.Fatalf("Failed to delete file: %v", err)
|
|
}
|
|
|
|
files, err = s.List()
|
|
if err != nil {
|
|
t.Fatalf("Failed to list after delete: %v", err)
|
|
}
|
|
if len(files) != 0 {
|
|
t.Errorf("Expected 0 files after delete, got %d", len(files))
|
|
}
|
|
|
|
// Test Get after delete (should fail)
|
|
_, err = s.Get("test.md")
|
|
if err != storage.ErrFileNotFound {
|
|
t.Errorf("Expected ErrFileNotFound, got %v", err)
|
|
}
|
|
|
|
t.Log("CRUD round-trip test passed")
|
|
}
|
|
|
|
func TestStorageValidation(t *testing.T) {
|
|
tmpDir, err := os.MkdirTemp("", "markdown-test-*")
|
|
if err != nil {
|
|
t.Fatalf("Failed to create temp dir: %v", err)
|
|
}
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
s, err := storage.New(tmpDir)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create storage: %v", err)
|
|
}
|
|
|
|
// Test invalid names
|
|
invalidNames := []string{"", "../test.md", "test.txt", "folder/test.md", "test.MD"}
|
|
for _, name := range invalidNames {
|
|
_, err := s.Create(name, "content")
|
|
if err != storage.ErrInvalidName {
|
|
t.Errorf("Expected ErrInvalidName for '%s', got %v", name, err)
|
|
}
|
|
}
|
|
|
|
t.Log("Validation test passed")
|
|
}
|
|
|
|
func TestStorageDuplicateCreate(t *testing.T) {
|
|
tmpDir, err := os.MkdirTemp("", "markdown-test-*")
|
|
if err != nil {
|
|
t.Fatalf("Failed to create temp dir: %v", err)
|
|
}
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
s, err := storage.New(tmpDir)
|
|
if err != nil {
|
|
t.Fatalf("Failed to create storage: %v", err)
|
|
}
|
|
|
|
_, err = s.Create("dup.md", "content")
|
|
if err != nil {
|
|
t.Fatalf("Failed to create first file: %v", err)
|
|
}
|
|
|
|
// Try to create duplicate
|
|
_, err = s.Create("dup.md", "content 2")
|
|
if err == nil {
|
|
t.Errorf("Expected error when creating duplicate file")
|
|
}
|
|
|
|
t.Log("Duplicate create test passed")
|
|
}
|