Skip to content

Opencode refine#9

Open
doombeaker wants to merge 2 commits into
mainfrom
opencode_refine
Open

Opencode refine#9
doombeaker wants to merge 2 commits into
mainfrom
opencode_refine

Conversation

@doombeaker
Copy link
Copy Markdown
Collaborator

添加测试用例 & 重构架构以提升可扩展性

背景

项目此前零测试覆盖,且 lib/ 层与 CLI 框架(urfave/cli)紧耦合,导致后续增加新功能(如 API 调用入口、SDK 接入)困难。

变更内容

1. 添加测试用例(68 个测试,核心模块全覆盖)

新增测试文件覆盖纯函数、验证逻辑、YAML 配置解析,以及基于 httptest 的 HTTP Client 模拟测试:

文件 覆盖内容
lib/version_test.go ParseVersion、Compare、Bump、CompareVersionStrings
lib/errors_test.go StepError、WithStep、GetStep、NewValidationError
lib/format/bytes_test.go FormatBytes(B → TB 全量程)
lib/validation_test.go ValidateModelName/Type/Path/BaseModel/CoverFile、IsSupportedCoverFormat、ValidateIntroFile、ReadIntroFile
lib/client_test.go 所有 BizyAPI 方法(基于 httptest mock server)+ handleError/handleResponse
config/yaml_config_test.go extractVersionNumber、AutoIncrementVersionNames、LoadYamlConfig、ValidateYamlConfig、NormalizeModelPaths

新增 make test 目标。

2. 解耦 lib/ 与 CLI 框架(P0)

  • lib/client.go:22 处 cli.Exit() → 纯 fmt.Errorf() / meta.ErrNo
  • lib/folder.go:7 处 cli.Exit() → 纯 fmt.Errorf()
  • lib/config/ 不再 import urfave/cli
  • CLI 退出逻辑仅保留在 cmd/

3. 提取 BizyAPI 接口 + 共享 http.Client(P1)

  • lib/client.go 新增 BizyAPI 接口(16 个方法),Client 自动满足该接口
  • Client 结构体改用共享 http.Client,带连接池(此前每次请求新建 http.Transport
  • lib/actions/ 接受 BizyAPI 接口而非具体 *Client,支持注入 mock 进行单元测试
  • UploadOptions.ClientUploadCover 参数从 *Client 改为 BizyAPI

4. 清理重复验证 + Argument 解耦(P2)

  • 删除整个 lib/validate/ 包(与 lib/validation.go 重复)
  • EnsureAbsPath() 合并到 lib/validation.go
  • Argument.Parse()config/ 移至 cmd/args.go,移除 config/urfave/cli 的依赖

影响范围

28 个文件变更:+1,454 行,−317 行

验证

  • go build ./... 编译通过
  • go test ./lib/... ./config/... — 68 个测试全部 PASS
  • lib/config/ 不再依赖 urfave/cli

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant