summaryrefslogtreecommitdiff
path: root/integrationtests/ask_test.go
blob: e18aa10424c0a265727d56d5ec19cbfe98c97271 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
package integrationtests

import (
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"regexp"
	"strconv"
	"strings"
	"testing"
	"time"

	"codeberg.org/snonux/hexai/internal/askcli"
)

var repoRoot string

func findRepoRoot() string {
	dir, err := os.Getwd()
	if err != nil {
		return ""
	}
	for {
		if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
			return dir
		}
		if _, err := os.Stat(filepath.Join(dir, ".git")); err == nil {
			return dir
		}
		parent := filepath.Dir(dir)
		if parent == dir {
			break
		}
		dir = parent
	}
	return ""
}

func init() {
	repoRoot = findRepoRoot()
}

func askBinaryPath() string {
	return filepath.Join(repoRoot, "cmd", "ask", "ask")
}

func runAsk(ctx context.Context, args []string) (stdout, stderr bytes.Buffer, exitCode int) {
	cmd := exec.CommandContext(ctx, askBinaryPath(), args...)
	cmd.Dir = repoRoot
	cmd.Stdout = &stdout
	cmd.Stderr = &stderr
	err := cmd.Run()
	if err == nil {
		return
	}
	ee, ok := err.(*exec.ExitError)
	if !ok {
		return bytes.Buffer{}, stderr, -1
	}
	return stdout, stderr, ee.ExitCode()
}

func runAskWithStdin(ctx context.Context, args []string, stdin string) (stdout, stderr bytes.Buffer, exitCode int) {
	cmd := exec.CommandContext(ctx, askBinaryPath(), args...)
	cmd.Dir = repoRoot
	cmd.Stdin = strings.NewReader(stdin)
	cmd.Stdout = &stdout
	cmd.Stderr = &stderr
	err := cmd.Run()
	if err == nil {
		return
	}
	ee, ok := err.(*exec.ExitError)
	if !ok {
		return bytes.Buffer{}, stderr, -1
	}
	return stdout, stderr, ee.ExitCode()
}

func runTask(ctx context.Context, args []string) (stdout, stderr bytes.Buffer, exitCode int) {
	cmd := exec.CommandContext(ctx, "task", args...)
	cmd.Dir = repoRoot
	cmd.Stdout = &stdout
	cmd.Stderr = &stderr
	err := cmd.Run()
	if err == nil {
		return
	}
	ee, ok := err.(*exec.ExitError)
	if !ok {
		return bytes.Buffer{}, stderr, -1
	}
	return stdout, stderr, ee.ExitCode()
}

func runTaskWithStdin(ctx context.Context, args []string, stdin string) (stdout, stderr bytes.Buffer, exitCode int) {
	cmd := exec.CommandContext(ctx, "task", args...)
	cmd.Dir = repoRoot
	cmd.Stdin = strings.NewReader(stdin)
	cmd.Stdout = &stdout
	cmd.Stderr = &stderr
	err := cmd.Run()
	if err == nil {
		return
	}
	ee, ok := err.(*exec.ExitError)
	if !ok {
		return bytes.Buffer{}, stderr, -1
	}
	return stdout, stderr, ee.ExitCode()
}

func createTask(ctx context.Context, desc string) (string, error) {
	stdout, _, code := runAskWithStdin(ctx, []string{"add", "+integrationtest", desc}, "yes\n")
	if code != 0 {
		return "", fmt.Errorf("create task failed (code %d): %s", code, stdout.String())
	}

	taskID := extractTaskID(stdout.String())
	if taskID == "" {
		return "", fmt.Errorf("could not extract task ID from output: %s", stdout.String())
	}

	time.Sleep(100 * time.Millisecond)

	infoOut, _, _ := runTask(ctx, []string{taskID, "info"})
	uuid := extractUUIDFromTaskInfo(infoOut.String())
	if uuid == "" {
		return "", fmt.Errorf("could not extract UUID from task info")
	}
	return uuid, nil
}

var taskUUIDRx = regexp.MustCompile(`UUID\s+(.+)`)

func extractUUIDFromTaskInfo(output string) string {
	if m := taskUUIDRx.FindStringSubmatch(output); len(m) > 1 {
		return strings.TrimSpace(m[1])
	}
	return ""
}

