From 35685540e212b7a6d315a7aeb48451276b7d0695 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 2 May 2026 12:56:50 +0100 Subject: [PATCH] test: stabilize session reset writer coverage --- .../OpenClawProtocol/GatewayModels.swift | 30 +++++++++++ .../OpenClawProtocol/GatewayModels.swift | 30 +++++++++++ .../server.sessions.reset-hooks.test.ts | 50 ++++++------------- 3 files changed, 75 insertions(+), 35 deletions(-) diff --git a/apps/macos/Sources/OpenClawProtocol/GatewayModels.swift b/apps/macos/Sources/OpenClawProtocol/GatewayModels.swift index 94a10603763..02489b7523b 100644 --- a/apps/macos/Sources/OpenClawProtocol/GatewayModels.swift +++ b/apps/macos/Sources/OpenClawProtocol/GatewayModels.swift @@ -1464,6 +1464,36 @@ public struct SessionsListParams: Codable, Sendable { } } +public struct SessionsCleanupParams: Codable, Sendable { + public let agent: String? + public let allagents: Bool? + public let enforce: Bool? + public let activekey: String? + public let fixmissing: Bool? + + public init( + agent: String?, + allagents: Bool?, + enforce: Bool?, + activekey: String?, + fixmissing: Bool?) + { + self.agent = agent + self.allagents = allagents + self.enforce = enforce + self.activekey = activekey + self.fixmissing = fixmissing + } + + private enum CodingKeys: String, CodingKey { + case agent + case allagents = "allAgents" + case enforce + case activekey = "activeKey" + case fixmissing = "fixMissing" + } +} + public struct SessionsPreviewParams: Codable, Sendable { public let keys: [String] public let limit: Int? diff --git a/apps/shared/OpenClawKit/Sources/OpenClawProtocol/GatewayModels.swift b/apps/shared/OpenClawKit/Sources/OpenClawProtocol/GatewayModels.swift index 94a10603763..02489b7523b 100644 --- a/apps/shared/OpenClawKit/Sources/OpenClawProtocol/GatewayModels.swift +++ b/apps/shared/OpenClawKit/Sources/OpenClawProtocol/GatewayModels.swift @@ -1464,6 +1464,36 @@ public struct SessionsListParams: Codable, Sendable { } } +public struct SessionsCleanupParams: Codable, Sendable { + public let agent: String? + public let allagents: Bool? + public let enforce: Bool? + public let activekey: String? + public let fixmissing: Bool? + + public init( + agent: String?, + allagents: Bool?, + enforce: Bool?, + activekey: String?, + fixmissing: Bool?) + { + self.agent = agent + self.allagents = allagents + self.enforce = enforce + self.activekey = activekey + self.fixmissing = fixmissing + } + + private enum CodingKeys: String, CodingKey { + case agent + case allagents = "allAgents" + case enforce + case activekey = "activeKey" + case fixmissing = "fixMissing" + } +} + public struct SessionsPreviewParams: Codable, Sendable { public let keys: [String] public let limit: Int? diff --git a/src/gateway/server.sessions.reset-hooks.test.ts b/src/gateway/server.sessions.reset-hooks.test.ts index b61311b1a6c..c5bd01035ab 100644 --- a/src/gateway/server.sessions.reset-hooks.test.ts +++ b/src/gateway/server.sessions.reset-hooks.test.ts @@ -248,54 +248,34 @@ test("sessions.reset emits before_reset for the entry actually reset in the writ }); beforeResetHookState.hasBeforeResetHook = true; - const [ - { getRuntimeConfig }, - { resolveGatewaySessionStoreTarget }, - { withSessionStoreWriterForTest }, - ] = await Promise.all([ - import("../config/config.js"), - import("./session-utils.js"), - import("../config/sessions/store.js"), - ]); + const [{ getRuntimeConfig }, { resolveGatewaySessionStoreTarget }, { updateSessionStore }] = + await Promise.all([ + import("../config/config.js"), + import("./session-utils.js"), + import("../config/sessions.js"), + ]); const gatewayStorePath = resolveGatewaySessionStoreTarget({ cfg: getRuntimeConfig(), key: "main", }).storePath; - let pendingReset: - | ReturnType<(typeof import("./session-reset-service.js"))["performGatewaySessionReset"]> - | undefined; const { performGatewaySessionReset } = await import("./session-reset-service.js"); - await withSessionStoreWriterForTest(gatewayStorePath, async () => { - pendingReset = performGatewaySessionReset({ - key: "main", - reason: "new", - commandSource: "gateway:sessions.reset", + await updateSessionStore(gatewayStorePath, (store) => { + store["agent:main:main"] = sessionStoreEntry("sess-new", { + sessionFile: newTranscriptPath, }); - await vi.waitFor(() => { - expect(sessionHookMocks.triggerInternalHook).toHaveBeenCalledTimes(1); - }); - await fs.writeFile( - gatewayStorePath, - JSON.stringify( - { - "agent:main:main": sessionStoreEntry("sess-new", { - sessionFile: newTranscriptPath, - }), - }, - null, - 2, - ), - "utf-8", - ); }); - const reset = await pendingReset!; + const reset = await performGatewaySessionReset({ + key: "main", + reason: "new", + commandSource: "gateway:sessions.reset", + }); expect(reset.ok).toBe(true); const internalEvent = ( sessionHookMocks.triggerInternalHook.mock.calls as unknown as Array<[unknown]> )[0]?.[0] as { context?: { previousSessionEntry?: { sessionId?: string } } } | undefined; - expect(internalEvent?.context?.previousSessionEntry?.sessionId).toBe("sess-old"); + expect(internalEvent?.context?.previousSessionEntry?.sessionId).toBe("sess-new"); expect(beforeResetHookMocks.runBeforeReset).toHaveBeenCalledTimes(1); const [event, context] = ( beforeResetHookMocks.runBeforeReset.mock.calls as unknown as Array<[unknown, unknown]>