test: scope unit coverage gate

This commit is contained in:
Vincent Koc
2026-05-05 13:28:34 -07:00
parent c319f3c4d5
commit 6455ed24cf
3 changed files with 104 additions and 2 deletions

View File

@@ -9,7 +9,7 @@ title: "Tests"
- Update and plugin package validation: [Testing updates and plugins](/help/testing-updates-plugins)
- `pnpm test:force`: Kills any lingering gateway process holding the default control port, then runs the full Vitest suite with an isolated gateway port so server tests dont collide with a running instance. Use this when a prior gateway run left port 18789 occupied.
- `pnpm test:coverage`: Runs the unit suite with V8 coverage (via `vitest.unit.config.ts`). This is a loaded-file unit coverage gate, not whole-repo all-file coverage. Thresholds are 70% lines/functions/statements and 55% branches. Because `coverage.all` is false, the gate measures files loaded by the unit coverage suite instead of treating every split-lane source file as uncovered.
- `pnpm test:coverage`: Runs the unit suite with V8 coverage (via `vitest.unit.config.ts`). This is a default-unit-lane coverage gate, not whole-repo all-file coverage. Thresholds are 70% lines/functions/statements and 55% branches. Because `coverage.all` is false and the default lane scopes coverage includes to non-fast unit tests with sibling source files, the gate measures source owned by this lane instead of every transitive import it happens to load.
- `pnpm test:coverage:changed`: Runs unit coverage only for files changed since `origin/main`.
- `pnpm test:changed`: cheap smart changed test run. It runs precise targets from direct test edits, sibling `*.test.ts` files, explicit source mappings, and the local import graph. Broad/config/package changes are skipped unless they map to precise tests.
- `OPENCLAW_TEST_CHANGED_BROAD=1 pnpm test:changed`: explicit broad changed test run. Use it when a test harness/config/package edit should fall back to Vitest's broader changed-test behavior.

View File

