feat: add parse endpoint, refactor recurring tasks, and improve web task completion

Extract CreateRecurringTask into engine package for reuse by both CLI
and API. Add POST /tasks/parse endpoint for CLI-style input parsing.
Remove FK constraint on change_log to preserve history after task
deletion. Update web frontend to filter completed tasks from view and
add mock mode support for development.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-14 22:39:11 +01:00
parent 0352c22b4f
commit 78881e1b07
15 changed files with 2118 additions and 128 deletions
+95 -1
View File
@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
"git.jnss.me/joakim/opal/internal/engine"
@@ -30,10 +31,32 @@ func errorResponse(w http.ResponseWriter, status int, message string) {
})
}
// ListTasks returns tasks based on filter query parameters
// ListTasks returns tasks based on filter query parameters or a named report
func ListTasks(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
// Check for report mode
if reportName := query.Get("report"); reportName != "" {
report, err := engine.GetReport(reportName)
if err != nil {
errorResponse(w, http.StatusBadRequest, err.Error())
return
}
tasks, err := report.Execute(nil)
if err != nil {
errorResponse(w, http.StatusInternalServerError, err.Error())
return
}
jsonResponse(w, http.StatusOK, map[string]interface{}{
"report": reportName,
"tasks": tasks,
"count": len(tasks),
})
return
}
// Build filter from query params
filter := engine.NewFilter()
@@ -409,6 +432,77 @@ func AddTaskTag(w http.ResponseWriter, r *http.Request) {
jsonResponse(w, http.StatusOK, task)
}
// ParseTaskRequest represents the request body for parsing a CLI-style task input
type ParseTaskRequest struct {
Input string `json:"input"`
}
// ParseTask accepts a raw CLI-style input string, parses it, and creates a task
func ParseTask(w http.ResponseWriter, r *http.Request) {
var req ParseTaskRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
errorResponse(w, http.StatusBadRequest, "invalid request body")
return
}
input := strings.TrimSpace(req.Input)
if input == "" {
errorResponse(w, http.StatusBadRequest, "input is required")
return
}
// Split input on whitespace
args := strings.Fields(input)
// Classify args: words with +/- prefix or containing : are modifiers
var descParts []string
var modifierArgs []string
for _, arg := range args {
if strings.HasPrefix(arg, "+") || strings.HasPrefix(arg, "-") || strings.Contains(arg, ":") {
modifierArgs = append(modifierArgs, arg)
} else {
descParts = append(descParts, arg)
}
}
if len(descParts) == 0 {
errorResponse(w, http.StatusBadRequest, "description is required")
return
}
description := strings.Join(descParts, " ")
// Parse modifiers
var mod *engine.Modifier
if len(modifierArgs) > 0 {
var err error
mod, err = engine.ParseModifier(modifierArgs)
if err != nil {
errorResponse(w, http.StatusBadRequest, fmt.Sprintf("failed to parse modifiers: %v", err))
return
}
}
// Check for recurring task
if mod != nil && mod.SetAttributes["recur"] != nil {
instance, err := engine.CreateRecurringTask(description, mod)
if err != nil {
errorResponse(w, http.StatusBadRequest, err.Error())
return
}
jsonResponse(w, http.StatusCreated, map[string]interface{}{"task": instance})
return
}
// Create regular task
task, err := engine.CreateTaskWithModifier(description, mod)
if err != nil {
errorResponse(w, http.StatusBadRequest, err.Error())
return
}
jsonResponse(w, http.StatusCreated, map[string]interface{}{"task": task})
}
// RemoveTaskTag removes a tag from a task
func RemoveTaskTag(w http.ResponseWriter, r *http.Request) {
uuidStr := chi.URLParam(r, "uuid")
@@ -0,0 +1,220 @@
package handlers
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"testing"
"git.jnss.me/joakim/opal/internal/engine"
)
func TestMain(m *testing.M) {
testDir := "/tmp/opal-handler-test"
engine.SetConfigDirOverride(testDir)
engine.SetDataDirOverride(testDir)
if err := os.MkdirAll(testDir, 0755); err != nil {
panic(err)
}
if err := engine.InitDB(); err != nil {
panic(err)
}
defer engine.CloseDB()
code := m.Run()
os.RemoveAll(testDir)
os.Exit(code)
}
func TestParseTask_DescriptionOnly(t *testing.T) {
body := `{"input": "buy groceries"}`
req := httptest.NewRequest(http.MethodPost, "/tasks/parse", bytes.NewBufferString(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
ParseTask(w, req)
if w.Code != http.StatusCreated {
t.Fatalf("expected 201, got %d: %s", w.Code, w.Body.String())
}
var resp map[string]interface{}
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("failed to decode response: %v", err)
}
data, ok := resp["data"].(map[string]interface{})
if !ok {
t.Fatal("expected data in response")
}
task, ok := data["task"].(map[string]interface{})
if !ok {
t.Fatal("expected task in data")
}
if task["Description"] != "buy groceries" {
t.Errorf("expected description 'buy groceries', got %v", task["Description"])
}
}
func TestParseTask_WithModifiers(t *testing.T) {
body := `{"input": "review PR priority:H project:backend +code"}`
req := httptest.NewRequest(http.MethodPost, "/tasks/parse", bytes.NewBufferString(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
ParseTask(w, req)
if w.Code != http.StatusCreated {
t.Fatalf("expected 201, got %d: %s", w.Code, w.Body.String())
}
var resp map[string]interface{}
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("failed to decode response: %v", err)
}
data := resp["data"].(map[string]interface{})
task := data["task"].(map[string]interface{})
if task["Description"] != "review PR" {
t.Errorf("expected description 'review PR', got %v", task["Description"])
}
if task["Project"] != "backend" {
t.Errorf("expected project 'backend', got %v", task["Project"])
}
}
func TestParseTask_WithRecurrence(t *testing.T) {
body := `{"input": "team meeting due:2026-03-01 recur:1w +meetings"}`
req := httptest.NewRequest(http.MethodPost, "/tasks/parse", bytes.NewBufferString(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
ParseTask(w, req)
if w.Code != http.StatusCreated {
t.Fatalf("expected 201, got %d: %s", w.Code, w.Body.String())
}
var resp map[string]interface{}
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("failed to decode response: %v", err)
}
data := resp["data"].(map[string]interface{})
task := data["task"].(map[string]interface{})
// The returned task should be the first instance (pending, with ParentUUID)
if task["ParentUUID"] == nil {
t.Error("expected ParentUUID to be set for recurring instance")
}
}
func TestParseTask_EmptyInput(t *testing.T) {
body := `{"input": ""}`
req := httptest.NewRequest(http.MethodPost, "/tasks/parse", bytes.NewBufferString(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
ParseTask(w, req)
if w.Code != http.StatusBadRequest {
t.Fatalf("expected 400, got %d: %s", w.Code, w.Body.String())
}
}
func TestParseTask_OnlyModifiers(t *testing.T) {
body := `{"input": "+tag priority:H"}`
req := httptest.NewRequest(http.MethodPost, "/tasks/parse", bytes.NewBufferString(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
ParseTask(w, req)
if w.Code != http.StatusBadRequest {
t.Fatalf("expected 400, got %d: %s", w.Code, w.Body.String())
}
}
func TestParseTask_InvalidModifier(t *testing.T) {
body := `{"input": "some task due:notadate"}`
req := httptest.NewRequest(http.MethodPost, "/tasks/parse", bytes.NewBufferString(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
ParseTask(w, req)
if w.Code != http.StatusBadRequest {
t.Fatalf("expected 400, got %d: %s", w.Code, w.Body.String())
}
}
func TestListTasks_WithReport(t *testing.T) {
// Create a task first so the report has something to return
_, err := engine.CreateTask("report test task")
if err != nil {
t.Fatalf("failed to create test task: %v", err)
}
req := httptest.NewRequest(http.MethodGet, "/tasks?report=list", nil)
w := httptest.NewRecorder()
ListTasks(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
}
var resp map[string]interface{}
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("failed to decode response: %v", err)
}
data := resp["data"].(map[string]interface{})
if data["report"] != "list" {
t.Errorf("expected report name 'list', got %v", data["report"])
}
if data["count"] == nil {
t.Error("expected count in response")
}
if data["tasks"] == nil {
t.Error("expected tasks in response")
}
}
func TestListTasks_UnknownReport(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/tasks?report=nonexistent", nil)
w := httptest.NewRecorder()
ListTasks(w, req)
if w.Code != http.StatusBadRequest {
t.Fatalf("expected 400, got %d: %s", w.Code, w.Body.String())
}
}
func TestListTasks_NoReport(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/tasks", nil)
w := httptest.NewRecorder()
ListTasks(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
}
var resp map[string]interface{}
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("failed to decode response: %v", err)
}
// Without report param, data should be the tasks array directly (not wrapped)
if resp["success"] != true {
t.Error("expected success to be true")
}
}