DEV: Add optional theme test step to the `smoke:test` rake task (#13418)

DEV: Add optional theme test step to the smoke:test rake task (#13418)

The purpose of this is to allow us to catch regressions for a feature we’ve built recently that allows theme tests to run in production. We recently had a regression that we didn’t notice for days, so to prevent that from happening again we’ll use this in our internal CI pipelines.

diff --git a/lib/tasks/smoke_test.rake b/lib/tasks/smoke_test.rake
index d3cab6a..5535d6b 100644
--- a/lib/tasks/smoke_test.rake
+++ b/lib/tasks/smoke_test.rake
@@ -81,4 +81,33 @@ task "smoke:test" do
   if results !~ /ALL PASSED/
     raise "FAILED"
   end
+
+  api_key = ENV["ADMIN_API_KEY"]
+  api_username = ENV["ADMIN_API_USERNAME"]
+  theme_url = ENV["SMOKE_TEST_THEME_URL"]
+
+  next if api_key.blank? && api_username.blank? && theme_url.blank?
+
+  puts "Running QUnit tests for theme #{theme_url.inspect} using API key #{api_key[0..3]}… and username #{api_username.inspect}"
+
+  query_params = {
+    seed: Random.new.seed,
+    theme_url: theme_url,
+    hidepassed: 1,
+    report_requests: 1
+  }
+  url += '/' if !url.end_with?('/')
+  full_url = "#{url}theme-qunit?#{query_params.to_query}"
+  timeout = 1000 * 60 * 10
+
+  sh(
+    "node",
+    "#{Rails.root}/test/run-qunit.js",
+    full_url,
+    timeout.to_s
+  )
+
+  if !$?.success?
+    raise "THEME TESTS FAILED!"
+  end
 end
diff --git a/test/run-qunit.js b/test/run-qunit.js
index f55a9d7..584e3f6 100644
--- a/test/run-qunit.js
+++ b/test/run-qunit.js
@@ -76,9 +76,15 @@ async function runAllTests() {
   }
 
   const { Inspector, Page, Runtime, Log } = protocol;
+  // eslint-disable-next-line
+  await Promise.all([
+    Inspector.enable(),
+    Page.enable(),
+    Runtime.enable(),
+    Log.enable(),
+  ]);
 
   // Documentation https://chromedevtools.github.io/devtools-protocol/tot/Log/#type-LogEntry
-  Log.enable();
   Log.entryAdded(({ entry }) => {
     let message = `${new Date(entry.timestamp).toISOString()} - (type: ${
       entry.source
@@ -89,9 +95,6 @@ async function runAllTests() {
     console.log(message);
   });
 
-  // eslint-disable-next-line
-  await Promise.all([Inspector.enable(), Page.enable(), Runtime.enable()]);
-
   Inspector.targetCrashed((entry) => {
     console.log("Chrome target crashed:");
     console.log(entry);
@@ -119,6 +122,31 @@ async function runAllTests() {
   });
 
   let url = args[0] + "&qunit_disable_auto_start=1";
+
+  const apiKey = process.env.ADMIN_API_KEY;
+  const apiUsername = process.env.ADMIN_API_USERNAME;
+  if (apiKey && apiUsername) {
+    const { Fetch } = protocol;
+    await Fetch.enable();
+    const urlObj = new URL(url);
+    Fetch.requestPaused((data) => {
+      const requestURL = new URL(data.request.url);
+      if (requestURL.hostname != urlObj.hostname) {
+        Fetch.continueRequest({
+          requestId: data.requestId,
+        });
+        return;
+      }
+      Fetch.continueRequest({
+        requestId: data.requestId,
+        headers: [
+          { name: "Api-Key", value: apiKey },
+          { name: "Api-Username", value: apiUsername },
+        ],
+      });
+    });
+  }
+
   console.log("navigate to ", url);
   Page.navigate({ url });
 

GitHub sha: c47f55253fd93fa2617069a20628ac6489cda1f1

This commit appears in #13418 which was approved by eviltrout. It was merged by OsamaSayegh.