2140d9e173
account list now routes to the agent role; an agent (EMCLI_KEY only) gets a JSON envelope of name/from/can_send, while the admin keeps the full text table. account add/edit/remove stay admin-only. Also emit the agent path's missing-key/open failure as a JSON Failure envelope (per spec), and update the stale run_test case that asserted the old admin-only behavior. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
70 lines
2.1 KiB
Go
70 lines
2.1 KiB
Go
package cli
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestRunUnknownCommand(t *testing.T) {
|
|
var out, errOut bytes.Buffer
|
|
code := Run([]string{"frobnicate"}, &out, &errOut)
|
|
if code == 0 {
|
|
t.Fatal("unknown command should be non-zero exit")
|
|
}
|
|
if !strings.Contains(errOut.String(), "unknown") {
|
|
t.Fatalf("stderr should mention unknown command: %q", errOut.String())
|
|
}
|
|
}
|
|
|
|
func TestAccountListMissingKeyFailsClosedAsJSON(t *testing.T) {
|
|
// `account list` is an agent command: with no DB key it fails closed before
|
|
// any DB work, emitting a JSON config-error envelope that names EMCLI_KEY.
|
|
var out, errOut bytes.Buffer
|
|
t.Setenv("EMCLI_KEY", "")
|
|
t.Setenv("EMCLI_ADMIN_KEY", "")
|
|
code := Run([]string{"account", "list"}, &out, &errOut)
|
|
if code == 0 {
|
|
t.Fatal("missing EMCLI_KEY must fail")
|
|
}
|
|
var env map[string]any
|
|
if err := json.Unmarshal(out.Bytes(), &env); err != nil {
|
|
t.Fatalf("agent account list error must be JSON, got out=%q err=%q", out.String(), errOut.String())
|
|
}
|
|
if env["error"] != true {
|
|
t.Fatalf("want error envelope: %v", env)
|
|
}
|
|
if !strings.Contains(out.String(), "EMCLI_KEY") {
|
|
t.Fatalf("should name the missing EMCLI_KEY, got %q", out.String())
|
|
}
|
|
}
|
|
|
|
func TestListUsageErrorIsJSON(t *testing.T) {
|
|
// Agent command with a missing required flag emits a JSON error envelope.
|
|
var out, errOut bytes.Buffer
|
|
t.Setenv("EMCLI_KEY", b64Key())
|
|
t.Setenv("EMCLI_DB", "") // default path is fine; command fails before connecting
|
|
code := Run([]string{"list"}, &out, &errOut) // missing --account
|
|
if code == 0 {
|
|
t.Fatal("missing --account should be non-zero")
|
|
}
|
|
var env map[string]any
|
|
if err := json.Unmarshal(out.Bytes(), &env); err != nil {
|
|
t.Fatalf("agent usage error must be JSON, got %q", out.String())
|
|
}
|
|
if env["error"] != true {
|
|
t.Fatalf("want error envelope: %v", env)
|
|
}
|
|
}
|
|
|
|
func b64Key() string {
|
|
// 32 zero bytes, base64.
|
|
return "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
|
|
}
|
|
|
|
func b64AgentKey() string {
|
|
// 32 bytes of 0x01, base64 — distinct from b64Key so slot mix-ups surface.
|
|
return "AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE="
|
|
}
|