| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- /* @vitest-environment node */
- // ---------------------------------------------------------------------------
- // Folder: lib/frontend
- // File: authMessages.test.js
- // Relative Path: lib/frontend/authMessages.test.js
- // ---------------------------------------------------------------------------
- import { describe, it, expect } from "vitest";
- import { ApiClientError } from "@/lib/frontend/apiClient";
- import { LOGIN_REASONS } from "@/lib/frontend/authRedirect";
- import { getLoginReasonAlert, getLoginErrorMessage } from "./authMessages.js";
- describe("lib/frontend/authMessages", () => {
- describe("getLoginReasonAlert", () => {
- it("returns alert copy for reason=expired", () => {
- expect(getLoginReasonAlert(LOGIN_REASONS.EXPIRED)).toEqual({
- title: "Session expired",
- description: "Your session has expired. Please log in again.",
- });
- });
- it("returns alert copy for reason=logged-out", () => {
- expect(getLoginReasonAlert(LOGIN_REASONS.LOGGED_OUT)).toEqual({
- title: "Logged out",
- description: "You have been logged out successfully.",
- });
- });
- it("returns null for unknown or missing reason", () => {
- expect(getLoginReasonAlert(null)).toBe(null);
- expect(getLoginReasonAlert("unknown")).toBe(null);
- });
- });
- describe("getLoginErrorMessage", () => {
- it("maps AUTH_INVALID_CREDENTIALS to a friendly message", () => {
- const err = new ApiClientError({
- status: 401,
- code: "AUTH_INVALID_CREDENTIALS",
- message: "Invalid credentials",
- });
- expect(getLoginErrorMessage(err)).toBe("Invalid username or password.");
- });
- it("maps CLIENT_NETWORK_ERROR to a friendly message", () => {
- const err = new ApiClientError({
- status: 0,
- code: "CLIENT_NETWORK_ERROR",
- message: "Network error",
- });
- expect(getLoginErrorMessage(err)).toBe(
- "Network error. Please check your connection and try again."
- );
- });
- it("uses a generic message for other errors", () => {
- expect(getLoginErrorMessage(new Error("boom"))).toBe(
- "Login failed. Please try again."
- );
- });
- });
- });
|