api.spec.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import { expect, test } from "@playwright/test";
  2. import { del, get, post, put } from "../src/lib/api";
  3. test.describe("API E2E Tests", () => {
  4. test("should make GET request to health endpoint", async () => {
  5. const response = await get("/health");
  6. expect(response).toHaveProperty("status", "healthy");
  7. });
  8. test.describe("Settings CRUD Operations", () => {
  9. test("should GET all settings", async () => {
  10. const response = await get("/config/settings");
  11. expect(Array.isArray(response) || typeof response === "object").toBe(
  12. true
  13. );
  14. });
  15. test("should POST new settings", async () => {
  16. const testSettings = {
  17. e2eTestKey: "e2eTestValue",
  18. timestamp: Date.now()
  19. };
  20. const response = await post("/config/settings", testSettings);
  21. expect(response).toBe(true); // API returns boolean true on success
  22. });
  23. test("should GET specific setting by key", async () => {
  24. // First create a setting
  25. const testKey = "e2eTestKey";
  26. const testValue = "e2eTestValue";
  27. await post("/config/settings", { [testKey]: testValue });
  28. // Then get it by key - API returns the value directly as plain text
  29. const response = await fetch(
  30. `http://localhost:3001/config/settings/${testKey}`
  31. );
  32. const text = await response.text();
  33. expect(text).toBe(testValue); // API returns the raw string value
  34. });
  35. test("should DELETE specific setting", async () => {
  36. // First create a setting
  37. const testKey = "e2eDeleteKey";
  38. await post("/config/settings", { [testKey]: "valueToDelete" });
  39. // Then delete it
  40. const response = await del(`/config/settings/${testKey}`);
  41. expect(response).toBeDefined();
  42. });
  43. });
  44. test.describe("Tasks CRUD Operations", () => {
  45. let createdTaskId: string;
  46. test("should GET all tasks", async () => {
  47. const response = await get("/tasks");
  48. expect(Array.isArray(response)).toBe(true);
  49. });
  50. test("should POST new task", async () => {
  51. const testTask = {
  52. name: "E2E Test Task",
  53. description: "Created by e2e test",
  54. status: "pending",
  55. type: "settings", // Required field
  56. createdAt: new Date().toISOString()
  57. };
  58. const response = await post("/tasks", testTask);
  59. expect(response).toHaveProperty("id");
  60. expect(response.name).toBe(testTask.name);
  61. createdTaskId = response.id;
  62. });
  63. test("should PUT update task", async () => {
  64. // Skip if no task was created
  65. if (!createdTaskId) {
  66. console.log("Skipping PUT test - no task ID available");
  67. return;
  68. }
  69. const updateData = {
  70. status: "completed"
  71. };
  72. const response = await put(`/tasks/${createdTaskId}`, updateData);
  73. expect(response).toHaveProperty("id", createdTaskId);
  74. expect(response.status).toBe(updateData.status);
  75. });
  76. test("should DELETE task", async () => {
  77. // Skip if no task was created
  78. if (!createdTaskId) {
  79. console.log("Skipping DELETE test - no task ID available");
  80. return;
  81. }
  82. const response = await del(`/tasks/${createdTaskId}`);
  83. expect(response).toBeDefined();
  84. });
  85. });
  86. test.describe("Files CRUD Operations", () => {
  87. test("should GET files list", async () => {
  88. const response = await get("/files");
  89. expect(Array.isArray(response) || typeof response === "object").toBe(
  90. true
  91. );
  92. });
  93. test("should GET file stats", async () => {
  94. const successfulStats = await get("/files/stats/successful");
  95. expect(
  96. typeof successfulStats === "number" ||
  97. typeof successfulStats === "object"
  98. ).toBe(true);
  99. const processedStats = await get("/files/stats/processed");
  100. expect(
  101. typeof processedStats === "number" || typeof processedStats === "object"
  102. ).toBe(true);
  103. });
  104. test("should POST new file", async () => {
  105. // Create a simple test file
  106. const testFile = {
  107. dataset: "e2e-test-dataset",
  108. filename: "test-file.txt",
  109. content: "This is test content for e2e testing"
  110. };
  111. try {
  112. const response = await post(
  113. `/files/${testFile.dataset}/${testFile.filename}`,
  114. {
  115. content: testFile.content
  116. }
  117. );
  118. expect(response).toBeDefined();
  119. } catch (_error) {
  120. // File creation might require specific format or authentication
  121. console.log(
  122. "File creation test skipped - endpoint may require specific setup"
  123. );
  124. }
  125. });
  126. test("should GET specific file", async () => {
  127. try {
  128. const response = await get("/files/e2e-test-dataset/test-file.txt");
  129. expect(response).toBeDefined();
  130. } catch (_error) {
  131. // File might not exist or endpoint might require different path
  132. console.log(
  133. "File retrieval test - file may not exist from previous test"
  134. );
  135. }
  136. });
  137. test("should DELETE file", async () => {
  138. try {
  139. const response = await del("/files/e2e-test-dataset/test-file.txt");
  140. expect(response).toBeDefined();
  141. } catch (_error) {
  142. // File might not exist
  143. console.log("File deletion test - file may not exist");
  144. }
  145. });
  146. });
  147. test.describe("Error Handling", () => {
  148. test("should handle 404 errors", async () => {
  149. await expect(get("/non-existent-endpoint")).rejects.toThrow();
  150. });
  151. test("should handle timeout correctly", async () => {
  152. // Test with a non-existent endpoint that returns 404
  153. await expect(
  154. get("/non-existent-endpoint-that-times-out")
  155. ).rejects.toThrow("Not Found");
  156. });
  157. });
  158. });