11// Matrix plugin module implements test runtime behavior.
2+ import fs from "node:fs" ;
3+ import path from "node:path" ;
24import {
35 implicitMentionKindWhen ,
46 resolveInboundMentionDecision ,
@@ -11,11 +13,27 @@ import {
1113 createPluginBlobStoreForTests ,
1214 createPluginStateKeyedStoreForTests ,
1315 createPluginStateSyncKeyedStoreForTests ,
16+ resetPluginStateStoreForTests ,
1417} from "openclaw/plugin-sdk/plugin-state-test-runtime" ;
15- import { vi } from "vitest" ;
18+ import { resolvePreferredOpenClawTmpDir } from "openclaw/plugin-sdk/temp-path" ;
19+ import { afterAll , vi } from "vitest" ;
1620import type { PluginRuntime } from "./runtime-api.js" ;
1721import { setMatrixRuntime } from "./runtime.js" ;
1822
23+ const defaultStateDir = fs . realpathSync (
24+ fs . mkdtempSync ( path . join ( resolvePreferredOpenClawTmpDir ( ) , "openclaw-matrix-test-state-" ) ) ,
25+ ) ;
26+
27+ afterAll ( ( ) => {
28+ resetPluginStateStoreForTests ( ) ;
29+ fs . rmSync ( defaultStateDir , {
30+ recursive : true ,
31+ force : true ,
32+ maxRetries : 5 ,
33+ retryDelay : 20 ,
34+ } ) ;
35+ } ) ;
36+
1937type MatrixTestRuntimeOptions = {
2038 cfg ?: Record < string , unknown > ;
2139 logging ?: Partial < PluginRuntime [ "logging" ] > ;
@@ -59,16 +77,15 @@ function createMatrixRuntimeMediaMock(
5977}
6078
6179export function installMatrixTestRuntime ( options : MatrixTestRuntimeOptions = { } ) : void {
62- const osHomedirForTest = ( ) => "/tmp" ;
80+ const stateDir = options . stateDir ?? defaultStateDir ;
6381 const defaultStateDirResolver : NonNullable < PluginRuntime [ "state" ] > [ "resolveStateDir" ] = (
6482 _env ,
65- homeDir ,
66- ) => options . stateDir ?? ( homeDir ?? ( ( ) => "/tmp" ) ) ( ) ;
83+ _homeDir ,
84+ ) => stateDir ;
6785 const resolvePluginStateEnv = ( storeOptions : OpenKeyedStoreOptions ) : NodeJS . ProcessEnv => ( {
6886 ...( storeOptions . env ?? process . env ) ,
6987 OPENCLAW_STATE_DIR :
70- storeOptions . env ?. OPENCLAW_STATE_DIR ?. trim ( ) ||
71- defaultStateDirResolver ( storeOptions . env , osHomedirForTest ) ,
88+ storeOptions . env ?. OPENCLAW_STATE_DIR ?. trim ( ) || defaultStateDirResolver ( storeOptions . env ) ,
7289 } ) ;
7390 const getRuntimeConfig = ( ) => options . cfg ?? { } ;
7491 const logging : PluginRuntime [ "logging" ] | undefined = options . logging
@@ -96,7 +113,7 @@ export function installMatrixTestRuntime(options: MatrixTestRuntimeOptions = {})
96113 openBlobStore : ( < T > ( storeOptions : OpenBlobStoreOptions ) =>
97114 createPluginBlobStoreForTests < T > ( "matrix" , storeOptions , {
98115 ...process . env ,
99- OPENCLAW_STATE_DIR : defaultStateDirResolver ( process . env , osHomedirForTest ) ,
116+ OPENCLAW_STATE_DIR : defaultStateDirResolver ( process . env ) ,
100117 } ) ) as PluginRuntime [ "state" ] [ "openBlobStore" ] ,
101118 openKeyedStore : ( < T > ( storeOptions : OpenKeyedStoreOptions ) =>
102119 createPluginStateKeyedStoreForTests < T > ( "matrix" , {
0 commit comments