import { test, expect } from "./fixtures/console-gate";

// TIRREDESIGN-5: the search-history sidebar header must read
// "Ранее искали" (not "Вы искали"). The block exists on Schedule
// and Online-Board start pages once the user has run at least one
// search; we seed a synthetic history entry via localStorage so
// the test is deterministic.

// Matches `searchHistoryItemSchema` in useSearchHistory.ts.
const SEED = [
  {
    type: "schedule-route" as const,
    url: "/ru-ru/schedule/route/MOW-LED-20260427-20260503",
    label: "Москва — Санкт-Петербург",
    params: {
      departure: "MOW",
      arrival: "LED",
      dateFrom: "20260427",
      dateTo: "20260503",
    },
  },
];

async function seedHistory(page: import("@playwright/test").Page) {
  // Storage uses sessionStorage with the `afl_` namespace prefix and is
  // keyed `history_${lang}` — see src/shared/hooks/useSearchHistory.ts.
  await page.addInitScript((items) => {
    window.sessionStorage.setItem("afl_history_ru", JSON.stringify(items));
  }, SEED);
}

test.describe("Search-history label is 'Ранее искали' (TIRREDESIGN-5)", () => {
  test("Schedule start page", async ({ page, consoleMessages }) => {
    await seedHistory(page);
    await page.goto("/ru-ru/schedule");
    const block = page.getByTestId("search-history");
    await expect(block).toBeVisible({ timeout: 15000 });
    await expect(block).toContainText("Ранее искали");
    await expect(block).not.toContainText("Вы искали");
  });

  test("Online-Board start page", async ({ page, consoleMessages }) => {
    await seedHistory(page);
    await page.goto("/ru-ru/onlineboard");
    const block = page.getByTestId("search-history");
    await expect(block).toBeVisible({ timeout: 15000 });
    await expect(block).toContainText("Ранее искали");
    await expect(block).not.toContainText("Вы искали");
  });
});