func extractTaskID(output string) string {
	output = strings.TrimSpace(output)
	lines := strings.Split(output, "\n")
	for _, line := range lines {
		line = strings.TrimSpace(line)
		if strings.Contains(line, "Created task") {
			fields := strings.Fields(line)
			for i, f := range fields {
				if f == "task" && i+1 < len(fields) {
					return strings.TrimSuffix(fields[i+1], ".")
				}
			}
		}
	}
	for _, line := range lines {
		line = strings.TrimSpace(line)
		if _, err := strconv.Atoi(line); err == nil {
			return line
		}
	}
	return ""
}

func deleteTask(ctx context.Context, uuid string) {
	runTaskWithStdin(ctx, []string{"uuid:" + uuid, "delete"}, "yes\n")
}

func listTasksWithTag(ctx context.Context, tag string) []askcli.TaskExport {
	stdout, _, _ := runTask(ctx, []string{"export", "project:hexai", "+agent"})
	var tasks []askcli.TaskExport
	if err := json.Unmarshal(stdout.Bytes(), &tasks); err != nil {
		return nil
	}
	var filtered []askcli.TaskExport
	for _, t := range tasks {
		if t.Status == "deleted" || t.Status == "completed" {
			continue
		}
		for _, t2 := range t.Tags {
			if t2 == tag {
				filtered = append(filtered, t)
				break
			}
		}
	}
	return filtered
}

type taskInfo struct {
	UUID        string
	Description string
	Status      string
	Priority    string
	Tags        []string
	Start       string
}

var uuidFieldRx = regexp.MustCompile(`UUID:\s+(.+)`)
var descFieldRx = regexp.MustCompile(`Description:\s+(.+)`)
var statusFieldRx = regexp.MustCompile(`Status:\s+(.+)`)
var priorityFieldRx = regexp.MustCompile(`Priority:\s+(.+)`)
var tagsFieldRx = regexp.MustCompile(`Tags:\s+(.+)`)
var startFieldRx = regexp.MustCompile(`Started:\s+(.+)`)

func parseTaskInfoText(output string, uuid string) taskInfo {
	ti := taskInfo{UUID: uuid}
	if m := uuidFieldRx.FindStringSubmatch(output); len(m) > 1 {
		ti.UUID = strings.TrimSpace(m[1])
	}
	if m := descFieldRx.FindStringSubmatch(output); len(m) > 1 {
		ti.Description = strings.TrimSpace(m[1])
	}
	if m := statusFieldRx.FindStringSubmatch(output); len(m) > 1 {
		ti.Status = strings.TrimSpace(m[1])
	}
	if m := priorityFieldRx.FindStringSubmatch(output); len(m) > 1 {
		ti.Priority = strings.TrimSpace(m[1])
	}
	if m := tagsFieldRx.FindStringSubmatch(output); len(m) > 1 {
		tagStr := strings.TrimSpace(m[1])
		ti.Tags = strings.Split(tagStr, ", ")
	}
	if m := startFieldRx.FindStringSubmatch(output); len(m) > 1 {
		ti.Start = strings.TrimSpace(m[1])
	}
	return ti
}

func getTaskInfoFast(ctx context.Context, uuid string) (taskInfo, bool) {
	stdout, _, code := runAsk(ctx, []string{"info", uuid})
	if code != 0 {
		return taskInfo{}, false
	}
	return parseTaskInfoText(stdout.String(), uuid), true
}

func TestMain(m *testing.M) {
	if repoRoot == "" {
		os.Exit(1)
	}
	askBin := askBinaryPath()
	if _, err := os.Stat(askBin); os.IsNotExist(err) {
		cmd := exec.Command("go", "build", "-o", askBin, "./cmd/ask/")
		cmd.Dir = repoRoot
		if out, err := cmd.CombinedOutput(); err != nil {
			fmt.Fprintf(os.Stderr, "failed to build ask binary: %v\n%s\n", err, out)
			os.Exit(1)
		}
	}
	os.Exit(m.Run())
}

func TestAdd(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()

	uuid, err := createTask(ctx, "integration test task for add")
	if err != nil {
		t.Fatalf("failed to create task: %v", err)
	}
	defer deleteTask(ctx, uuid)

	tasks := listTasksWithTag(ctx, "integrationtest")
	found := false
	for _, task := range tasks {
		if task.UUID == uuid {
			found = true
			break
		}
	}
	if !found {
		t.Errorf("task %s not found in export", uuid)
	}
}

