package unit import ( "testing" "time" "github.com/smartystreets/goconvey/convey" testcase "github.com/subconverter-go/internal/testing" ) // TestTestCaseBasic 测试 TestCase 模型的基本功能 // 验证 TestCase 模型的创建、设置和基本操作 func TestTestCaseBasic(t *testing.T) { convey.Convey("TestCase 基本功能测试", t, func() { convey.Convey("创建和基本配置", func() { tc := testcase.NewUnitTest("TC001", "Basic Test") convey.So(tc.ID, convey.ShouldEqual, "TC001") convey.So(tc.Name, convey.ShouldEqual, "Basic Test") convey.So(tc.Type, convey.ShouldEqual, testcase.UNIT) convey.So(tc.Status, convey.ShouldEqual, testcase.PENDING) convey.So(tc.Priority, convey.ShouldEqual, testcase.MEDIUM) }) convey.Convey("链式调用配置", func() { tc := testcase.NewUnitTest("TC002", "Chain Test"). SetDescription("Test chain calls"). SetPriority(testcase.HIGH). SetTimeout(60*time.Second). AddTag("smoke"). AddTag("regression") convey.So(tc.Description, convey.ShouldEqual, "Test chain calls") convey.So(tc.Priority, convey.ShouldEqual, testcase.HIGH) convey.So(tc.Timeout, convey.ShouldEqual, 60*time.Second) convey.So(tc.HasTag("smoke"), convey.ShouldBeTrue) convey.So(tc.HasTag("regression"), convey.ShouldBeTrue) }) convey.Convey("测试数据设置", func() { input := map[string]interface{}{ "target": "clash", "url": "https://example.com/subscribe", } expectation := map[string]interface{}{ "success": true, "nodes": 10, } tc := testcase.NewUnitTest("TC003", "Data Test"). SetInput(input). SetExpectation(expectation) convey.So(tc.Input, convey.ShouldResemble, input) convey.So(tc.Expectation, convey.ShouldResemble, expectation) }) convey.Convey("执行生命周期", func() { tc := testcase.NewUnitTest("TC004", "Lifecycle Test") tc.SetInput("test").SetExpectation("result") // 初始状态 convey.So(tc.Status, convey.ShouldEqual, testcase.PENDING) convey.So(tc.Attempts, convey.ShouldEqual, 0) convey.So(tc.ShouldRun(), convey.ShouldBeTrue) // 开始执行 tc.Start() convey.So(tc.Status, convey.ShouldEqual, testcase.RUNNING) convey.So(tc.Attempts, convey.ShouldEqual, 1) convey.So(tc.IsRunning(), convey.ShouldBeTrue) convey.So(tc.ShouldRun(), convey.ShouldBeFalse) // 完成执行 tc.Complete(true, "actual result", nil) convey.So(tc.Status, convey.ShouldEqual, testcase.PASSED) convey.So(tc.IsSuccessful(), convey.ShouldBeTrue) convey.So(tc.IsFailed(), convey.ShouldBeFalse) convey.So(tc.Duration > 0, convey.ShouldBeTrue) convey.So(tc.ShouldRun(), convey.ShouldBeFalse) }) convey.Convey("测试验证", func() { convey.Convey("有效测试用例", func() { tc := testcase.NewUnitTest("TC005", "Valid Test") tc.SetInput("input").SetExpectation("output") err := tc.Validate() convey.So(err, convey.ShouldBeNil) }) convey.Convey("无效ID", func() { tc := testcase.NewUnitTest("", "Test") tc.SetInput("input").SetExpectation("output") err := tc.Validate() convey.So(err, convey.ShouldNotBeNil) convey.So(err.Error(), convey.ShouldContain, "ID cannot be empty") }) convey.Convey("空输入", func() { tc := testcase.NewUnitTest("TC006", "Input Test") tc.SetInput(nil).SetExpectation("output") err := tc.Validate() convey.So(err, convey.ShouldNotBeNil) convey.So(err.Error(), convey.ShouldContain, "input cannot be nil") }) }) convey.Convey("序列化功能", func() { original := testcase.NewUnitTest("TC007", "Serialize Test") original. SetDescription("Test serialization"). SetPriority(testcase.HIGH). SetInput("test input"). SetExpectation("expected output"). AddTag("serialize") jsonStr, err := original.ToJSON() convey.So(err, convey.ShouldBeNil) convey.So(jsonStr, convey.ShouldNotBeEmpty) convey.So(jsonStr, convey.ShouldContain, "Serialize Test") // 反序列化 newTC := &testcase.TestCase{} err = newTC.FromJSON(jsonStr) convey.So(err, convey.ShouldBeNil) convey.So(newTC.ID, convey.ShouldEqual, original.ID) convey.So(newTC.Name, convey.ShouldEqual, original.Name) convey.So(newTC.Description, convey.ShouldEqual, original.Description) }) convey.Convey("克隆功能", func() { original := testcase.NewUnitTest("TC008", "Clone Test") original. SetDescription("Test cloning"). SetPriority(testcase.CRITICAL). SetInput("input"). SetExpectation("output"). AddTag("clone") clone := original.Clone() convey.So(clone.ID, convey.ShouldEqual, "TC008_clone") convey.So(clone.Name, convey.ShouldEqual, original.Name) convey.So(clone.Description, convey.ShouldEqual, original.Description) convey.So(clone.Priority, convey.ShouldEqual, original.Priority) convey.So(clone.Input, convey.ShouldEqual, original.Input) convey.So(clone.Expectation, convey.ShouldEqual, original.Expectation) convey.So(clone.Tags, convey.ShouldResemble, original.Tags) // 验证状态被重置 convey.So(clone.Status, convey.ShouldEqual, testcase.PENDING) convey.So(original.Status, convey.ShouldEqual, testcase.PENDING) }) }) }