Files
OmniRoute/tests/unit/grok-cli-provider-limits-ui.test.ts
Diego Rodrigues de Sa e Souza 3d4f3e4960 test(infra): retry recursive temp-dir removal instead of failing a shard on ENOTEMPTY (#11966) (#11968)
* test(infra): retry recursive temp-dir removal instead of failing a shard on ENOTEMPTY (#11966)

Two shards on release/v3.8.51 went red in one day with the same signature —
"ENOTEMPTY, Directory not empty: /tmp/omniroute-<test>-XXXXXX" — from
combo-same-provider-cascade (Unit Tests fast-path 4/4, on a PR that touches only
.github/) and auth-policy-embeddings-webfetch-7785 (the 20k-test TIA step). Both pass
alone and on re-run: the cleanup races something still writing into the directory
(SQLite WAL/-shm checkpoint, a worker, the backup) and under a loaded hosted runner
the window opens. 1154 test files do their own cleanup with
fs.rmSync(dir, { recursive: true, force: true }); 57 already asked for retries.

One-shot codemod (scripts/ad-hoc/codemod-rm-maxretries.mjs, kept for the record):
every rm / rmSync / rmdirSync option object with `recursive: true` and no
`maxRetries` gains `maxRetries: 5, retryDelay: 100` — Node itself then retries
ENOTEMPTY/EBUSY/EPERM for up to ~0.5 s before giving up. 2243 call sites in 1292
files under tests/, the shared tests/_setup/isolateDataDir.ts exit hook included.
Only the option object changes: no call site, assertion or import is touched.

Validation: prettier and ESLint (with the frozen suppressions) clean on all 1292
files; a random 20-file sample runs green (quota-redis-store hangs identically on
the untouched tree — it needs a Redis on localhost, an environment matter). The
four unit shards on this PR are the full run.

* fix(quality): let check-forgotten-sibling-tests read a 1,000-file diff

The gate shells out to `git diff` through execFileSync with Node's default 1 MB
maxBuffer; the 1,292-file codemod in this PR is the first diff large enough to
overflow it, and the gate died with `spawnSync git ENOBUFS` before comparing
anything. 64 MB is far above any real PR and costs nothing when unused.
2026-08-29 01:17:40 -03:00

304 lines
9.1 KiB
TypeScript
Raw 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
);
});