view seobeo/tests/seobeo_web_server_test.c @ 279:b3b547563ec7

Add Google connector service and agent wiki Implement the C/Seobeo Google Drive and Gmail connector with encrypted OAuth storage, Zenbu authentication, browser testing, AI tool discovery, chunked HTTP decoding, and Bazel coverage. Consolidate repository guidance into progressive wiki documentation and enforce arena-first allocation for new first-party C code. Co-authored-by: Copilot <[email protected]> Copilot-Session: 84c338fd-0939-4bb3-b7f3-1062eb213e5d
author MrJuneJune <me@mrjunejune.com>
date Mon, 17 Aug 2026 22:22:36 -0700
parents 7eb79fd91c7e
children
line wrap: on
line source

#include "seobeo/seobeo.h"

pid_t start_test_server(const char *server_binary)
{
  pid_t server_pid = fork();

  if (server_pid < 0)
  {
    perror("fork");
    return -1;
  }

  if (server_pid == 0)
  {
    printf("Starting server on port 8080...\n");
    execl(server_binary, server_binary, NULL);
    perror("execl failed");
    exit(1);
  }

  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 immediately with code: %d\n", WEXITSTATUS(status));
    }
    else if (WIFSIGNALED(status))
    {
      fprintf(stderr, "Server was killed by signal: %d\n", WTERMSIG(status));
    }
    return -1;
  }

  sleep(2);
  printf("Server ready\n\n");

  return server_pid;
}

void Test_Echo()
{
  printf("\n=== Test: Multiple Messages ===\n");

  Seobeo_WebSocket *p_ws = Seobeo_WebSocket_Connect("ws://127.0.0.1:8080/echo");
  if (!p_ws)
  {
    printf("Failed to connect\n");
    return;
  }

  const char *messages[] = {
    "Message 1",
    "Message 2",
    "Message 3"
  };

  for (int i = 0; i < 3; i++)
  {
    printf("Sending: %s\n", messages[i]);
    Seobeo_WebSocket_Send_Text(p_ws, messages[i]);
    usleep(100000);
  }

  printf("Receiving responses...\n");
  int received = 0;
  int attempts = 0;

  while (received < 3 && attempts < 200)
  {
    Seobeo_WebSocket_Message *p_msg = Seobeo_WebSocket_Receive(p_ws);
    if (p_msg)
    {
      if (p_msg->opcode == SEOBEO_WS_OPCODE_TEXT)
      {
        printf("Response %d: %.*s\n", received + 1, (int)p_msg->length, (char*)p_msg->data);
        received++;
      }
      Seobeo_WebSocket_Message_Destroy(p_msg);
    }

    usleep(10000);
    attempts++;
  }
  printf("Received %d/%d messages\n", received, 3);
  Seobeo_WebSocket_Destroy(p_ws);
}

void Test_Chat()
{
  printf("\n=== Test: Multiple Messages ===\n");

  Seobeo_WebSocket *p_ws = Seobeo_WebSocket_Connect("ws://127.0.0.1:8080/chat");
  if (!p_ws)
  {
    printf("Failed to connect\n");
    return;
  }

  const char *messages[] = {
    "Message 1",
    "Message 2",
    "Message 3"
  };

  for (int i = 0; i < 3; i++)
  {
    printf("Sending: %s\n", messages[i]);
    Seobeo_WebSocket_Send_Text(p_ws, messages[i]);
    usleep(100000);
  }

  printf("Receiving responses...\n");
  int received = 0;
  int attempts = 0;

  while (received < 3 && attempts < 200)
  {
    Seobeo_WebSocket_Message *p_msg = Seobeo_WebSocket_Receive(p_ws);
    if (p_msg)
    {
      if (p_msg->opcode == SEOBEO_WS_OPCODE_TEXT)
      {
        printf("Response %d: %.*s\n", received + 1, (int)p_msg->length, (char*)p_msg->data);
        received++;
      }
      Seobeo_WebSocket_Message_Destroy(p_msg);
    }

    usleep(10000);
    attempts++;
  }
  printf("Received %d/%d messages\n", received, 3);
  Seobeo_WebSocket_Destroy(p_ws);
}

void stop_test_server(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[])
{
  const char *server_binary = "./websocket_server_example";
  if (argc > 1)
  {
    server_binary = argv[1];
  }

  pid_t server_pid = start_test_server(server_binary);

  Test_Echo();
  Test_Chat();

  stop_test_server(server_pid);
}