/// <reference types="cypress" />
export {};

Cypress.env("AUTO_LOGIN", false);

describe("admins management - authentication and route protection", () => {
  after(() => {
    Cypress.env("AUTO_LOGIN", true);
  });

  beforeEach(() => {
    cy.clearCookies();
    cy.clearLocalStorage();
  });

  it("redirects unauthorized users to login page", () => {
    cy.visit("/admins");
    cy.location("pathname").should("eq", "/auth/sign-in");
  });

  it("handles expired token by redirecting to login after refresh failure", () => {
    cy.setAuthCookies();
    cy.intercept("GET", "**/api/proxy/users**", {
      statusCode: 401,
      body: { error: { message: "Token expired" } },
    }).as("getAdmins401");
    cy.intercept("POST", "**/api/proxy/profile/refresh-token", {
      statusCode: 401,
      body: { error: { message: "Refresh token expired" } },
    }).as("refresh401");

    cy.visit("/admins");
    cy.wait("@getAdmins401");
    cy.wait("@refresh401");
    cy.location("pathname").should("eq", "/auth/sign-in");
  });
});
