fix: deduplicate shared fishing plans
This commit is contained in:
@@ -16,10 +16,15 @@ import Layout from "../layouts/Layout.astro";
|
||||
const planText = (key: string, value: unknown) => typeof value === "string" ? value.slice(0, planFieldLimits[key] ?? 240) : "";
|
||||
const normalisePlan = (value: unknown): Array<Record<string, string>> => {
|
||||
if (!Array.isArray(value)) return [];
|
||||
return value.filter(item => item && typeof item === "object" && typeof item.key === "string" && item.key.startsWith("/spots/")).slice(0, 5).map(item => {
|
||||
const seen = new Set<string>();
|
||||
return value.filter(item => item && typeof item === "object" && typeof item.key === "string" && item.key.startsWith("/spots/")).map(item => {
|
||||
const source = item as Record<string, unknown>;
|
||||
return Object.fromEntries(["key", "waterbody", "coordinates", "baits", "method", "retrieve", "risk", "freshness", "confidence"].map(key => [key, planText(key, source[key])]));
|
||||
});
|
||||
}).filter(item => {
|
||||
if (seen.has(item.key)) return false;
|
||||
seen.add(item.key);
|
||||
return true;
|
||||
}).slice(0, 5);
|
||||
};
|
||||
const readPlan = (): Array<Record<string, string>> => {
|
||||
try { return normalisePlan(JSON.parse(localStorage.getItem(planStorageKey) || "[]")); }
|
||||
|
||||
@@ -76,3 +76,21 @@ test("plan preserves a real zero confidence and bounds imported text", async ({
|
||||
expect(stored[0].confidence).toBe("0");
|
||||
expect(stored[0].oversized).toBeUndefined();
|
||||
});
|
||||
|
||||
test("plan import deduplicates the same point before applying the five-item cap", async ({ page }) => {
|
||||
await page.goto("/plan");
|
||||
await page.evaluate(() => localStorage.setItem("rf4spotter:fishing-plan", JSON.stringify([
|
||||
{ key: "/spots/vyunok-6331x6332", waterbody: "Вьюнок", coordinates: "6331:6332", confidence: "0" },
|
||||
{ key: "/spots/vyunok-6331x6332", waterbody: "Вьюнок", coordinates: "6331:6332", confidence: "80" },
|
||||
{ key: "/spots/vyunok-7450x7451", waterbody: "Вьюнок", coordinates: "7450:7451", confidence: "40" },
|
||||
])));
|
||||
await page.reload();
|
||||
|
||||
await expect(page.getByText("2 из 5 точек")).toBeVisible();
|
||||
await expect(page.locator(".plan-card")).toHaveCount(2);
|
||||
const stored = await page.evaluate(() => JSON.parse(localStorage.getItem("rf4spotter:fishing-plan") || "[]"));
|
||||
expect(stored.map((item: { key: string }) => item.key)).toEqual([
|
||||
"/spots/vyunok-6331x6332",
|
||||
"/spots/vyunok-7450x7451",
|
||||
]);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user