mirror of
https://github.com/larksuite/cli.git
synced 2026-07-03 14:02:43 +08:00
* refactor: retire legacy error envelopes and enforce typed contract
Consolidate all command error reporting onto the typed errs.* contract, remove
the legacy error surface that predated it, and tighten the lint guards so the
contract holds across the whole repository going forward.
Every failure now reaches stderr as one envelope shape: a category, an
optional subtype, a human- and agent-readable message, and a recovery hint,
with invalid parameters listed under `params`. The legacy ExitError envelope,
its constructors, and the boundary bridge that promoted untyped config and
authorization errors are deleted, leaving a single path from error to wire.
Predicate commands keep their silent-exit behavior through a dedicated signal
that carries only an exit code.
Infrastructure paths that still emitted ad-hoc envelopes — flag parsing,
unknown commands and subcommands, plugin and policy guards, confirmation
prompts, and auth/config failures — now classify into the same taxonomy.
Business, API, auth, and config exit codes are preserved; the one behavioral
change is that Cobra usage failures (missing required flag, unknown command,
bad arguments) now emit the typed validation envelope and exit 2, matching the
explicit flag and subcommand guards, instead of Cobra's plain-text exit 1.
Enforcement is repo-wide rather than per-path:
- The errscontract guards run by default everywhere instead of through a
migration allowlist, so legacy envelopes cannot be reintroduced anywhere.
- errorlint runs across the whole repository: every error wrap must use %w and
every comparison must use errors.Is/errors.As, so interior wraps stay legal
but can no longer break the chain the typed boundary relies on.
- The errs-no-bare-wrap guard is keyed by structural prefix instead of an
explicit per-domain allowlist, so new shortcut domains are covered without
editing a list. It runs where forbidigo is enabled (the shortcut domains and
the auth/config/service command groups); repo-wide chain integrity for the
remaining command paths is carried by errorlint above.
* test: align cli_e2e success assertions to the ok envelope
The api and service success path now emits the {"ok":true} envelope, so the
cli_e2e workflow assertions that still expected the old {"code":0} shape via
AssertStdoutStatus(t, 0) fail once they run with live credentials. Switch those
workflow assertions to AssertStdoutStatus(t, true); the fake-payload helper test
in core_test.go keeps its code-shape assertion.
201 lines
7.3 KiB
Go
201 lines
7.3 KiB
Go
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package config
|
|
|
|
import (
|
|
"path/filepath"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/larksuite/cli/internal/core"
|
|
"github.com/larksuite/cli/internal/output"
|
|
)
|
|
|
|
// fakeBinder is a test double for SourceBinder. selectCandidate only touches
|
|
// Name and ConfigPath (for error messages); ListCandidates/Build are not called
|
|
// from selectCandidate, so we can leave them as no-ops.
|
|
type fakeBinder struct {
|
|
name string
|
|
path string
|
|
}
|
|
|
|
func (b *fakeBinder) Name() string { return b.name }
|
|
func (b *fakeBinder) ConfigPath() string { return b.path }
|
|
func (b *fakeBinder) ListCandidates() ([]Candidate, error) { return nil, nil }
|
|
func (b *fakeBinder) Build(appID string) (*core.AppConfig, error) { return nil, nil }
|
|
|
|
// tuiUnreachable is a tuiPrompt that fails the test if called. It's the
|
|
// guardrail that proves the non-TUI decision paths really do stay out of the
|
|
// interactive prompt — otherwise a green test could still hide a silent TUI.
|
|
func tuiUnreachable(t *testing.T) func([]Candidate) (*Candidate, error) {
|
|
t.Helper()
|
|
return func([]Candidate) (*Candidate, error) {
|
|
t.Fatal("tuiPrompt must not be called in flag mode")
|
|
return nil, nil
|
|
}
|
|
}
|
|
|
|
// assertCandidate compares the full Candidate struct via DeepEqual so that
|
|
// any future field added to Candidate is covered automatically.
|
|
func assertCandidate(t *testing.T, got *Candidate, want Candidate) {
|
|
t.Helper()
|
|
if got == nil {
|
|
t.Fatal("expected non-nil Candidate")
|
|
}
|
|
if !reflect.DeepEqual(*got, want) {
|
|
t.Errorf("candidate mismatch:\n got: %+v\n want: %+v", *got, want)
|
|
}
|
|
}
|
|
|
|
func TestSelectCandidate_ZeroCandidates_OpenClaw(t *testing.T) {
|
|
b := &fakeBinder{name: "openclaw", path: "/tmp/openclaw.json"}
|
|
_, err := selectCandidate(b, nil, "", false, tuiUnreachable(t))
|
|
assertExitError(t, err, output.ExitAuth, wantErrDetail{
|
|
Type: "config",
|
|
Message: "no Feishu app configured in openclaw.json",
|
|
Hint: "configure channels.feishu.appId in openclaw.json",
|
|
})
|
|
}
|
|
|
|
func TestSelectCandidate_ZeroCandidates_GenericSource(t *testing.T) {
|
|
// Locks in the generic fallback so that any future source added to
|
|
// newBinder gets a well-formed validation error on "zero candidates"
|
|
// even before it has a bespoke error message.
|
|
b := &fakeBinder{name: "hermes", path: "/tmp/.env"}
|
|
_, err := selectCandidate(b, nil, "", false, tuiUnreachable(t))
|
|
assertExitError(t, err, output.ExitAuth, wantErrDetail{
|
|
Type: "config",
|
|
Message: "hermes: no app configured",
|
|
})
|
|
}
|
|
|
|
func TestSelectCandidate_SingleCandidate_NoFlag_AutoSelect(t *testing.T) {
|
|
b := &fakeBinder{name: "openclaw", path: "/tmp/openclaw.json"}
|
|
candidates := []Candidate{{AppID: "cli_only", Label: "default"}}
|
|
got, err := selectCandidate(b, candidates, "", false, tuiUnreachable(t))
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
assertCandidate(t, got, Candidate{AppID: "cli_only", Label: "default"})
|
|
}
|
|
|
|
func TestSelectCandidate_AppIDFlag_ExactMatch(t *testing.T) {
|
|
b := &fakeBinder{name: "openclaw", path: "/tmp/openclaw.json"}
|
|
candidates := []Candidate{
|
|
{AppID: "cli_work", Label: "work"},
|
|
{AppID: "cli_home", Label: "home"},
|
|
}
|
|
got, err := selectCandidate(b, candidates, "cli_home", false, tuiUnreachable(t))
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
assertCandidate(t, got, Candidate{AppID: "cli_home", Label: "home"})
|
|
}
|
|
|
|
func TestSelectCandidate_AppIDFlag_NoMatch(t *testing.T) {
|
|
b := &fakeBinder{name: "openclaw", path: "/tmp/openclaw.json"}
|
|
candidates := []Candidate{
|
|
{AppID: "cli_work", Label: "work"},
|
|
{AppID: "cli_home", Label: "home"},
|
|
}
|
|
_, err := selectCandidate(b, candidates, "nonexistent", false, tuiUnreachable(t))
|
|
assertExitError(t, err, output.ExitValidation, wantErrDetail{
|
|
Type: "validation",
|
|
Message: `--app-id "nonexistent" not found in openclaw.json`,
|
|
Hint: "available app IDs:\n cli_work (work)\n cli_home (home)",
|
|
})
|
|
}
|
|
|
|
func TestSelectCandidate_MultiCandidate_NoFlag_NonTUI(t *testing.T) {
|
|
// Flag-mode with multiple candidates and no --app-id must produce a
|
|
// validation error and the candidate list, never an interactive prompt.
|
|
// isTUI is the single gate; a real terminal alone must not trigger TUI.
|
|
b := &fakeBinder{name: "openclaw", path: "/tmp/openclaw.json"}
|
|
candidates := []Candidate{
|
|
{AppID: "cli_work", Label: "work"},
|
|
{AppID: "cli_home", Label: "home"},
|
|
}
|
|
_, err := selectCandidate(b, candidates, "", false, tuiUnreachable(t))
|
|
assertExitError(t, err, output.ExitValidation, wantErrDetail{
|
|
Type: "validation",
|
|
Message: "multiple accounts in openclaw.json; pass --app-id <id>",
|
|
Hint: "available app IDs:\n cli_work (work)\n cli_home (home)",
|
|
})
|
|
}
|
|
|
|
func TestSelectCandidate_MultiCandidate_NoFlag_TUI(t *testing.T) {
|
|
b := &fakeBinder{name: "openclaw", path: "/tmp/openclaw.json"}
|
|
candidates := []Candidate{
|
|
{AppID: "cli_work", Label: "work"},
|
|
{AppID: "cli_home", Label: "home"},
|
|
}
|
|
var gotCandidates []Candidate
|
|
got, err := selectCandidate(b, candidates, "", true, func(cs []Candidate) (*Candidate, error) {
|
|
gotCandidates = cs
|
|
return &cs[1], nil
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
// Whole-slice DeepEqual so additions to Candidate propagate to this check.
|
|
if !reflect.DeepEqual(gotCandidates, candidates) {
|
|
t.Errorf("tuiPrompt received %+v, want %+v", gotCandidates, candidates)
|
|
}
|
|
assertCandidate(t, got, Candidate{AppID: "cli_home", Label: "home"})
|
|
}
|
|
|
|
func TestSelectCandidate_SingleCandidate_WrongFlag(t *testing.T) {
|
|
// Even with only one candidate, a wrong --app-id must error rather than
|
|
// silently auto-selecting. An explicit mismatch is always a user mistake,
|
|
// not a reason to override their intent.
|
|
b := &fakeBinder{name: "openclaw", path: "/tmp/openclaw.json"}
|
|
candidates := []Candidate{{AppID: "cli_only"}}
|
|
_, err := selectCandidate(b, candidates, "nonexistent", false, tuiUnreachable(t))
|
|
assertExitError(t, err, output.ExitValidation, wantErrDetail{
|
|
Type: "validation",
|
|
Message: `--app-id "nonexistent" not found in openclaw.json`,
|
|
Hint: "available app IDs:\n cli_only",
|
|
})
|
|
}
|
|
|
|
func TestSelectCandidate_AppIDFlag_WinsOverTUI(t *testing.T) {
|
|
// An explicit --app-id short-circuits the prompt even in TUI mode: a
|
|
// flag the user typed should never be second-guessed by an interactive
|
|
// prompt asking the same question.
|
|
b := &fakeBinder{name: "openclaw", path: "/tmp/openclaw.json"}
|
|
candidates := []Candidate{
|
|
{AppID: "cli_a"},
|
|
{AppID: "cli_b"},
|
|
}
|
|
got, err := selectCandidate(b, candidates, "cli_b", true, tuiUnreachable(t))
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
assertCandidate(t, got, Candidate{AppID: "cli_b"})
|
|
}
|
|
|
|
func TestResolveLarkChannelConfigPath_Default(t *testing.T) {
|
|
home := t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
t.Setenv("LARK_CHANNEL_CONFIG", "")
|
|
|
|
got := resolveLarkChannelConfigPath()
|
|
want := filepath.Join(home, ".lark-channel", "config.json")
|
|
if got != want {
|
|
t.Fatalf("resolveLarkChannelConfigPath() = %q, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestResolveLarkChannelConfigPath_EnvOverride(t *testing.T) {
|
|
home := t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
t.Setenv("LARK_CHANNEL_CONFIG", "~/bridge/projection.json")
|
|
|
|
got := resolveLarkChannelConfigPath()
|
|
want := filepath.Join(home, "bridge", "projection.json")
|
|
if got != want {
|
|
t.Fatalf("resolveLarkChannelConfigPath() = %q, want %q", got, want)
|
|
}
|
|
}
|