|
1 | 1 | import fs from "node:fs"; |
2 | 2 | import os from "node:os"; |
3 | 3 | import path from "node:path"; |
4 | | -import { afterEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
5 | 5 | import { |
6 | 6 | getHomeDir, |
| 7 | + getQQBotDataDir, |
| 8 | + getQQBotDataPath, |
7 | 9 | resolveQQBotLocalMediaPath, |
8 | 10 | resolveQQBotPayloadLocalFilePath, |
9 | 11 | } from "./platform.js"; |
10 | 12 |
|
| 13 | +describe("QQBot data directory with QQBOT_DATA_DIR", () => { |
| 14 | + const createdPaths: string[] = []; |
| 15 | + let originalDataDir: string | undefined; |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + originalDataDir = process.env.QQBOT_DATA_DIR; |
| 19 | + }); |
| 20 | + |
| 21 | + afterEach(() => { |
| 22 | + vi.restoreAllMocks(); |
| 23 | + if (originalDataDir === undefined) { |
| 24 | + delete process.env.QQBOT_DATA_DIR; |
| 25 | + } else { |
| 26 | + process.env.QQBOT_DATA_DIR = originalDataDir; |
| 27 | + } |
| 28 | + for (const target of createdPaths.splice(0)) { |
| 29 | + fs.rmSync(target, { recursive: true, force: true }); |
| 30 | + } |
| 31 | + }); |
| 32 | + |
| 33 | + it("uses QQBOT_DATA_DIR when set", () => { |
| 34 | + const customDir = fs.mkdtempSync(path.join(os.tmpdir(), "qqbot-custom-data-")); |
| 35 | + createdPaths.push(customDir); |
| 36 | + process.env.QQBOT_DATA_DIR = customDir; |
| 37 | + |
| 38 | + const dataDir = getQQBotDataDir(); |
| 39 | + expect(dataDir).toBe(customDir); |
| 40 | + expect(fs.existsSync(dataDir)).toBe(true); |
| 41 | + }); |
| 42 | + |
| 43 | + it("uses QQBOT_DATA_DIR with subpaths", () => { |
| 44 | + const customDir = fs.mkdtempSync(path.join(os.tmpdir(), "qqbot-custom-data-")); |
| 45 | + createdPaths.push(customDir); |
| 46 | + process.env.QQBOT_DATA_DIR = customDir; |
| 47 | + |
| 48 | + const sessionsDir = getQQBotDataDir("sessions", "test"); |
| 49 | + expect(sessionsDir).toBe(path.join(customDir, "sessions", "test")); |
| 50 | + expect(fs.existsSync(sessionsDir)).toBe(true); |
| 51 | + }); |
| 52 | + |
| 53 | + it("falls back to ~/.openclaw/qqbot when QQBOT_DATA_DIR is not set", () => { |
| 54 | + delete process.env.QQBOT_DATA_DIR; |
| 55 | + const homeDir = getHomeDir(); |
| 56 | + const expectedDir = path.join(homeDir, ".openclaw", "qqbot"); |
| 57 | + |
| 58 | + const dataDir = getQQBotDataDir(); |
| 59 | + expect(dataDir).toBe(expectedDir); |
| 60 | + }); |
| 61 | + |
| 62 | + it("expands tilde in QQBOT_DATA_DIR", () => { |
| 63 | + const homeDir = getHomeDir(); |
| 64 | + const customDir = path.join(homeDir, "custom-qqbot-data"); |
| 65 | + process.env.QQBOT_DATA_DIR = "~/custom-qqbot-data"; |
| 66 | + |
| 67 | + const dataDir = getQQBotDataPath(); |
| 68 | + expect(dataDir).toBe(customDir); |
| 69 | + }); |
| 70 | + |
| 71 | + it("returns path without creating when using getQQBotDataPath", () => { |
| 72 | + const customDir = path.join(os.tmpdir(), "qqbot-not-created-yet"); |
| 73 | + process.env.QQBOT_DATA_DIR = customDir; |
| 74 | + |
| 75 | + const dataPath = getQQBotDataPath("not-created", "subdir"); |
| 76 | + expect(dataPath).toBe(path.join(customDir, "not-created", "subdir")); |
| 77 | + expect(fs.existsSync(dataPath)).toBe(false); |
| 78 | + }); |
| 79 | +}); |
| 80 | + |
11 | 81 | describe("qqbot local media path remapping", () => { |
12 | 82 | const createdPaths: string[] = []; |
13 | 83 |
|
|
0 commit comments