DEV: spec fixes

DEV: spec fixes

diff --git a/app/lib/discourse_automation/scriptable.rb b/app/lib/discourse_automation/scriptable.rb
index f0d21b5..ae29f19 100644
--- a/app/lib/discourse_automation/scriptable.rb
+++ b/app/lib/discourse_automation/scriptable.rb
@@ -22,7 +22,7 @@ module DiscourseAutomation
       @version = 0
       @fields = []
       @placeholders = [:site_title]
-      @triggerables = (@@plugin_triggerables[name.to_sym] || []) + []
+      @triggerables = (@@plugin_triggerables[name&.to_sym] || [])
       @script = proc {}
       @not_found = false
       @forced_triggerable = nil
diff --git a/app/serializers/discourse_automation/automation_serializer.rb b/app/serializers/discourse_automation/automation_serializer.rb
index b7a3fc3..538e999 100644
--- a/app/serializers/discourse_automation/automation_serializer.rb
+++ b/app/serializers/discourse_automation/automation_serializer.rb
@@ -42,7 +42,7 @@ module DiscourseAutomation
         with_trigger_doc: I18n.exists?(script_with_trigger_key, :en) ? I18n.t(script_with_trigger_key) : nil,
         forced_triggerable: scriptable.forced_triggerable,
         not_found: scriptable.not_found,
-        templates: process_templates(scriptable),
+        templates: process_templates(scriptable.fields.filter { |f| !f[:triggerable] || f[:triggerable].to_sym == object.trigger&.to_sym }),
         fields: process_fields(object.fields.where(target: 'script'))
       }
     end
@@ -57,16 +57,16 @@ module DiscourseAutomation
         description: I18n.t("#{key}.#{object.trigger}.description"),
         doc: I18n.exists?(doc_key, :en) ? I18n.t(doc_key) : nil,
         not_found: triggerable.not_found,
-        templates: process_templates(triggerable),
+        templates: process_templates(triggerable.fields),
         fields: process_fields(object.fields.where(target: 'trigger'))
       }
     end
 
     private
 
-    def process_templates(targetable)
+    def process_templates(fields)
       ActiveModel::ArraySerializer.new(
-          targetable.fields,
+          fields,
           each_serializer: DiscourseAutomation::TemplateSerializer, scope: { automation: object }
         ).as_json
     end
diff --git a/spec/jobs/discourse_automation_tracker_spec.rb b/spec/jobs/discourse_automation_tracker_spec.rb
index 4acdc51..0c10825 100644
--- a/spec/jobs/discourse_automation_tracker_spec.rb
+++ b/spec/jobs/discourse_automation_tracker_spec.rb
@@ -3,6 +3,10 @@
 require_relative '../discourse_automation_helper'
 
 describe Jobs::DiscourseAutomationTracker do
