118 lines
3.7 KiB
Go
118 lines
3.7 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/settings"
|
|
)
|
|
|
|
func TestTableBindingRestartsOnlyAfterActiveSave(t *testing.T) {
|
|
restarted := make(chan struct{}, 1)
|
|
var saved settings.ResourceBinding
|
|
repo := &validatedSettingsRepository{
|
|
validateTable: func(_ context.Context, binding settings.ResourceBinding) (map[string]string, error) {
|
|
return map[string]string{"answer": "field-answer"}, nil
|
|
},
|
|
saveBinding: func(_ context.Context, binding settings.ResourceBinding) error {
|
|
saved = binding
|
|
return nil
|
|
},
|
|
restart: func() { restarted <- struct{}{} },
|
|
restartDelay: 0,
|
|
}
|
|
if err := repo.SaveBinding(context.Background(), settings.ResourceBinding{Kind: "question_table", TenantKey: "tenant", BaseID: "base", SheetID: "sheet", OperatorID: "operator"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !saved.Activate || saved.ValidationCode != "schema_ok" || saved.FieldMapping["answer"] != "field-answer" {
|
|
t.Fatalf("binding=%+v", saved)
|
|
}
|
|
select {
|
|
case <-restarted:
|
|
case <-time.After(50 * time.Millisecond):
|
|
t.Fatal("restart not requested")
|
|
}
|
|
}
|
|
|
|
func TestTableBindingDoesNotRestartOnValidationOrSaveFailure(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
validateErr error
|
|
saveErr error
|
|
}{
|
|
{name: "validation failure", validateErr: errors.New("schema failed")},
|
|
{name: "save failure", saveErr: errors.New("save failed")},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
restarted := make(chan struct{}, 1)
|
|
repo := &validatedSettingsRepository{
|
|
validateTable: func(context.Context, settings.ResourceBinding) (map[string]string, error) {
|
|
return map[string]string{"answer": "field"}, tc.validateErr
|
|
},
|
|
saveBinding: func(context.Context, settings.ResourceBinding) error { return tc.saveErr },
|
|
restart: func() { restarted <- struct{}{} },
|
|
}
|
|
if err := repo.SaveBinding(context.Background(), settings.ResourceBinding{Kind: "question_table"}); err == nil {
|
|
t.Fatal("expected failure")
|
|
}
|
|
select {
|
|
case <-restarted:
|
|
t.Fatal("must not restart after failed activation")
|
|
case <-time.After(50 * time.Millisecond):
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSupportGroupActivationRestartsOnlyAfterTestAndSaveSucceed(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
testErr error
|
|
saveErr error
|
|
wantSaved bool
|
|
wantRestart bool
|
|
}{
|
|
{name: "test fails", testErr: errors.New("send failed")},
|
|
{name: "save fails", saveErr: errors.New("save failed"), wantSaved: true},
|
|
{name: "success", wantSaved: true, wantRestart: true},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
var saved settings.ResourceBinding
|
|
restarted := make(chan struct{}, 1)
|
|
repo := &validatedSettingsRepository{
|
|
testSupport: func(context.Context, settings.ResourceBinding) error { return tc.testErr },
|
|
saveBinding: func(_ context.Context, binding settings.ResourceBinding) error {
|
|
saved = binding
|
|
return tc.saveErr
|
|
},
|
|
restart: func() { restarted <- struct{}{} },
|
|
restartDelay: 0,
|
|
}
|
|
err := repo.TestAndActivateSupportGroup(context.Background(), settings.ResourceBinding{
|
|
Kind: "support_group", TenantKey: "tenant", ConversationID: "group",
|
|
})
|
|
if (err != nil) != (tc.testErr != nil || tc.saveErr != nil) {
|
|
t.Fatalf("err=%v", err)
|
|
}
|
|
if got := saved.Kind != ""; got != tc.wantSaved {
|
|
t.Fatalf("saved=%v want=%v", got, tc.wantSaved)
|
|
}
|
|
if tc.wantSaved && (saved.ValidationCode != "support_test_ok" || !saved.Activate) {
|
|
t.Fatalf("binding=%+v", saved)
|
|
}
|
|
select {
|
|
case <-restarted:
|
|
if !tc.wantRestart {
|
|
t.Fatal("unexpected restart")
|
|
}
|
|
case <-time.After(50 * time.Millisecond):
|
|
if tc.wantRestart {
|
|
t.Fatal("restart not requested")
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|