feat(FN-3263): strengthen worker-budget regression test coverage

Strengthens regression test coverage for worker-budget behavior in the vitest workers test suite, adding 14 lines to cover edge cases.

Fusion-Task-Id: FN-3263
This commit is contained in:
Fusion
2026-05-03 09:46:49 -07:00
committed by gsxdsm
parent f50e7c5305
commit 0bb923b044
2 changed files with 75 additions and 13 deletions

View File

@@ -4,6 +4,12 @@ interface ComputeMaxWorkersOptions {
defaultCap?: number;
}
function parsePositiveInt(value: string | undefined): number | undefined {
const parsed = Number.parseInt(value ?? "", 10);
if (!Number.isFinite(parsed) || parsed <= 0) return undefined;
return parsed;
}
// Shared worker-budget computation for every package's vitest.config.
//
// Resolution order:
@@ -19,21 +25,21 @@ export function computeMaxWorkers(options: ComputeMaxWorkersOptions = {}): numbe
const cpuCap = Math.max(1, cpus().length - 1);
const explicit = Number.parseInt(process.env.VITEST_MAX_WORKERS ?? "", 10);
if (Number.isFinite(explicit) && explicit > 0) {
const clamped = Math.min(Math.max(1, explicit), cpuCap);
process.env.VITEST_MAX_WORKERS = String(clamped);
return clamped;
}
const totalBudget = Number.parseInt(process.env.FUSION_TEST_TOTAL_WORKERS ?? "", 10);
const concurrency = Math.max(
1,
Number.parseInt(process.env.FUSION_TEST_CONCURRENCY ?? "1", 10) || 1,
);
const explicit = parsePositiveInt(process.env.VITEST_MAX_WORKERS);
const totalBudget = parsePositiveInt(process.env.FUSION_TEST_TOTAL_WORKERS);
const concurrency = Math.max(1, parsePositiveInt(process.env.FUSION_TEST_CONCURRENCY) ?? 1);
let workers: number;
if (Number.isFinite(totalBudget) && totalBudget > 0) {
if (explicit !== undefined) {
// In recursive workspace runs we provide a global worker budget via
// FUSION_TEST_TOTAL_WORKERS/FUSION_TEST_CONCURRENCY. Clamp explicit
// VITEST_MAX_WORKERS to that per-package share so `VITEST_MAX_WORKERS=4`
// at the workspace root doesn't fan out to 4 workers in every package.
const workspaceBudget = totalBudget !== undefined
? Math.max(1, Math.floor(totalBudget / concurrency))
: undefined;
workers = workspaceBudget !== undefined ? Math.min(explicit, workspaceBudget) : explicit;
} else if (totalBudget !== undefined) {
workers = Math.max(1, Math.floor(totalBudget / concurrency));
} else {
workers = defaultCap;

View File

@@ -0,0 +1,56 @@
import { afterEach, describe, expect, it } from "vitest";
import { cpus } from "node:os";
import { computeMaxWorkers } from "../__test-utils__/vitest-workers";
const ORIGINAL_ENV = { ...process.env };
describe("computeMaxWorkers", () => {
afterEach(() => {
process.env = { ...ORIGINAL_ENV };
});
it("uses explicit VITEST_MAX_WORKERS for package-scoped runs", () => {
process.env.VITEST_MAX_WORKERS = "4";
delete process.env.FUSION_TEST_TOTAL_WORKERS;
delete process.env.FUSION_TEST_CONCURRENCY;
const workers = computeMaxWorkers({ defaultCap: 2 });
const cpuCap = Math.max(1, cpus().length - 1);
expect(workers).toBe(Math.min(4, cpuCap));
expect(process.env.VITEST_MAX_WORKERS).toBe(String(workers));
});
it("clamps explicit VITEST_MAX_WORKERS to workspace per-package budget", () => {
process.env.VITEST_MAX_WORKERS = "4";
process.env.FUSION_TEST_TOTAL_WORKERS = "4";
process.env.FUSION_TEST_CONCURRENCY = "2";
const workers = computeMaxWorkers({ defaultCap: 2 });
expect(workers).toBe(2);
expect(process.env.VITEST_MAX_WORKERS).toBe("2");
});
it("still derives workers from workspace budget when explicit override is absent", () => {
delete process.env.VITEST_MAX_WORKERS;
process.env.FUSION_TEST_TOTAL_WORKERS = "6";
process.env.FUSION_TEST_CONCURRENCY = "2";
const workers = computeMaxWorkers({ defaultCap: 2 });
expect(workers).toBe(3);
expect(process.env.VITEST_MAX_WORKERS).toBe("3");
});
it("ignores invalid env values and falls back to default cap", () => {
process.env.VITEST_MAX_WORKERS = "abc";
process.env.FUSION_TEST_TOTAL_WORKERS = "0";
process.env.FUSION_TEST_CONCURRENCY = "-1";
const workers = computeMaxWorkers({ defaultCap: 2 });
expect(workers).toBe(2);
expect(process.env.VITEST_MAX_WORKERS).toBe("2");
});
});