Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
internal/remote/testdata/** text eol=lf
internal/vault/testdata/** text eol=lf
internal/mcp/testdata/** text eol=lf
internal/cli/testdata/** text eol=lf
internal/tui/testdata/** text eol=lf
150 changes: 150 additions & 0 deletions internal/cli/remote_parity_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package cli

import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"reflect"
"sync"
"testing"
)

// remoteServer serves the recorded responses in testdata/remote-tasks as a
// ZenNotes server and records each note body that a client writes.
type remoteServer struct {
URL string
mu sync.Mutex
writes []string
}

func (s *remoteServer) Writes() []string {
s.mu.Lock()
defer s.mu.Unlock()
return append([]string(nil), s.writes...)
}

func newRemoteServer(t *testing.T) *remoteServer {
t.Helper()
dir := filepath.Join("testdata", "remote-tasks")
file := func(name string) []byte {
b, err := os.ReadFile(filepath.Join(dir, name))
if err != nil {
t.Fatal(err)
}
return b
}
var tasks []map[string]any
if err := json.Unmarshal(file("tasks.json"), &tasks); err != nil {
t.Fatal(err)
}
s := &remoteServer{}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.URL.Path {
case "/api/vault":
_, _ = w.Write(file("vault.json"))
case "/api/vault/settings":
_, _ = w.Write(file("settings.json"))
case "/api/notes":
_, _ = w.Write(file("notes.json"))
case "/api/tasks":
_, _ = w.Write(file("tasks.json"))
case "/api/tasks/for":
out := []map[string]any{}
for _, task := range tasks {
if task["sourcePath"] == r.URL.Query().Get("path") {
out = append(out, task)
}
}
_ = json.NewEncoder(w).Encode(out)
case "/api/notes/read":
rel := r.URL.Query().Get("path")
var metas []map[string]any
_ = json.Unmarshal(file("notes.json"), &metas)
for _, meta := range metas {
if meta["path"] == rel {
meta["body"] = string(file(filepath.Join("notes", filepath.FromSlash(rel))))
_ = json.NewEncoder(w).Encode(meta)
return
}
}
http.NotFound(w, r)
case "/api/notes/write":
var in map[string]string
_ = json.NewDecoder(r.Body).Decode(&in)
s.mu.Lock()
s.writes = append(s.writes, in["path"]+"\n"+in["body"])
s.mu.Unlock()
_ = json.NewEncoder(w).Encode(map[string]any{
"path": in["path"], "title": "x", "folder": "inbox", "tags": []string{}, "wikilinks": []string{},
})
default:
http.NotFound(w, r)
}
}))
t.Cleanup(server.Close)
s.URL = server.URL
return s
}

// useRemoteServer points zn at the server for the rest of the test.
func useRemoteServer(t *testing.T, s *remoteServer) {
t.Helper()
isolatedCLI(t)
t.Setenv("ZENNOTES_SERVER", s.URL)
t.Setenv("ZENNOTES_REMOTE_TOKEN", "token")
}

// Against a server, `zn task list` prints the same bytes as zn v0.18.1 for
// a checkbox task with `@repeat:` and for a task note. The `scheduled` value
// that the server sends stays in the JSON.
func TestRemoteTaskListMatchesReleasedOutput(t *testing.T) {
s := newRemoteServer(t)
useRemoteServer(t, s)
for _, tc := range []struct {
golden string
flags []string
}{
{"remote-tasks.json", []string{"--json"}},
{"remote-tasks-all.json", []string{"--json", "--all"}},
{"remote-tasks.txt", nil},
} {
want, err := os.ReadFile(filepath.Join("testdata", tc.golden))
if err != nil {
t.Fatal(err)
}
args := append([]string{"task", "list"}, tc.flags...)
out := captureOutput(t, func() {
if code := Main(args); code != 0 {
t.Errorf("zn %v exited %d", args, code)
}
})
if out != string(want) {
t.Errorf("zn %v differs from testdata/%s:\n%s", args, tc.golden, out)
}
}
}

// Against a server, `zn task toggle` of a `@repeat:` task writes the next
// open line and the finished line, as zn v0.18.1 does.
func TestRemoteToggleOfRepeatingTaskMakesNextOccurrence(t *testing.T) {
s := newRemoteServer(t)
useRemoteServer(t, s)
args := []string{"task", "toggle", "inbox/Work.md#0"}
captureOutput(t, func() {
if code := Main(args); code != 0 {
t.Errorf("zn %v exited %d", args, code)
}
})
want := []string{"inbox/Work.md\n# Work\n\n" +
"- [ ] Water plants @repeat:weekly due:2099-01-12\n" +
"- [x] Water plants due:2099-01-05\n" +
"- [ ] Send the invoice due:2026-11-12\n" +
"- [x] Pay the bill due:2026-11-01\n" +
"- [-] Cancel the trip\n"}
if got := s.Writes(); !reflect.DeepEqual(got, want) {
t.Fatalf("writes:\n%q\nwant:\n%q", got, want)
}
}
66 changes: 66 additions & 0 deletions internal/cli/tasks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,69 @@ func TestTaskTogglePrintsWhereARecurringTaskWent(t *testing.T) {
t.Fatalf("%q", data)
}
}

// A vault of checkbox tasks and task notes lists the same JSON bytes as zn
// v0.18.1, with and without --all.
func TestTaskListJSONMatchesReleasedForDefaultVault(t *testing.T) {
isolatedCLI(t)
root := t.TempDir()
if err := os.CopyFS(root, os.DirFS(filepath.Join("testdata", "default-tasks"))); err != nil {
t.Fatal(err)
}
for _, tc := range []struct {
golden string
flags []string
}{
{"default-tasks.json", nil},
{"default-tasks-all.json", []string{"--all"}},
} {
want, err := os.ReadFile(filepath.Join("testdata", tc.golden))
if err != nil {
t.Fatal(err)
}
args := append([]string{"task", "list", "--json", "--vault", root}, tc.flags...)
out := captureOutput(t, func() {
if code := Main(args); code != 0 {
t.Errorf("zn %v exited %d", args, code)
}
})
if out != string(want) {
t.Errorf("zn %v differs from testdata/%s:\n%s", args, tc.golden, out)
}
}
}

// This vault holds only the `@repeat:` values of released zn: daily, weekly,
// weekdays, `mon/thu` and `after-3d`. It also holds a date in `@remind:`. It
// lists the same text and JSON as zn v0.18.1. `zn task list`, `zn task list
// --json` and `zn task list --json --all` over testdata/released-repeat made
// the golden files.
func TestTaskListMatchesReleasedForUpstreamRepeat(t *testing.T) {
isolatedCLI(t)
root := t.TempDir()
if err := os.CopyFS(root, os.DirFS(filepath.Join("testdata", "released-repeat"))); err != nil {
t.Fatal(err)
}
for _, tc := range []struct {
golden string
flags []string
}{
{"released-repeat.txt", nil},
{"released-repeat.json", []string{"--json"}},
{"released-repeat-all.json", []string{"--json", "--all"}},
} {
want, err := os.ReadFile(filepath.Join("testdata", tc.golden))
if err != nil {
t.Fatal(err)
}
args := append([]string{"task", "list", "--vault", root}, tc.flags...)
out := captureOutput(t, func() {
if code := Main(args); code != 0 {
t.Errorf("zn %v exited %d", args, code)
}
})
if out != string(want) {
t.Errorf("zn %v differs from testdata/%s:\n%s", args, tc.golden, out)
}
}
}
Loading
Loading