Files
bear-cli/test/bear_cli/config_test.exs
T
alex 8cc8c41416 feat: 實作 bear CLI PAT 模式 MVP(login/whoami/token/logout/status)
- 新增 Elixir + Mix escript 專案(Req 0.5+、Jason)
- login --token <PAT>:以 PAT 驗證 /userinfo 後寫入憑證檔(0600)
- whoami:GET /userinfo 顯示身分 claims(支援 --json)
- token:印出 access token 供 pipe(--refresh 在 PAT 模式忽略)
- logout:清除本機憑證;status:純本機判定
- 憑證檔採原子寫入(rename)、0600、O_CREAT|O_EXCL 防 symlink
- 全域選項:--issuer/--config/--json/-v/-h/--version
- 29 個單元測試;mix precommit(compile --warnings-as-errors + format + test)
- 更新 README.md 與 docs/commands.md 反映 PAT 模式 MVP

Device Flow 登入待伺服器端 P1(bear 倉庫)完成後再接。issue #2
2026-08-30 14:24:51 +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