func TestList(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()

	uuid, err := createTask(ctx, "integration test task for list")
	if err != nil {
		t.Fatalf("failed to create task: %v", err)
	}
	defer deleteTask(ctx, uuid)

	stdout, _, code := runAsk(ctx, []string{"list"})
	if code != 0 {
		t.Fatalf("list failed with code %d: %s", code, stdout.String())
	}
	if !strings.Contains(stdout.String(), "integration test task for list") {
		t.Errorf("list output does not contain expected task description")
	}
}

func TestAll(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()

	uuid, err := createTask(ctx, "integration test task for all")
	if err != nil {
		t.Fatalf("failed to create task: %v", err)
	}
	defer deleteTask(ctx, uuid)

	stdout, _, code := runAsk(ctx, []string{"all"})
	if code != 0 {
		t.Fatalf("all failed with code %d: %s", code, stdout.String())
	}
	if !strings.Contains(stdout.String(), "integration test task for all") {
		t.Errorf("all output does not contain expected task description")
	}
}

func TestReady(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()

	uuid, err := createTask(ctx, "integration test task for ready")
	if err != nil {
		t.Fatalf("failed to create task: %v", err)
	}
	defer deleteTask(ctx, uuid)

	stdout, _, code := runAsk(ctx, []string{"ready"})
	if code != 0 {
		t.Fatalf("ready failed with code %d: %s", code, stdout.String())
	}
	if !strings.Contains(stdout.String(), "integration test task for ready") {
		t.Errorf("ready output does not contain expected task description")
	}
}

func TestInfo(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()

	uuid, err := createTask(ctx, "integration test task for info")
	if err != nil {
		t.Fatalf("failed to create task: %v", err)
	}
	defer deleteTask(ctx, uuid)

	ti, ok := getTaskInfoFast(ctx, uuid)
	if !ok {
		t.Fatalf("info failed or returned no output")
	}
	if ti.UUID != uuid {
		t.Errorf("info uuid mismatch: got %s, want %s", ti.UUID, uuid)
	}
	if !strings.Contains(ti.Description, "integration test task for info") {
		t.Errorf("info description mismatch: %s", ti.Description)
	}
}

func TestAnnotate(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()

	uuid, err := createTask(ctx, "integration test task for annotate")
	if err != nil {
		t.Fatalf("failed to create task: %v", err)
	}
	defer deleteTask(ctx, uuid)

	note := "this is a test annotation"
	stdout, _, code := runAskWithStdin(ctx, []string{"annotate", uuid, note}, "yes\n")
	if code != 0 {
		t.Fatalf("annotate failed with code %d: %s", code, stdout.String())
	}

	ti, ok := getTaskInfoFast(ctx, uuid)
	if !ok {
		t.Fatalf("could not get task info after annotate")
	}
	_ = ti
}

func TestStart(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()

	uuid, err := createTask(ctx, "integration test task for start")
	if err != nil {
		t.Fatalf("failed to create task: %v", err)
	}
	defer deleteTask(ctx, uuid)

	stdout, _, code := runAskWithStdin(ctx, []string{"start", uuid}, "yes\n")
	if code != 0 {
		t.Fatalf("start failed with code %d: %s", code, stdout.String())
	}

	ti, ok := getTaskInfoFast(ctx, uuid)
	if !ok {
		t.Fatalf("could not get task info after start")
	}
	if ti.Start == "" {
		t.Errorf("task start field is empty after start")
	}
}

func TestStop(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()

	uuid, err := createTask(ctx, "integration test task for stop")
	if err != nil {
		t.Fatalf("failed to create task: %v", err)
	}
	defer deleteTask(ctx, uuid)

	runAskWithStdin(ctx, []string{"start", uuid}, "yes\n")

	stdout, _, code := runAskWithStdin(ctx, []string{"stop", uuid}, "yes\n")
	if code != 0 {
		t.Fatalf("stop failed with code %d: %s", code, stdout.String())
	}

	ti, ok := getTaskInfoFast(ctx, uuid)
	if !ok {
		t.Fatalf("could not get task info after stop")
	}
	if ti.Start != "" {
		t.Errorf("task start field is not empty after stop: %s", ti.Start)
	}
}

