测试 1: 基础 Workflow
(ctx context.Context)
| 38 | |
| 39 | // 测试 1: 基础 Workflow |
| 40 | func testBasicWorkflow(ctx context.Context) { |
| 41 | wf := workflow.New("BasicTest").WithStream() |
| 42 | |
| 43 | wf.AddStep(workflow.NewFunctionStep("step1", func(ctx context.Context, input *workflow.StepInput) (*workflow.StepOutput, error) { |
| 44 | return &workflow.StepOutput{ |
| 45 | Content: "Step 1 完成", |
| 46 | Metadata: make(map[string]any), |
| 47 | }, nil |
| 48 | })) |
| 49 | |
| 50 | wf.AddStep(workflow.NewFunctionStep("step2", func(ctx context.Context, input *workflow.StepInput) (*workflow.StepOutput, error) { |
| 51 | return &workflow.StepOutput{ |
| 52 | Content: fmt.Sprintf("Step 2 接收: %v", input.PreviousStepContent), |
| 53 | Metadata: make(map[string]any), |
| 54 | }, nil |
| 55 | })) |
| 56 | |
| 57 | if err := wf.Validate(); err != nil { |
| 58 | fmt.Printf(" ❌ 验证失败: %v\n", err) |
| 59 | return |
| 60 | } |
| 61 | |
| 62 | input := &workflow.WorkflowInput{Input: "测试输入"} |
| 63 | reader := wf.Execute(ctx, input) |
| 64 | for { |
| 65 | event, err := reader.Recv() |
| 66 | if err != nil { |
| 67 | if errors.Is(err, io.EOF) { |
| 68 | break |
| 69 | } |
| 70 | fmt.Printf(" ❌ 错误: %v\n", err) |
| 71 | continue |
| 72 | } |
| 73 | if event.Type == workflow.EventWorkflowCompleted { |
| 74 | fmt.Printf(" ✅ 成功: %v\n", event.Data.(map[string]any)["output"]) |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // 测试 2: 所有步骤类型 |
| 80 | func testAllStepTypes(ctx context.Context) { |
no test coverage detected