1+ import { mkdtempSync , rmSync } from "node:fs" ;
2+ import os from "node:os" ;
13import path from "node:path" ;
24import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts" ;
35import { resetInboundDedupe } from "openclaw/plugin-sdk/reply-runtime" ;
46import type { GetReplyOptions , MsgContext } from "openclaw/plugin-sdk/reply-runtime" ;
5- import { beforeEach , vi , type Mock } from "vitest" ;
7+ import { afterEach , beforeEach , vi , type Mock } from "vitest" ;
68import type { TelegramBotDeps } from "./bot-deps.js" ;
79import {
810 resetTopicNameCacheForTest ,
@@ -72,6 +74,20 @@ async function defaultSaveMediaBuffer(buffer: Buffer, contentType?: string) {
7274}
7375
7476const saveMediaBufferSpy : Mock = vi . fn ( defaultSaveMediaBuffer ) ;
77+ let mediaHarnessStoreRoot : string | undefined ;
78+
79+ function ensureMediaHarnessStoreRoot ( ) : string {
80+ mediaHarnessStoreRoot ??= mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-telegram-media-e2e-" ) ) ;
81+ return mediaHarnessStoreRoot ;
82+ }
83+
84+ function cleanupMediaHarnessStoreRoot ( ) : void {
85+ if ( ! mediaHarnessStoreRoot ) {
86+ return ;
87+ }
88+ rmSync ( mediaHarnessStoreRoot , { recursive : true , force : true } ) ;
89+ mediaHarnessStoreRoot = undefined ;
90+ }
7591
7692export function setNextSavedMediaPath ( params : {
7793 path : string ;
@@ -181,7 +197,7 @@ export const telegramBotDepsForTest: TelegramBotDeps = {
181197 channels : { telegram : { dmPolicy : "open" , allowFrom : [ "*" ] } } ,
182198 } ) as OpenClawConfig ) as TelegramBotDeps [ "getRuntimeConfig" ] ,
183199 resolveStorePath : vi . fn (
184- ( storePath ?: string ) => storePath ?? "/tmp/telegram-media- sessions.json",
200+ ( storePath ?: string ) => storePath ?? path . join ( ensureMediaHarnessStoreRoot ( ) , " sessions.json") ,
185201 ) as TelegramBotDeps [ "resolveStorePath" ] ,
186202 readChannelAllowFromStore : vi . fn ( async ( ) => [ ] ) as TelegramBotDeps [ "readChannelAllowFromStore" ] ,
187203 upsertChannelPairingRequest : vi . fn ( async ( ) => ( {
@@ -201,6 +217,8 @@ export const telegramBotDepsForTest: TelegramBotDeps = {
201217} ;
202218
203219beforeEach ( ( ) => {
220+ cleanupMediaHarnessStoreRoot ( ) ;
221+ ensureMediaHarnessStoreRoot ( ) ;
204222 resetInboundDedupe ( ) ;
205223 topicNameStoresForTest . clear ( ) ;
206224 resetTopicNameCacheForTest ( ) ;
@@ -209,6 +227,10 @@ beforeEach(() => {
209227 resetReadRemoteMediaBufferMock ( ) ;
210228} ) ;
211229
230+ afterEach ( ( ) => {
231+ cleanupMediaHarnessStoreRoot ( ) ;
232+ } ) ;
233+
212234vi . doMock ( "./bot.runtime.js" , ( ) => ( {
213235 ...telegramBotRuntimeForTest ,
214236} ) ) ;
@@ -262,7 +284,8 @@ vi.doMock("./bot-message-context.session.runtime.js", async () => {
262284 return {
263285 ...actual ,
264286 readSessionUpdatedAt : ( ) => undefined ,
265- resolveStorePath : ( storePath ?: string ) => storePath ?? "/tmp/sessions.json" ,
287+ resolveStorePath : ( storePath ?: string ) =>
288+ storePath ?? path . join ( ensureMediaHarnessStoreRoot ( ) , "sessions.json" ) ,
266289 } ;
267290} ) ;
268291
0 commit comments