
      import { Temporal } from "@js-temporal/polyfill";
      import { URLPattern } from "urlpattern-polyfill";
      globalThis.addEventListener = () => {};
    
import { Create, Follow, Person } from "../lookup-Dvhilldd.js";
import { assertEquals } from "../assert_equals-DSbWqCm3.js";
import "../assert-MZs1qjMx.js";
import "../assert_instance_of-DHz7EHNU.js";
import { MemoryKvStore } from "../kv-CRZrzyXm.js";
import { createFederation } from "../middleware-C7m5m72k.js";
import "../client-pY7-3icS.js";
import "../types-C2XVl6gj.js";
import "../actor-B5o1xrFI.js";
import "../key-jo5CucF6.js";
import "../http-pgzDkXD0.js";
import "../ld-CWuhgMM4.js";
import "../owner-_G6LyiEi.js";
import { signObject } from "../proof-BkMSGqJy.js";
import "../docloader-DUfba_vD.js";
import "../kv-cache-DN9pfMBe.js";
import "../inbox-BDF2VbZ8.js";
import "../builder-CiifhBKW.js";
import "../collection-BzWsN9pB.js";
import "../keycache-C5k5PTyM.js";
import "../negotiation-C4nFufNk.js";
import "../retry-CfF8Gn4d.js";
import "../send-BCzFH66x.js";
import { mockDocumentLoader, test } from "../testing-cwlO8Sns.js";
import "../std__assert-DWivtrGR.js";
import "../assert_rejects-Ce45JcFg.js";
import "../assert_throws-BNXdRGWP.js";
import "../assert_not_equals-C80BG-_5.js";
import { ed25519Multikey, ed25519PrivateKey, ed25519PublicKey } from "../keys-BSzUEVJu.js";

