
      import { Temporal } from "@js-temporal/polyfill";
      import { URLPattern } from "urlpattern-polyfill";
      globalThis.addEventListener = () => {};
    
import "../lookup-Csp1y88W.js";
import { MockKvStore, kvCache } from "../kv-cache-DN9pfMBe.js";
import { mockDocumentLoader, test } from "../testing-YUoSZp2V.js";
import { preloadedContexts } from "@fedify/vocab-runtime";
import { deepStrictEqual, throws } from "node:assert";

//#region src/utils/kv-cache.test.ts
test("kvCache()", async (t) => {
	const kv = new MockKvStore();
	await t.step("cached", async () => {
		const loader = kvCache({
			kv,
			loader: mockDocumentLoader,
			rules: [
				["https://example.org/", Temporal.Duration.from({ days: 1 })],
				[new URL("https://example.net/"), Temporal.Duration.from({ days: 1 })],
				[new URLPattern("https://example.com/*"), Temporal.Duration.from({ days: 30 })]
			],
			prefix: ["_test", "cached"]
		});
		const result = await loader("https://example.com/object");
		deepStrictEqual(result, {
			contextUrl: null,
			documentUrl: "https://example.com/object",
			document: {
				"@context": "https://www.w3.org/ns/activitystreams",
				id: "https://example.com/object",
				name: "Fetched object",
				type: "Object"
			}
		});
		const cache = await kv.get([
			"_test",
			"cached",
			"https://example.com/object"
		]);
		deepStrictEqual(cache, result);
		await kv.set([
			"_test",
			"cached",
			"https://example.org/"
		], {
			contextUrl: null,
			documentUrl: "https://example.org/",
			document: { "id": "https://example.org/" }
		});
		const result2 = await loader("https://example.org/");
		deepStrictEqual(result2, {
			contextUrl: null,
			documentUrl: "https://example.org/",
			document: { "id": "https://example.org/" }
		});
		await kv.set([
			"_test",
			"cached",
			"https://example.net/"
		], {
			contextUrl: null,
			documentUrl: "https://example.net/",
			document: { "id": "https://example.net/" }
		});
		const result3 = await loader("https://example.net/");
		deepStrictEqual(result3, {
			contextUrl: null,
			documentUrl: "https://example.net/",
			document: { "id": "https://example.net/" }
		});
	});
	await t.step("not cached", async () => {
		const loader = kvCache({
			kv,
			loader: mockDocumentLoader,
			rules: [],
			prefix: ["_test", "not cached"]
		});
		const result = await loader("https://example.com/object");
		deepStrictEqual(result, {
			contextUrl: null,
			documentUrl: "https://example.com/object",
			document: {
				"@context": "https://www.w3.org/ns/activitystreams",
				id: "https://example.com/object",
				name: "Fetched object",
				type: "Object"
			}
		});
		const cache = await kv.get([
			"test2",
			"not cached",
			"https://example.com/object"
		]);
		deepStrictEqual(cache, void 0);
	});
	await t.step("maximum cache duration", () => {
		throws(() => kvCache({
			kv,
			loader: mockDocumentLoader,
			rules: [["https://example.com/", Temporal.Duration.from({
				days: 30,
				seconds: 1
			})]]
		}), TypeError, "The maximum cache duration is 30 days");
		throws(() => kvCache({
			kv,
			loader: mockDocumentLoader,
			rules: [[new URLPattern("https://example.com/*"), Temporal.Duration.from({
				days: 30,
				seconds: 1
			})]]
		}), TypeError, "The maximum cache duration is 30 days");
	});
	await t.step("on kv store exception", async () => {
		class KvStoreThrowsException {
			get(_key) {
				throw new Error("Failed to get key");
			}
			set(_key, _value, _options) {
				throw new Error("Failed to set key");
			}
			delete(_key) {
				throw new Error("Failed to delete key");
			}
		}
		const loader = kvCache({
			kv: new KvStoreThrowsException(),
			loader: mockDocumentLoader,
			rules: [
				["https://example.org/", Temporal.Duration.from({ days: 1 })],
				[new URL("https://example.net/"), Temporal.Duration.from({ days: 1 })],
				[new URLPattern("https://example.com/*"), Temporal.Duration.from({ days: 30 })]
			],
			prefix: ["_test", "not cached"]
		});
		const result = await loader("https://example.com/object");
		deepStrictEqual(result, {
			contextUrl: null,
			documentUrl: "https://example.com/object",
			document: {
				"@context": "https://www.w3.org/ns/activitystreams",
				id: "https://example.com/object",
				name: "Fetched object",
				type: "Object"
			}
		});
	});
	await t.step("preloaded contexts bypass cache", async () => {
		const kv$1 = new MockKvStore();
		let loaderCalled = false;
		const mockLoader = (url) => {
			loaderCalled = true;
			return Promise.resolve({
				contextUrl: null,
				documentUrl: url,
				document: { "mock": "document" }
			});
		};
		const loader = kvCache({
			kv: kv$1,
			loader: mockLoader,
			prefix: ["_test", "preloaded"]
		});
		const activityStreamsUrl = "https://www.w3.org/ns/activitystreams";
		loaderCalled = false;
		const result = await loader(activityStreamsUrl);
		deepStrictEqual(result, {
			contextUrl: null,
			documentUrl: activityStreamsUrl,
			document: preloadedContexts[activityStreamsUrl]
		});
		deepStrictEqual(loaderCalled, false, "Loader should not be called for preloaded contexts");
		const cachedValue = await kv$1.get([
			"_test",
			"preloaded",
			activityStreamsUrl
		]);
		deepStrictEqual(cachedValue, void 0, "Preloaded contexts should not be cached in KV store");
		const securityUrl = "https://w3id.org/security/v1";
		loaderCalled = false;
		const result2 = await loader(securityUrl);
		deepStrictEqual(result2, {
			contextUrl: null,
			documentUrl: securityUrl,
			document: preloadedContexts[securityUrl]
		});
		deepStrictEqual(loaderCalled, false, "Loader should not be called for preloaded contexts");
		const nonPreloadedUrl = "https://example.com/not-preloaded";
		loaderCalled = false;
		const result3 = await loader(nonPreloadedUrl);
		deepStrictEqual(result3, {
			contextUrl: null,
			documentUrl: nonPreloadedUrl,
			document: { "mock": "document" }
		});
		deepStrictEqual(loaderCalled, true, "Loader should be called for non-preloaded URLs");
		const cachedValue2 = await kv$1.get([
			"_test",
			"preloaded",
			nonPreloadedUrl
		]);
		deepStrictEqual(cachedValue2, result3, "Non-preloaded URLs should be cached");
	});
});

//#endregion