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
+3 -91
View File
@@ -4,10 +4,8 @@ import (
"fmt"
"os"
"strings"
"time"
"git.jnss.me/joakim/opal/internal/engine"
"github.com/google/uuid"
"github.com/spf13/cobra"
)
@@ -101,99 +99,13 @@ func parseAddArgs(args []string) (string, []string, error) {
}
func addRecurringTask(description string, mod *engine.Modifier) error {
// Extract recurrence pattern
recurPattern := mod.SetAttributes["recur"]
if recurPattern == nil {
return fmt.Errorf("no recurrence pattern specified")
}
// Validate: recurring tasks must have due date
if mod.SetAttributes["due"] == nil {
return fmt.Errorf("recurring tasks require a due date (use due:YYYY-MM-DD or due:monday)")
}
duration, err := engine.ParseRecurrencePattern(*recurPattern)
instance, err := engine.CreateRecurringTask(description, mod)
if err != nil {
return fmt.Errorf("invalid recurrence pattern: %w", err)
return err
}
// Create template task (without saving yet)
now := time.Now()
template := &engine.Task{
UUID: uuid.New(),
Status: engine.StatusRecurring,
Description: description,
Priority: engine.PriorityDefault,
Created: now,
Modified: now,
RecurrenceDuration: &duration,
Tags: []string{},
}
// Create modifier without the recur attribute
tempMod := &engine.Modifier{
SetAttributes: make(map[string]*string),
AttributeOrder: []string{},
AddTags: mod.AddTags,
RemoveTags: mod.RemoveTags,
}
// Copy all attributes except recur
for _, key := range mod.AttributeOrder {
if key != "recur" {
val := mod.SetAttributes[key]
tempMod.SetAttributes[key] = val
tempMod.AttributeOrder = append(tempMod.AttributeOrder, key)
}
}
// Apply modifiers to template before first save
if err := tempMod.ApplyToNew(template); err != nil {
return fmt.Errorf("failed to apply modifiers to template: %w", err)
}
// Save template
if err := template.Save(); err != nil {
return fmt.Errorf("failed to save template: %w", err)
}
// Add tags to template (requires task.ID from save)
for _, tag := range mod.AddTags {
if err := template.AddTag(tag); err != nil {
return fmt.Errorf("failed to add tag to template: %w", err)
}
}
// Create first instance
instance := &engine.Task{
UUID: uuid.New(),
Status: engine.StatusPending,
Description: description,
Priority: template.Priority,
Created: now,
Modified: now,
ParentUUID: &template.UUID,
Due: template.Due,
Wait: template.Wait,
Scheduled: template.Scheduled,
Project: template.Project,
Tags: []string{},
}
if err := instance.Save(); err != nil {
return fmt.Errorf("failed to save first instance: %w", err)
}
// Copy tags to instance
for _, tag := range template.Tags {
if err := instance.AddTag(tag); err != nil {
return fmt.Errorf("failed to add tag to instance: %w", err)
}
}
fmt.Printf("Created recurring task %s\n", template.UUID)
fmt.Printf("Created recurring task %s\n", *instance.ParentUUID)
fmt.Printf("First instance: %s\n", instance.UUID)
fmt.Printf("Recurrence: %s\n", engine.FormatRecurrenceDuration(duration))
return nil
}
+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")
}
}
+1
View File
@@ -53,6 +53,7 @@ func (s *Server) setupRoutes() {
r.Route("/tasks", func(r chi.Router) {
r.Get("/", handlers.ListTasks)
r.Post("/", handlers.CreateTask)
r.Post("/parse", handlers.ParseTask)
r.Get("/{uuid}", handlers.GetTask)
r.Put("/{uuid}", handlers.UpdateTask)
r.Delete("/{uuid}", handlers.DeleteTask)
+2 -2
View File
@@ -173,13 +173,13 @@ func runMigrations() error {
);
-- Change log (key:value format like edit command)
-- No FK on task_uuid: change logs must survive task deletion
CREATE TABLE change_log (
id INTEGER PRIMARY KEY AUTOINCREMENT,
task_uuid TEXT NOT NULL,
change_type TEXT NOT NULL,
changed_at INTEGER NOT NULL,
data TEXT NOT NULL,
FOREIGN KEY (task_uuid) REFERENCES tasks(uuid) ON DELETE CASCADE
data TEXT NOT NULL
);
CREATE INDEX idx_change_log_timestamp ON change_log(changed_at);
+5 -2
View File
@@ -150,13 +150,16 @@ func TestFilterToSQL(t *testing.T) {
}
// Test tag filter
// Note: without a status filter, ToSQL adds an implicit template exclusion
// condition, so args will contain [StatusRecurring, "urgent"]
filter = &Filter{
IncludeTags: []string{"urgent"},
Attributes: map[string]string{},
}
where, args = filter.ToSQL()
if len(args) != 1 || args[0] != "urgent" {
t.Errorf("Expected tag 'urgent' in args, got %v", args)
if len(args) != 2 || args[1] != "urgent" {
t.Errorf("Expected tag 'urgent' as second arg (after template exclusion), got %v", args)
}
}
+88
View File
@@ -100,6 +100,94 @@ func CalculateNextDue(currentDue time.Time, recurrence time.Duration) time.Time
return currentDue.Add(recurrence)
}
// CreateRecurringTask creates a recurring task template and its first instance.
// It validates the recurrence pattern and due date, creates the template with
// StatusRecurring, then creates the first pending instance linked to it.
// Returns the first instance task.
func CreateRecurringTask(description string, mod *Modifier) (*Task, error) {
recurPattern := mod.SetAttributes["recur"]
if recurPattern == nil {
return nil, fmt.Errorf("no recurrence pattern specified")
}
if mod.SetAttributes["due"] == nil {
return nil, fmt.Errorf("recurring tasks require a due date (use due:YYYY-MM-DD or due:monday)")
}
duration, err := ParseRecurrencePattern(*recurPattern)
if err != nil {
return nil, fmt.Errorf("invalid recurrence pattern: %w", err)
}
now := time.Now()
template := &Task{
UUID: uuid.New(),
Status: StatusRecurring,
Description: description,
Priority: PriorityDefault,
Created: now,
Modified: now,
RecurrenceDuration: &duration,
Tags: []string{},
}
// Build modifier without the recur attribute
tempMod := &Modifier{
SetAttributes: make(map[string]*string),
AttributeOrder: []string{},
AddTags: mod.AddTags,
RemoveTags: mod.RemoveTags,
}
for _, key := range mod.AttributeOrder {
if key != "recur" {
tempMod.SetAttributes[key] = mod.SetAttributes[key]
tempMod.AttributeOrder = append(tempMod.AttributeOrder, key)
}
}
if err := tempMod.ApplyToNew(template); err != nil {
return nil, fmt.Errorf("failed to apply modifiers to template: %w", err)
}
if err := template.Save(); err != nil {
return nil, fmt.Errorf("failed to save template: %w", err)
}
for _, tag := range mod.AddTags {
if err := template.AddTag(tag); err != nil {
return nil, fmt.Errorf("failed to add tag to template: %w", err)
}
}
// Create first instance
instance := &Task{
UUID: uuid.New(),
Status: StatusPending,
Description: description,
Priority: template.Priority,
Created: now,
Modified: now,
ParentUUID: &template.UUID,
Due: template.Due,
Wait: template.Wait,
Scheduled: template.Scheduled,
Project: template.Project,
Tags: []string{},
}
if err := instance.Save(); err != nil {
return nil, fmt.Errorf("failed to save first instance: %w", err)
}
for _, tag := range template.Tags {
if err := instance.AddTag(tag); err != nil {
return nil, fmt.Errorf("failed to add tag to instance: %w", err)
}
}
return instance, nil
}
// SpawnNextInstance creates a new task instance from completed recurring task
func SpawnNextInstance(completedInstance *Task) error {
if completedInstance.ParentUUID == nil {