@@ -6,6 +6,7 @@ import {
createUnitVitestConfigWithOptions,
loadExtraExcludePatternsFromEnv,
loadIncludePatternsFromEnv,
resolveDefaultUnitCoverageIncludePatterns,
} from "./vitest/vitest.unit.config.ts";
const patternFiles = createPatternFileHelper("openclaw-vitest-unit-config-");
@@ -118,6 +119,41 @@ describe("unit vitest config", () => {
);
});
it("scopes default coverage to source files owned by the unit lane", () => {
const unitConfig = createUnitVitestConfig({});
expect(unitConfig.test?.coverage?.include).toEqual(
expect.arrayContaining([
"src/commitments/runtime.ts",
"src/media-generation/runtime-shared.ts",
"src/web-search/runtime.ts",
]),
);
expect(unitConfig.test?.coverage?.include).not.toEqual(
expect.arrayContaining(["src/markdown/render.ts", "src/security/audit-workspace-skills.ts"]),
);
});
it("derives default coverage includes from non-fast unit tests with sibling source files", () => {
expect(resolveDefaultUnitCoverageIncludePatterns()).toEqual(
expect.arrayContaining([
"packages/memory-host-sdk/src/host/embeddings.ts",
"src/commitments/store.ts",
"src/tools/planner.ts",
]),
);
});
it("leaves coverage include filters unset for explicit unit include lists", () => {
const unitConfig = createUnitVitestConfigWithOptions(
{},
{
includePatterns: ["src/commitments/runtime.test.ts"],
},
);
expect(unitConfig.test?.coverage?.include).toBeUndefined();
});
it("keeps bundled unit include files out of the resolved exclude list", () => {
const unitConfig = createUnitVitestConfigWithOptions(
{},

View File

@@ -1,14 +1,18 @@
import fs from "node:fs";
import path from "node:path";
import { defineConfig } from "vitest/config";
import { loadPatternListFromEnv, narrowIncludePatternsForCli } from "./vitest.pattern-file.ts";
import { resolveVitestIsolation } from "./vitest.scoped-config.ts";
import {
nonIsolatedRunnerPath,
repoRoot,
resolveRepoRootPath,
sharedVitestConfig,
} from "./vitest.shared.config.ts";
import { getUnitFastTestFiles } from "./vitest.unit-fast-paths.mjs";
import {
isBundledPluginDependentUnitTestFile,
isUnitConfigTestFile,
unitTestAdditionalExcludePatterns,
unitTestIncludePatterns,
} from "./vitest.unit-paths.mjs";
@@ -28,6 +32,58 @@ export function loadExtraExcludePatternsFromEnv(
return loadPatternListFromEnv("OPENCLAW_VITEST_EXTRA_EXCLUDE_FILE", env) ?? [];
}
const defaultUnitCoverageRoots = ["src", "packages", "test"] as const;
function toRepoPath(filePath: string): string {
return path.relative(repoRoot, filePath).split(path.sep).join("/");
}
function collectTestFiles(dir: string): string[] {
if (!fs.existsSync(dir)) {
return [];
}
const files: string[] = [];
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
if (entry.name === "node_modules" || entry.name === "dist" || entry.name === "coverage") {
continue;
}
const entryPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
files.push(...collectTestFiles(entryPath));
} else if (entry.isFile() && entry.name.endsWith(".test.ts")) {
files.push(toRepoPath(entryPath));
}
}
return files;
}
function resolveSiblingSourceFile(testFile: string): string | null {
if (!testFile.endsWith(".test.ts")) {
return null;
}
const sourceFile = testFile.replace(/\.test\.ts$/u, ".ts");
return fs.existsSync(resolveRepoRootPath(sourceFile)) ? sourceFile : null;
}
export function resolveDefaultUnitCoverageIncludePatterns(
unitFastTestFiles = getUnitFastTestFiles(),
): string[] {
const fastTestFiles = new Set(unitFastTestFiles);
const sourceFiles = new Set<string>();
for (const root of defaultUnitCoverageRoots) {
for (const testFile of collectTestFiles(resolveRepoRootPath(root))) {
if (!isUnitConfigTestFile(testFile) || fastTestFiles.has(testFile)) {
continue;
}
const sourceFile = resolveSiblingSourceFile(testFile);
if (sourceFile !== null) {
sourceFiles.add(sourceFile);
}
}
}
return [...sourceFiles].toSorted((left, right) => left.localeCompare(right));
}
export function createUnitVitestConfigWithOptions(
env: Record<string, string | undefined> = process.env,
options: {
@@ -40,8 +96,15 @@ export function createUnitVitestConfigWithOptions(
) {
const isolate = resolveVitestIsolation(env);
const unitFastTestFiles = getUnitFastTestFiles();
const envIncludePatterns = loadIncludePatternsFromEnv(env);
const defaultIncludePatterns = options.includePatterns ?? unitTestIncludePatterns;
const cliIncludePatterns = narrowIncludePatternsForCli(defaultIncludePatterns, options.argv);
const coverageIncludePatterns =
options.includePatterns === undefined &&
envIncludePatterns === null &&
cliIncludePatterns === null
? resolveDefaultUnitCoverageIncludePatterns(unitFastTestFiles)
: null;
const protectedIncludeFiles = new Set(
defaultIncludePatterns.filter((pattern) => isBundledPluginDependentUnitTestFile(pattern)),
);
@@ -66,7 +129,7 @@ export function createUnitVitestConfigWithOptions(
),
),
],
include: loadIncludePatternsFromEnv(env) ?? cliIncludePatterns ?? defaultIncludePatterns,
include: envIncludePatterns ?? cliIncludePatterns ?? defaultIncludePatterns,
exclude: [
...new Set([
...exclude,
@@ -78,6 +141,9 @@ export function createUnitVitestConfigWithOptions(
],
coverage: {
...sharedTest.coverage,
...(coverageIncludePatterns !== null && coverageIncludePatterns.length > 0
? { include: coverageIncludePatterns }
: {}),
exclude: [
...new Set([
...(sharedTest.coverage?.exclude ?? []),