feat: implement WYSIWYG markdown editor

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
This commit is contained in:
2026-02-06 08:53:52 -05:00
parent 5782d08950
commit a80de1730c
36 changed files with 9646 additions and 0 deletions

212
backend/test/server_test.go Normal file
View File

@@ -0,0 +1,212 @@
package test
import (
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"github.com/evanreichard/markdown-editor/internal/api"
"github.com/evanreichard/markdown-editor/internal/logging"
"github.com/evanreichard/markdown-editor/internal/router"
"github.com/evanreichard/markdown-editor/internal/storage"
)
func TestStaticAssets(t *testing.T) {
// Create temporary directories
tmpDataDir, err := os.MkdirTemp("", "markdown-data-*")
if err != nil {
t.Fatalf("Failed to create temp data dir: %v", err)
}
defer os.RemoveAll(tmpDataDir)
tmpStaticDir, err := os.MkdirTemp("", "markdown-static-*")
if err != nil {
t.Fatalf("Failed to create temp static dir: %v", err)
}
defer os.RemoveAll(tmpStaticDir)
// Create a test static file
testHTML := "<!DOCTYPE html><html><body>Test</body></html>"
err = os.WriteFile(filepath.Join(tmpStaticDir, "index.html"), []byte(testHTML), 0644)
if err != nil {
t.Fatalf("Failed to create test file: %v", err)
}
// Initialize storage and handlers
store, err := storage.New(tmpDataDir)
if err != nil {
t.Fatalf("Failed to create storage: %v", err)
}
handlers := api.New(store)
// Create router with static directory
mux := router.New(handlers, tmpStaticDir)
// Create test server
ts := httptest.NewServer(mux)
defer ts.Close()
// Test serving index.html
resp, err := http.Get(ts.URL + "/")
if err != nil {
t.Fatalf("Failed to get index.html: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status 200, got %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Failed to read response body: %v", err)
}
if strings.TrimSpace(string(body)) != strings.TrimSpace(testHTML) {
t.Errorf("Response body mismatch")
}
t.Log("Static assets test passed")
}
func TestAPIRoutes(t *testing.T) {
logging.Init()
tmpDir, err := os.MkdirTemp("", "markdown-api-*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
store, err := storage.New(tmpDir)
if err != nil {
t.Fatalf("Failed to create storage: %v", err)
}
handlers := api.New(store)
mux := router.New(handlers, "")
ts := httptest.NewServer(mux)
defer ts.Close()
// Test GET /api/files (empty)
resp, err := http.Get(ts.URL + "/api/files")
if err != nil {
t.Fatalf("Failed to get files: %v", err)
}
resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status 200 for empty list, got %d", resp.StatusCode)
}
// Test POST /api/files
reqBody := `{"name":"test.md","content":"# Test"}`
resp, err = http.Post(ts.URL+"/api/files", "application/json", strings.NewReader(reqBody))
if err != nil {
t.Fatalf("Failed to create file: %v", err)
}
resp.Body.Close()
if resp.StatusCode != http.StatusCreated {
t.Errorf("Expected status 201 for create, got %d", resp.StatusCode)
}
// Test GET /api/files (with file)
resp, err = http.Get(ts.URL + "/api/files")
if err != nil {
t.Fatalf("Failed to get files: %v", err)
}
resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status 200 for list, got %d", resp.StatusCode)
}
// Test GET /api/files/test.md
resp, err = http.Get(ts.URL + "/api/files/test.md")
if err != nil {
t.Fatalf("Failed to get file: %v", err)
}
resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status 200 for get, got %d", resp.StatusCode)
}
// Test PUT /api/files/test.md
reqBody = `{"content":"# Updated"}`
req, _ := http.NewRequest("PUT", ts.URL+"/api/files/test.md", strings.NewReader(reqBody))
req.Header.Set("Content-Type", "application/json")
resp, err = http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("Failed to update file: %v", err)
}
resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status 200 for update, got %d", resp.StatusCode)
}
// Test DELETE /api/files/test.md
req, _ = http.NewRequest("DELETE", ts.URL+"/api/files/test.md", nil)
resp, err = http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("Failed to delete file: %v", err)
}
resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
t.Errorf("Expected status 204 for delete, got %d", resp.StatusCode)
}
t.Log("API routes test passed")
}
func TestErrorResponses(t *testing.T) {
tmpDir, err := os.MkdirTemp("", "markdown-error-*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tmpDir)
store, err := storage.New(tmpDir)
if err != nil {
t.Fatalf("Failed to create storage: %v", err)
}
handlers := api.New(store)
mux := router.New(handlers, "")
ts := httptest.NewServer(mux)
defer ts.Close()
// Test 404 for non-existent file
resp, err := http.Get(ts.URL + "/api/files/nonexistent.md")
if err != nil {
t.Fatalf("Failed to get file: %v", err)
}
resp.Body.Close()
if resp.StatusCode != http.StatusNotFound {
t.Errorf("Expected status 404, got %d", resp.StatusCode)
}
// Test 400 for invalid name
resp, err = http.Get(ts.URL + "/api/files/invalid.txt")
if err != nil {
t.Fatalf("Failed to get file: %v", err)
}
resp.Body.Close()
if resp.StatusCode != http.StatusBadRequest {
t.Errorf("Expected status 400, got %d", resp.StatusCode)
}
t.Log("Error responses test passed")
}

View File

@@ -0,0 +1,139 @@
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")
}