Files
bear-cli/test/bear_cli/config_test.exs
T
alex 76a14686c1 feat: 實作 bear CLI 骨架與 PAT 模式共用基礎(issue #6)
- 專案骨架:Elixir + Mix escript(單檔可執行 bear),HTTP 一律 Req
- 設定/憑證檔讀寫:0600、原子寫入(rename)、O_CREAT|O_EXCL 防 symlink
- PAT Bearer 呼叫 /userinfo;--json 輸出;規格化退出碼
- 補齊 main 規格承諾的 BEAR_TOKEN 環境變數(優先於憑證檔、不寫入)
- login 無效 PAT 退出碼 3(依 docs/commands.md §3.1);token --refresh 用法錯誤 2(§3.3)
- 35 個單元測試;mix precommit(compile --warnings-as-errors + format + test)全綠
- 沿用已關閉 PR #3 的實作(feat/pat-mode-mvp 分支),文件改依 main 現行版本更新

apps 管理指令待 #5 規格與 alterminal/bear#28 API 就緒後實作。
2026-09-07 22:44:25 +08:00

64 lines
1.8 KiB
Elixir

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