diff --git a/internal/cli/add.go b/internal/cli/add.go index 5123e04..81cd777 100644 --- a/internal/cli/add.go +++ b/internal/cli/add.go @@ -37,7 +37,7 @@ FLAGS --status Optional initial status string --who Who was involved (free-form) --where Where it happened (free-form) - --event-time When it happened (free-form: "2024年夏天", "last Tuesday", ISO 8601) + --event-time When it happened (free-form: "summer 2024", "last Tuesday", ISO 8601) STDIN FORMAT When piping JSON, provide a single object with the same field names: diff --git a/internal/cli/root.go b/internal/cli/root.go index 7563387..4fc821e 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "fmt" + "io" "os" "github.com/senguoyun-guosheng/graphmind/internal/db" @@ -270,9 +271,15 @@ func pluralize(singular, plural string, n int) string { } func writeJSON(v any) { - enc := json.NewEncoder(os.Stdout) + enc := newJSONEncoder(os.Stdout) if pretty { enc.SetIndent("", " ") } enc.Encode(v) } + +func newJSONEncoder(w io.Writer) *json.Encoder { + enc := json.NewEncoder(w) + enc.SetEscapeHTML(false) + return enc +} diff --git a/internal/cli/root_test.go b/internal/cli/root_test.go new file mode 100644 index 0000000..e9c9c3c --- /dev/null +++ b/internal/cli/root_test.go @@ -0,0 +1,58 @@ +package cli + +import ( + "bytes" + "strings" + "testing" + + "github.com/senguoyun-guosheng/graphmind/internal/model" +) + +func TestNewJSONEncoderDoesNotEscapeAngleBrackets(t *testing.T) { + var out bytes.Buffer + env := model.Envelope{ + OK: true, + Summary: "Listed 0 nodes.", + NextSteps: []string{"gm cat — inspect a specific node"}, + } + + if err := newJSONEncoder(&out).Encode(env); err != nil { + t.Fatalf("encode json: %v", err) + } + + got := out.String() + if strings.Contains(got, `\u003c`) || strings.Contains(got, `\u003e`) { + t.Fatalf("expected angle brackets to remain unescaped, got %q", got) + } + if !strings.Contains(got, `"gm cat — inspect a specific node"`) { + t.Fatalf("expected literal angle brackets in output, got %q", got) + } +} + +func TestNewJSONEncoderPreservesUserUnicodeText(t *testing.T) { + var out bytes.Buffer + env := model.Envelope{ + OK: true, + Data: map[string]string{"title": "中文节点", "description": "用户输入:上海会议"}, + Summary: "Retrieved 1 node.", + NextSteps: []string{"gm cat — inspect the node"}, + } + + if err := newJSONEncoder(&out).Encode(env); err != nil { + t.Fatalf("encode json: %v", err) + } + + got := out.String() + if !strings.Contains(got, `"summary":"Retrieved 1 node."`) { + t.Fatalf("expected English summary in output, got %q", got) + } + if !strings.Contains(got, `"gm cat — inspect the node"`) { + t.Fatalf("expected English next step in output, got %q", got) + } + if !strings.Contains(got, `"title":"中文节点"`) { + t.Fatalf("expected Chinese user content in output, got %q", got) + } + if !strings.Contains(got, `"description":"用户输入:上海会议"`) { + t.Fatalf("expected Chinese user description in output, got %q", got) + } +}