+  before do
+    SiteSetting.discourse_automation_enabled = true
+  end
+
   describe 'pending automation' do
     fab!(:automation) {
       Fabricate(
diff --git a/spec/scripts/flag_post_on_words_spec.rb b/spec/scripts/flag_post_on_words_spec.rb
index 3ffd04a..0e89a61 100644
--- a/spec/scripts/flag_post_on_words_spec.rb
+++ b/spec/scripts/flag_post_on_words_spec.rb
@@ -9,6 +9,7 @@ describe 'FlagPostsOnWords' do
   fab!(:automation) { Fabricate(:automation, script: DiscourseAutomation::Scriptable::FLAG_POST_ON_WORDS, trigger: DiscourseAutomation::Triggerable::POST_CREATED_EDITED) }
 
   before do
+    SiteSetting.discourse_automation_enabled = true
     automation.fields.create!(
       component: 'text_list',
       name: 'words',
diff --git a/spec/scripts/gift_exchange_spec.rb b/spec/scripts/gift_exchange_spec.rb
index 4b172fa..5f0cd9d 100644
--- a/spec/scripts/gift_exchange_spec.rb
+++ b/spec/scripts/gift_exchange_spec.rb
@@ -10,6 +10,8 @@ describe 'GiftExchange' do
   fab!(:user_3) { Fabricate(:user) }
 
   before do
+    SiteSetting.discourse_automation_enabled = true
+
     gift_group.add(user_1)
     gift_group.add(user_2)
     gift_group.add(user_3)
diff --git a/spec/scripts/send_pms_spec.rb b/spec/scripts/send_pms_spec.rb
index a3b6886..d7fe86e 100644
--- a/spec/scripts/send_pms_spec.rb
+++ b/spec/scripts/send_pms_spec.rb
@@ -6,6 +6,8 @@ describe 'SendPms' do
   fab!(:automation) { Fabricate(:automation, script: DiscourseAutomation::Scriptable::SEND_PMS, trigger: 'stalled_wiki') }
 
   before do
+    SiteSetting.discourse_automation_enabled = true
+
     automation.upsert_field!('sender', 'user', { value: Discourse.system_user.username })
     automation.upsert_field!('sendable_pms', 'pms', { value: [{ title: 'A message from %%SENDER_USERNAME%%', raw: 'This is a message sent to @%%RECEIVER_USERNAME%%' }] })
   end
diff --git a/spec/serializers/automation_serializer_spec.rb b/spec/serializers/automation_serializer_spec.rb
index 431bb96..5762cb6 100644
--- a/spec/serializers/automation_serializer_spec.rb
+++ b/spec/serializers/automation_serializer_spec.rb
@@ -29,4 +29,43 @@ describe DiscourseAutomation::AutomationSerializer do
       expect(serializer.next_pending_automation_at).to_not be
     end
   end
+
+  context 'script with fields limited to a specific trigger' do
+    before do
+      DiscourseAutomation::Scriptable.add('foo') do
+        field :bar, component: :text, triggerable: DiscourseAutomation::Triggerable::TOPIC
+      end
+    end
+
+    context 'automation is not using the specific trigger' do
+      fab!(:automation) do
+        Fabricate(
+          :automation,
+          script: 'foo',
+          trigger: DiscourseAutomation::Triggerable::POST_CREATED_EDITED
+        )
+      end
+
+      it 'filters the field' do
+        serializer = DiscourseAutomation::AutomationSerializer.new(automation, scope: Guardian.new(user), root: false)
+        expect(serializer.script[:templates]).to eq([])
+      end
+    end
+
+    context 'automation is using the specific trigger' do
+      fab!(:automation) do
+        Fabricate(
+          :automation,
+          script: 'foo',
+          trigger: DiscourseAutomation::Triggerable::TOPIC
+        )
+      end
+
+      it 'doesn’t filter the field' do
+        serializer = DiscourseAutomation::AutomationSerializer.new(automation, scope: Guardian.new(user), root: false)
+        expect(serializer.script[:templates].length).to eq(1)
+        expect(serializer.script[:templates].first[:name]).to eq(:bar)
+      end
+    end
+  end
 end
diff --git a/spec/triggers/post_created_edited_spec.rb b/spec/triggers/post_created_edited_spec.rb
index fb29d20..0879431 100644
--- a/spec/triggers/post_created_edited_spec.rb
+++ b/spec/triggers/post_created_edited_spec.rb
@@ -3,6 +3,10 @@
 require_relative '../discourse_automation_helper'
 
 describe 'PostCreatedEdited' do
+  before do
+    SiteSetting.discourse_automation_enabled = true
+  end
+
   let(:basic_topic_params) { { title: 'hello world topic', raw: 'my name is fred', archetype_id: 1 } }
   fab!(:user) { Fabricate(:user) }
   fab!(:automation) { Fabricate(:automation, trigger: DiscourseAutomation::Triggerable::POST_CREATED_EDITED) }
diff --git a/spec/triggers/user_added_to_group_spec.rb b/spec/triggers/user_added_to_group_spec.rb
index e371682..973fd3b 100644
--- a/spec/triggers/user_added_to_group_spec.rb
+++ b/spec/triggers/user_added_to_group_spec.rb
@@ -13,6 +13,7 @@ describe 'UserAddedToGroup' do
   }
 
   before do
+    SiteSetting.discourse_automation_enabled = true
     automation.upsert_field!('joined_group', 'group', { value: tracked_group.id }, target: 'trigger')
   end
 
diff --git a/spec/triggers/user_promoted_spec.rb b/spec/triggers/user_promoted_spec.rb
index bb1423b..b04386d 100644
--- a/spec/triggers/user_promoted_spec.rb
+++ b/spec/triggers/user_promoted_spec.rb
@@ -3,6 +3,10 @@
 require_relative '../discourse_automation_helper'
 
 describe 'UserPromoted' do
+  before do
+    SiteSetting.discourse_automation_enabled = true
+  end
+
   fab!(:user) { Fabricate(:user, trust_level: TrustLevel[0]) }
   fab!(:automation) { Fabricate(:automation, trigger: DiscourseAutomation::Triggerable::USER_PROMOTED) }
 

GitHub sha: b96f11cc027166d7a8392613ef5ff22bb3c677ba