func TestDone(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()

	uuid, err := createTask(ctx, "integration test task for done")
	if err != nil {
		t.Fatalf("failed to create task: %v", err)
	}

	stdout, _, code := runAskWithStdin(ctx, []string{"done", uuid}, "yes\n")
	if code != 0 {
		t.Fatalf("done failed with code %d: %s", code, stdout.String())
	}

	ti, ok := getTaskInfoFast(ctx, uuid)
	if !ok {
		t.Fatalf("could not get task info after done")
	}
	if strings.ToLower(ti.Status) != "completed" {
		t.Errorf("task status = %s, want completed", ti.Status)
	}

	deleteTask(ctx, uuid)
}

func TestPriority(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()

	uuid, err := createTask(ctx, "integration test task for priority")
	if err != nil {
		t.Fatalf("failed to create task: %v", err)
	}
	defer deleteTask(ctx, uuid)

	stdout, _, code := runAskWithStdin(ctx, []string{"priority", uuid, "H"}, "yes\n")
	if code != 0 {
		t.Fatalf("priority failed with code %d: %s", code, stdout.String())
	}

	ti, ok := getTaskInfoFast(ctx, uuid)
	if !ok {
		t.Fatalf("could not get task info after priority")
	}
	if ti.Priority != "H" {
		t.Errorf("task priority = %s, want H", ti.Priority)
	}
}

func TestTag(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()

	uuid, err := createTask(ctx, "integration test task for tag")
	if err != nil {
		t.Fatalf("failed to create task: %v", err)
	}
	defer deleteTask(ctx, uuid)

	stdout, _, code := runAskWithStdin(ctx, []string{"tag", uuid, "+cli"}, "yes\n")
	if code != 0 {
		t.Fatalf("tag add failed with code %d: %s", code, stdout.String())
	}

	ti, ok := getTaskInfoFast(ctx, uuid)
	if !ok {
		t.Fatalf("could not get task info after tag")
	}
	found := false
	for _, tg := range ti.Tags {
		if tg == "cli" {
			found = true
			break
		}
	}
	if !found {
		t.Errorf("tag cli not found on task: %+v", ti.Tags)
	}

	runAskWithStdin(ctx, []string{"tag", uuid, "-cli"}, "yes\n")

	ti2, _ := getTaskInfoFast(ctx, uuid)
	for _, tg := range ti2.Tags {
		if tg == "cli" {
			t.Errorf("tag cli should have been removed")
			break
		}
	}
}

func TestDepAdd(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()

	uuid1, err := createTask(ctx, "integration test dep target")
	if err != nil {
		t.Fatalf("failed to create task: %v", err)
	}
	defer deleteTask(ctx, uuid1)

	uuid2, err := createTask(ctx, "integration test dep dependent")
	if err != nil {
		t.Fatalf("failed to create task: %v", err)
	}
	defer deleteTask(ctx, uuid2)

	stdout, _, code := runAskWithStdin(ctx, []string{"dep", "add", uuid2, uuid1}, "yes\n")
	if code != 0 {
		t.Fatalf("dep add failed with code %d: %s", code, stdout.String())
	}

	tasks := listTasksWithTag(ctx, "integrationtest")
	for _, task := range tasks {
		if task.UUID == uuid2 {
			found := false
			for _, dep := range task.Depends {
				if dep == uuid1 {
					found = true
					break
				}
			}
			if !found {
				t.Errorf("dependency %s not found on task", uuid1)
			}
			break
		}
	}
}

func TestDepList(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()

	uuid1, err := createTask(ctx, "integration test dep list target")
	if err != nil {
		t.Fatalf("failed to create task: %v", err)
	}
	defer deleteTask(ctx, uuid1)

	uuid2, err := createTask(ctx, "integration test dep list dependent")
	if err != nil {
		t.Fatalf("failed to create task: %v", err)
	}
	defer deleteTask(ctx, uuid2)

	runAskWithStdin(ctx, []string{"dep", "add", uuid2, uuid1}, "yes\n")

	stdout, _, code := runAsk(ctx, []string{"dep", "list", uuid2})
	if code != 0 {
		t.Fatalf("dep list failed with code %d: %s", code, stdout.String())
	}
	if !strings.Contains(stdout.String(), uuid1) {
		t.Errorf("dep list output does not contain target uuid: %s", stdout.String())
	}
}

