
      import { Temporal } from "@js-temporal/polyfill";
      import { URLPattern } from "urlpattern-polyfill";
      globalThis.addEventListener = () => {};
    
import { Activity, Application, Endpoints, Group, Person, Service } from "../lookup-Csp1y88W.js";
import { assertEquals } from "../assert_equals-DSbWqCm3.js";
import { assert } from "../assert-MZs1qjMx.js";
import "../assert_instance_of-DHz7EHNU.js";
import "../actor-BV5H5O8U.js";
import "../key-DpaoFQ82.js";
import { verifyRequest } from "../http-CqzO68Am.js";
import { doesActorOwnKey } from "../owner-CuAVQ6t7.js";
import { extractInboxes, sendActivity } from "../send-BDf5TQos.js";
import { mockDocumentLoader, test } from "../testing-YUoSZp2V.js";
import "../std__assert-DWivtrGR.js";
import { assertFalse, assertRejects } from "../assert_rejects-Ce45JcFg.js";
import "../assert_throws-BNXdRGWP.js";
import { assertNotEquals } from "../assert_not_equals-C80BG-_5.js";
import { ed25519Multikey, ed25519PrivateKey, rsaPrivateKey2, rsaPublicKey2 } from "../keys-DLwYJ_nN.js";
import { esm_default } from "../esm-DGtLO56g.js";

