Skip to content
Open
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
51 changes: 51 additions & 0 deletions cmd/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,54 @@ func TestConfigRemoveCmd_FlagParsing(t *testing.T) {
t.Fatal("expected factory to be preserved in options")
}
}

func TestConfigInitRun_NewPropagatesBrandFlagToCreateFlow(t *testing.T) {
t.Setenv("LARKSUITE_CLI_CONFIG_DIR", t.TempDir())

f, stdout, _, _ := cmdutil.TestFactory(t, nil)
opts := &ConfigInitOptions{
Factory: f,
Ctx: context.Background(),
New: true,
Brand: "lark",
Lang: "en",
}

oldRunCreateAppFlow := runCreateAppFlowFn
defer func() {
runCreateAppFlowFn = oldRunCreateAppFlow
}()

var gotBrand core.LarkBrand
runCreateAppFlowFn = func(ctx context.Context, f *cmdutil.Factory, brandOverride core.LarkBrand, msg *initMsg) (*configInitResult, error) {
gotBrand = brandOverride
return &configInitResult{
Mode: "create",
Brand: brandOverride,
AppID: "cli_test_lark",
AppSecret: "secret123",
}, nil
}

if err := configInitRun(opts); err != nil {
t.Fatalf("unexpected error: %v", err)
}

if gotBrand != core.BrandLark {
t.Fatalf("runCreateAppFlow received brand %q, want %q", gotBrand, core.BrandLark)
}

cfg, err := core.LoadMultiAppConfig()
if err != nil {
t.Fatalf("failed to load saved config: %v", err)
}
if len(cfg.Apps) != 1 {
t.Fatalf("expected exactly one app in config, got %d", len(cfg.Apps))
}
if cfg.Apps[0].Brand != core.BrandLark {
t.Fatalf("saved config brand = %q, want %q", cfg.Apps[0].Brand, core.BrandLark)
}
if !strings.Contains(stdout.String(), `"brand": "lark"`) {
t.Fatalf("stdout = %q, want JSON output to include saved lark brand", stdout.String())
}
}
2 changes: 1 addition & 1 deletion cmd/config/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func configInitRun(opts *ConfigInitOptions) error {

// Mode 3: Create new app directly (--new)
if opts.New {
result, err := runCreateAppFlow(opts.Ctx, f, core.BrandFeishu, msg)
result, err := runCreateAppFlowFn(opts.Ctx, f, parseBrand(opts.Brand), msg)
if err != nil {
return err
}
Expand Down
5 changes: 4 additions & 1 deletion cmd/config/init_interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ type configInitResult struct {
AppSecret string
}

// runCreateAppFlowFn is a small test seam for configInitRun.
var runCreateAppFlowFn = runCreateAppFlow

// runInteractiveConfigInit shows an interactive TUI for config init.
func runInteractiveConfigInit(ctx context.Context, f *cmdutil.Factory, msg *initMsg) (*configInitResult, error) {
// Phase 1: Choose mode
Expand Down Expand Up @@ -196,7 +199,7 @@ func runCreateAppFlow(ctx context.Context, f *cmdutil.Factory, brandOverride cor
// Step 4: Handle Lark brand special case
// If tenant_brand=lark and no client_secret, retry with lark brand endpoint
if result.ClientSecret == "" && result.UserInfo != nil && result.UserInfo.TenantBrand == "lark" {
// fmt.Fprintf(f.IOStreams.ErrOut, "%s\n", msg.DetectedLarkTenant)
fmt.Fprintf(f.IOStreams.ErrOut, "%s\n", msg.DetectedLarkTenant)
result, err = larkauth.PollAppRegistration(ctx, httpClient, core.BrandLark, authResp.DeviceCode, authResp.Interval, authResp.ExpiresIn, f.IOStreams.ErrOut)
if err != nil {
return nil, output.ErrAuth("lark endpoint retry failed: %v", err)
Expand Down