Merge remote-tracking branch 'origin/master' into riya/frontend-activity-auth-boundary-test
This commit is contained in:
@@ -19,20 +19,29 @@ import type { ComponentProps } from "react";
|
||||
|
||||
import { isLikelyValidClerkPublishableKey } from "@/auth/clerkKey";
|
||||
|
||||
function isE2EAuthBypassEnabled(): boolean {
|
||||
// Used only for Cypress E2E to keep tests secretless and deterministic.
|
||||
// When enabled, we treat the user as signed in and skip Clerk entirely.
|
||||
return process.env.NEXT_PUBLIC_E2E_AUTH_BYPASS === "1";
|
||||
}
|
||||
|
||||
export function isClerkEnabled(): boolean {
|
||||
// IMPORTANT: keep this in sync with AuthProvider; otherwise components like
|
||||
// <SignedOut/> may render without a <ClerkProvider/> and crash during prerender.
|
||||
if (isE2EAuthBypassEnabled()) return false;
|
||||
return isLikelyValidClerkPublishableKey(
|
||||
process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY,
|
||||
);
|
||||
}
|
||||
|
||||
export function SignedIn(props: { children: ReactNode }) {
|
||||
if (isE2EAuthBypassEnabled()) return <>{props.children}</>;
|
||||
if (!isClerkEnabled()) return null;
|
||||
return <ClerkSignedIn>{props.children}</ClerkSignedIn>;
|
||||
}
|
||||
|
||||
export function SignedOut(props: { children: ReactNode }) {
|
||||
if (isE2EAuthBypassEnabled()) return null;
|
||||
if (!isClerkEnabled()) return <>{props.children}</>;
|
||||
return <ClerkSignedOut>{props.children}</ClerkSignedOut>;
|
||||
}
|
||||
@@ -58,6 +67,15 @@ export function useUser() {
|
||||
}
|
||||
|
||||
export function useAuth() {
|
||||
if (isE2EAuthBypassEnabled()) {
|
||||
return {
|
||||
isLoaded: true,
|
||||
isSignedIn: true,
|
||||
userId: "e2e-user",
|
||||
sessionId: "e2e-session",
|
||||
getToken: async () => "e2e-token",
|
||||
} as const;
|
||||
}
|
||||
if (!isClerkEnabled()) {
|
||||
return {
|
||||
isLoaded: true,
|
||||
|
||||
@@ -20,6 +20,23 @@ describe("createExponentialBackoff", () => {
|
||||
expect(backoff.nextDelayMs()).toBe(250); // capped
|
||||
});
|
||||
|
||||
it("clamps invalid numeric options and treats negative jitter as zero", () => {
|
||||
vi.spyOn(Math, "random").mockReturnValue(0.9999);
|
||||
|
||||
// baseMs: NaN should clamp to min (50)
|
||||
// maxMs: Infinity should clamp to min (= baseMs)
|
||||
// jitter: negative -> treated as 0 (no extra delay)
|
||||
const backoff = createExponentialBackoff({
|
||||
baseMs: Number.NaN,
|
||||
maxMs: Number.POSITIVE_INFINITY,
|
||||
jitter: -1,
|
||||
});
|
||||
|
||||
// With maxMs clamped to baseMs, delay will always be baseMs
|
||||
expect(backoff.nextDelayMs()).toBe(50);
|
||||
expect(backoff.nextDelayMs()).toBe(50);
|
||||
});
|
||||
|
||||
it("reset brings attempt back to zero", () => {
|
||||
vi.spyOn(Math, "random").mockReturnValue(0);
|
||||
|
||||
@@ -30,4 +47,15 @@ describe("createExponentialBackoff", () => {
|
||||
backoff.reset();
|
||||
expect(backoff.attempt()).toBe(0);
|
||||
});
|
||||
|
||||
it("uses defaults when options are omitted", () => {
|
||||
vi.spyOn(Math, "random").mockReturnValue(0);
|
||||
|
||||
const backoff = createExponentialBackoff();
|
||||
expect(backoff.attempt()).toBe(0);
|
||||
|
||||
// Default baseMs is 1000 (clamped within bounds), jitter default is 0.2.
|
||||
// With Math.random=0, delay should be the normalized base (1000).
|
||||
expect(backoff.nextDelayMs()).toBe(1000);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user