diff --git a/cmd/dobotai/settings_binding_validator.go b/cmd/dobotai/settings_binding_validator.go index 747ca72..ee069b3 100644 --- a/cmd/dobotai/settings_binding_validator.go +++ b/cmd/dobotai/settings_binding_validator.go @@ -13,12 +13,13 @@ import ( type validatedSettingsRepository struct { *postgres.SettingsRepository - clientID string - secret string - testSupport func(context.Context, settings.ResourceBinding) error - saveBinding func(context.Context, settings.ResourceBinding) error - restart func() - restartDelay time.Duration + clientID string + secret string + testSupport func(context.Context, settings.ResourceBinding) error + validateTable func(context.Context, settings.ResourceBinding) (map[string]string, error) + saveBinding func(context.Context, settings.ResourceBinding) error + restart func() + restartDelay time.Duration } func (r *validatedSettingsRepository) TestAndActivateSupportGroup(ctx context.Context, binding settings.ResourceBinding) error { @@ -60,35 +61,73 @@ func (r *validatedSettingsRepository) TestAndActivateSupportGroup(ctx context.Co } func (r *validatedSettingsRepository) SaveBinding(ctx context.Context, binding settings.ResourceBinding) error { + activate := false switch binding.Kind { case "question_table", "faq_table": - client, err := questiontable.NewClient(questiontable.Config{ - ClientID: r.clientID, Secret: r.secret, BaseID: binding.BaseID, - SheetID: binding.SheetID, OperatorID: binding.OperatorID, - }, nil) - if err != nil { - return errors.New("table_binding_invalid_config") + var mapping map[string]string + var err error + if r.validateTable != nil { + mapping, err = r.validateTable(ctx, binding) + } else { + mapping, err = r.readAndMapTable(ctx, binding) } - fields, err := client.ReadFields(ctx) - if err != nil { - return err - } - required := questiontable.QuestionSchema() - if binding.Kind == "faq_table" { - required = questiontable.FAQSchema() - } - mapping, err := questiontable.MapRequiredFields(fields, required) if err != nil { return err } binding.FieldMapping = mapping binding.ValidationCode = "schema_ok" binding.Activate = true + activate = true case "support_group": binding.ValidationCode = "pending_test_message" binding.Activate = false default: return errors.New("unsupported_binding_kind") } - return r.SettingsRepository.SaveBinding(ctx, binding) + var err error + if r.saveBinding != nil { + err = r.saveBinding(ctx, binding) + } else if r.SettingsRepository != nil { + err = r.SettingsRepository.SaveBinding(ctx, binding) + } else { + err = errors.New("binding_repository_unavailable") + } + if err != nil { + return err + } + if activate { + r.requestRestart() + } + return nil +} + +func (r *validatedSettingsRepository) readAndMapTable(ctx context.Context, binding settings.ResourceBinding) (map[string]string, error) { + client, err := questiontable.NewClient(questiontable.Config{ + ClientID: r.clientID, Secret: r.secret, BaseID: binding.BaseID, + SheetID: binding.SheetID, OperatorID: binding.OperatorID, + }, nil) + if err != nil { + return nil, errors.New("table_binding_invalid_config") + } + fields, err := client.ReadFields(ctx) + if err != nil { + return nil, err + } + required := questiontable.QuestionSchema() + if binding.Kind == "faq_table" { + required = questiontable.FAQSchema() + } + return questiontable.MapRequiredFields(fields, required) +} + +func (r *validatedSettingsRepository) requestRestart() { + if r.restart == nil { + return + } + go func() { + if r.restartDelay > 0 { + time.Sleep(r.restartDelay) + } + r.restart() + }() } diff --git a/cmd/dobotai/settings_binding_validator_test.go b/cmd/dobotai/settings_binding_validator_test.go index 0976f99..0a0c759 100644 --- a/cmd/dobotai/settings_binding_validator_test.go +++ b/cmd/dobotai/settings_binding_validator_test.go @@ -9,6 +9,63 @@ import ( "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