func TestDepRm(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()

	uuid1, err := createTask(ctx, "integration test dep rm target")
	if err != nil {
		t.Fatalf("failed to create task: %v", err)
	}
	defer deleteTask(ctx, uuid1)

	uuid2, err := createTask(ctx, "integration test dep rm dependent")
	if err != nil {
		t.Fatalf("failed to create task: %v", err)
	}
	defer deleteTask(ctx, uuid2)

	runAskWithStdin(ctx, []string{"dep", "add", uuid2, uuid1}, "yes\n")

	stdout, _, code := runAskWithStdin(ctx, []string{"dep", "rm", uuid2, uuid1}, "yes\n")
	if code != 0 {
		t.Fatalf("dep rm failed with code %d: %s", code, stdout.String())
	}

	tasks := listTasksWithTag(ctx, "integrationtest")
	for _, task := range tasks {
		if task.UUID == uuid2 {
			for _, dep := range task.Depends {
				if dep == uuid1 {
					t.Errorf("dependency should have been removed")
					break
				}
			}
			break
		}
	}
}

func TestModify(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()

	uuid, err := createTask(ctx, "integration test task for modify")
	if err != nil {
		t.Fatalf("failed to create task: %v", err)
	}
	defer deleteTask(ctx, uuid)

	stdout, _, code := runAskWithStdin(ctx, []string{"modify", uuid, "priority:H"}, "yes\n")
	if code != 0 {
		t.Fatalf("modify failed with code %d: %s", code, stdout.String())
	}

	ti, ok := getTaskInfoFast(ctx, uuid)
	if !ok {
		t.Fatalf("could not get task info after modify")
	}
	if ti.Priority != "H" {
		t.Errorf("task priority = %s, want H", ti.Priority)
	}
}

func TestDenotate(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()

	uuid, err := createTask(ctx, "integration test task for denotate")
	if err != nil {
		t.Fatalf("failed to create task: %v", err)
	}
	defer deleteTask(ctx, uuid)

	runAskWithStdin(ctx, []string{"annotate", uuid, "annotation to remove"}, "yes\n")

	tiBefore, _ := getTaskInfoFast(ctx, uuid)
	descBefore := tiBefore.Description

	_, _, code := runAskWithStdin(ctx, []string{"denotate", uuid, "annotation to remove"}, "yes\n")
	if code != 0 {
		t.Fatalf("denotate returned non-zero code: %d", code)
	}

	tiAfter, _ := getTaskInfoFast(ctx, uuid)
	if tiAfter.Description != descBefore {
		t.Errorf("denotate changed description unexpectedly: %s -> %s", descBefore, tiAfter.Description)
	}
}

func TestDelete(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()

	uuid, err := createTask(ctx, "integration test task for delete")
	if err != nil {
		t.Fatalf("failed to create task: %v", err)
	}

	stdout, _, code := runAskWithStdin(ctx, []string{"delete", uuid}, "yes\n")
	if code != 0 {
		t.Fatalf("delete failed with code %d: %s", code, stdout.String())
	}

	tasks := listTasksWithTag(ctx, "integrationtest")
	for _, task := range tasks {
		if task.UUID == uuid {
			t.Errorf("task should have been deleted but still exists")
			break
		}
	}
}

func TestUrgency(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()

	uuid, err := createTask(ctx, "integration test task for urgency")
	if err != nil {
		t.Fatalf("failed to create task: %v", err)
	}
	defer deleteTask(ctx, uuid)

	stdout, _, code := runAsk(ctx, []string{"urgency"})
	if code != 0 {
		t.Fatalf("urgency failed with code %d: %s", code, stdout.String())
	}
	if !strings.Contains(stdout.String(), "integration test task for urgency") {
		t.Errorf("urgency output does not contain expected task description")
	}
}

func TestDefaultCommand(t *testing.T) {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()

	uuid, err := createTask(ctx, "integration test for default command")
	if err != nil {
		t.Fatalf("failed to create task: %v", err)
	}
	defer deleteTask(ctx, uuid)

	stdout, _, code := runAsk(ctx, []string{})
	if code != 0 {
		t.Fatalf("default command (list) failed with code %d: %s", code, stdout.String())
	}
	if !strings.Contains(stdout.String(), "integration test for default command") {
		t.Errorf("default command output does not contain expected task description")
	}
}