feat(cli): command router, real IMAP wiring, flag-based admin

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-22 00:09:38 +01:00
parent e1d86dc587
commit e1e5f245e1
4 changed files with 345 additions and 2 deletions
+56
View File
@@ -0,0 +1,56 @@
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 TestRunVersionIsJSONForAgentButTextHere(t *testing.T) {
// `account list` with no DB key should fail closed with a usage/config error,
// proving the key check happens before any DB work.
var out, errOut bytes.Buffer
t.Setenv("EMCLI_KEY", "")
code := Run([]string{"account", "list"}, &out, &errOut)
if code == 0 {
t.Fatal("missing EMCLI_KEY must fail")
}
if !strings.Contains(out.String()+errOut.String(), "EMCLI_KEY") {
t.Fatalf("should mention EMCLI_KEY, got out=%q err=%q", out.String(), errOut.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="
}