authMessages.test.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* @vitest-environment node */
  2. // ---------------------------------------------------------------------------
  3. // Folder: lib/frontend
  4. // File: authMessages.test.js
  5. // Relative Path: lib/frontend/authMessages.test.js
  6. // ---------------------------------------------------------------------------
  7. import { describe, it, expect } from "vitest";
  8. import { ApiClientError } from "@/lib/frontend/apiClient";
  9. import { LOGIN_REASONS } from "@/lib/frontend/authRedirect";
  10. import { getLoginReasonAlert, getLoginErrorMessage } from "./authMessages.js";
  11. describe("lib/frontend/authMessages", () => {
  12. describe("getLoginReasonAlert", () => {
  13. it("returns alert copy for reason=expired", () => {
  14. expect(getLoginReasonAlert(LOGIN_REASONS.EXPIRED)).toEqual({
  15. title: "Session expired",
  16. description: "Your session has expired. Please log in again.",
  17. });
  18. });
  19. it("returns alert copy for reason=logged-out", () => {
  20. expect(getLoginReasonAlert(LOGIN_REASONS.LOGGED_OUT)).toEqual({
  21. title: "Logged out",
  22. description: "You have been logged out successfully.",
  23. });
  24. });
  25. it("returns null for unknown or missing reason", () => {
  26. expect(getLoginReasonAlert(null)).toBe(null);
  27. expect(getLoginReasonAlert("unknown")).toBe(null);
  28. });
  29. });
  30. describe("getLoginErrorMessage", () => {
  31. it("maps AUTH_INVALID_CREDENTIALS to a friendly message", () => {
  32. const err = new ApiClientError({
  33. status: 401,
  34. code: "AUTH_INVALID_CREDENTIALS",
  35. message: "Invalid credentials",
  36. });
  37. expect(getLoginErrorMessage(err)).toBe("Invalid username or password.");
  38. });
  39. it("maps CLIENT_NETWORK_ERROR to a friendly message", () => {
  40. const err = new ApiClientError({
  41. status: 0,
  42. code: "CLIENT_NETWORK_ERROR",
  43. message: "Network error",
  44. });
  45. expect(getLoginErrorMessage(err)).toBe(
  46. "Network error. Please check your connection and try again."
  47. );
  48. });
  49. it("uses a generic message for other errors", () => {
  50. expect(getLoginErrorMessage(new Error("boom"))).toBe(
  51. "Login failed. Please try again."
  52. );
  53. });
  54. });
  55. });