first commit
Some checks failed
CI/CD Pipeline / Test (push) Failing after 22m19s
CI/CD Pipeline / Security Scan (push) Failing after 5m57s
CI/CD Pipeline / Build (amd64, darwin) (push) Has been skipped
CI/CD Pipeline / Build (amd64, linux) (push) Has been skipped
CI/CD Pipeline / Build (amd64, windows) (push) Has been skipped
CI/CD Pipeline / Build (arm64, darwin) (push) Has been skipped
CI/CD Pipeline / Build (arm64, linux) (push) Has been skipped
CI/CD Pipeline / Build Docker Image (push) Has been skipped
CI/CD Pipeline / Create Release (push) Has been skipped
Some checks failed
CI/CD Pipeline / Test (push) Failing after 22m19s
CI/CD Pipeline / Security Scan (push) Failing after 5m57s
CI/CD Pipeline / Build (amd64, darwin) (push) Has been skipped
CI/CD Pipeline / Build (amd64, linux) (push) Has been skipped
CI/CD Pipeline / Build (amd64, windows) (push) Has been skipped
CI/CD Pipeline / Build (arm64, darwin) (push) Has been skipped
CI/CD Pipeline / Build (arm64, linux) (push) Has been skipped
CI/CD Pipeline / Build Docker Image (push) Has been skipped
CI/CD Pipeline / Create Release (push) Has been skipped
This commit is contained in:
427
tests/generator/generator_manager_test.go
Normal file
427
tests/generator/generator_manager_test.go
Normal file
@@ -0,0 +1,427 @@
|
||||
package generator_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
|
||||
gen "github.com/subconverter-go/internal/generator"
|
||||
"github.com/subconverter-go/internal/logging"
|
||||
"github.com/subconverter-go/internal/parser"
|
||||
)
|
||||
|
||||
func TestGeneratorManager(t *testing.T) {
|
||||
Convey("GeneratorManager", t, func() {
|
||||
// 创建日志记录器
|
||||
logger, err := logging.NewLogger(&logging.LoggingConfig{
|
||||
Level: "info",
|
||||
Format: "text",
|
||||
Output: "stdout",
|
||||
})
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
// 创建生成器管理器
|
||||
generatorMgr := gen.NewGeneratorManager(logger)
|
||||
|
||||
Convey("应该正确注册所有生成器", func() {
|
||||
formats := generatorMgr.GetSupportedFormats()
|
||||
So(formats, ShouldContain, "clash")
|
||||
So(formats, ShouldContain, "clashr")
|
||||
So(formats, ShouldContain, "surge")
|
||||
So(formats, ShouldContain, "quantumultx")
|
||||
So(formats, ShouldContain, "quanx")
|
||||
So(formats, ShouldContain, "loon")
|
||||
So(formats, ShouldContain, "surfboard")
|
||||
So(formats, ShouldContain, "v2ray")
|
||||
So(len(formats), ShouldEqual, 8)
|
||||
})
|
||||
|
||||
Convey("应该能获取指定的生成器", func() {
|
||||
clashGenerator, err := generatorMgr.GetGenerator("clash")
|
||||
So(err, ShouldBeNil)
|
||||
So(clashGenerator, ShouldNotBeNil)
|
||||
|
||||
clashRGenerator, err := generatorMgr.GetGenerator("clashr")
|
||||
So(err, ShouldBeNil)
|
||||
So(clashRGenerator, ShouldNotBeNil)
|
||||
|
||||
surgeGenerator, err := generatorMgr.GetGenerator("surge")
|
||||
So(err, ShouldBeNil)
|
||||
So(surgeGenerator, ShouldNotBeNil)
|
||||
|
||||
// 测试不支持的格式
|
||||
_, err = generatorMgr.GetGenerator("unsupported")
|
||||
So(err, ShouldNotBeNil)
|
||||
So(err.Error(), ShouldContainSubstring, "generator not found")
|
||||
})
|
||||
|
||||
Convey("应该能验证生成选项", func() {
|
||||
options := &gen.GenerationOptions{
|
||||
Name: "Test Configuration",
|
||||
}
|
||||
|
||||
err := generatorMgr.ValidateOptions("clash", options)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
// 测试无效选项
|
||||
invalidOptions := &gen.GenerationOptions{
|
||||
Name: "",
|
||||
}
|
||||
err = generatorMgr.ValidateOptions("clash", invalidOptions)
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestClashGenerator(t *testing.T) {
|
||||
Convey("ClashGenerator", t, func() {
|
||||
logger, err := logging.NewLogger(&logging.LoggingConfig{
|
||||
Level: "info",
|
||||
Format: "text",
|
||||
Output: "stdout",
|
||||
})
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
generator := gen.NewClashGenerator(logger)
|
||||
|
||||
Convey("应该能生成Clash配置", func() {
|
||||
configs := []*parser.ProxyConfig{
|
||||
{
|
||||
Name: "test-ss",
|
||||
Server: "192.168.1.1",
|
||||
Port: 8388,
|
||||
Type: "ss",
|
||||
Protocol: "ss",
|
||||
Settings: map[string]interface{}{
|
||||
"method": "aes-256-cfb",
|
||||
"password": "password",
|
||||
},
|
||||
UDP: true,
|
||||
},
|
||||
}
|
||||
|
||||
options := &gen.GenerationOptions{
|
||||
Name: "Test Clash",
|
||||
Group: "proxy",
|
||||
IPv6: true,
|
||||
Mode: "rule",
|
||||
}
|
||||
|
||||
result, err := generator.Generate(configs, options)
|
||||
So(err, ShouldBeNil)
|
||||
So(result, ShouldNotBeNil)
|
||||
So(result, ShouldContainSubstring, "test-ss")
|
||||
So(result, ShouldContainSubstring, "aes-256-cfb")
|
||||
})
|
||||
|
||||
Convey("应该能验证Clash配置选项", func() {
|
||||
options := &gen.GenerationOptions{
|
||||
Name: "Test Configuration",
|
||||
Mode: "rule",
|
||||
}
|
||||
|
||||
err := generator.ValidateOptions(options)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
// 测试无效模式
|
||||
invalidOptions := &gen.GenerationOptions{
|
||||
Name: "Test Configuration",
|
||||
Mode: "invalid-mode",
|
||||
}
|
||||
err = generator.ValidateOptions(invalidOptions)
|
||||
So(err, ShouldNotBeNil)
|
||||
So(err.Error(), ShouldContainSubstring, "invalid mode")
|
||||
})
|
||||
|
||||
Convey("应该返回支持的格式", func() {
|
||||
formats := generator.GetSupportedFormats()
|
||||
So(formats, ShouldResemble, []string{"clash"})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestSurgeGenerator(t *testing.T) {
|
||||
Convey("SurgeGenerator", t, func() {
|
||||
logger, err := logging.NewLogger(&logging.LoggingConfig{
|
||||
Level: "info",
|
||||
Format: "text",
|
||||
Output: "stdout",
|
||||
})
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
generator := gen.NewSurgeGenerator(logger)
|
||||
|
||||
Convey("应该能生成Surge配置", func() {
|
||||
configs := []*parser.ProxyConfig{
|
||||
{
|
||||
Name: "test-ss",
|
||||
Server: "192.168.1.1",
|
||||
Port: 8388,
|
||||
Type: "ss",
|
||||
Protocol: "ss",
|
||||
Settings: map[string]interface{}{
|
||||
"method": "aes-256-cfb",
|
||||
"password": "password",
|
||||
},
|
||||
UDP: true,
|
||||
},
|
||||
}
|
||||
|
||||
options := &gen.GenerationOptions{
|
||||
Name: "Test Surge",
|
||||
Group: "proxy",
|
||||
IPv6: true,
|
||||
}
|
||||
|
||||
result, err := generator.Generate(configs, options)
|
||||
So(err, ShouldBeNil)
|
||||
So(result, ShouldNotBeNil)
|
||||
So(result, ShouldContainSubstring, "[Proxy]")
|
||||
So(result, ShouldContainSubstring, "test-ss")
|
||||
So(result, ShouldContainSubstring, "aes-256-cfb")
|
||||
})
|
||||
|
||||
Convey("应该能验证Surge配置选项", func() {
|
||||
options := &gen.GenerationOptions{
|
||||
Name: "Test Configuration",
|
||||
}
|
||||
|
||||
err := generator.ValidateOptions(options)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("应该返回支持的格式", func() {
|
||||
formats := generator.GetSupportedFormats()
|
||||
So(formats, ShouldResemble, []string{"surge"})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestQuantumultXGenerator(t *testing.T) {
|
||||
Convey("QuantumultXGenerator", t, func() {
|
||||
logger, err := logging.NewLogger(&logging.LoggingConfig{
|
||||
Level: "info",
|
||||
Format: "text",
|
||||
Output: "stdout",
|
||||
})
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
generator := gen.NewQuantumultXGenerator(logger)
|
||||
|
||||
Convey("应该能生成Quantumult X配置", func() {
|
||||
configs := []*parser.ProxyConfig{
|
||||
{
|
||||
Name: "test-ss",
|
||||
Server: "192.168.1.1",
|
||||
Port: 8388,
|
||||
Type: "ss",
|
||||
Protocol: "ss",
|
||||
Settings: map[string]interface{}{
|
||||
"method": "aes-256-cfb",
|
||||
"password": "password",
|
||||
},
|
||||
UDP: true,
|
||||
},
|
||||
}
|
||||
|
||||
options := &gen.GenerationOptions{
|
||||
Name: "Test Quantumult X",
|
||||
Group: "proxy",
|
||||
IPv6: true,
|
||||
}
|
||||
|
||||
result, err := generator.Generate(configs, options)
|
||||
So(err, ShouldBeNil)
|
||||
So(result, ShouldNotBeNil)
|
||||
So(result, ShouldContainSubstring, "[Server]")
|
||||
So(result, ShouldContainSubstring, "test-ss")
|
||||
So(result, ShouldContainSubstring, "aes-256-cfb")
|
||||
})
|
||||
|
||||
Convey("应该能验证Quantumult X配置选项", func() {
|
||||
options := &gen.GenerationOptions{
|
||||
Name: "Test Configuration",
|
||||
}
|
||||
|
||||
err := generator.ValidateOptions(options)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("应该返回支持的格式", func() {
|
||||
formats := generator.GetSupportedFormats()
|
||||
So(formats, ShouldResemble, []string{"quantumultx"})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestLoonGenerator(t *testing.T) {
|
||||
Convey("LoonGenerator", t, func() {
|
||||
logger, err := logging.NewLogger(&logging.LoggingConfig{
|
||||
Level: "info",
|
||||
Format: "text",
|
||||
Output: "stdout",
|
||||
})
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
generator := gen.NewLoonGenerator(logger)
|
||||
|
||||
Convey("应该能生成Loon配置", func() {
|
||||
configs := []*parser.ProxyConfig{
|
||||
{
|
||||
Name: "test-ss",
|
||||
Server: "192.168.1.1",
|
||||
Port: 8388,
|
||||
Type: "ss",
|
||||
Protocol: "ss",
|
||||
Settings: map[string]interface{}{
|
||||
"method": "aes-256-cfb",
|
||||
"password": "password",
|
||||
},
|
||||
UDP: true,
|
||||
},
|
||||
}
|
||||
|
||||
options := &gen.GenerationOptions{
|
||||
Name: "Test Loon",
|
||||
Group: "proxy",
|
||||
IPv6: true,
|
||||
}
|
||||
|
||||
result, err := generator.Generate(configs, options)
|
||||
So(err, ShouldBeNil)
|
||||
So(result, ShouldNotBeNil)
|
||||
So(result, ShouldContainSubstring, "[Proxy]")
|
||||
So(result, ShouldContainSubstring, "test-ss")
|
||||
So(result, ShouldContainSubstring, "aes-256-cfb")
|
||||
})
|
||||
|
||||
Convey("应该能验证Loon配置选项", func() {
|
||||
options := &gen.GenerationOptions{
|
||||
Name: "Test Configuration",
|
||||
}
|
||||
|
||||
err := generator.ValidateOptions(options)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("应该返回支持的格式", func() {
|
||||
formats := generator.GetSupportedFormats()
|
||||
So(formats, ShouldResemble, []string{"loon"})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestSurfboardGenerator(t *testing.T) {
|
||||
Convey("SurfboardGenerator", t, func() {
|
||||
logger, err := logging.NewLogger(&logging.LoggingConfig{
|
||||
Level: "info",
|
||||
Format: "text",
|
||||
Output: "stdout",
|
||||
})
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
generator := gen.NewSurfboardGenerator(logger)
|
||||
|
||||
Convey("应该能生成Surfboard配置", func() {
|
||||
configs := []*parser.ProxyConfig{
|
||||
{
|
||||
Name: "test-ss",
|
||||
Server: "192.168.1.1",
|
||||
Port: 8388,
|
||||
Type: "ss",
|
||||
Protocol: "ss",
|
||||
Settings: map[string]interface{}{
|
||||
"method": "aes-256-cfb",
|
||||
"password": "password",
|
||||
},
|
||||
UDP: true,
|
||||
},
|
||||
}
|
||||
|
||||
options := &gen.GenerationOptions{
|
||||
Name: "Test Surfboard",
|
||||
Group: "proxy",
|
||||
IPv6: true,
|
||||
}
|
||||
|
||||
result, err := generator.Generate(configs, options)
|
||||
So(err, ShouldBeNil)
|
||||
So(result, ShouldNotBeNil)
|
||||
So(result, ShouldContainSubstring, "[Proxy]")
|
||||
So(result, ShouldContainSubstring, "test-ss")
|
||||
So(result, ShouldContainSubstring, "aes-256-cfb")
|
||||
})
|
||||
|
||||
Convey("应该能验证Surfboard配置选项", func() {
|
||||
options := &gen.GenerationOptions{
|
||||
Name: "Test Configuration",
|
||||
}
|
||||
|
||||
err := generator.ValidateOptions(options)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("应该返回支持的格式", func() {
|
||||
formats := generator.GetSupportedFormats()
|
||||
So(formats, ShouldResemble, []string{"surfboard"})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestV2RayGenerator(t *testing.T) {
|
||||
Convey("V2RayGenerator", t, func() {
|
||||
logger, err := logging.NewLogger(&logging.LoggingConfig{
|
||||
Level: "info",
|
||||
Format: "text",
|
||||
Output: "stdout",
|
||||
})
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
generator := gen.NewV2RayGenerator(logger)
|
||||
|
||||
Convey("应该能生成V2Ray配置", func() {
|
||||
configs := []*parser.ProxyConfig{
|
||||
{
|
||||
Name: "test-vmess",
|
||||
Server: "192.168.1.1",
|
||||
Port: 443,
|
||||
Type: "vmess",
|
||||
Protocol: "vmess",
|
||||
Settings: map[string]interface{}{
|
||||
"uuid": "12345678-1234-1234-1234-123456789012",
|
||||
"alterId": 0,
|
||||
},
|
||||
UDP: true,
|
||||
},
|
||||
}
|
||||
|
||||
options := &gen.GenerationOptions{
|
||||
Name: "Test V2Ray",
|
||||
Group: "proxy",
|
||||
IPv6: true,
|
||||
}
|
||||
|
||||
result, err := generator.Generate(configs, options)
|
||||
So(err, ShouldBeNil)
|
||||
So(result, ShouldNotBeNil)
|
||||
So(result, ShouldContainSubstring, "inbounds")
|
||||
So(result, ShouldContainSubstring, "outbounds")
|
||||
So(result, ShouldContainSubstring, "routing")
|
||||
So(result, ShouldContainSubstring, "12345678-1234-1234-1234-123456789012")
|
||||
})
|
||||
|
||||
Convey("应该能验证V2Ray配置选项", func() {
|
||||
options := &gen.GenerationOptions{
|
||||
Name: "Test Configuration",
|
||||
}
|
||||
|
||||
err := generator.ValidateOptions(options)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("应该返回支持的格式", func() {
|
||||
formats := generator.GetSupportedFormats()
|
||||
So(formats, ShouldResemble, []string{"v2ray"})
|
||||
})
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user