| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- /* @vitest-environment node */
- import { describe, it, expect } from "vitest";
- import {
- normalizeUsernameForConfirmation,
- isUsernameConfirmationMatch,
- normalizeBranchIdInput,
- normalizeBranchNumberInput,
- formatBranchIdFromNumberInput,
- extractBranchNumberInputFromBranchId,
- isValidBranchIdFormat,
- evaluateBranchExistence,
- } from "./userManagementUx.js";
- describe("lib/frontend/admin/users/userManagementUx", () => {
- describe("username confirmation", () => {
- it("normalizes to lowercase and trims", () => {
- expect(normalizeUsernameForConfirmation(" Alice.Admin ")).toBe(
- "alice.admin",
- );
- });
- it("matches typed username case-insensitively after trimming", () => {
- expect(
- isUsernameConfirmationMatch({
- expectedUsername: "branch.user",
- typedUsername: " BRANCH.USER ",
- }),
- ).toBe(true);
- });
- it("returns false for empty or mismatching values", () => {
- expect(
- isUsernameConfirmationMatch({
- expectedUsername: "branch.user",
- typedUsername: "other.user",
- }),
- ).toBe(false);
- expect(
- isUsernameConfirmationMatch({
- expectedUsername: "",
- typedUsername: "",
- }),
- ).toBe(false);
- });
- });
- describe("branch formatting", () => {
- it("normalizes branchId input to uppercase + trim", () => {
- expect(normalizeBranchIdInput(" nl01 ")).toBe("NL01");
- });
- it("normalizes numeric branch input and strips non-digits", () => {
- expect(normalizeBranchNumberInput(" 001 ")).toBe("1");
- expect(normalizeBranchNumberInput(" 32a ")).toBe("32");
- expect(normalizeBranchNumberInput("000")).toBe("0");
- expect(normalizeBranchNumberInput("abc")).toBe("");
- });
- it("formats NL branchId with 2+ digit policy", () => {
- expect(formatBranchIdFromNumberInput("1")).toBe("NL01");
- expect(formatBranchIdFromNumberInput("32")).toBe("NL32");
- expect(formatBranchIdFromNumberInput("200")).toBe("NL200");
- });
- it("extracts numeric branch input from an existing branchId", () => {
- expect(extractBranchNumberInputFromBranchId("NL01")).toBe("01");
- expect(extractBranchNumberInputFromBranchId("nl200")).toBe("200");
- expect(extractBranchNumberInputFromBranchId("XX1")).toBe("");
- });
- it("validates branchId format", () => {
- expect(isValidBranchIdFormat("NL01")).toBe(true);
- expect(isValidBranchIdFormat("nl200")).toBe(true);
- expect(isValidBranchIdFormat("XX1")).toBe(false);
- expect(isValidBranchIdFormat("NL")).toBe(false);
- });
- });
- describe("branch existence decision", () => {
- it("blocks submit for unknown branch only when list is ready", () => {
- expect(
- evaluateBranchExistence({
- role: "branch",
- branchId: "NL99",
- branchesStatus: "ready",
- availableBranchIds: ["NL01", "NL02"],
- }).shouldBlockSubmit,
- ).toBe(true);
- });
- it("allows submit when branch exists in ready list", () => {
- expect(
- evaluateBranchExistence({
- role: "branch",
- branchId: "NL02",
- branchesStatus: "ready",
- availableBranchIds: ["NL01", "NL02"],
- }).shouldBlockSubmit,
- ).toBe(false);
- });
- it("is fail-open when list fetch failed", () => {
- const result = evaluateBranchExistence({
- role: "branch",
- branchId: "NL99",
- branchesStatus: "error",
- availableBranchIds: null,
- });
- expect(result.listError).toBe(true);
- expect(result.shouldBlockSubmit).toBe(false);
- });
- it("is irrelevant for non-branch roles", () => {
- const result = evaluateBranchExistence({
- role: "admin",
- branchId: "NL99",
- branchesStatus: "ready",
- availableBranchIds: ["NL01"],
- });
- expect(result.isBranchRole).toBe(false);
- expect(result.shouldBlockSubmit).toBe(false);
- });
- });
- });
|