Skip to content

Commit ef478c1

Browse files
authored
test(matrix): isolate runtime state (#118892)
1 parent 7741cbd commit ef478c1

2 files changed

Lines changed: 59 additions & 7 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Matrix tests cover isolated runtime state fixtures.
2+
import fs from "node:fs";
3+
import path from "node:path";
4+
import { resolvePreferredOpenClawTmpDir } from "openclaw/plugin-sdk/temp-path";
5+
import { describe, expect, it } from "vitest";
6+
import { getMatrixRuntime } from "./runtime.js";
7+
import { installMatrixTestRuntime } from "./test-runtime.js";
8+
9+
describe("installMatrixTestRuntime", () => {
10+
it("uses a canonical isolated state directory by default", () => {
11+
installMatrixTestRuntime();
12+
13+
const runtime = getMatrixRuntime();
14+
const stateDir = runtime.state.resolveStateDir(process.env);
15+
const tempRoot = fs.realpathSync(resolvePreferredOpenClawTmpDir());
16+
17+
expect(stateDir).toBe(fs.realpathSync(stateDir));
18+
expect(path.dirname(stateDir)).toBe(tempRoot);
19+
expect(path.basename(stateDir)).toMatch(/^openclaw-matrix-test-state-/u);
20+
21+
const store = runtime.state.openSyncKeyedStore<string>({
22+
namespace: "test-runtime",
23+
maxEntries: 1,
24+
});
25+
store.register("state", "isolated");
26+
expect(store.lookup("state")).toBe("isolated");
27+
});
28+
29+
it("preserves an explicit state directory override", () => {
30+
const stateDir = path.join(resolvePreferredOpenClawTmpDir(), "matrix-explicit-state");
31+
installMatrixTestRuntime({ stateDir });
32+
33+
expect(getMatrixRuntime().state.resolveStateDir(process.env)).toBe(stateDir);
34+
});
35+
});

‎extensions/matrix/src/test-runtime.ts‎

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// Matrix plugin module implements test runtime behavior.
2+
import fs from "node:fs";
3+
import path from "node:path";
24
import {
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";
1620
import type { PluginRuntime } from "./runtime-api.js";
1721
import { 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+
1937
type MatrixTestRuntimeOptions = {
2038
cfg?: Record<string, unknown>;
2139
logging?: Partial<PluginRuntime["logging"]>;
@@ -59,16 +77,15 @@ function createMatrixRuntimeMediaMock(
5977
}
6078

6179
export 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

Comments
 (0)