
      import { Temporal } from "@js-temporal/polyfill";
      import { URLPattern } from "urlpattern-polyfill";
      globalThis.addEventListener = () => {};
    
import { lookupWebFinger } from "../type-BSR-vRXo.js";
import { assertEquals } from "../assert_equals-DSbWqCm3.js";
import "../assert-MZs1qjMx.js";
import "../assert_instance_of-DHz7EHNU.js";
import "../lookup-q8DeFAjR.js";
import { test } from "../testing-Cqwzq9nj.js";
import "../std__assert-X-_kMxKM.js";
import "../assert_rejects-DiIiJbZn.js";
import "../assert_is_error-BPGph1Jx.js";
import "../assert_not_equals-f3m3epl3.js";
import "../assert_throws-BOO88avQ.js";
import { esm_default } from "../esm-CIFbziGC.js";
import { withTimeout } from "es-toolkit";

//#region src/webfinger/lookup.test.ts
test({
	name: "lookupWebFinger()",
	sanitizeOps: false,
	sanitizeResources: false,
	async fn(t) {
		await t.step("invalid resource", async () => {
			assertEquals(await lookupWebFinger("acct:johndoe"), null);
			assertEquals(await lookupWebFinger(new URL("acct:johndoe")), null);
			assertEquals(await lookupWebFinger("acct:johndoe@"), null);
			assertEquals(await lookupWebFinger(new URL("acct:johndoe@")), null);
		});
		await t.step("connection refused", async () => {
			assertEquals(await lookupWebFinger("acct:johndoe@fedify-test.internal"), null);
			assertEquals(await lookupWebFinger("https://fedify-test.internal/foo"), null);
		});
		esm_default.spyGlobal();
		esm_default.get("begin:https://example.com/.well-known/webfinger?", { status: 404 });
		await t.step("not found", async () => {
			assertEquals(await lookupWebFinger("acct:johndoe@example.com"), null);
			assertEquals(await lookupWebFinger("https://example.com/foo"), null);
		});
		const expected = {
			subject: "acct:johndoe@example.com",
			links: []
		};
		esm_default.removeRoutes();
		esm_default.get("https://example.com/.well-known/webfinger?resource=acct%3Ajohndoe%40example.com", { body: expected });
		await t.step("acct", async () => {
			assertEquals(await lookupWebFinger("acct:johndoe@example.com"), expected);
		});
		const expected2 = {
			subject: "https://example.com/foo",
			links: []
		};
		esm_default.removeRoutes();
		esm_default.get("https://example.com/.well-known/webfinger?resource=https%3A%2F%2Fexample.com%2Ffoo", { body: expected2 });
		await t.step("https", async () => {
			assertEquals(await lookupWebFinger("https://example.com/foo"), expected2);
		});
		esm_default.removeRoutes();
		esm_default.get("begin:https://example.com/.well-known/webfinger?", { body: "not json" });
		await t.step("invalid response", async () => {
			assertEquals(await lookupWebFinger("acct:johndoe@example.com"), null);
		});
		esm_default.removeRoutes();
		esm_default.get("begin:https://localhost/.well-known/webfinger?", {
			subject: "acct:test@localhost",
			links: [{
				rel: "self",
				type: "application/activity+json",
				href: "https://localhost/actor"
			}]
		});
		await t.step("private address", async () => {
			assertEquals(await lookupWebFinger("acct:test@localhost"), null);
			assertEquals(await lookupWebFinger("acct:test@localhost", { allowPrivateAddress: true }), {
				subject: "acct:test@localhost",
				links: [{
					rel: "self",
					type: "application/activity+json",
					href: "https://localhost/actor"
				}]
			});
		});
		esm_default.removeRoutes();
		esm_default.get("begin:https://example.com/.well-known/webfinger?", {
			status: 302,
			headers: { Location: "/.well-known/webfinger2" }
		});
		esm_default.get("begin:https://example.com/.well-known/webfinger2", { body: expected });
		await t.step("redirection", async () => {
			assertEquals(await lookupWebFinger("acct:johndoe@example.com"), expected);
		});
		esm_default.removeRoutes();
		esm_default.get("begin:https://example.com/.well-known/webfinger?", {
			status: 302,
			headers: { Location: "/.well-known/webfinger" }
		});
		await t.step("infinite redirection", async () => {
			const result = await withTimeout(() => lookupWebFinger("acct:johndoe@example.com"), 2e3);
			assertEquals(result, null);
		});
		esm_default.removeRoutes();
		esm_default.get("begin:https://example.com/.well-known/webfinger?", {
			status: 302,
			headers: { Location: "ftp://example.com/" }
		});
		await t.step("redirection to different protocol", async () => {
			assertEquals(await lookupWebFinger("acct:johndoe@example.com"), null);
		});
		esm_default.removeRoutes();
		esm_default.get("begin:https://example.com/.well-known/webfinger?", {
			status: 302,
			headers: { Location: "https://localhost/" }
		});
		await t.step("redirection to private address", async () => {
			assertEquals(await lookupWebFinger("acct:johndoe@example.com"), null);
		});
		esm_default.removeRoutes();
		let redirectCount = 0;
		esm_default.get("begin:https://example.com/.well-known/webfinger", () => {
			redirectCount++;
			if (redirectCount < 3) return {
				status: 302,
				headers: { Location: `/.well-known/webfinger?redirect=${redirectCount}` }
			};
			return { body: expected };
		});
		await t.step("custom maxRedirection", async () => {
			redirectCount = 0;
			assertEquals(await lookupWebFinger("acct:johndoe@example.com", { maxRedirection: 2 }), null);
			redirectCount = 0;
			assertEquals(await lookupWebFinger("acct:johndoe@example.com", { maxRedirection: 3 }), expected);
			redirectCount = 0;
			assertEquals(await lookupWebFinger("acct:johndoe@example.com"), expected);
		});
		esm_default.removeRoutes();
		esm_default.get("begin:https://example.com/.well-known/webfinger?", () => new Promise((resolve) => {
			const timeoutId = setTimeout(() => {
				resolve({ body: expected });
			}, 1e3);
			return () => clearTimeout(timeoutId);
		}));
		await t.step("request cancellation", async () => {
			const controller = new AbortController();
			const promise = lookupWebFinger("acct:johndoe@example.com", { signal: controller.signal });
			controller.abort();
			assertEquals(await promise, null);
		});
		esm_default.removeRoutes();
		let redirectCount2 = 0;
		esm_default.get("begin:https://example.com/.well-known/webfinger", () => {
			redirectCount2++;
			if (redirectCount2 === 1) return {
				status: 302,
				headers: { Location: "/.well-known/webfinger2" }
			};
			return new Promise((resolve) => {
				const timeoutId = setTimeout(() => {
					resolve({ body: expected });
				}, 1e3);
				return () => clearTimeout(timeoutId);
			});
		});
		await t.step("cancellation during redirection", async () => {
			const controller = new AbortController();
			const promise = lookupWebFinger("acct:johndoe@example.com", { signal: controller.signal });
			setTimeout(() => controller.abort(), 100);
			assertEquals(await promise, null);
		});
		esm_default.removeRoutes();
		esm_default.get("begin:https://example.com/.well-known/webfinger?", () => new Promise((resolve) => {
			const timeoutId = setTimeout(() => {
				resolve({ body: expected });
			}, 500);
			return () => clearTimeout(timeoutId);
		}));
		await t.step("cancellation with immediate abort", async () => {
			const controller = new AbortController();
			controller.abort();
			const result = await lookupWebFinger("acct:johndoe@example.com", { signal: controller.signal });
			assertEquals(result, null);
		});
		esm_default.removeRoutes();
		esm_default.get("begin:https://example.com/.well-known/webfinger?", { body: expected });
		await t.step("successful request with signal", async () => {
			const controller = new AbortController();
			const result = await lookupWebFinger("acct:johndoe@example.com", { signal: controller.signal });
			assertEquals(result, expected);
		});
		esm_default.hardReset();
	}
});

//#endregion