//#region src/federation/idempotency.test.ts
const kv = new MemoryKvStore();
const federationOptions = {
	kv,
	documentLoaderFactory: () => mockDocumentLoader,
	authenticatedDocumentLoaderFactory: () => mockDocumentLoader
};
function createTestFederation() {
	const federation = createFederation(federationOptions);
	federation.setActorDispatcher("/users/{identifier}", (_, identifier) => identifier === "john" ? new Person({}) : null).setKeyPairsDispatcher(() => [{
		privateKey: ed25519PrivateKey,
		publicKey: ed25519PublicKey.publicKey
	}]);
	return federation;
}
test("Federation.setInboxListeners().withIdempotency() - per-origin strategy", async () => {
	const federation = createTestFederation();
	const processedActivities = [];
	federation.setInboxListeners("/users/{identifier}/inbox", "/inbox").withIdempotency("per-origin").on(Create, (ctx, activity$1) => {
		processedActivities.push([ctx.recipient, activity$1]);
	});
	const activity = new Create({
		id: new URL("https://example.com/activities/1"),
		actor: new URL("https://example.com/person2")
	});
	const signedActivity = await signObject(activity, ed25519PrivateKey, ed25519Multikey.id);
	const body = JSON.stringify(await signedActivity.toJsonLd({ contextLoader: mockDocumentLoader }));
	let response = await federation.fetch(new Request("https://example.com/users/john/inbox", {
		method: "POST",
		headers: { "Content-Type": "application/activity+json" },
		body
	}), { contextData: void 0 });
	assertEquals(response.status, 202);
	assertEquals(processedActivities.length, 1);
	assertEquals(processedActivities[0][0], "john");
	response = await federation.fetch(new Request("https://example.com/inbox", {
		method: "POST",
		headers: { "Content-Type": "application/activity+json" },
		body
	}), { contextData: void 0 });
	assertEquals(response.status, 202);
	assertEquals(processedActivities.length, 1);
	await kv.delete([
		"_fedify",
		"activityIdempotence",
		"https://example.com:https://example.com/activities/1"
	]);
});
test("Federation.setInboxListeners().withIdempotency() - per-inbox strategy", async () => {
	const federation = createTestFederation();
	const processedActivities = [];
	federation.setInboxListeners("/users/{identifier}/inbox", "/inbox").withIdempotency("per-inbox").on(Create, (ctx, activity$1) => {
		processedActivities.push([ctx.recipient, activity$1]);
	});
	const activity = new Create({
		id: new URL("https://example.com/activities/2"),
		actor: new URL("https://example.com/person2")
	});
	const signedActivity = await signObject(activity, ed25519PrivateKey, ed25519Multikey.id);
	const body = JSON.stringify(await signedActivity.toJsonLd({ contextLoader: mockDocumentLoader }));
	let response = await federation.fetch(new Request("https://example.com/users/john/inbox", {
		method: "POST",
		headers: { "Content-Type": "application/activity+json" },
		body
	}), { contextData: void 0 });
	assertEquals(response.status, 202);
	assertEquals(processedActivities.length, 1);
	assertEquals(processedActivities[0][0], "john");
	response = await federation.fetch(new Request("https://example.com/inbox", {
		method: "POST",
		headers: { "Content-Type": "application/activity+json" },
		body
	}), { contextData: void 0 });
	assertEquals(response.status, 202);
	assertEquals(processedActivities.length, 2);
	assertEquals(processedActivities[1][0], null);
	response = await federation.fetch(new Request("https://example.com/users/john/inbox", {
		method: "POST",
		headers: { "Content-Type": "application/activity+json" },
		body
	}), { contextData: void 0 });
	assertEquals(response.status, 202);
	assertEquals(processedActivities.length, 2);
});
test("Federation.setInboxListeners().withIdempotency() - global strategy", async () => {
	const federation = createTestFederation();
	const processedActivities = [];
	federation.setInboxListeners("/users/{identifier}/inbox", "/inbox").withIdempotency("global").on(Create, (ctx, activity$1) => {
		processedActivities.push([ctx.recipient, activity$1]);
	});
	const activity = new Create({
		id: new URL("https://example.com/activities/3"),
		actor: new URL("https://example.com/person2")
	});
	const signedActivity = await signObject(activity, ed25519PrivateKey, ed25519Multikey.id);
	const body = JSON.stringify(await signedActivity.toJsonLd({ contextLoader: mockDocumentLoader }));
	let response = await federation.fetch(new Request("https://example.com/users/john/inbox", {
		method: "POST",
		headers: { "Content-Type": "application/activity+json" },
		body
	}), { contextData: void 0 });
	assertEquals(response.status, 202);
	assertEquals(processedActivities.length, 1);
	response = await federation.fetch(new Request("https://example.com/inbox", {
		method: "POST",
		headers: { "Content-Type": "application/activity+json" },
		body
	}), { contextData: void 0 });
	assertEquals(response.status, 202);
	assertEquals(processedActivities.length, 1);
});
test("Federation.setInboxListeners().withIdempotency() - custom callback", async () => {
	const federation = createTestFederation();
	const processedActivities = [];
	federation.setInboxListeners("/users/{identifier}/inbox", "/inbox").withIdempotency((ctx, activity) => {
		if (activity instanceof Follow) return null;
		const inboxId = ctx.recipient ?? "shared";
		return `${ctx.origin}:${activity.id?.href}:${inboxId}`;
	}).on(Create, (ctx, activity) => {
		processedActivities.push([ctx.recipient, activity]);
	}).on(Follow, (ctx, activity) => {
		processedActivities.push([ctx.recipient, activity]);
	});
	const createActivity = new Create({
		id: new URL("https://example.com/activities/4"),
		actor: new URL("https://example.com/person2")
	});
	const signedCreate = await signObject(createActivity, ed25519PrivateKey, ed25519Multikey.id);
	const createBody = JSON.stringify(await signedCreate.toJsonLd({ contextLoader: mockDocumentLoader }));
	let response = await federation.fetch(new Request("https://example.com/users/john/inbox", {
		method: "POST",
		headers: { "Content-Type": "application/activity+json" },
		body: createBody
	}), { contextData: void 0 });
	assertEquals(response.status, 202);
	assertEquals(processedActivities.length, 1);
	response = await federation.fetch(new Request("https://example.com/users/john/inbox", {
		method: "POST",
		headers: { "Content-Type": "application/activity+json" },
		body: createBody
	}), { contextData: void 0 });
	assertEquals(response.status, 202);
	assertEquals(processedActivities.length, 1);
	const followActivity = new Follow({
		id: new URL("https://example.com/activities/5"),
		actor: new URL("https://example.com/person2")
	});
	const signedFollow = await signObject(followActivity, ed25519PrivateKey, ed25519Multikey.id);
	const followBody = JSON.stringify(await signedFollow.toJsonLd({ contextLoader: mockDocumentLoader }));
	response = await federation.fetch(new Request("https://example.com/users/john/inbox", {
		method: "POST",
		headers: { "Content-Type": "application/activity+json" },
		body: followBody
	}), { contextData: void 0 });
	assertEquals(response.status, 202);
	assertEquals(processedActivities.length, 2);
	response = await federation.fetch(new Request("https://example.com/users/john/inbox", {
		method: "POST",
		headers: { "Content-Type": "application/activity+json" },
		body: followBody
	}), { contextData: void 0 });
	assertEquals(response.status, 202);
	assertEquals(processedActivities.length, 3);
});

//#endregion