Reorganize repo structure and standards references

This commit is contained in:
david committed 2026-06-29 05:03:42 +02:00
1 parent 3cb93a81be
commit be2054274d
5885 files changed
+2588 -195

No files matched your search

@@ -0,0 +1,143 @@
import { describe, expect, it } from "vitest";
import {
entitlementLimitTypes,
featureGateRegistry,
getFeatureGate,
getStandardEntitlementPlan,
getStripePricePlaceholder,
getStripeProductPlaceholder,
listStandardEntitlementPlanIds,
standardEntitlementPlans,
type EntitlementPlanFeatureAssignment,
type FeatureKey,
} from "./index.js";
const expectedPlanIds = ["starter", "pro", "business"] as const;
const expectedFeatureKeys = [
"crm-basic",
"support-tickets",
"knowledge-base-read",
"knowledge-base-tenant",
"files",
"users",
"tasks",
"api",
"email-marketing",
"imported-lists",
"crm-based-lists",
"onboarding",
"workflow-engine",
"mcp",
"advanced-builder",
"import-export",
"rate-limit-per-minute",
"rate-limit-per-second",
"priority-support",
"ai-credits",
"private-generations",
] as const satisfies readonly FeatureKey[];
const unsafeFixturePattern =
/(?:prod_[a-z0-9]|price_[a-z0-9]|sk_(?:live|test)|pk_(?:live|test)|api[_ -]?key|secret|password|token|tenant[_ -]?id|customer[_ -]?id|subscription[_ -]?id|acct_[a-z0-9]|cus_[a-z0-9]|sub_[a-z0-9])/i;
function limitTypeName(limitType: number): string | undefined {
return Object.entries(entitlementLimitTypes).find(([, value]) => value === limitType)?.[0];
}
function assignmentStrings(assignment: EntitlementPlanFeatureAssignment): readonly string[] {
return [assignment.featureKey, assignment.label, String(assignment.limit ?? ""), String(assignment.order), String(assignment.limitType)];
}
describe("standardEntitlementPlans", () => {
it("exports public standard plans in deterministic order", () => {
expect(listStandardEntitlementPlanIds()).toEqual(expectedPlanIds);
expect(standardEntitlementPlans.map((plan) => plan.metadata.sortOrder)).toEqual([10, 20, 30]);
expect(standardEntitlementPlans.map((plan) => plan.metadata.slug)).toEqual(expectedPlanIds);
expect(standardEntitlementPlans.every((plan) => plan.metadata.isPublic)).toBe(true);
});
it("uses unique plan IDs and immutable plan fixtures", () => {
const planIds = listStandardEntitlementPlanIds();
expect(new Set(planIds).size).toBe(planIds.length);
expect(Object.isFrozen(standardEntitlementPlans)).toBe(true);
for (const plan of standardEntitlementPlans) {
expect(Object.isFrozen(plan)).toBe(true);
expect(Object.isFrozen(plan.metadata)).toBe(true);
expect(Object.isFrozen(plan.features)).toBe(true);
}
});
it("covers every registered standard feature in every public plan", () => {
for (const plan of standardEntitlementPlans) {
expect(
plan.features.map((feature) => feature.featureKey),
`${plan.id} feature coverage`
).toEqual(expectedFeatureKeys);
expect(
plan.features.map((feature) => feature.featureKey),
`${plan.id} registry coverage`
).toEqual(featureGateRegistry.map((feature) => feature.key));
}
});
it("keeps plan feature assignments in registry order", () => {
const expectedOrders = featureGateRegistry.map((feature) => feature.defaultOrder);
for (const plan of standardEntitlementPlans) {
expect(
plan.features.map((feature) => feature.order),
`${plan.id} feature order`
).toEqual(expectedOrders);
}
});
it("references only known feature keys and supported limit types", () => {
for (const plan of standardEntitlementPlans) {
for (const assignment of plan.features) {
const featureGate = getFeatureGate(assignment.featureKey);
const registryLimitType = limitTypeName(assignment.limitType);
expect(featureGate, `${plan.id}.${assignment.featureKey} known feature`).toBeDefined();
expect(registryLimitType, `${plan.id}.${assignment.featureKey} limit type name`).toBeDefined();
expect(featureGate?.supportedLimitTypes, `${plan.id}.${assignment.featureKey} supported limit type`).toContain(
registryLimitType?.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`)
);
}
}
});
it("exposes lookup helpers without mutating fixture order", () => {
expect(getStandardEntitlementPlan("pro")?.metadata.publicName).toBe("Pro");
expect(getStandardEntitlementPlan("pro")?.metadata.stripeProductId).toBe(getStripeProductPlaceholder("pro"));
expect(getStandardEntitlementPlan("pro")?.metadata.stripePriceId).toBe(getStripePricePlaceholder("pro"));
expect(getStandardEntitlementPlan("unknown-plan")).toBeUndefined();
expect(listStandardEntitlementPlanIds()).toEqual(expectedPlanIds);
});
it("uses deterministic safe Stripe placeholders and no secret-looking fixture strings", () => {
for (const plan of standardEntitlementPlans) {
expect(plan.metadata.stripeProductId, `${plan.id} product id placeholder`).toBe(getStripeProductPlaceholder(plan.id));
expect(plan.metadata.stripePriceId, `${plan.id} price id placeholder`).toBe(getStripePricePlaceholder(plan.id));
expect(plan.metadata.stripeProductId, `${plan.id} non-empty product placeholder`).not.toBe("");
expect(plan.metadata.stripePriceId, `${plan.id} non-empty price placeholder`).not.toBe("");
expect(getStandardEntitlementPlan(plan.id)?.metadata.stripeProductId, `${plan.id} lookup product placeholder`).toBe(plan.metadata.stripeProductId);
expect(getStandardEntitlementPlan(plan.id)?.metadata.stripePriceId, `${plan.id} lookup price placeholder`).toBe(plan.metadata.stripePriceId);
const strings = [
plan.id,
plan.metadata.slug,
plan.metadata.publicName,
plan.metadata.description,
plan.metadata.stripeProductId,
plan.metadata.stripePriceId,
...plan.features.flatMap(assignmentStrings),
];
for (const value of strings) {
expect(value, `${plan.id} safe fixture value`).not.toMatch(unsafeFixturePattern);
}
}
});
});