mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-01 07:21:12 +00:00
refactor(deadcode): privatize runtime test seams (#108338)
* refactor(sessions): privatize runtime test seams * refactor(commitments): privatize extraction test seams * refactor(flows): privatize doctor health seams * refactor(tasks): privatize registry test seams * build(commitments): bundle Docker test support * chore(deadcode): shrink runtime export baseline
This commit is contained in:
committed by
GitHub
parent
8d5592548b
commit
3d76a4af92
@@ -9,12 +9,12 @@ import {
|
||||
} from "../infra/heartbeat-runner.test-utils.js";
|
||||
import { closeOpenClawStateDatabaseForTest } from "../state/openclaw-state-db.js";
|
||||
import { withEnvAsync } from "../test-utils/env.js";
|
||||
import { enqueueCommitmentExtraction } from "./runtime.js";
|
||||
import {
|
||||
configureCommitmentExtractionRuntime,
|
||||
drainCommitmentExtractionQueue,
|
||||
enqueueCommitmentExtraction,
|
||||
resetCommitmentExtractionRuntimeForTests,
|
||||
} from "./runtime.js";
|
||||
} from "./runtime.test-support.js";
|
||||
import { readCommitmentsForTest } from "./store.test-utils.js";
|
||||
import type { CommitmentExtractionBatchResult, CommitmentExtractionItem } from "./types.js";
|
||||
|
||||
|
||||
40
src/commitments/extraction.test-support.ts
Normal file
40
src/commitments/extraction.test-support.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import type { OpenClawConfig } from "../config/config.js";
|
||||
import "./extraction.js";
|
||||
import type {
|
||||
CommitmentCandidate,
|
||||
CommitmentExtractionBatchResult,
|
||||
CommitmentExtractionItem,
|
||||
} from "./types.js";
|
||||
|
||||
type ValidatedCommitmentCandidate = {
|
||||
item: CommitmentExtractionItem;
|
||||
candidate: CommitmentCandidate;
|
||||
earliestMs: number;
|
||||
latestMs: number;
|
||||
timezone: string;
|
||||
};
|
||||
|
||||
type CommitmentExtractionTestApi = {
|
||||
validateCommitmentCandidates(params: {
|
||||
cfg?: OpenClawConfig;
|
||||
items: CommitmentExtractionItem[];
|
||||
result: CommitmentExtractionBatchResult;
|
||||
nowMs?: number;
|
||||
}): ValidatedCommitmentCandidate[];
|
||||
};
|
||||
|
||||
function getTestApi(): CommitmentExtractionTestApi {
|
||||
const api = (globalThis as Record<PropertyKey, unknown>)[
|
||||
Symbol.for("openclaw.commitmentExtractionTestApi")
|
||||
];
|
||||
if (!api) {
|
||||
throw new Error("commitment extraction test API is unavailable");
|
||||
}
|
||||
return api as CommitmentExtractionTestApi;
|
||||
}
|
||||
|
||||
export function validateCommitmentCandidates(
|
||||
params: Parameters<CommitmentExtractionTestApi["validateCommitmentCandidates"]>[0],
|
||||
): ValidatedCommitmentCandidate[] {
|
||||
return getTestApi().validateCommitmentCandidates(params);
|
||||
}
|
||||
@@ -10,8 +10,8 @@ import {
|
||||
buildCommitmentExtractionPrompt,
|
||||
parseCommitmentExtractionOutput,
|
||||
persistCommitmentExtractionResult,
|
||||
validateCommitmentCandidates,
|
||||
} from "./extraction.js";
|
||||
import { validateCommitmentCandidates } from "./extraction.test-support.js";
|
||||
import { readCommitmentsForTest } from "./store.test-utils.js";
|
||||
import type { CommitmentCandidate, CommitmentExtractionItem } from "./types.js";
|
||||
|
||||
|
||||
@@ -273,7 +273,7 @@ function resolveMinimumDueMs(params: {
|
||||
return params.nowMs + intervalMs;
|
||||
}
|
||||
|
||||
export function validateCommitmentCandidates(params: {
|
||||
function validateCommitmentCandidates(params: {
|
||||
cfg?: OpenClawConfig;
|
||||
items: CommitmentExtractionItem[];
|
||||
result: CommitmentExtractionBatchResult;
|
||||
@@ -370,3 +370,8 @@ export async function persistCommitmentExtractionResult(params: {
|
||||
}
|
||||
return created;
|
||||
}
|
||||
|
||||
if (process.env.VITEST || process.env.NODE_ENV === "test") {
|
||||
(globalThis as Record<PropertyKey, unknown>)[Symbol.for("openclaw.commitmentExtractionTestApi")] =
|
||||
{ validateCommitmentCandidates };
|
||||
}
|
||||
|
||||
47
src/commitments/runtime.test-support.ts
Normal file
47
src/commitments/runtime.test-support.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import type { OpenClawConfig } from "../config/config.js";
|
||||
import "./runtime.js";
|
||||
import type { CommitmentExtractionBatchResult, CommitmentExtractionItem } from "./types.js";
|
||||
|
||||
type TimerHandle = ReturnType<typeof setTimeout>;
|
||||
|
||||
type CommitmentExtractionRuntime = {
|
||||
extractBatch?: (params: {
|
||||
cfg?: OpenClawConfig;
|
||||
items: CommitmentExtractionItem[];
|
||||
}) => Promise<CommitmentExtractionBatchResult>;
|
||||
resolveDefaultModel?: (params: { cfg: OpenClawConfig; agentId?: string }) => {
|
||||
provider: string;
|
||||
model: string;
|
||||
};
|
||||
setTimer?: (callback: () => void, delayMs: number) => TimerHandle;
|
||||
clearTimer?: (timer: TimerHandle) => void;
|
||||
forceInTests?: boolean;
|
||||
};
|
||||
|
||||
type CommitmentRuntimeTestApi = {
|
||||
configureCommitmentExtractionRuntime(next: CommitmentExtractionRuntime): void;
|
||||
drainCommitmentExtractionQueue(): Promise<number>;
|
||||
resetCommitmentExtractionRuntimeForTests(): void;
|
||||
};
|
||||
|
||||
function getTestApi(): CommitmentRuntimeTestApi {
|
||||
const api = (globalThis as Record<PropertyKey, unknown>)[
|
||||
Symbol.for("openclaw.commitmentRuntimeTestApi")
|
||||
];
|
||||
if (!api) {
|
||||
throw new Error("commitment runtime test API is unavailable");
|
||||
}
|
||||
return api as CommitmentRuntimeTestApi;
|
||||
}
|
||||
|
||||
export function configureCommitmentExtractionRuntime(next: CommitmentExtractionRuntime): void {
|
||||
getTestApi().configureCommitmentExtractionRuntime(next);
|
||||
}
|
||||
|
||||
export async function drainCommitmentExtractionQueue(): Promise<number> {
|
||||
return await getTestApi().drainCommitmentExtractionQueue();
|
||||
}
|
||||
|
||||
export function resetCommitmentExtractionRuntimeForTests(): void {
|
||||
getTestApi().resetCommitmentExtractionRuntimeForTests();
|
||||
}
|
||||
@@ -6,12 +6,12 @@ import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import type { OpenClawConfig } from "../config/config.js";
|
||||
import { closeOpenClawStateDatabaseForTest } from "../state/openclaw-state-db.js";
|
||||
import { captureEnv, setTestEnvValue } from "../test-utils/env.js";
|
||||
import { enqueueCommitmentExtraction } from "./runtime.js";
|
||||
import {
|
||||
configureCommitmentExtractionRuntime,
|
||||
drainCommitmentExtractionQueue,
|
||||
enqueueCommitmentExtraction,
|
||||
resetCommitmentExtractionRuntimeForTests,
|
||||
} from "./runtime.js";
|
||||
} from "./runtime.test-support.js";
|
||||
import { readCommitmentsForTest, seedCommitmentsForTest } from "./store.test-utils.js";
|
||||
import type { CommitmentExtractionBatchResult, CommitmentExtractionItem } from "./types.js";
|
||||
|
||||
|
||||
@@ -91,12 +91,12 @@ function scheduleDrainSoon(debounceMs: number): void {
|
||||
}
|
||||
|
||||
/** Installs runtime hooks for extraction tests or alternate batch extraction. */
|
||||
export function configureCommitmentExtractionRuntime(next: CommitmentExtractionRuntime): void {
|
||||
function configureCommitmentExtractionRuntime(next: CommitmentExtractionRuntime): void {
|
||||
runtime = next;
|
||||
}
|
||||
|
||||
/** Clears queued work, timers, and injected hooks for isolated tests. */
|
||||
export function resetCommitmentExtractionRuntimeForTests(): void {
|
||||
function resetCommitmentExtractionRuntimeForTests(): void {
|
||||
if (timer) {
|
||||
clearTimer(timer);
|
||||
}
|
||||
@@ -299,7 +299,7 @@ function takeAgentBatch(
|
||||
}
|
||||
|
||||
/** Drains queued extraction work in batches and returns processed item count. */
|
||||
export async function drainCommitmentExtractionQueue(): Promise<number> {
|
||||
async function drainCommitmentExtractionQueue(): Promise<number> {
|
||||
if (draining) {
|
||||
return 0;
|
||||
}
|
||||
@@ -357,3 +357,15 @@ export async function drainCommitmentExtractionQueue(): Promise<number> {
|
||||
draining = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
process.env.VITEST ||
|
||||
process.env.NODE_ENV === "test" ||
|
||||
process.env.OPENCLAW_COMMITMENTS_SAFETY_E2E === "1"
|
||||
) {
|
||||
(globalThis as Record<PropertyKey, unknown>)[Symbol.for("openclaw.commitmentRuntimeTestApi")] = {
|
||||
configureCommitmentExtractionRuntime,
|
||||
drainCommitmentExtractionQueue,
|
||||
resetCommitmentExtractionRuntimeForTests,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user