Simpl-Resultat/src/shared/profileGate.test.ts
le king fu b89074e6c6 feat(gating): multi-profile gate (Base+), non-destructive
A Free user keeps full access to their active profile; profiles beyond
it show a lock in ProfileSwitcher and open an upsell dialog instead of
switching. Creating a profile beyond the first is locked at the single
creation point, ProfileFormModal (reached from both ProfileSwitcher and
ProfileSelectionPage), with a race guard in handleSave covering the
license boot window. Both creation entries stay visible with a lock
(locked-not-hidden). Nothing is ever removed from profiles.json — an
upgrade to Base/Premium makes every profile reappear untouched.

- New pure predicates in src/shared/profileGate.ts
  (isProfileSwitchLocked, isProfileCreationLocked) + 10 vitest
- ProfileFormModal upsell panel reuses upsell.* keys WITHOUT UpsellGate:
  the modal also opens from ProfileSelectionPage, which renders outside
  BrowserRouter, where UpsellGate's useNavigate would throw
- UpsellGate gains an optional onNavigate callback so the
  ProfileSwitcher upsell dialog can close itself after navigation
- No lock while the license is loading (anti-flash, ready guard);
  zero new i18n keys; no DB migration

Resolves #300

Generated autonomously by /autopilot run of 2026-07-20

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-20 22:24:23 -04:00

59 lines
2.3 KiB
TypeScript

import { describe, it, expect } from "vitest";
import {
isProfileSwitchLocked,
isProfileCreationLocked,
type EntitlementGate,
} from "./profileGate";
const LOCKED_GATE: EntitlementGate = { allowed: false, ready: true };
const ALLOWED_GATE: EntitlementGate = { allowed: true, ready: true };
const BOOTING_GATE: EntitlementGate = { allowed: false, ready: false };
describe("isProfileSwitchLocked", () => {
it("locks profiles beyond the active one when gate is active (Free, ready)", () => {
expect(isProfileSwitchLocked("p2", "p1", LOCKED_GATE)).toBe(true);
expect(isProfileSwitchLocked("p3", "p1", LOCKED_GATE)).toBe(true);
});
it("keeps the active profile accessible for a Free user", () => {
expect(isProfileSwitchLocked("p1", "p1", LOCKED_GATE)).toBe(false);
});
it("locks nothing while the license is not ready (anti-flash at boot)", () => {
expect(isProfileSwitchLocked("p2", "p1", BOOTING_GATE)).toBe(false);
expect(isProfileSwitchLocked("p1", "p1", BOOTING_GATE)).toBe(false);
});
it("locks nothing when multi-profile is allowed (Base/Premium)", () => {
expect(isProfileSwitchLocked("p2", "p1", ALLOWED_GATE)).toBe(false);
});
it("locks nothing when no active profile resolves (never lock out of ALL profiles)", () => {
expect(isProfileSwitchLocked("p1", null, LOCKED_GATE)).toBe(false);
expect(isProfileSwitchLocked("p2", null, LOCKED_GATE)).toBe(false);
});
it("stays unlocked when allowed even if not ready (allowed short-circuits)", () => {
expect(isProfileSwitchLocked("p2", "p1", { allowed: true, ready: false })).toBe(false);
});
});
describe("isProfileCreationLocked", () => {
it("locks creation for a Free user who already has at least one profile", () => {
expect(isProfileCreationLocked(1, LOCKED_GATE)).toBe(true);
expect(isProfileCreationLocked(5, LOCKED_GATE)).toBe(true);
});
it("never blocks creating the FIRST profile (empty config)", () => {
expect(isProfileCreationLocked(0, LOCKED_GATE)).toBe(false);
});
it("locks nothing while the license is not ready (anti-flash at boot)", () => {
expect(isProfileCreationLocked(3, BOOTING_GATE)).toBe(false);
});
it("locks nothing when multi-profile is allowed (Base/Premium)", () => {
expect(isProfileCreationLocked(3, ALLOWED_GATE)).toBe(false);
expect(isProfileCreationLocked(0, ALLOWED_GATE)).toBe(false);
});
});