Files
OmniRoute/tests/unit/grok-cli-provider-limits-ui.test.ts
Diego Rodrigues de Sa e Souza 93265eede3 test(infra): retry recursive temp-dir removal on main (main twin of #11968) (#12246)
* test(infra): retry recursive temp-dir removal on main (main twin of #11968)

`main` has been red since b342c1a361 on the vitest and integration gates:

  ✖ tests/unit/autoCombo/provider-family-combos.test.ts > auto/<family>
  ✖ chat pipeline applies Codex OAuth fingerprint and priority tier inside combos

Both call resetStorage() from beforeEach, which does an fs.rmSync(TEST_DATA_DIR,
{recursive: true, force: true}) with no retry, and intermittently loses the race
with a not-yet-released SQLite handle (ENOTEMPTY).

release/v3.8.51 fixed this in #11968 with a mechanical codemod adding
maxRetries/retryDelay to every recursive rm/rmSync/rmdirSync under tests/, but
that PR landed only on the release branch. Because main only receives work at
the release squash, it stayed broken for the whole cycle — and repo-wide gates
then turn every open PR into main red on checks unrelated to their diff.

This is the --base main twin: re-runs the same codemod that already shipped on
the release branch (scripts/ad-hoc/codemod-rm-maxretries.mjs), so the two
branches converge on identical test-teardown semantics. Test-only; no product
logic is touched.

The remaining three failures reported on #12133 (unit full suite exceeding its
4800s ceiling, package-artifact exceeding 1200s, and the boot-smoke that is
skipped as a consequence) are runner-contention timeouts, not code defects —
validate-release-green.mjs runs those heavy gates concurrently on one shared
hosted runner. There is no fix to port for those.

* chore(scripts): carry the rm-maxretries codemod onto main alongside its output

The codemod that generated the previous commit lives in the repo on
release/v3.8.51 (added by #11968) but was never on main. Bringing it over keeps
the tool next to the change it produced, so the transformation stays
reproducible and auditable from either branch.
2026-09-01 01:48:00 -03:00

304 lines
9.1 KiB
TypeScript
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-grok-limits-ui-"));
process.env.DATA_DIR = TEST_DATA_DIR;
process.env.STORAGE_ENCRYPTION_KEY = "grok-provider-limits-ui-test-key-32-bytes-minimum";
process.env.DISABLE_SQLITE_AUTO_BACKUP = "true";
const { parseQuotaData, resolvePlanValue, buildProviderLimitsResolvedPlans, normalizePlanTier } =
await import("../../src/app/(dashboard)/dashboard/usage/components/ProviderLimits/utils.tsx");
const { PROVIDER_LABEL } =
await import("../../src/app/(dashboard)/dashboard/usage/components/ProviderLimits/constants.ts");
const { USAGE_SUPPORTED_PROVIDERS } = await import("../../src/shared/constants/providers.ts");
const {
buildGrokBillingCardRows,
formatGrokMinorUnits,
GROK_BUILD_ADDITIONAL_CREDITS_URL,
sanitizeGrokBillingStatus,
} = await import("../../src/shared/utils/grokBilling.ts");
type GrokBillingTranslator =
typeof import("../../src/shared/utils/grokBilling.ts").GrokBillingTranslator;
const baseBilling = {
currency: "USD" as const,
autoTopUp: { available: false },
additionalCreditsUrl: GROK_BUILD_ADDITIONAL_CREDITS_URL,
};
test.after(() => {
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
});
test("Grok Build product aliases normalize to one stable row and preserve collisions", () => {
const parsed = parseQuotaData("grok-cli", {
quotas: {
weekly: {
used: 37.25,
total: 100,
remaining: 62.75,
remainingPercentage: 62.75,
resetAt: "2099-08-03T00:00:00.000Z",
isPercentageOnly: true,
},
product_grok_build: {
displayName: "Grok Build",
used: 12.5,
total: 100,
remaining: 87.5,
remainingPercentage: 87.5,
resetAt: "2099-08-03T00:00:00.000Z",
isPercentageOnly: true,
},
product_grok_build_2: {
displayName: "Grok Build",
used: 25,
total: 100,
remaining: 75,
remainingPercentage: 75,
resetAt: "2099-08-03T00:00:00.000Z",
isPercentageOnly: true,
},
},
});
assert.deepEqual(
parsed.map(({ name, displayName, remainingPercentage }) => ({
name,
displayName,
remainingPercentage,
})),
[
{ name: "weekly", displayName: undefined, remainingPercentage: 62.75 },
{ name: "product_grok_build", displayName: "Grok Build", remainingPercentage: 87.5 },
{ name: "product_grok_build_2", displayName: "Grok Build", remainingPercentage: 75 },
]
);
});
test("grok-cli plan display never infers persisted provider-specific tiers", () => {
assert.equal(
resolvePlanValue(
null,
{ subscriptionTier: "Persisted Secret Tier", plan: "Persisted Plan" },
"grok-cli"
),
null
);
assert.equal(
resolvePlanValue(
"Future Experimental Tier",
{ subscriptionTier: "Persisted Tier" },
"grok-cli"
),
"Future Experimental Tier"
);
});
test("page-level tier stats/filters ignore persisted Grok Free/Enterprise without live plan", () => {
const connections = [
{
id: "grok-free",
provider: "grok-cli",
providerSpecificData: {
tier: "Free",
plan: "Free",
subscriptionTier: "Free",
},
},
{
id: "grok-enterprise",
provider: "grok-cli",
providerSpecificData: {
tier: "Enterprise",
plan: "Enterprise",
subscriptionTier: "Enterprise",
},
},
{
id: "grok-live",
provider: "grok-cli",
providerSpecificData: {
tier: "Free",
plan: "Free",
subscriptionTier: "Free",
},
},
{
id: "codex-fallback",
provider: "codex",
providerSpecificData: { chatgptPlanType: "Pro" },
},
{
id: "claude-fallback",
provider: "claude",
providerSpecificData: { plan: "Pro" },
},
];
const quotaData = {
"grok-free": { plan: null },
"grok-enterprise": {},
"grok-live": { plan: "Enterprise" },
"codex-fallback": { plan: "unknown" },
"claude-fallback": { plan: null },
};
const resolvedPlans = buildProviderLimitsResolvedPlans(connections, quotaData);
assert.equal(resolvedPlans["grok-free"], null);
assert.equal(resolvedPlans["grok-enterprise"], null);
assert.equal(resolvedPlans["grok-live"], "Enterprise");
assert.equal(resolvedPlans["codex-fallback"], "Pro");
assert.equal(resolvedPlans["claude-fallback"], "Pro");
const tierByConnection = Object.fromEntries(
connections.map((conn) => [conn.id, normalizePlanTier(resolvedPlans[conn.id])])
);
assert.equal(tierByConnection["grok-free"].key, "unknown");
assert.equal(tierByConnection["grok-enterprise"].key, "unknown");
assert.equal(tierByConnection["grok-live"].key, "enterprise");
assert.equal(tierByConnection["codex-fallback"].key, "pro");
assert.equal(tierByConnection["claude-fallback"].key, "pro");
// Filter/stat bucket classification must not invent Free/Enterprise from PSD.
assert.notEqual(tierByConnection["grok-free"].key, "free");
assert.notEqual(tierByConnection["grok-enterprise"].key, "enterprise");
const tierCounts = {
free: 0,
enterprise: 0,
pro: 0,
unknown: 0,
};
for (const conn of connections) {
const key = tierByConnection[conn.id]?.key || "unknown";
if (key in tierCounts) tierCounts[key] += 1;
}
assert.equal(tierCounts.free, 0);
assert.equal(tierCounts.enterprise, 1); // only live Grok Enterprise
assert.equal(tierCounts.pro, 2); // Codex + Claude fallbacks unchanged
assert.equal(tierCounts.unknown, 2); // persisted Free + Enterprise without live plan
});
test("Grok billing rows omit a missing balance and show an explicit localized zero", () => {
const missing = buildGrokBillingCardRows(baseBilling, "en-US");
assert.equal(
missing.some((row) => row.kind === "balance"),
false
);
assert.deepEqual(missing[0], {
kind: "status",
label: "Auto Top-Up",
value: "Unavailable",
});
const zero = buildGrokBillingCardRows({ ...baseBilling, extraCreditsMinorUnits: 0 }, "de-DE");
assert.deepEqual(zero[0], {
kind: "balance",
label: "Extra Usage Credits",
value: "0,00 $",
});
});
test("Grok billing rows distinguish disabled and unavailable and translate enabled details", () => {
const translate: GrokBillingTranslator = (key, fallback) =>
({
grokExtraUsageCredits: "Credits translated",
grokAutoTopUp: "Top-up translated",
grokAutoTopUpEnabled: "On translated",
grokAutoTopUpAt: "threshold translated",
grokAutoTopUpAdd: "add translated",
grokAutoTopUpMax: "maximum translated",
grokAutoTopUpMonth: "month translated",
grokAdditionalCredits: "Buy translated",
})[key] ?? fallback;
const disabled = buildGrokBillingCardRows(
{ ...baseBilling, autoTopUp: { available: true, enabled: false } },
"en-US",
translate
);
assert.equal(disabled.find((row) => row.kind === "status")?.value, "Disabled");
const enabled = buildGrokBillingCardRows(
{
...baseBilling,
extraCreditsMinorUnits: 0,
autoTopUp: {
available: true,
enabled: true,
thresholdMinorUnits: 500,
amountMinorUnits: 2000,
maxMonthlyMinorUnits: 10000,
},
},
"en-US",
translate
);
assert.deepEqual(enabled, [
{ kind: "balance", label: "Credits translated", value: "$0.00" },
{
kind: "status",
label: "Top-up translated",
value:
"On translated · threshold translated $5.00 · add translated $20.00 · maximum translated $100.00/month translated",
},
{
kind: "link",
label: "Buy translated",
href: GROK_BUILD_ADDITIONAL_CREDITS_URL,
target: "_blank",
rel: "noreferrer noopener",
},
]);
});
test("Provider Limits exposes only the sanitized Grok billing contract", () => {
assert.ok(USAGE_SUPPORTED_PROVIDERS.includes("grok-cli"));
assert.equal(PROVIDER_LABEL["grok-cli"], "Grok Build");
const billing = sanitizeGrokBillingStatus({
currency: "USD",
extraCreditsMinorUnits: 0,
autoTopUp: {
available: true,
enabled: true,
thresholdMinorUnits: 500,
amountMinorUnits: 2000,
maxMonthlyMinorUnits: 10000,
paymentMethodId: "secret",
},
additionalCreditsUrl: GROK_BUILD_ADDITIONAL_CREDITS_URL,
rawBody: "secret",
});
assert.deepEqual(billing, {
currency: "USD",
extraCreditsMinorUnits: 0,
autoTopUp: {
available: true,
enabled: true,
thresholdMinorUnits: 500,
amountMinorUnits: 2000,
maxMonthlyMinorUnits: 10000,
},
additionalCreditsUrl: GROK_BUILD_ADDITIONAL_CREDITS_URL,
});
assert.equal(formatGrokMinorUnits(billing?.extraCreditsMinorUnits, "USD", "en-US"), "$0.00");
assert.equal(formatGrokMinorUnits(billing?.autoTopUp.amountMinorUnits, "USD", "en-US"), "$20.00");
assert.equal(
sanitizeGrokBillingStatus({
currency: "USD",
autoTopUp: { available: false },
additionalCreditsUrl: "https://attacker.invalid/credits",
}),
undefined
);
});