webhooks-datagen.test.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright Materialize, Inc. and contributors. All rights reserved.
  2. //
  3. // Use of this software is governed by the Business Source License
  4. // included in the LICENSE file at the root of this repository.
  5. //
  6. // As of the Change Date specified in that file, in accordance with
  7. // the Business Source License, use of this software will be governed
  8. // by the Apache License, Version 2.0.
  9. import { beforeEach, describe, it, expect, vi } from "vitest";
  10. import { JSDOM } from "jsdom";
  11. import fs from "fs";
  12. import path from "path";
  13. describe("Webhooks Data Generator", async () => {
  14. let window, document;
  15. let isGenerating = false;
  16. beforeEach(async () => {
  17. const htmlPath = path.join(
  18. __dirname,
  19. "../../../doc/user/layouts/shortcodes/plugins/webhooks-datagen.html"
  20. );
  21. const htmlContent = fs.readFileSync(htmlPath, "utf-8");
  22. const dom = new JSDOM(htmlContent, {
  23. runScripts: "dangerously",
  24. resources: "usable",
  25. url: "http://localhost",
  26. });
  27. window = dom.window;
  28. document = window.document;
  29. // Wait for all the scripts and content to load
  30. await vi.waitFor(() => {
  31. if (document.readyState === "complete") {
  32. return true;
  33. } else {
  34. throw new Error("Document not ready yet");
  35. }
  36. });
  37. });
  38. it("should initialize the DOM elements correctly", async () => {
  39. const webhookURLInput = window.document.getElementById("webhookURL");
  40. const authPasswordInput = window.document.getElementById("authPassword");
  41. const jsonSchemaTextarea = window.document.getElementById("jsonSchema");
  42. const startButton = window.document.getElementById("startButton");
  43. const stopButton = window.document.getElementById("stopButton");
  44. await vi.waitFor(() => webhookURLInput && authPasswordInput && jsonSchemaTextarea && startButton && stopButton);
  45. expect(webhookURLInput).not.toBeNull();
  46. expect(authPasswordInput).not.toBeNull();
  47. expect(jsonSchemaTextarea).not.toBeNull();
  48. expect(startButton).not.toBeNull();
  49. expect(stopButton).not.toBeNull();
  50. });
  51. it("should not start generation without valid URL and password", async () => {
  52. const startButton = window.document.getElementById("startButton");
  53. const webhookURLInput = window.document.getElementById("webhookURL");
  54. const authPasswordInput = window.document.getElementById("authPassword");
  55. // Leave the URL and password empty
  56. webhookURLInput.value = "";
  57. authPasswordInput.value = "";
  58. startButton.click();
  59. expect(startButton.style.display).not.toBe("none");
  60. });
  61. it("should enable the stop button when generation starts", async () => {
  62. const startButton = window.document.getElementById("startButton");
  63. const stopButton = window.document.getElementById("stopButton");
  64. const webhookURLInput = window.document.getElementById("webhookURL");
  65. const authPasswordInput = window.document.getElementById("authPassword");
  66. webhookURLInput.value = "http://localhost";
  67. authPasswordInput.value = "password";
  68. startButton.click();
  69. await vi.waitFor(() => !stopButton.disabled);
  70. expect(stopButton.disabled).toBe(false);
  71. });
  72. it("should populate JSON schema based on selected use case", async () => {
  73. const jsonSchemaTextarea = window.document.getElementById("jsonSchema");
  74. const useCaseSelect = window.document.getElementById("useCaseSelect");
  75. // Select a use case
  76. useCaseSelect.value = "sensorData";
  77. useCaseSelect.dispatchEvent(new window.Event("change"));
  78. expect(jsonSchemaTextarea.value.trim()).not.toBe("");
  79. });
  80. it("should clear logs when the stop button is clicked", async () => {
  81. const stopButton = window.document.getElementById("stopButton");
  82. const logOutputDiv = window.document.getElementById("logOutput");
  83. logOutputDiv.innerHTML = "<p>Log message</p>";
  84. stopButton.click();
  85. expect(logOutputDiv.innerHTML).toBe(logOutputDiv.innerHTML);
  86. });
  87. it("should remove the error message if the JSON schema is corrected", async () => {
  88. const jsonSchemaTextarea = window.document.getElementById("jsonSchema");
  89. const jsonErrorDiv = window.document.getElementById("jsonError");
  90. // Input invalid JSON and then correct it
  91. jsonSchemaTextarea.value = "invalid json";
  92. jsonSchemaTextarea.dispatchEvent(new window.Event("blur"));
  93. await vi.waitFor(() => jsonErrorDiv.style.display === "block");
  94. jsonSchemaTextarea.value = '{"valid": "json"}';
  95. jsonSchemaTextarea.dispatchEvent(new window.Event("blur"));
  96. await vi.waitFor(() => jsonErrorDiv.style.display === "none");
  97. expect(jsonErrorDiv.style.display).toBe("none");
  98. });
  99. it("should have empty webhook URL and auth password inputs initially", async () => {
  100. const webhookURLInput = window.document.getElementById("webhookURL");
  101. const authPasswordInput = window.document.getElementById("authPassword");
  102. await vi.waitFor(() => webhookURLInput && authPasswordInput);
  103. expect(webhookURLInput.value).toBe("");
  104. expect(authPasswordInput.value).toBe("");
  105. });
  106. it("should not start generation with empty webhook URL and auth password", async () => {
  107. const startButton = window.document.getElementById("startButton");
  108. const webhookURLInput = window.document.getElementById("webhookURL");
  109. const authPasswordInput = window.document.getElementById("authPassword");
  110. await vi.waitFor(() => startButton && webhookURLInput && authPasswordInput);
  111. webhookURLInput.value = "";
  112. authPasswordInput.value = "";
  113. startButton.click();
  114. await vi.waitFor(() => isGenerating);
  115. expect(isGenerating).toBe(false);
  116. });
  117. it("should stop data generation and enable start button when stop button is clicked", async () => {
  118. const startButton = window.document.getElementById("startButton");
  119. const stopButton = window.document.getElementById("stopButton");
  120. // Trigger generation
  121. startButton.click();
  122. await vi.waitFor(() => isGenerating);
  123. stopButton.click();
  124. await vi.waitFor(() => !isGenerating);
  125. expect(isGenerating).toBe(false);
  126. expect(startButton.style.display).not.toBe("none");
  127. });
  128. });