//#region src/federation/send.test.ts
test("extractInboxes()", () => {
	const recipients = [
		new Person({
			id: new URL("https://example.com/alice"),
			inbox: new URL("https://example.com/alice/inbox"),
			endpoints: new Endpoints({ sharedInbox: new URL("https://example.com/inbox") })
		}),
		new Application({
			id: new URL("https://example.com/app"),
			inbox: new URL("https://example.com/app/inbox"),
			endpoints: new Endpoints({ sharedInbox: new URL("https://example.com/inbox") })
		}),
		new Group({
			id: new URL("https://example.org/group"),
			inbox: new URL("https://example.org/group/inbox")
		}),
		new Service({
			id: new URL("https://example.net/service"),
			inbox: new URL("https://example.net/service/inbox"),
			endpoints: new Endpoints({ sharedInbox: new URL("https://example.net/inbox") })
		})
	];
	let inboxes = extractInboxes({ recipients });
	assertEquals(inboxes, {
		"https://example.com/alice/inbox": {
			actorIds: new Set(["https://example.com/alice"]),
			sharedInbox: false
		},
		"https://example.com/app/inbox": {
			actorIds: new Set(["https://example.com/app"]),
			sharedInbox: false
		},
		"https://example.org/group/inbox": {
			actorIds: new Set(["https://example.org/group"]),
			sharedInbox: false
		},
		"https://example.net/service/inbox": {
			actorIds: new Set(["https://example.net/service"]),
			sharedInbox: false
		}
	});
	inboxes = extractInboxes({
		recipients,
		preferSharedInbox: true
	});
	assertEquals(inboxes, {
		"https://example.com/inbox": {
			actorIds: new Set(["https://example.com/alice", "https://example.com/app"]),
			sharedInbox: true
		},
		"https://example.org/group/inbox": {
			actorIds: new Set(["https://example.org/group"]),
			sharedInbox: false
		},
		"https://example.net/inbox": {
			actorIds: new Set(["https://example.net/service"]),
			sharedInbox: true
		}
	});
	inboxes = extractInboxes({
		recipients,
		excludeBaseUris: [new URL("https://foo.bar/")]
	});
	assertEquals(inboxes, {
		"https://example.com/alice/inbox": {
			actorIds: new Set(["https://example.com/alice"]),
			sharedInbox: false
		},
		"https://example.com/app/inbox": {
			actorIds: new Set(["https://example.com/app"]),
			sharedInbox: false
		},
		"https://example.org/group/inbox": {
			actorIds: new Set(["https://example.org/group"]),
			sharedInbox: false
		},
		"https://example.net/service/inbox": {
			actorIds: new Set(["https://example.net/service"]),
			sharedInbox: false
		}
	});
	inboxes = extractInboxes({
		recipients,
		excludeBaseUris: [new URL("https://example.com/")]
	});
	assertEquals(inboxes, {
		"https://example.org/group/inbox": {
			actorIds: new Set(["https://example.org/group"]),
			sharedInbox: false
		},
		"https://example.net/service/inbox": {
			actorIds: new Set(["https://example.net/service"]),
			sharedInbox: false
		}
	});
	inboxes = extractInboxes({
		recipients,
		preferSharedInbox: true,
		excludeBaseUris: [new URL("https://example.com/")]
	});
	assertEquals(inboxes, {
		"https://example.org/group/inbox": {
			actorIds: new Set(["https://example.org/group"]),
			sharedInbox: false
		},
		"https://example.net/inbox": {
			actorIds: new Set(["https://example.net/service"]),
			sharedInbox: true
		}
	});
});
test("sendActivity()", async (t) => {
	esm_default.spyGlobal();
	let httpSigVerified = null;
	let request = null;
	esm_default.post("https://example.com/inbox", async (cl) => {
		httpSigVerified = false;
		request = cl.request.clone();
		const options = {
			documentLoader: mockDocumentLoader,
			contextLoader: mockDocumentLoader
		};
		const key = await verifyRequest(request, options);
		const activity = await Activity.fromJsonLd(await request.json(), options);
		if (key != null && await doesActorOwnKey(activity, key, options)) httpSigVerified = true;
		if (httpSigVerified) return new Response("", { status: 202 });
		return new Response("", { status: 401 });
	});
	await t.step("success", async () => {
		const activity = {
			"@context": "https://www.w3.org/ns/activitystreams",
			"type": "Create",
			"id": "https://example.com/activity",
			"actor": "https://example.com/person"
		};
		await sendActivity({
			activity,
			keys: [{
				privateKey: rsaPrivateKey2,
				keyId: rsaPublicKey2.id
			}],
			inbox: new URL("https://example.com/inbox"),
			headers: new Headers({ "X-Test": "test" })
		});
		assert(httpSigVerified);
		assertNotEquals(request, null);
		assertEquals(request?.method, "POST");
		assertEquals(request?.url, "https://example.com/inbox");
		assertEquals(request?.headers.get("Content-Type"), "application/activity+json");
		assertEquals(request?.headers.get("X-Test"), "test");
		httpSigVerified = null;
		await assertRejects(() => sendActivity({
			activity: {
				...activity,
				actor: "https://example.com/person2"
			},
			keys: [{
				privateKey: ed25519PrivateKey,
				keyId: ed25519Multikey.id
			}],
			inbox: new URL("https://example.com/inbox")
		}));
		assertFalse(httpSigVerified);
		assertNotEquals(request, null);
		assertEquals(request?.method, "POST");
		assertEquals(request?.url, "https://example.com/inbox");
		assertEquals(request?.headers.get("Content-Type"), "application/activity+json");
	});
	esm_default.post("https://example.com/inbox2", {
		status: 500,
		body: "something went wrong"
	});
	await t.step("failure", async () => {
		const activity = {
			"@context": "https://www.w3.org/ns/activitystreams",
			"type": "Create",
			"id": "https://example.com/activity",
			"actor": "https://example.com/person"
		};
		await assertRejects(() => sendActivity({
			activity,
			activityId: "https://example.com/activity",
			keys: [{
				privateKey: rsaPrivateKey2,
				keyId: rsaPublicKey2.id
			}],
			inbox: new URL("https://example.com/inbox2")
		}), Error, "Failed to send activity https://example.com/activity to https://example.com/inbox2 (500 Internal Server Error):\nsomething went wrong");
	});
	esm_default.hardReset();
});

//#endregion