package bridge

import (
	"io"
	"path/filepath"
	"testing"
)

func TestRunActionSwitchControllerWithoutLocalAPI(t *testing.T) {
	t.Parallel()

	configPath := filepath.Join(t.TempDir(), "mirage-ui.json")
	app := &App{configPath: configPath}

	if err := app.RunAction(io.Discard, ActionRequest{
		Name:            "switch-controller",
		ControllerInput: "corp-a",
	}); err != nil {
		t.Fatalf("RunAction(switch-controller) error = %v", err)
	}

	state, err := loadConfig(configPath)
	if err != nil {
		t.Fatalf("loadConfig() error = %v", err)
	}
	if got, want := state.ControllerInput, "corp-a"; got != want {
		t.Fatalf("ControllerInput = %q, want %q", got, want)
	}
}

func TestRunActionSwitchControllerResetWithoutLocalAPI(t *testing.T) {
	t.Parallel()

	configPath := filepath.Join(t.TempDir(), "mirage-ui.json")
	if err := saveConfig(configPath, configState{ControllerInput: "corp-a"}); err != nil {
		t.Fatalf("saveConfig() error = %v", err)
	}
	app := &App{configPath: configPath}

	if err := app.RunAction(io.Discard, ActionRequest{
		Name:            "switch-controller",
		ControllerInput: "   ",
	}); err != nil {
		t.Fatalf("RunAction(reset switch-controller) error = %v", err)
	}

	state, err := loadConfig(configPath)
	if err != nil {
		t.Fatalf("loadConfig() error = %v", err)
	}
	if state.ControllerInput != "" {
		t.Fatalf("ControllerInput = %q, want empty after reset", state.ControllerInput)
	}
}
