import { Temporal } from "@js-temporal/polyfill";
import "urlpattern-polyfill";
globalThis.addEventListener = () => {};
import { t as test } from "../testing-BQeTO4ss.mjs";
import { t as assertEquals } from "../assert_equals-Ew3jOFa3.mjs";
import "../std__assert-2v7gYiZp.mjs";
import { t as MemoryKvStore } from "../kv-OuT2IU64.mjs";
//#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);
		assertEquals(entries.find((e) => e.key.length === 2 && e.key[1] === "a")?.value, "value-a");
		assertEquals((await Array.fromAsync(store.list(["nonexistent"]))).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");
		assertEquals((await Array.fromAsync(store.list())).length, 3);
		await store.delete(["a"]);
		await store.delete(["b", "c"]);
		await store.delete([
			"d",
			"e",
			"f"
		]);
	});
});
//#endregion
export {};
