Skip to content

Commit 2e97f72

Browse files
committed
fix(qqbot): use state resolver for credential backups
1 parent 4e78163 commit 2e97f72

3 files changed

Lines changed: 34 additions & 12 deletions

File tree

extensions/qqbot/src/engine/utils/data-paths.test.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ import { getCredentialBackupFile, getLegacyCredentialBackupFile } from "./data-p
66

77
const createdStateDirs: string[] = [];
88

9+
function createTempDir(prefix: string): string {
10+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
11+
createdStateDirs.push(dir);
12+
return dir;
13+
}
14+
915
describe("qqbot credential backup paths", () => {
1016
afterEach(() => {
1117
vi.unstubAllEnvs();
@@ -15,8 +21,7 @@ describe("qqbot credential backup paths", () => {
1521
});
1622

1723
it("scopes credential backups to the active OPENCLAW_STATE_DIR", () => {
18-
const stateDir = fs.mkdtempSync(path.join(os.tmpdir(), "qqbot-state-"));
19-
createdStateDirs.push(stateDir);
24+
const stateDir = createTempDir("qqbot-state-");
2025
vi.stubEnv("OPENCLAW_STATE_DIR", stateDir);
2126

2227
expect(getCredentialBackupFile("default")).toBe(
@@ -28,9 +33,8 @@ describe("qqbot credential backup paths", () => {
2833
});
2934

3035
it("keeps same account IDs isolated across different state directories", () => {
31-
const stateDirA = fs.mkdtempSync(path.join(os.tmpdir(), "qqbot-state-a-"));
32-
const stateDirB = fs.mkdtempSync(path.join(os.tmpdir(), "qqbot-state-b-"));
33-
createdStateDirs.push(stateDirA, stateDirB);
36+
const stateDirA = createTempDir("qqbot-state-a-");
37+
const stateDirB = createTempDir("qqbot-state-b-");
3438

3539
vi.stubEnv("OPENCLAW_STATE_DIR", stateDirA);
3640
const gatewayAPath = getCredentialBackupFile("default");
@@ -46,4 +50,25 @@ describe("qqbot credential backup paths", () => {
4650
);
4751
expect(gatewayBPath).not.toBe(gatewayAPath);
4852
});
53+
54+
it("uses OPENCLAW_HOME for default credential backup state", () => {
55+
const homeDir = createTempDir("qqbot-openclaw-home-");
56+
vi.stubEnv("OPENCLAW_STATE_DIR", "");
57+
vi.stubEnv("OPENCLAW_HOME", homeDir);
58+
59+
expect(getCredentialBackupFile("default")).toBe(
60+
path.join(homeDir, ".openclaw", "qqbot", "data", "credential-backup-default.json"),
61+
);
62+
});
63+
64+
it("expands tilde state-dir overrides through the canonical state resolver", () => {
65+
const homeDir = createTempDir("qqbot-home-");
66+
vi.stubEnv("HOME", homeDir);
67+
vi.stubEnv("OPENCLAW_HOME", "");
68+
vi.stubEnv("OPENCLAW_STATE_DIR", "~/gateway-a");
69+
70+
expect(getCredentialBackupFile("default")).toBe(
71+
path.join(homeDir, "gateway-a", "qqbot", "data", "credential-backup-default.json"),
72+
);
73+
});
4974
});

extensions/qqbot/src/engine/utils/data-paths.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212

1313
import path from "node:path";
14-
import { getQQBotDataPath, normalizePath } from "./platform.js";
14+
import { resolveStateDir } from "openclaw/plugin-sdk/state-paths";
1515

1616
/**
1717
* Normalise an identifier so it is safe to embed in a filename.
@@ -22,12 +22,7 @@ function safeName(id: string): string {
2222
}
2323

2424
function getCredentialBackupRoot(): string {
25-
const stateDir =
26-
process.env.OPENCLAW_STATE_DIR?.trim() || process.env.CLAWDBOT_STATE_DIR?.trim();
27-
if (stateDir) {
28-
return path.join(normalizePath(stateDir), "qqbot", "data");
29-
}
30-
return getQQBotDataPath("data");
25+
return path.join(resolveStateDir(process.env), "qqbot", "data");
3126
}
3227

3328
// ---- credential backup ----

extensions/qqbot/src/engine/utils/platform-storage-laziness.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { afterEach, describe, expect, it, vi } from "vitest";
66
const createdHomes: string[] = [];
77

88
async function useMockHome(homeDir: string): Promise<void> {
9+
vi.stubEnv("HOME", homeDir);
910
vi.resetModules();
1011
vi.doMock("node:os", async (importOriginal) => {
1112
const actual = await importOriginal<typeof import("node:os")>();
@@ -26,6 +27,7 @@ function makeHome(): string {
2627
describe("qqbot storage laziness", () => {
2728
afterEach(() => {
2829
vi.doUnmock("node:os");
30+
vi.unstubAllEnvs();
2931
vi.resetModules();
3032
for (const home of createdHomes.splice(0)) {
3133
fs.rmSync(home, { recursive: true, force: true });

0 commit comments

Comments
 (0)