From 3e5c494c83aae3aa5949ccecf3bf7da4395e8500 Mon Sep 17 00:00:00 2001
From: Ole Langbehn <ole.langbehn@inoio.de>
Date: Sat, 19 Oct 2024 01:30:42 +0200
Subject: [PATCH] forgot to add smoketest directory

---
 smoketest/main.js | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
 create mode 100644 smoketest/main.js

diff --git a/smoketest/main.js b/smoketest/main.js
new file mode 100644
index 0000000..e9d2a77
--- /dev/null
+++ b/smoketest/main.js
@@ -0,0 +1,40 @@
+import http from 'k6/http'
+import { check, sleep } from 'k6'
+
+// You don't need to change anything in this section, it's k6 glue code.
+// See the default function at the end of the file for defining your smoketest.
+// This configuration only executes 1 test, enough for a smoketest. The smoketest will fail on any check failing.
+const allChecksNeedToPassTreshold = { checks: [{ threshold: 'rate==1', abortOnFail: true }] }
+export const options = {
+  vus: 1,
+  iterations: 1,
+  thresholds: allChecksNeedToPassTreshold,
+}
+
+/**
+ * Performs a GraphQL query and checks the response using the provided function. Fails if any of the provided expectations are not met.
+ * @param {string} query The GraphQL query to perform
+ * @param {(response: http.Response) => Array<boolean>} checkFunction
+ *   A function that takes the HTTP response as an argument and returns an array
+ *   of boolean values, each indicating success or failure of a test.
+ */
+function forQuery(query, checkFunction) {
+  const response = http.post(`${__ENV.BASE_URL}`, JSON.stringify({ query }), {
+    headers: { 'Content-Type': 'application/json' },
+  })
+  checkFunction(response)
+}
+
+// Define your smoketest(s) here.
+export default () => {
+  forQuery(`{detectLanguage(text:"Hi")}`, (response) => {
+    check(response, {
+      'is status 200': (r) => r.status === 200,
+    })
+    check(JSON.parse(response.body), {
+      // there can be multiple tests here, e.g.
+      //"contains topics object": (r) => typeof r.data.topics != null,
+      'contains schema description': (r) => typeof r.data.detectLanguage === "string",
+    })
+  })
+}
-- 
GitLab