优化结构

This commit is contained in:
Yvan
2026-08-21 22:54:40 +08:00
parent e4ba0dced9
commit 430feaaff6
20 changed files with 604 additions and 20 deletions
@@ -1,6 +1,39 @@
package biz
import "testing"
import (
"context"
"encoding/json"
"testing"
)
type integrationConfigRepoTestDouble struct {
saves int
}
func (*integrationConfigRepoTestDouble) ListIntegrationConfigs(context.Context, string) ([]*IntegrationConfig, error) {
return nil, nil
}
func (*integrationConfigRepoTestDouble) FindIntegrationConfig(context.Context, string, string) (*IntegrationConfig, error) {
return nil, nil
}
func (r *integrationConfigRepoTestDouble) SaveIntegrationConfig(context.Context, *IntegrationConfig) error {
r.saves++
return nil
}
func (*integrationConfigRepoTestDouble) DeleteIntegrationConfig(context.Context, string, string) error {
return nil
}
type integrationConnectionTesterDouble struct {
calls int
config *IntegrationConfig
}
func (t *integrationConnectionTesterDouble) TestIntegration(_ context.Context, config *IntegrationConfig) error {
t.calls++
t.config = config
return nil
}
func TestCommunicationIntegrationDefinitionsAndValidation(t *testing.T) {
for _, target := range []struct{ kind, provider string }{
@@ -30,3 +63,36 @@ func TestCommunicationIntegrationValidationRejectsInvalidValues(t *testing.T) {
t.Fatal("invalid websocket path was accepted")
}
}
func TestIntegrationConfigTestDoesNotPersistCandidate(t *testing.T) {
repo := &integrationConfigRepoTestDouble{}
tester := &integrationConnectionTesterDouble{}
usecase := NewIntegrationConfigUsecase(repo, tester)
raw, _ := json.Marshal(map[string]any{"path": "/candidate"})
err := usecase.Test(context.Background(), &IntegrationConfig{
Kind: " WebSocket ",
Provider: " Melody ",
Enabled: false,
Values: raw,
})
if err != nil {
t.Fatal(err)
}
if repo.saves != 0 {
t.Fatalf("candidate config was persisted %d times", repo.saves)
}
if tester.calls != 1 || tester.config == nil {
t.Fatalf("connection tester calls = %d, config = %#v", tester.calls, tester.config)
}
if tester.config.Kind != IntegrationKindWebSocket || tester.config.Provider != "melody" || !tester.config.Enabled {
t.Fatalf("tested config = %#v", tester.config)
}
values := map[string]any{}
if err = json.Unmarshal(tester.config.Values, &values); err != nil {
t.Fatal(err)
}
if values["path"] != "/candidate" || values["write_wait"] != "10s" {
t.Fatalf("tested values = %#v", values)
}
}