import { t as debug_default } from "./debug-DrrWgeSu.js";
import { t as main_exports } from "./main-BOydGWpm.js";
import { n as Encryption } from "./main-DsUS7ncp.js";
import { IncomingMessage, ServerResponse, createServer } from "node:http";
import { Socket } from "node:net";
import Macroable from "@poppinss/macroable";
//#region src/test_utils/http.ts
/**
* Http server utils are used to start the AdonisJS HTTP server
* during testing. It provides methods to start and stop the server
* for integration testing.
*
* @example
* const testUtils = new TestUtils(app)
* const httpUtils = testUtils.httpServer()
*
* const closeServer = await httpUtils.start()
* // Make HTTP requests to test endpoints
* await closeServer() // Clean up
*/
var HttpServerUtils = class {
	/**
	* Reference to the test utils instance
	*/
	#utils;
	/**
	* Creates a new HttpServerUtils instance
	*
	* @param utils - The test utils instance
	*/
	constructor(utils) {
		this.#utils = utils;
	}
	/**
	* Starts the http server a given host and port
	*
	* @param nodeHttpServer - The Node.js HTTP server instance
	*/
	#listen(nodeHttpServer) {
		return new Promise((resolve, reject) => {
			const host = process.env.HOST || "0.0.0.0";
			const port = Number(process.env.PORT || "3333");
			nodeHttpServer.listen(port, host);
			nodeHttpServer.once("listening", () => {
				debug_default("listening to utils http server, host :%s, port: %s", host, port);
				resolve({
					port,
					host
				});
			});
			nodeHttpServer.once("error", (error) => {
				reject(error);
			});
		});
	}
	/**
	* Testing hook to start the HTTP server to listen for new request.
	* The return value is a function to close the HTTP server.
	*
	* @param serverCallback - Optional callback to create custom HTTP server instance
	*/
	async start(serverCallback) {
		const createHTTPServer = serverCallback || createServer;
		const server = await this.#utils.app.container.make("server");
		await server.boot();
		const httpServer = createHTTPServer(server.handle.bind(server));
		server.setNodeServer(httpServer);
		await this.#listen(httpServer);
		return () => {
			return new Promise((resolve, reject) => {
				httpServer.close((error) => {
					if (error) reject(error);
					else resolve();
				});
			});
		};
	}
};
//#endregion
//#region src/test_utils/main.ts
/**
* Test utils has a collection of helper methods to make testing
* experience great for AdonisJS applications. It provides utilities
* for HTTP testing, context creation, and cookie handling.
*
* @example
* const testUtils = new TestUtils(app)
* await testUtils.boot()
*
* const ctx = await testUtils.createHttpContext()
* const httpUtils = testUtils.httpServer()
*/
var TestUtils = class extends Macroable {
	app;
	/**
	* Flag to track if test utils have been booted
	*/
	#booted = false;
	/**
	* Check if utils have been booted
	*/
	get isBooted() {
		return this.#booted;
	}
	/**
	* Creates a new TestUtils instance
	*
	* @param app - The application service instance
	*/
	constructor(app) {
		super();
		this.app = app;
	}
	/**
	* Boot test utils. It requires the app to be booted
	* and container to have all the bindings
	*/
	async boot() {
		if (!this.isBooted) {
			this.#booted = true;
			this.cookies = new main_exports.CookieClient(await this.app.container.make(Encryption));
		}
	}
	/**
	* Returns an instance of the HTTP server testing
	* utils
	*/
	httpServer() {
		return new HttpServerUtils(this);
	}
	/**
	* Create an instance of HTTP context for testing
	*
	* @param options - Options for creating HTTP context with custom req/res objects
	*/
	async createHttpContext(options = {}) {
		const req = options.req || new IncomingMessage(new Socket());
		const res = options.res || new ServerResponse(req);
		const server = await this.app.container.make("server");
		const request = server.createRequest(req, res);
		const response = server.createResponse(req, res);
		return server.createHttpContext(request, response, this.app.container.createResolver());
	}
};
//#endregion
export { TestUtils as t };
