import { Temporal } from "@js-temporal/polyfill";
import "urlpattern-polyfill";
globalThis.addEventListener = () => {};
import { $ as UrlError } from "../vocab-C5EUmVTj.mjs";
import { a as mockDocumentLoader, t as test } from "../testing-BQeTO4ss.mjs";
import { t as assertEquals } from "../assert_equals-Ew3jOFa3.mjs";
import "../std__assert-2v7gYiZp.mjs";
import { t as assertRejects } from "../assert_rejects-CJC9ThS-.mjs";
import { l as verifyRequest } from "../http-B7ppDy7F.mjs";
import { t as getAuthenticatedDocumentLoader } from "../authdocloader-Bzj36aN0.mjs";
import { t as esm_default } from "../esm-D-9VAGKG.mjs";
import { i as rsaPrivateKey2 } from "../keys-BkXWqCAE.mjs";
//#region src/runtime/authdocloader.test.ts
test("getAuthenticatedDocumentLoader()", async (t) => {
	esm_default.spyGlobal();
	esm_default.get("begin:https://example.com/object", async (cl) => {
		const v = await verifyRequest(cl.request, {
			documentLoader: mockDocumentLoader,
			contextLoader: mockDocumentLoader,
			currentTime: Temporal.Now.instant()
		});
		return new Response(JSON.stringify(v != null), { headers: { "Content-Type": "application/json" } });
	});
	await t.step("test", async () => {
		assertEquals(await (await getAuthenticatedDocumentLoader({
			keyId: new URL("https://example.com/key2"),
			privateKey: rsaPrivateKey2
		}))("https://example.com/object"), {
			contextUrl: null,
			documentUrl: "https://example.com/object",
			document: true
		});
	});
	esm_default.hardReset();
	await t.step("deny non-HTTP/HTTPS", async () => {
		const loader = await getAuthenticatedDocumentLoader({
			keyId: new URL("https://example.com/key2"),
			privateKey: rsaPrivateKey2
		});
		assertRejects(() => loader("ftp://localhost"), UrlError);
	});
	await t.step("deny private network", async () => {
		const loader = await getAuthenticatedDocumentLoader({
			keyId: new URL("https://example.com/key2"),
			privateKey: rsaPrivateKey2
		});
		assertRejects(() => loader("http://localhost"), UrlError);
	});
});
test("getAuthenticatedDocumentLoader() cancellation", {
	sanitizeResources: false,
	sanitizeOps: false
}, async (t) => {
	esm_default.spyGlobal();
	await t.step("document loader cancellation", async () => {
		esm_default.get("https://example.com/slow-object", () => new Promise((resolve) => {
			setTimeout(() => {
				resolve({
					status: 200,
					headers: { "Content-Type": "application/activity+json" },
					body: {
						"@context": "https://www.w3.org/ns/activitystreams",
						type: "Note",
						content: "Slow response"
					}
				});
			}, 1e3);
		}));
		const loader = getAuthenticatedDocumentLoader({
			keyId: new URL("https://example.com/key2"),
			privateKey: rsaPrivateKey2
		});
		const controller = new AbortController();
		const promise = loader("https://example.com/slow-object", { signal: controller.signal });
		controller.abort();
		await assertRejects(() => promise, Error);
		await assertRejects(() => loader("https://example.com/object", { signal: controller.signal }), Error);
	});
	await t.step("immediate cancellation", async () => {
		const loader = getAuthenticatedDocumentLoader({
			keyId: new URL("https://example.com/key2"),
			privateKey: rsaPrivateKey2
		});
		const controller = new AbortController();
		controller.abort();
		await assertRejects(() => loader("https://example.com/object", { signal: controller.signal }), Error);
	});
	esm_default.hardReset();
});
//#endregion
export {};
