import { i as normalizeLowercaseStringOrEmpty } from "./string-coerce-CIT1-v_a.js";
import { t as isTruthyEnvValue } from "./env-CStj6mGv.js";
import { a as createPinnedLookup, d as resolvePinnedHostnameWithPolicy, f as ssrf_exports, u as resolvePinnedHostname } from "./ssrf-DgtdLyJJ.js";
import { a as beforeEach, n as vi, r as afterEach } from "./test.p_J6dB8a-Dyq-JxSD.js";
import { t as withFetchPreconnect } from "./fetch-mock-EAzg-1iq.js";
import { n as cleanupSessionStateForTest, r as captureEnv } from "./temp-home-B6M9TKRF.js";
import path from "node:path";
import os from "node:os";
import fs from "node:fs/promises";
//#region src/media-understanding/audio.test-helpers.ts
function resolveRequestUrl(input) {
	if (typeof input === "string") return input;
	if (input instanceof URL) return input.toString();
	return input.url;
}
function installPinnedHostnameTestHooks() {
	const resolvePinnedHostname$1 = resolvePinnedHostname;
	const resolvePinnedHostnameWithPolicy$1 = resolvePinnedHostnameWithPolicy;
	const lookupMock = vi.fn();
	let resolvePinnedHostnameSpy = null;
	let resolvePinnedHostnameWithPolicySpy = null;
	beforeEach(() => {
		lookupMock.mockResolvedValue([{
			address: "93.184.216.34",
			family: 4
		}]);
		resolvePinnedHostnameSpy = vi.spyOn(ssrf_exports, "resolvePinnedHostname").mockImplementation((hostname) => resolvePinnedHostname$1(hostname, lookupMock));
		resolvePinnedHostnameWithPolicySpy = vi.spyOn(ssrf_exports, "resolvePinnedHostnameWithPolicy").mockImplementation((hostname, params) => resolvePinnedHostnameWithPolicy$1(hostname, {
			...params,
			lookupFn: lookupMock
		}));
	});
	afterEach(() => {
		lookupMock.mockReset();
		resolvePinnedHostnameSpy?.mockRestore();
		resolvePinnedHostnameWithPolicySpy?.mockRestore();
		resolvePinnedHostnameSpy = null;
		resolvePinnedHostnameWithPolicySpy = null;
	});
}
function createAuthCaptureJsonFetch(responseBody) {
	let seenAuth = null;
	return {
		fetchFn: withFetchPreconnect(async (_input, init) => {
			seenAuth = new Headers(init?.headers).get("authorization");
			return new Response(JSON.stringify(responseBody), {
				status: 200,
				headers: { "content-type": "application/json" }
			});
		}),
		getAuthHeader: () => seenAuth
	};
}
function createRequestCaptureJsonFetch(responseBody) {
	let seenUrl = null;
	let seenInit;
	return {
		fetchFn: withFetchPreconnect(async (input, init) => {
			seenUrl = resolveRequestUrl(input);
			seenInit = init;
			return new Response(JSON.stringify(responseBody), {
				status: 200,
				headers: { "content-type": "application/json" }
			});
		}),
		getRequest: () => ({
			url: seenUrl,
			init: seenInit
		})
	};
}
//#endregion
//#region src/agents/live-test-helpers.ts
const LIVE_OK_PROMPT = "Reply with the word ok.";
function isLiveTestEnabled(extraEnvVars = [], env = process.env) {
	return [
		...extraEnvVars,
		"LIVE",
		"KAIJIBOT_LIVE_TEST"
	].some((name) => isTruthyEnvValue(env[name]));
}
function createSingleUserPromptMessage(content = LIVE_OK_PROMPT) {
	return [{
		role: "user",
		content,
		timestamp: Date.now()
	}];
}
function extractNonEmptyAssistantText(content) {
	return content.filter((block) => block.type === "text").map((block) => block.text?.trim() ?? "").filter(Boolean).join(" ");
}
//#endregion
//#region src/test-helpers/http.ts
function jsonResponse(body, status = 200) {
	return new Response(JSON.stringify(body), {
		status,
		headers: { "Content-Type": "application/json" }
	});
}
function requestUrl(input) {
	if (typeof input === "string") return input;
	if (input instanceof URL) return input.toString();
	return input.url;
}
function requestBodyText(body) {
	return typeof body === "string" ? body : "{}";
}
//#endregion
//#region src/test-helpers/ssrf.ts
function mockPinnedHostnameResolution(addresses = ["93.184.216.34"]) {
	return vi.spyOn(ssrf_exports, "resolvePinnedHostname").mockImplementation(async (hostname) => {
		const normalized = normalizeLowercaseStringOrEmpty(hostname).replace(/\.$/, "");
		const pinnedAddresses = [...addresses];
		return {
			hostname: normalized,
			addresses: pinnedAddresses,
			lookup: createPinnedLookup({
				hostname: normalized,
				addresses: pinnedAddresses
			})
		};
	});
}
//#endregion
//#region src/test-helpers/state-dir-env.ts
function snapshotStateDirEnv() {
	return captureEnv(["KAIJIBOT_STATE_DIR"]);
}
function restoreStateDirEnv(snapshot) {
	snapshot.restore();
}
function setStateDirEnv(stateDir) {
	process.env.KAIJIBOT_STATE_DIR = stateDir;
}
async function withStateDirEnv(prefix, fn) {
	const snapshot = snapshotStateDirEnv();
	const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), prefix));
	const stateDir = path.join(tempRoot, "state");
	await fs.mkdir(stateDir, { recursive: true });
	setStateDirEnv(stateDir);
	try {
		return await fn({
			tempRoot,
			stateDir
		});
	} finally {
		await cleanupSessionStateForTest().catch(() => void 0);
		restoreStateDirEnv(snapshot);
		await fs.rm(tempRoot, {
			recursive: true,
			force: true
		});
	}
}
//#endregion
export { requestUrl as a, isLiveTestEnabled as c, installPinnedHostnameTestHooks as d, requestBodyText as i, createAuthCaptureJsonFetch as l, mockPinnedHostnameResolution as n, createSingleUserPromptMessage as o, jsonResponse as r, extractNonEmptyAssistantText as s, withStateDirEnv as t, createRequestCaptureJsonFetch as u };
