All files / tests/utils integration-helpers.ts

80% Statements 12/15
100% Branches 4/4
66.66% Functions 4/6
85.71% Lines 12/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70                              8x   8x             8x   5x                 1x 1x                 1x       1x 1x         1x               6x         6x  
/**
 * Integration Test Utilities
 *
 * Helper functions for integration tests that require a running server.
 */
 
import http from "http";
import * as childProcess from "child_process";
 
/**
 * Synchronously check if the server is running.
 * Uses curl or a quick TCP check to avoid async issues with Jest's describe().
 * Returns true if server responds, false otherwise.
 */
export function isServerRunningSync(): boolean {
  try {
    // Use curl with a very short timeout to check server availability
    childProcess.execSync(
      "curl -s --max-time 1 http://localhost:3000/api/sketches > /dev/null 2>&1",
      {
        timeout: 2000,
        stdio: "pipe",
      },
    );
    return true;
  } catch {
    return false;
  }
}
 
/**
 * Async check if the server is running by making a simple HTTP request.
 * Returns true if server responds, false otherwise.
 */
export async function isServerRunning(): Promise<boolean> {
  return new Promise((resolve) => {
    const req = http.request(
      {
        hostname: "localhost",
        port: 3000,
        path: "/api/sketches",
        method: "GET",
        timeout: 1000,
      },
      (res) => {
        resolve(res.statusCode !== undefined && res.statusCode < 500);
      },
    );
 
    req.on("error", () => resolve(false));
    req.on("timeout", () => {
      req.destroy();
      resolve(false);
    });
 
    req.end();
  });
}
 
/**
 * Cached sync server status (evaluated once at module load).
 * This is safe to use at the module level for describe.skip logic.
 */
export const SERVER_AVAILABLE = isServerRunningSync();
 
/**
 * Helper to create conditional describe - skips if server not available.
 */
export const describeIfServer = SERVER_AVAILABLE ? describe : describe.skip;