diff options
| author | Paul Buetow <paul@buetow.org> | 2026-06-05 10:26:16 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-06-05 10:26:16 +0300 |
| commit | 1433f7a13ede0c819ec4f8fd4027ad3df8daa94f (patch) | |
| tree | d3e98bc150711350585dd8203c5b50cc93243e52 /internal/askcli/command_edit_test.go | |
| parent | 0a52adf5752835da01e8a29df15e415398165c48 (diff) | |
Add 'ask edit' subcommand and collapse multi-line task descriptions
- ask edit opens $EDITOR and creates a task from the (multi-line) content,
reusing the shared internal/editor package
- collapse newlines in list output and fish completion so multi-line
descriptions render on one line and don't break completion
Bump version to 0.40.0
Amp-Thread-ID: https://ampcode.com/threads/T-019e96a1-9c8e-73d6-95b4-b55cb12cc762
Co-authored-by: Amp <amp@ampcode.com>
Diffstat (limited to 'internal/askcli/command_edit_test.go')
| -rw-r--r-- | internal/askcli/command_edit_test.go | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/internal/askcli/command_edit_test.go b/internal/askcli/command_edit_test.go new file mode 100644 index 0000000..3f4b778 --- /dev/null +++ b/internal/askcli/command_edit_test.go @@ -0,0 +1,86 @@ +package askcli + +import ( + "bytes" + "context" + "errors" + "io" + "strings" + "testing" +) + +func stubEditorCapture(t *testing.T, content string, err error) { + t.Helper() + old := captureFromEditor + captureFromEditor = func() (string, error) { + return content, err + } + t.Cleanup(func() { captureFromEditor = old }) +} + +func TestHandleEdit_Success(t *testing.T) { + now := useIsolatedTaskAliasCache(t) + writeTaskAliasCacheForTest(t, taskAliasCache{ + NextID: 1, + Entries: []taskAliasCacheEntry{ + {UUID: "existing-uuid", Alias: "0", CreatedAt: now}, + }, + }) + // editor.OpenTempAndEdit trims content, so mimic that here. + stubEditorCapture(t, "Multi line\ntask description", nil) + + var capturedArgs []string + d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) { + capturedArgs = args + _, _ = io.WriteString(stdout, "Created task abc-123-def.") + return 0, nil + }}) + + var stdout, stderr bytes.Buffer + code, _ := d.Dispatch(context.Background(), []string{"edit"}, nil, &stdout, &stderr) + if code != 0 { + t.Fatalf("edit code = %d stderr = %q", code, stderr.String()) + } + if got := strings.TrimSpace(stdout.String()); got != "created task 1" { + t.Fatalf("stdout = %q, want created task 1", stdout.String()) + } + if len(capturedArgs) == 0 || capturedArgs[len(capturedArgs)-1] != "Multi line\ntask description" { + t.Fatalf("description arg = %v, want trimmed multi-line content", capturedArgs) + } +} + +func TestHandleEdit_EmptyContentAborts(t *testing.T) { + stubEditorCapture(t, "", nil) + + d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) { + t.Fatalf("runner should not be called on empty content") + return 0, nil + }}) + + var stdout, stderr bytes.Buffer + code, _ := d.Dispatch(context.Background(), []string{"edit"}, nil, &stdout, &stderr) + if code != 1 { + t.Fatalf("edit code = %d, want 1", code) + } + if !strings.Contains(stderr.String(), "empty description") { + t.Fatalf("stderr = %q, want empty description error", stderr.String()) + } +} + +func TestHandleEdit_EditorError(t *testing.T) { + stubEditorCapture(t, "", errors.New("boom")) + + d := NewDispatcher(&spyRunner{runFn: func(ctx context.Context, args []string, stdin io.Reader, stdout, stderr io.Writer) (int, error) { + t.Fatalf("runner should not be called when editor fails") + return 0, nil + }}) + + var stdout, stderr bytes.Buffer + code, _ := d.Dispatch(context.Background(), []string{"edit"}, nil, &stdout, &stderr) + if code != 1 { + t.Fatalf("edit code = %d, want 1", code) + } + if !strings.Contains(stderr.String(), "boom") { + t.Fatalf("stderr = %q, want editor error", stderr.String()) + } +} |
