
      import { Temporal } from "@js-temporal/polyfill";
      import { URLPattern } from "urlpattern-polyfill";
      globalThis.addEventListener = () => {};
    
import "../lookup-B25HiNR_.js";
import { assertEquals } from "../assert_equals-DSbWqCm3.js";
import "../assert-MZs1qjMx.js";
import "../assert_instance_of-DHz7EHNU.js";
import { MemoryKvStore } from "../kv-QzKcOQgP.js";
import { test } from "../testing-BbJqK9NR.js";
import "../std__assert-DWivtrGR.js";
import "../assert_rejects-Ce45JcFg.js";
import "../assert_throws-BNXdRGWP.js";
import "../assert_not_equals-C80BG-_5.js";

//#region src/federation/kv.test.ts
test("MemoryKvStore", async (t) => {
	const store = new MemoryKvStore();
	await t.step("set() & get()", async () => {
		await store.set(["foo", "bar"], "foobar");
		assertEquals(await store.get(["foo", "bar"]), "foobar");
		assertEquals(await store.get(["foo"]), void 0);
		await store.set(["foo", "baz"], "baz", { ttl: Temporal.Duration.from({ seconds: 0 }) });
		await new Promise((resolve) => setTimeout(resolve, 10));
		assertEquals(await store.get(["foo", "baz"]), void 0);
	});
	await t.step("delete()", async () => {
		await store.delete(["foo", "bar"]);
		assertEquals(await store.get(["foo", "bar"]), void 0);
	});
	await t.step("cas()", async () => {
		await store.set(["foo", "bar"], "foobar");
		assertEquals(await store.cas(["foo", "bar"], "bar", "baz"), false);
		assertEquals(await store.get(["foo", "bar"]), "foobar");
		assertEquals(await store.cas(["foo", "bar"], "foobar", "baz"), true);
		assertEquals(await store.get(["foo", "bar"]), "baz");
		await store.delete(["foo", "bar"]);
		assertEquals(await store.cas(["foo", "bar"], "foobar", "baz"), false);
		assertEquals(await store.get(["foo", "bar"]), void 0);
		assertEquals(await store.cas(["foo", "bar"], void 0, "baz"), true);
		assertEquals(await store.get(["foo", "bar"]), "baz");
	});
	await t.step("list()", async () => {
		await store.set(["prefix", "a"], "value-a");
		await store.set(["prefix", "b"], "value-b");
		await store.set([
			"prefix",
			"nested",
			"c"
		], "value-c");
		await store.set(["other", "x"], "value-x");
		await store.set(["prefix"], "exact-match");
		const entries = await Array.fromAsync(store.list(["prefix"]));
		assertEquals(entries.length, 4);
		const entryA = entries.find((e) => e.key.length === 2 && e.key[1] === "a");
		assertEquals(entryA?.value, "value-a");
		const noMatch = await Array.fromAsync(store.list(["nonexistent"]));
		assertEquals(noMatch.length, 0);
		await store.delete(["prefix", "a"]);
		await store.delete(["prefix", "b"]);
		await store.delete([
			"prefix",
			"nested",
			"c"
		]);
		await store.delete(["other", "x"]);
		await store.delete(["prefix"]);
	});
	await t.step("list() filters expired entries", async () => {
		await store.set(["expired", "old"], "old-value", { ttl: Temporal.Duration.from({ milliseconds: 1 }) });
		await store.set(["expired", "valid"], "valid-value");
		await new Promise((r) => setTimeout(r, 10));
		const entries = await Array.fromAsync(store.list(["expired"]));
		assertEquals(entries.length, 1);
		assertEquals(entries[0].value, "valid-value");
		await store.delete(["expired", "valid"]);
	});
	await t.step("list() with empty prefix", async () => {
		await store.delete(["foo", "bar"]);
		await store.set(["a"], "value-a");
		await store.set(["b", "c"], "value-bc");
		await store.set([
			"d",
			"e",
			"f"
		], "value-def");
		const entries = await Array.fromAsync(store.list());
		assertEquals(entries.length, 3);
		await store.delete(["a"]);
		await store.delete(["b", "c"]);
		await store.delete([
			"d",
			"e",
			"f"
		]);
	});
});

//#endregion