diff mrjunejune/test/create_snapshots.c @ 67:6626ec933933

[Seobeo] Separated out Client Server logic. Created test tools.
author June Park <parkjune1995@gmail.com>
date Wed, 24 Dec 2025 09:15:55 -0800
parents
children 092afa595764
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mrjunejune/test/create_snapshots.c	Wed Dec 24 09:15:55 2025 -0800
@@ -0,0 +1,173 @@
+#include "seobeo/seobeo.h"
+#include "seobeo/snapshot_creator.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/wait.h>
+#include <signal.h>
+
+#define TEST_PORT "6969"
+#define TEST_HOST "127.0.0.1"
+#define SNAPSHOT_DIR "mrjunejune/test/snapshots"
+
+// Start the server process
+static pid_t start_server(const char *server_binary)
+{
+  pid_t server_pid = fork();
+
+  if (server_pid < 0)
+  {
+    perror("fork");
+    return -1;
+  }
+
+  if (server_pid == 0)
+  {
+    // Child process - run server
+    printf("Starting server on port %s...\n", TEST_PORT);
+    execl(server_binary, server_binary, NULL);
+    perror("execl failed");
+    exit(1);
+  }
+
+  // Parent - verify server started
+  printf("Server started (PID: %d)\n", server_pid);
+
+  usleep(100000);
+  int status;
+  pid_t result = waitpid(server_pid, &status, WNOHANG);
+  if (result != 0)
+  {
+    if (WIFEXITED(status))
+    {
+      fprintf(stderr, "Server exited with code: %d\n", WEXITSTATUS(status));
+    }
+    else if (WIFSIGNALED(status))
+    {
+      fprintf(stderr, "Server killed by signal: %d\n", WTERMSIG(status));
+    }
+    return -1;
+  }
+
+  sleep(2);
+  printf("Server ready\n\n");
+
+  return server_pid;
+}
+
+// Stop the server process
+static void stop_server_test(pid_t server_pid)
+{
+  if (server_pid > 0)
+  {
+    printf("\nStopping server (PID: %d)...\n", server_pid);
+    kill(server_pid, SIGTERM);
+    waitpid(server_pid, NULL, 0);
+    printf("Server stopped\n");
+  }
+}
+
+int main(int argc, char *argv[])
+{
+  printf("=== Seobeo Snapshot Creator ===\n\n");
+
+  // Get workspace directory (where the source files are)
+  const char *workspace_dir = getenv("BUILD_WORKSPACE_DIRECTORY");
+  if (!workspace_dir)
+  {
+    fprintf(stderr, "Error: BUILD_WORKSPACE_DIRECTORY not set\n");
+    fprintf(stderr, "This binary must be run with 'bazel run'\n");
+    return 1;
+  }
+
+  // Construct full path to snapshot directory
+  char snapshot_path[1024];
+  snprintf(snapshot_path, sizeof(snapshot_path), "%s/%s", workspace_dir, SNAPSHOT_DIR);
+
+  // Get server binary path
+  const char *server_binary = "./mrjunejune_server";
+  if (argc > 1)
+  {
+    server_binary = argv[1];
+  }
+
+  printf("Workspace: %s\n", workspace_dir);
+  printf("Server binary: %s\n", server_binary);
+  printf("Snapshot directory: %s\n\n", snapshot_path);
+
+  // Start server
+  pid_t server_pid = start_server(server_binary);
+  if (server_pid < 0)
+  {
+    fprintf(stderr, "Failed to start server\n");
+    return 1;
+  }
+
+  // Define snapshots to create - paths that should succeed (200 OK)
+  SnapshotConfig success_snapshots[] = {
+    {"/", 200, snapshot_path, TEST_HOST, TEST_PORT},
+    {"/index.html", 200, snapshot_path, TEST_HOST, TEST_PORT},
+  };
+  int num_success = sizeof(success_snapshots) / sizeof(success_snapshots[0]);
+
+  // Define snapshots for error paths (404)
+  SnapshotConfig error_snapshots[] = {
+    {"/nonexistent", 404, snapshot_path, TEST_HOST, TEST_PORT},
+    {"/does/not/exist", 404, snapshot_path, TEST_HOST, TEST_PORT},
+    {"/missing.html", 404, snapshot_path, TEST_HOST, TEST_PORT},
+  };
+  int num_errors = sizeof(error_snapshots) / sizeof(error_snapshots[0]);
+
+  int total_failed = 0;
+  int total_passed = 0;
+
+  // Create success snapshots
+  printf("Creating snapshots for successful paths:\n\n");
+  for (int i = 0; i < num_success; i++)
+  {
+    if (Seobeo_Snapshot_Create(&success_snapshots[i]) == 0)
+    {
+      total_passed++;
+    }
+    else
+    {
+      total_failed++;
+    }
+  }
+
+  // Create error snapshots
+  printf("Creating snapshots for error paths:\n\n");
+  for (int i = 0; i < num_errors; i++)
+  {
+    if (Seobeo_Snapshot_Create(&error_snapshots[i]) == 0)
+    {
+      total_passed++;
+    }
+    else
+    {
+      total_failed++;
+    }
+  }
+
+  // Stop server
+  stop_server_test(server_pid);
+
+  // Print summary
+  printf("\n=== Summary ===\n");
+  printf("Snapshots created: %d\n", total_passed);
+  printf("Failed: %d\n", total_failed);
+
+  if (total_failed == 0)
+  {
+    printf("\n✓ All snapshots created successfully!\n");
+    printf("\nSnapshots saved to: %s/\n", snapshot_path);
+    printf("\nRun tests to verify:\n");
+    printf("  bazel test //mrjunejune:integration_test\n");
+    return 0;
+  }
+  else
+  {
+    printf("\n✗ Some snapshots failed\n");
+    return 1;
+  }
+}