




















@@ -1,21 +1,31 @@
1-import fs from "node:fs";
2-import path from "node:path";
3-import { afterEach, beforeEach, describe, expect, it } from "vitest";
1+import { beforeEach, describe, expect, it, vi } from "vitest";
42import * as stickerCache from "./sticker-cache-store.js";
536-const TEST_CACHE_DIR = "/tmp/openclaw-test-sticker-cache/telegram";
7-const TEST_CACHE_FILE = path.join(TEST_CACHE_DIR, "sticker-cache.json");
4+const jsonStoreMocks = vi.hoisted(() => {
5+const store: { value: unknown } = { value: null };
6+return {
7+ store,
8+loadJsonFile: vi.fn(() => store.value),
9+saveJsonFile: vi.fn((_file: string, value: unknown) => {
10+store.value = structuredClone(value);
11+}),
12+};
13+});
14+15+vi.mock("openclaw/plugin-sdk/json-store", () => ({
16+loadJsonFile: jsonStoreMocks.loadJsonFile,
17+saveJsonFile: jsonStoreMocks.saveJsonFile,
18+}));
19+20+vi.mock("openclaw/plugin-sdk/state-paths", () => ({
21+resolveStateDir: () => "/tmp/openclaw-test-sticker-cache",
22+}));
823924describe("sticker-cache", () => {
1025beforeEach(() => {
11-process.env.OPENCLAW_STATE_DIR = "/tmp/openclaw-test-sticker-cache";
12-fs.rmSync("/tmp/openclaw-test-sticker-cache", { recursive: true, force: true });
13-fs.mkdirSync(TEST_CACHE_DIR, { recursive: true });
14-});
15-16-afterEach(() => {
17-fs.rmSync("/tmp/openclaw-test-sticker-cache", { recursive: true, force: true });
18-delete process.env.OPENCLAW_STATE_DIR;
26+jsonStoreMocks.store.value = null;
27+jsonStoreMocks.loadJsonFile.mockClear();
28+jsonStoreMocks.saveJsonFile.mockClear();
1929});
20302131describe("getCachedSticker", () => {
@@ -40,7 +50,7 @@ describe("sticker-cache", () => {
4050expect(result).toEqual(sticker);
4151});
425243-it("returns null after cache is cleared", () => {
53+it("returns null after backing store is cleared", () => {
4454const sticker = {
4555fileId: "file123",
4656fileUniqueId: "unique123",
@@ -51,8 +61,7 @@ describe("sticker-cache", () => {
5161stickerCache.cacheSticker(sticker);
5262expect(stickerCache.getCachedSticker("unique123")).not.toBeNull();
536354-// Manually clear the cache file
55-fs.rmSync(TEST_CACHE_FILE, { force: true });
64+jsonStoreMocks.store.value = null;
56655766expect(stickerCache.getCachedSticker("unique123")).toBeNull();
5867});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。