Files
ai_academy/cli/cmd/config/view_test.go
T
runny 9058149576
Build & Publish (dsh-academy) / End-to-end inside dsh (latest, advisory) (push) Skipped
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Frontend / Docker multi-stage image (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-academy) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-academy) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-academy) / Publish to npm (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Helm / chart (push) Canceled after 0s
refactor: 项目更名 WeKnora → Academy
- 全量替换代码/配置/文档中的项目名(保留上游 GitHub 链接、weixin 文档域名、Docker Hub 镜像名)
- 重命名 32 个含项目名的文件/目录;Go module 路径改为 academy(含 client/cli 子模块)
- docreader proto 描述符采用等长替换,保持序列化字节布局有效
- Dockerfile.app 构建网络修复: browserskill 离线产物通道、PyPI/apt 国内镜像
- 运行验证: go build ✓ docker compose up ✓ 健康检查 ✓
2026-11-13 20:52:51 +08:00

121 lines
4.4 KiB
Go

package configcmd
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"academy/cli/internal/cmdutil"
"academy/cli/internal/config"
"academy/cli/internal/iostreams"
sdk "academy/client"
)
type viewEnvelope struct {
OK bool `json:"ok"`
Data struct {
ActiveProfile string `json:"active_profile"`
ProfileSource string `json:"profile_source"`
AuthSource string `json:"auth_source"`
Host string `json:"host"`
KBID string `json:"kb_id"`
KBSource string `json:"kb_source"`
FormatDefault string `json:"format_default"`
ConfigFile string `json:"config_file"`
} `json:"data"`
}
func runViewJSON(t *testing.T, f *cmdutil.Factory) viewEnvelope {
t.Helper()
out, _ := iostreams.SetForTest(t)
// --format is a root-level persistent flag, so drive runView directly with
// a JSON FormatOptions rather than through standalone leaf flag parsing.
cmd := NewCmdView(f)
require.NoError(t, runView(cmd, f, &cmdutil.FormatOptions{Mode: cmdutil.FormatJSON}))
var env viewEnvelope
require.NoError(t, json.Unmarshal(out.Bytes(), &env), "envelope: %q", out.String())
return env
}
// TestConfigView_ConfigProfileSource: with a current_profile set in config and
// no flag/env override, config view reports the profile, its source, and the
// host — and resolves the KB to "(unresolved)" (no --kb/env/link) without error.
func TestConfigView_ConfigProfileSource(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
cfg := &config.Config{
CurrentProfile: "prod",
Profiles: map[string]config.Profile{"prod": {Host: "https://kb.example.com"}},
}
require.NoError(t, config.Save(cfg))
env := runViewJSON(t, networkFreeFactory(t))
assert.True(t, env.OK)
assert.Equal(t, "prod", env.Data.ActiveProfile)
assert.Equal(t, "config current_profile", env.Data.ProfileSource)
assert.Equal(t, "https://kb.example.com", env.Data.Host)
assert.Equal(t, "(unresolved)", env.Data.KBSource, "no --kb/env/link must report unresolved, not error")
assert.Equal(t, "json", env.Data.FormatDefault)
assert.NotEmpty(t, env.Data.ConfigFile)
}
// TestConfigView_ProfileOverrideSource: the --profile override (set on the
// Factory by root's PersistentPreRunE) wins over config and is reported as
// such, so an agent can see why a non-default profile is active.
func TestConfigView_ProfileOverrideSource(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
cfg := &config.Config{
CurrentProfile: "prod",
Profiles: map[string]config.Profile{"prod": {Host: "https://kb.example.com"}},
}
require.NoError(t, config.Save(cfg))
f := networkFreeFactory(t)
f.ProfileOverride = "other"
env := runViewJSON(t, f)
assert.Equal(t, "other", env.Data.ActiveProfile)
assert.Equal(t, "--profile flag", env.Data.ProfileSource)
}
// TestConfigView_NoProfile: config view succeeds with an empty config —
// active_profile "" / source "(none)" — never erroring on a fresh install.
func TestConfigView_NoProfile(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
env := runViewJSON(t, networkFreeFactory(t))
assert.True(t, env.OK)
assert.Equal(t, "", env.Data.ActiveProfile)
assert.Equal(t, "(none)", env.Data.ProfileSource)
}
// networkFreeFactory builds a Factory whose Client closure fails the test if
// invoked — config view must resolve everything locally (ResolveKBLocal,
// ActiveProfile) and never build the SDK client.
func networkFreeFactory(t *testing.T) *cmdutil.Factory {
t.Helper()
f := cmdutil.New()
f.Client = func() (*sdk.Client, error) {
t.Fatal("config view must not build the SDK client / hit the network")
return nil, nil
}
return f
}
// TestConfigView_EnvCredentialSurfaced: when ACADEMY_API_KEY + ACADEMY_HOST are
// set, config view reports the env override (auth_source + the env host), not
// the bypassed config profile's host.
func TestConfigView_EnvCredentialSurfaced(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
cfg := &config.Config{
CurrentProfile: "prod",
Profiles: map[string]config.Profile{"prod": {Host: "https://configured.example.com"}},
}
require.NoError(t, config.Save(cfg))
t.Setenv("ACADEMY_API_KEY", "sk-test")
t.Setenv("ACADEMY_HOST", "https://env-override.example.com")
env := runViewJSON(t, networkFreeFactory(t))
assert.Contains(t, env.Data.AuthSource, "ACADEMY_API_KEY env")
assert.Equal(t, "https://env-override.example.com", env.Data.Host, "host must be the env override, not the profile host")
}