defmodule BearCli.ConfigTest do use ExUnit.Case, async: false alias BearCli.Config @nonexistent "/nonexistent/bear_cli_config.json" setup do old_issuer = System.get_env("BEAR_ISSUER") old_config = System.get_env("BEAR_CONFIG") on_exit(fn -> restore_env("BEAR_ISSUER", old_issuer) restore_env("BEAR_CONFIG", old_config) end) System.delete_env("BEAR_ISSUER") System.put_env("BEAR_CONFIG", @nonexistent) :ok end test "defaults to the built-in issuer when nothing else is set" do assert Config.resolve_issuer(nil) == "https://alterminal.com" end test "CLI flag takes precedence over everything" do System.put_env("BEAR_ISSUER", "https://env.example.com") assert Config.resolve_issuer("https://flag.example.com") == "https://flag.example.com" end test "BEAR_ISSUER env wins over config and default" do System.put_env("BEAR_ISSUER", "https://env.example.com") assert Config.resolve_issuer(nil) == "https://env.example.com" end test "config file issuer wins over default" do path = write_config(%{"issuer" => "https://config.example.com"}) on_exit(fn -> File.rm(path) end) assert Config.resolve_issuer(nil, path) == "https://config.example.com" end test "config path override is honored" do path = write_config(%{"issuer" => "https://override.example.com"}) on_exit(fn -> File.rm(path) end) assert Config.resolve_issuer(nil, path) == "https://override.example.com" end defp write_config(map) do path = Path.join(System.tmp_dir!(), "bear_cli_config_#{System.unique_integer([:positive])}.json") File.write!(path, Jason.encode!(map)) path end defp restore_env(key, nil), do: System.delete_env(key) defp restore_env(key, value), do: System.put_env(key, value) end