Files
co-lite/solvway-admin-portal/app/lib/use-pos-client-key.test.ts
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 15:55:45 +08:00

78 lines
3.3 KiB
TypeScript

// @vitest-environment jsdom
import { describe, it, expect } from "vitest";
import { renderHook, act } from "@testing-library/react";
import { usePosClientKey } from "./use-pos-client-key";
// add-event-pos-wallet-tender Task 9.2: the POS wallet sale's idempotency key. Mirrors the
// customer portal's useCheckoutClientKey lifecycle — the counter dedups on this key, so it must
// stay stable across a failed submit and its retry, and change only on success or a new cart.
describe("usePosClientKey", () => {
it("generates a non-blank key on first mount", () => {
const { result } = renderHook(() => usePosClientKey("MY0001|77|15:1|PRODUCT=5"));
expect(result.current.clientKey).toEqual(expect.any(String));
expect(result.current.clientKey.trim().length).toBeGreaterThan(0);
});
it("stays stable across a failed-then-retried submit (same cart, no success)", () => {
const { result, rerender } = renderHook(
({ signature }) => usePosClientKey(signature),
{ initialProps: { signature: "MY0001|77|15:1|PRODUCT=5" } },
);
const first = result.current.clientKey;
rerender({ signature: "MY0001|77|15:1|PRODUCT=5" });
expect(result.current.clientKey).toBe(first);
rerender({ signature: "MY0001|77|15:1|PRODUCT=5" });
expect(result.current.clientKey).toBe(first);
});
it("regenerates after a successful sale (onOrderPlaced)", () => {
const { result } = renderHook(() => usePosClientKey("MY0001|77|15:1|PRODUCT=5"));
const first = result.current.clientKey;
act(() => result.current.onOrderPlaced());
expect(result.current.clientKey).not.toBe(first);
});
it("regenerates when the cart signature changes (member, event, lines or a wallet amount)", () => {
const { result, rerender } = renderHook(
({ signature }) => usePosClientKey(signature),
{ initialProps: { signature: "MY0001|77|15:1|PRODUCT=5" } },
);
const first = result.current.clientKey;
rerender({ signature: "MY0001|77|15:1|PRODUCT=6" });
expect(result.current.clientKey).not.toBe(first);
});
it("with remember, a remounted hook reuses the key for the same signature until the order is placed", () => {
// redesign-pos-sale-flow: the split panel lives in the payment dialog and unmounts when it closes;
// a retry of the same allocation after reopening must reuse the key so the counter dedups it.
const first = renderHook(() => usePosClientKey("SPLIT|remember-1", { remember: true }));
const key = first.result.current.clientKey;
first.unmount();
const second = renderHook(() => usePosClientKey("SPLIT|remember-1", { remember: true }));
expect(second.result.current.clientKey).toBe(key);
act(() => second.result.current.onOrderPlaced());
const rotated = second.result.current.clientKey;
expect(rotated).not.toBe(key);
second.unmount();
const third = renderHook(() => usePosClientKey("SPLIT|remember-1", { remember: true }));
expect(third.result.current.clientKey).toBe(rotated);
});
it("without remember, a remount mints a new key", () => {
const first = renderHook(() => usePosClientKey("CASH|no-remember"));
const key = first.result.current.clientKey;
first.unmount();
const second = renderHook(() => usePosClientKey("CASH|no-remember"));
expect(second.result.current.clientKey).not.toBe(key);
});
});