Drop one-shot tmp/axe scripts and agent i18n scratch so the Gitea tree is deployable.
226 lines
8.2 KiB
TypeScript
226 lines
8.2 KiB
TypeScript
/**
|
|
* Plan / Nav capability gating (node:test).
|
|
*
|
|
* Run from apps/web:
|
|
* node --experimental-strip-types --test src/lib/plan-capabilities.test.ts
|
|
*/
|
|
import assert from "node:assert/strict";
|
|
import { describe, it } from "node:test";
|
|
|
|
import {
|
|
LEGACY_NAV_DENIED_FEATURES,
|
|
LEGACY_PRIMARY_NAV,
|
|
SETTINGS_TAB_FEATURES,
|
|
auditCapabilityForCohort,
|
|
canFeature,
|
|
featureKeyForHref,
|
|
navItemAllowed,
|
|
settingsTabAllowed,
|
|
type FeatureMap,
|
|
type NavGateCapabilities,
|
|
type NavGateItem
|
|
} from "./plan-capabilities.ts";
|
|
|
|
const NAV_SAMPLE: NavGateItem[] = [
|
|
{ href: "/dashboard", feature: "dashboard.overview", section: "overview" },
|
|
{ href: "/products", feature: "catalog.products", section: "catalog" },
|
|
{ href: "/feeds", feature: "feeds.list", section: "feeds" },
|
|
{ href: "/stores", feature: "stores.hub", section: "stores" },
|
|
{ href: "/processing", feature: "processing.monitor", section: "processing" },
|
|
{ href: "/campaigns", feature: "marketing.campaigns", section: "marketing" },
|
|
{ href: "/integrations/ai", feature: "integrations.ai", section: "integrations" },
|
|
{ href: "/billing", feature: "billing.overview", section: "account" },
|
|
{ href: "/settings", feature: "settings.profile", section: "account" },
|
|
{ href: "/support", feature: "support.center", section: "account" },
|
|
{ href: "/admin", adminOnly: true, section: "platform" }
|
|
];
|
|
|
|
function capsFromFeatures(
|
|
features: FeatureMap | null,
|
|
status: NavGateCapabilities["status"] = features ? "ready" : "missing"
|
|
): NavGateCapabilities {
|
|
return {
|
|
features,
|
|
status,
|
|
can: (key) => canFeature(features, key, status),
|
|
sectionEnabled: () => true
|
|
};
|
|
}
|
|
|
|
function visibleHrefs(
|
|
features: FeatureMap | null,
|
|
opts: { showAdmin?: boolean; unlockAllFeatures?: boolean; status?: NavGateCapabilities["status"] } = {}
|
|
): string[] {
|
|
const caps = capsFromFeatures(features, opts.status ?? (features ? "ready" : "missing"));
|
|
return NAV_SAMPLE.filter((item) =>
|
|
navItemAllowed(item, caps, {
|
|
showAdmin: opts.showAdmin ?? false,
|
|
unlockAllFeatures: opts.unlockAllFeatures ?? false
|
|
})
|
|
).map((item) => item.href);
|
|
}
|
|
|
|
describe("canFeature", () => {
|
|
it("hides Stores / API keys / premium nav when matrix is missing (fail-closed)", () => {
|
|
assert.equal(canFeature(null, "stores.hub"), false);
|
|
assert.equal(canFeature(null, "stores.shopify"), false);
|
|
assert.equal(canFeature(null, "settings.api_keys"), false);
|
|
assert.equal(canFeature(null, "processing.monitor"), false);
|
|
assert.equal(canFeature(null, "marketing.campaigns"), false);
|
|
assert.equal(canFeature(null, "integrations.ai"), false);
|
|
assert.equal(canFeature(null, "dashboard.overview"), true);
|
|
assert.equal(canFeature(null, "catalog.products"), true);
|
|
assert.equal(canFeature(null, "settings.profile"), true);
|
|
});
|
|
|
|
it("enforces an explicit deny map (legacy / Free-style)", () => {
|
|
const features: FeatureMap = {
|
|
"dashboard.overview": true,
|
|
"catalog.products": true,
|
|
"feeds.list": true,
|
|
"billing.overview": true,
|
|
"settings.profile": true,
|
|
"stores.hub": false,
|
|
"settings.api_keys": false,
|
|
"processing.monitor": false,
|
|
"marketing.campaigns": false,
|
|
"integrations.ai": false
|
|
};
|
|
assert.equal(canFeature(features, "stores.hub"), false);
|
|
assert.equal(canFeature(features, "settings.api_keys"), false);
|
|
assert.equal(canFeature(features, "processing.monitor"), false);
|
|
assert.equal(canFeature(features, "dashboard.overview"), true);
|
|
// Missing key with matrix present → fail closed
|
|
assert.equal(canFeature(features, "stores.woocommerce"), false);
|
|
});
|
|
|
|
it("parent deny hides descendants", () => {
|
|
const features: FeatureMap = { "catalog.products": false, "catalog.products.process_ai_titles": true };
|
|
assert.equal(canFeature(features, "catalog.products.process_ai_titles"), false);
|
|
const storeSection: FeatureMap = { stores: false, "stores.shopify": true };
|
|
assert.equal(canFeature(storeSection, "stores.shopify"), false);
|
|
});
|
|
});
|
|
|
|
describe("navItemAllowed", () => {
|
|
it("hides Stores / Processing / marketing / integrations when denied in the matrix", () => {
|
|
const features: FeatureMap = Object.fromEntries([
|
|
...LEGACY_PRIMARY_NAV.map((item) => [item.feature, true] as const),
|
|
...LEGACY_NAV_DENIED_FEATURES.map((key) => [key, false] as const)
|
|
]);
|
|
const hrefs = visibleHrefs(features);
|
|
assert.ok(hrefs.includes("/dashboard"));
|
|
assert.ok(hrefs.includes("/products"));
|
|
assert.ok(hrefs.includes("/feeds"));
|
|
assert.ok(hrefs.includes("/billing"));
|
|
assert.ok(hrefs.includes("/settings"));
|
|
assert.ok(!hrefs.includes("/stores"));
|
|
assert.ok(!hrefs.includes("/processing"));
|
|
assert.ok(!hrefs.includes("/campaigns"));
|
|
assert.ok(!hrefs.includes("/integrations/ai"));
|
|
// support.center is in LEGACY_NAV_DENIED_FEATURES
|
|
assert.ok(!hrefs.includes("/support"));
|
|
assert.ok(!hrefs.includes("/admin"));
|
|
});
|
|
|
|
it("hides premium sections while capabilities are still loading (tenant)", () => {
|
|
const hrefs = visibleHrefs(null, { status: "loading" });
|
|
assert.deepEqual(
|
|
hrefs.sort(),
|
|
["/billing", "/dashboard", "/feeds", "/products", "/settings", "/support"].sort()
|
|
);
|
|
assert.ok(!hrefs.includes("/stores"));
|
|
});
|
|
|
|
it("hides Stores when matrix is missing/error (tenant fail-closed)", () => {
|
|
for (const status of ["missing", "error"] as const) {
|
|
const hrefs = visibleHrefs(null, { status });
|
|
assert.ok(!hrefs.includes("/stores"), `stores visible under status=${status}`);
|
|
assert.ok(!hrefs.includes("/campaigns"), `campaigns visible under status=${status}`);
|
|
assert.ok(hrefs.includes("/dashboard"), `dashboard hidden under status=${status}`);
|
|
}
|
|
});
|
|
|
|
it("section master switch OFF hides Stores even if feature key would allow", () => {
|
|
const features: FeatureMap = { "stores.hub": true, "dashboard.overview": true };
|
|
const caps: NavGateCapabilities = {
|
|
features,
|
|
status: "ready",
|
|
can: (key) => canFeature(features, key),
|
|
sectionEnabled: (section) => section !== "stores"
|
|
};
|
|
assert.equal(
|
|
navItemAllowed(
|
|
{ href: "/stores", feature: "stores.hub", section: "stores" },
|
|
caps,
|
|
{ showAdmin: false, unlockAllFeatures: false }
|
|
),
|
|
false
|
|
);
|
|
});
|
|
|
|
it("unlockAllFeatures previews Stores while matrix is loading", () => {
|
|
const hrefs = visibleHrefs(null, { status: "loading", unlockAllFeatures: true });
|
|
assert.ok(hrefs.includes("/stores"));
|
|
assert.ok(hrefs.includes("/campaigns"));
|
|
});
|
|
|
|
it("unlockAllFeatures still respects an explicit deny map", () => {
|
|
const features: FeatureMap = { "stores.hub": false, "dashboard.overview": true };
|
|
const hrefs = visibleHrefs(features, { unlockAllFeatures: true });
|
|
assert.ok(!hrefs.includes("/stores"));
|
|
assert.ok(hrefs.includes("/dashboard"));
|
|
});
|
|
});
|
|
|
|
describe("settingsTabAllowed / API keys", () => {
|
|
it("hides api-keys tab when settings.api_keys is denied", () => {
|
|
const features: FeatureMap = {
|
|
"settings.profile": true,
|
|
"settings.api_keys": false
|
|
};
|
|
const can = (key: string) => canFeature(features, key);
|
|
assert.equal(settingsTabAllowed("profile", can), true);
|
|
assert.equal(settingsTabAllowed("api-keys", can), false);
|
|
assert.equal(SETTINGS_TAB_FEATURES["api-keys"], "settings.api_keys");
|
|
});
|
|
|
|
it("hides api-keys when matrix is missing (PAID_FAIL_CLOSED)", () => {
|
|
const can = (key: string) => canFeature(null, key, "missing");
|
|
assert.equal(settingsTabAllowed("api-keys", can), false);
|
|
assert.equal(settingsTabAllowed("profile", can), true);
|
|
});
|
|
});
|
|
|
|
describe("featureKeyForHref", () => {
|
|
it("maps Stores and API settings routes to feature keys", () => {
|
|
assert.equal(featureKeyForHref("/stores"), "stores.hub");
|
|
assert.equal(featureKeyForHref("/stores/wizard"), "stores.hub");
|
|
assert.equal(featureKeyForHref("/settings"), "settings.profile");
|
|
});
|
|
});
|
|
describe("A1 PAYG vs Demo Growth audit matrix", () => {
|
|
const keys = ["stores.hub", "settings.api_keys", "processing.monitor"] as const;
|
|
|
|
it("A1 PAYG denies stores, allows api_keys and processing", () => {
|
|
assert.deepEqual(
|
|
Object.fromEntries(keys.map((k) => [k, auditCapabilityForCohort("a1_payg", k)])),
|
|
{
|
|
"stores.hub": false,
|
|
"settings.api_keys": true,
|
|
"processing.monitor": true
|
|
}
|
|
);
|
|
});
|
|
|
|
it("Demo Growth allows stores, api_keys, and processing", () => {
|
|
assert.deepEqual(
|
|
Object.fromEntries(keys.map((k) => [k, auditCapabilityForCohort("growth", k)])),
|
|
{
|
|
"stores.hub": true,
|
|
"settings.api_keys": true,
|
|
"processing.monitor": true
|
|
}
|
|
);
|
|
});
|
|
}); |