import {
  createFileSystemOrMemoryDriver
} from "../chunk-T4PQK4WA.js";
import {
  createClient
} from "../chunk-AWJU3AZB.js";
import "../chunk-VBU4P2IX.js";
import {
  buildManagerRouter
} from "../chunk-MICYEINX.js";
import "../chunk-LXUQ667X.js";
import "../chunk-XQWBEBLD.js";
import {
  getLogger
} from "../chunk-IZ4M5TJU.js";
import "../chunk-7HTNH26M.js";
import "../chunk-424PT5DM.js";

// src/test/mod.ts
import { serve as honoServe } from "@hono/node-server";
import { createNodeWebSocket } from "@hono/node-ws";
import invariant from "invariant";

// src/test/log.ts
function logger() {
  return getLogger("test");
}

// src/test/mod.ts
async function setupTest(c, registry) {
  var _a;
  registry.config.test = { ...registry.config.test, enabled: true };
  const driver = createFileSystemOrMemoryDriver(
    true,
    { path: `/tmp/rivetkit-test-${crypto.randomUUID()}` }
  );
  let upgradeWebSocket;
  registry.config.driver = driver;
  registry.config.inspector = {
    enabled: true,
    token: () => "token"
  };
  const parsedConfig = registry.parseConfig();
  const managerDriver = (_a = driver.manager) == null ? void 0 : _a.call(driver, parsedConfig);
  invariant(managerDriver, "missing manager driver");
  const getUpgradeWebSocket = () => upgradeWebSocket;
  managerDriver.setGetUpgradeWebSocket(getUpgradeWebSocket);
  const { router } = buildManagerRouter(
    parsedConfig,
    managerDriver,
    getUpgradeWebSocket
  );
  const nodeWebSocket = createNodeWebSocket({ app: router });
  upgradeWebSocket = nodeWebSocket.upgradeWebSocket;
  const server = honoServe({
    fetch: router.fetch,
    hostname: "127.0.0.1",
    port: 0
  });
  if (!server.listening) {
    await new Promise((resolve) => {
      server.once("listening", () => resolve());
    });
  }
  invariant(
    nodeWebSocket.injectWebSocket !== void 0,
    "should have injectWebSocket"
  );
  nodeWebSocket.injectWebSocket(server);
  const address = server.address();
  invariant(address && typeof address !== "string", "missing server address");
  const port = address.port;
  const endpoint = `http://127.0.0.1:${port}`;
  logger().info({ msg: "test server listening", port });
  c.onTestFinished(async () => {
    await new Promise((resolve) => server.close(() => resolve(void 0)));
  });
  const client = createClient({
    endpoint,
    namespace: "default",
    runnerName: "default",
    disableMetadataLookup: true
  });
  c.onTestFinished(async () => await client.dispose());
  return { client };
}
export {
  setupTest
};
//# sourceMappingURL=mod.js.map