mirror of
https://github.com/black-ant/Ant-Browser.git
synced 2026-07-14 18:48:55 +08:00
channel: master version: 1.0.0 source-ref: master published-at-utc: 2026-03-13T15:19:28Z
180 lines
5.0 KiB
Go
180 lines
5.0 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestLoadBackfillsLegacyConfig(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tempDir := t.TempDir()
|
|
configPath := filepath.Join(tempDir, "config.yaml")
|
|
legacyConfig := `
|
|
app:
|
|
used_cd_keys:
|
|
- GITHUB_STAR_REWARD
|
|
logging: {}
|
|
browser: {}
|
|
`
|
|
if err := os.WriteFile(configPath, []byte(legacyConfig), 0o644); err != nil {
|
|
t.Fatalf("写入测试配置失败: %v", err)
|
|
}
|
|
|
|
cfg, err := Load(configPath)
|
|
if err != nil {
|
|
t.Fatalf("加载配置失败: %v", err)
|
|
}
|
|
|
|
if cfg.Database.Type != "sqlite" {
|
|
t.Fatalf("Database.Type 未补齐: got=%q", cfg.Database.Type)
|
|
}
|
|
if cfg.Database.SQLite.Path != "data/app.db" {
|
|
t.Fatalf("Database.SQLite.Path 未补齐: got=%q", cfg.Database.SQLite.Path)
|
|
}
|
|
if cfg.App.Name != "Ant Browser" {
|
|
t.Fatalf("App.Name 未补齐: got=%q", cfg.App.Name)
|
|
}
|
|
if cfg.App.MaxProfileLimit != 6 {
|
|
t.Fatalf("MaxProfileLimit 计算错误: got=%d want=6", cfg.App.MaxProfileLimit)
|
|
}
|
|
if cfg.Runtime.MaxMemoryMB != 1024 || cfg.Runtime.GCPercent != 100 {
|
|
t.Fatalf("Runtime 未补齐: got=%+v", cfg.Runtime)
|
|
}
|
|
if cfg.Logging.Level != "info" || cfg.Logging.FilePath != "data/logs/app.log" {
|
|
t.Fatalf("Logging 基础字段未补齐: got=%+v", cfg.Logging)
|
|
}
|
|
if !cfg.Logging.Interceptor.Enabled || !cfg.Logging.Interceptor.LogParameters || !cfg.Logging.Interceptor.LogResults {
|
|
t.Fatalf("Interceptor 默认值未补齐: got=%+v", cfg.Logging.Interceptor)
|
|
}
|
|
if len(cfg.Logging.Interceptor.SensitiveFields) == 0 {
|
|
t.Fatalf("Interceptor.SensitiveFields 未补齐")
|
|
}
|
|
if cfg.Browser.UserDataRoot != "data" {
|
|
t.Fatalf("Browser.UserDataRoot 未补齐: got=%q", cfg.Browser.UserDataRoot)
|
|
}
|
|
if len(cfg.Browser.DefaultFingerprintArgs) == 0 || len(cfg.Browser.DefaultLaunchArgs) == 0 {
|
|
t.Fatalf("Browser 默认启动参数未补齐")
|
|
}
|
|
if cfg.Browser.Cores == nil || cfg.Browser.Proxies == nil || cfg.Browser.Profiles == nil {
|
|
t.Fatalf("Browser 列表字段应初始化为空切片")
|
|
}
|
|
if cfg.LaunchServer.Port != 0 {
|
|
t.Fatalf("LaunchServer.Port 未补齐: got=%d", cfg.LaunchServer.Port)
|
|
}
|
|
}
|
|
|
|
func TestLoadPreservesExplicitConfig(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tempDir := t.TempDir()
|
|
configPath := filepath.Join(tempDir, "config.yaml")
|
|
customConfig := `
|
|
database:
|
|
type: sqlite
|
|
sqlite:
|
|
path: custom/app.db
|
|
app:
|
|
name: Custom App
|
|
window:
|
|
width: 1400
|
|
height: 800
|
|
min_width: 900
|
|
min_height: 600
|
|
max_profile_limit: 20
|
|
used_cd_keys: []
|
|
runtime:
|
|
max_memory_mb: 2048
|
|
gc_percent: 80
|
|
logging:
|
|
level: debug
|
|
file_enabled: true
|
|
file_path: custom.log
|
|
format: json
|
|
buffer_size: 8
|
|
async_queue_size: 2000
|
|
flush_interval_ms: 500
|
|
rotation:
|
|
enabled: true
|
|
max_size_mb: 10
|
|
max_age: 3
|
|
max_backups: 2
|
|
time_interval: hourly
|
|
interceptor:
|
|
enabled: false
|
|
log_parameters: false
|
|
log_results: false
|
|
sensitive_fields: []
|
|
browser:
|
|
user_data_root: custom_data
|
|
default_fingerprint_args:
|
|
- --fingerprint-brand=Edge
|
|
default_launch_args:
|
|
- --start-maximized
|
|
default_proxy: direct://
|
|
default_bookmarks: []
|
|
cores: []
|
|
proxies: []
|
|
profiles: []
|
|
launch_server:
|
|
port: 30000
|
|
`
|
|
if err := os.WriteFile(configPath, []byte(customConfig), 0o644); err != nil {
|
|
t.Fatalf("写入测试配置失败: %v", err)
|
|
}
|
|
|
|
cfg, err := Load(configPath)
|
|
if err != nil {
|
|
t.Fatalf("加载配置失败: %v", err)
|
|
}
|
|
|
|
if cfg.App.Name != "Custom App" || cfg.App.MaxProfileLimit != 20 {
|
|
t.Fatalf("App 显式配置被覆盖: got=%+v", cfg.App)
|
|
}
|
|
if cfg.Database.SQLite.Path != "custom/app.db" {
|
|
t.Fatalf("Database.SQLite.Path 显式配置被覆盖: got=%q", cfg.Database.SQLite.Path)
|
|
}
|
|
if cfg.Runtime.MaxMemoryMB != 2048 || cfg.Runtime.GCPercent != 80 {
|
|
t.Fatalf("Runtime 显式配置被覆盖: got=%+v", cfg.Runtime)
|
|
}
|
|
if cfg.Logging.Level != "debug" || cfg.Logging.Format != "json" || !cfg.Logging.FileEnabled {
|
|
t.Fatalf("Logging 显式配置被覆盖: got=%+v", cfg.Logging)
|
|
}
|
|
if cfg.Logging.Interceptor.Enabled {
|
|
t.Fatalf("Interceptor.Enabled 显式 false 被覆盖")
|
|
}
|
|
if len(cfg.Browser.DefaultFingerprintArgs) != 1 || cfg.Browser.DefaultFingerprintArgs[0] != "--fingerprint-brand=Edge" {
|
|
t.Fatalf("Browser.DefaultFingerprintArgs 显式配置被覆盖: got=%v", cfg.Browser.DefaultFingerprintArgs)
|
|
}
|
|
if cfg.Browser.UserDataRoot != "custom_data" || cfg.Browser.DefaultProxy != "direct://" {
|
|
t.Fatalf("Browser 显式配置被覆盖: got=%+v", cfg.Browser)
|
|
}
|
|
if cfg.LaunchServer.Port != 30000 {
|
|
t.Fatalf("LaunchServer.Port 显式配置被覆盖: got=%d", cfg.LaunchServer.Port)
|
|
}
|
|
}
|
|
|
|
func TestLoadMigratesLegacyRootLogPath(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tempDir := t.TempDir()
|
|
configPath := filepath.Join(tempDir, "config.yaml")
|
|
legacyConfig := `
|
|
logging:
|
|
file_path: logs/app.log
|
|
`
|
|
if err := os.WriteFile(configPath, []byte(legacyConfig), 0o644); err != nil {
|
|
t.Fatalf("写入测试配置失败: %v", err)
|
|
}
|
|
|
|
cfg, err := Load(configPath)
|
|
if err != nil {
|
|
t.Fatalf("加载配置失败: %v", err)
|
|
}
|
|
|
|
if cfg.Logging.FilePath != "data/logs/app.log" {
|
|
t.Fatalf("legacy 根目录日志路径未迁移: got=%q", cfg.Logging.FilePath)
|
|
}
|
|
}
|