fix(tui): encode empty json rows consistently

This commit is contained in:
Vincent Koc 2026-05-02 07:42:33 -07:00
parent f3fd8fc704
commit c2a352409e
No known key found for this signature in database
2 changed files with 13 additions and 0 deletions

View File

@ -77,6 +77,9 @@ func Browse(ctx context.Context, opts BrowseOptions) error {
opts.Stdout = os.Stdout
}
if opts.JSON {
if opts.Rows == nil {
opts.Rows = []Row{}
}
enc := json.NewEncoder(opts.Stdout)
enc.SetIndent("", " ")
return enc.Encode(opts.Rows)

View File

@ -35,6 +35,16 @@ func TestBrowseJSONUsesUniversalRows(t *testing.T) {
}
}
func TestBrowseJSONEncodesNilRowsAsEmptyArray(t *testing.T) {
var out bytes.Buffer
if err := Browse(context.Background(), BrowseOptions{AppName: "discrawl", JSON: true, Stdout: &out}); err != nil {
t.Fatalf("Browse json: %v", err)
}
if strings.TrimSpace(out.String()) != "[]" {
t.Fatalf("json = %q, want []", out.String())
}
}
func TestRowItemUsesSharedArchiveShape(t *testing.T) {
item := Row{
Source: "discord",