Skip to content
Merged
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: 1 addition & 1 deletion internal/cli/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 8 additions & 1 deletion internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"os"

"github.com/senguoyun-guosheng/graphmind/internal/db"
Expand Down Expand Up @@ -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
}
58 changes: 58 additions & 0 deletions internal/cli/root_test.go
Original file line number Diff line number Diff line change
@@ -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 <id> — 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 <id> — 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 <id> — 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 <id> — 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)
}
}
Loading