22
33import { expectDefined } from "@openclaw/normalization-core" ;
44import { MAX_TIMER_TIMEOUT_MS } from "@openclaw/normalization-core/number-coercion" ;
5- import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
5+ import { afterAll , afterEach , beforeAll , beforeEach , describe , expect , it , vi } from "vitest" ;
66import type { SessionEntry } from "../config/sessions.js" ;
77import type { ProviderResolveModelRoutesContext } from "../plugin-sdk/provider-model-types.js" ;
88import { getCurrentPluginMetadataSnapshot } from "../plugins/current-plugin-metadata-snapshot.js" ;
@@ -14,16 +14,40 @@ import {
1414 mintSecretSentinel ,
1515 resolveSecretSentinel ,
1616} from "../secrets/sentinel.js" ;
17+ import {
18+ createOpenClawTestState ,
19+ type OpenClawTestState ,
20+ } from "../test-utils/openclaw-test-state.js" ;
21+ import { guardModelFixtureWorkspace } from "./embedded-agent-runner/model.fixture.test-support.js" ;
1722import {
1823 createModelGenerationFixture ,
19- GENERATION_WORKSPACE_DIR ,
2024 publishCurrentModelGeneration ,
2125 resetModelGenerationFixtureState ,
2226} from "./embedded-agent-runner/model.generation-scope.test-support.js" ;
2327import type { AgentHarnessHostCapabilities } from "./harness/host-capability-types.js" ;
2428import type { AgentHarness } from "./harness/types.js" ;
2529import type { AgentRuntimeAuthPlan } from "./runtime-plan/types.js" ;
2630
31+ let state : OpenClawTestState ;
32+ let workspaceGuard : ReturnType < typeof guardModelFixtureWorkspace > ;
33+ beforeAll ( async ( ) => {
34+ state = await createOpenClawTestState ( { label : "btw-model" } ) ;
35+ } ) ;
36+ beforeEach ( ( ) => {
37+ workspaceGuard = guardModelFixtureWorkspace ( state . root ) ;
38+ } ) ;
39+ afterEach ( ( ) => {
40+ try {
41+ workspaceGuard . verify ( ) ;
42+ } finally {
43+ workspaceGuard . spy . mockRestore ( ) ;
44+ }
45+ } ) ;
46+ afterAll ( async ( ) => {
47+ defaultPluginMetadataSnapshot = undefined ;
48+ await state . cleanup ( ) ;
49+ } ) ;
50+
2751const streamSimpleMock = vi . fn ( ) ;
2852const readFileMock = vi . fn ( ) ;
2953const parseSessionEntriesMock = vi . fn ( ) ;
@@ -728,11 +752,16 @@ describe("runBtwSideQuestion", () => {
728752 resolveProviderEntryApiKeyProfileReferenceMock . mockReset ( ) ;
729753 resolveProviderEntryApiKeyProfileReferenceMock . mockReturnValue ( { kind : "none" } ) ;
730754 clearAgentHarnesses ( ) ;
731- defaultPluginMetadataSnapshot ??= resolvePluginMetadataSnapshot ( {
732- config : { } ,
733- workspaceDir : "/tmp/workspace" ,
734- allowCurrent : false ,
735- } ) ;
755+ if ( ! defaultPluginMetadataSnapshot ) {
756+ defaultPluginMetadataSnapshot = resolvePluginMetadataSnapshot ( {
757+ config : { } ,
758+ workspaceDir : state . workspaceDir ,
759+ allowCurrent : false ,
760+ } ) ;
761+ expect ( workspaceGuard . spy ) . toHaveBeenCalledWith (
762+ expect . objectContaining ( { workspaceDir : state . workspaceDir } ) ,
763+ ) ;
764+ }
736765 preparedRuntimeSnapshotState . snapshot = {
737766 metadataSnapshot : defaultPluginMetadataSnapshot ,
738767 } ;
@@ -916,13 +945,17 @@ describe("runBtwSideQuestion", () => {
916945 it ( "keeps model, runtime auth, and stream selection on prepared A after current advances to B" , async ( ) => {
917946 const cfg = { agents : { entries : { main : { default : true } } } } as never ;
918947 const generationA = createModelGenerationFixture ( {
948+ agentDir : state . agentDir ( ) ,
949+ workspaceDir : state . workspaceDir ,
919950 config : cfg ,
920951 label : "btw-a" ,
921952 provider : "local-proxy" ,
922953 requestProvider : "local-proxy" ,
923954 modelId : "side-model" ,
924955 } ) ;
925956 const generationB = createModelGenerationFixture ( {
957+ agentDir : state . agentDir ( ) ,
958+ workspaceDir : state . workspaceDir ,
926959 config : cfg ,
927960 label : "btw-b" ,
928961 provider : "local-proxy" ,
@@ -931,7 +964,7 @@ describe("runBtwSideQuestion", () => {
931964 } ) ;
932965 preparedRuntimeSnapshotState . snapshot = generationA . preparedModelRuntime ;
933966 preparedRuntimeSnapshotState . useSnapshotPluginRegistry = true ;
934- resolveAgentWorkspaceDirMock . mockReturnValue ( GENERATION_WORKSPACE_DIR ) ;
967+ resolveAgentWorkspaceDirMock . mockReturnValue ( state . workspaceDir ) ;
935968 publishCurrentModelGeneration ( generationB ) ;
936969 const runtimeAuthA = vi . fn ( async ( ) => ( { apiKey : "runtime-auth-a" } ) ) ;
937970 const runtimeAuthB = vi . fn ( async ( ) => ( { apiKey : "runtime-auth-b" } ) ) ;
@@ -953,7 +986,7 @@ describe("runBtwSideQuestion", () => {
953986 resolveModelWithRegistryMock . mockImplementation ( ( ) => {
954987 const snapshot = getCurrentPluginMetadataSnapshot ( {
955988 config : cfg ,
956- workspaceDir : GENERATION_WORKSPACE_DIR ,
989+ workspaceDir : state . workspaceDir ,
957990 } ) ;
958991 const label = snapshot === generationA . metadataSnapshot ? "A" : "B" ;
959992 return {
0 commit comments