import { describe, expect, it } from "vitest";
import { matchAudience, matchPath, pickTour } from "./match.js";
import type { Tour } from "./types.js";

const base: Tour = {
	id: "tr_b",
	version: 1,
	name: "B",
	trigger: { urlPattern: "/dashboard*", frequency: "once" },
	steps: [{ id: "st", type: "modal", title: "Hi", body: "" }],
};

describe("matchPath", () => {
	it("accepte le joker unique", () => {
		expect(matchPath("/dashboard*", "/dashboard")).toBe(true);
		expect(matchPath("/dashboard*", "/dashboard/settings")).toBe(true);
		expect(matchPath("/dashboard*", "/other")).toBe(false);
	});
});

describe("pickTour", () => {
	it("choisit l identifiant le plus ancien en cas d egalite", () => {
		const a = {
			...base,
			id: "tr_a",
			trigger: { urlPattern: "*", frequency: "always" },
		};
		const c = {
			...base,
			id: "tr_c",
			trigger: { urlPattern: "*", frequency: "always" },
		};
		expect(pickTour([c, a], "/", {}, () => false)?.id).toBe("tr_a");
	});

	it("ignore un parcours already seen en frequency once", () => {
		expect(
			pickTour([base], "/dashboard", {}, (id) => id === "tr_b"),
		).toBeUndefined();
	});

	it("filtre l audience", () => {
		expect(
			matchAudience({ match: { role: ["admin"] } }, { role: "user" }),
		).toBe(false);
		expect(
			matchAudience({ match: { role: ["admin"] } }, { role: "admin" }),
		).toBe(true);
	});
});
