fix(anthropic-recovery): improve executor test coverage and assertions
🤖 Generated with assistance of OhMyOpenCode
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
/// <reference types="bun-types" />
|
/// <reference types="bun-types" />
|
||||||
import { afterEach, beforeEach, describe, expect, mock, spyOn, test } from "bun:test"
|
import { afterEach, beforeEach, describe, expect, mock, spyOn, test } from "bun:test"
|
||||||
|
import { OhMyOpenCodeConfigSchema } from "../../config"
|
||||||
import { executeCompact } from "./executor"
|
import { executeCompact } from "./executor"
|
||||||
import type { AutoCompactState } from "./types"
|
import type { AutoCompactState } from "./types"
|
||||||
import * as recoveryStrategy from "./recovery-strategy"
|
import * as recoveryStrategy from "./recovery-strategy"
|
||||||
@@ -80,6 +81,7 @@ describe("executeCompact lock management", () => {
|
|||||||
let autoCompactState: AutoCompactState
|
let autoCompactState: AutoCompactState
|
||||||
let mockClient: any
|
let mockClient: any
|
||||||
let fakeTimeouts: FakeTimeouts
|
let fakeTimeouts: FakeTimeouts
|
||||||
|
let pluginConfig: ReturnType<typeof OhMyOpenCodeConfigSchema.parse>
|
||||||
const sessionID = "test-session-123"
|
const sessionID = "test-session-123"
|
||||||
const directory = "/test/dir"
|
const directory = "/test/dir"
|
||||||
const msg = { providerID: "anthropic", modelID: "claude-opus-4-6" }
|
const msg = { providerID: "anthropic", modelID: "claude-opus-4-6" }
|
||||||
@@ -87,7 +89,7 @@ describe("executeCompact lock management", () => {
|
|||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
// given: Fresh state for each test
|
// given: Fresh state for each test
|
||||||
autoCompactState = {
|
autoCompactState = {
|
||||||
pendingCompact: new Set<string>(),
|
pendingCompact: new Set<string>([sessionID]),
|
||||||
errorDataBySession: new Map(),
|
errorDataBySession: new Map(),
|
||||||
retryStateBySession: new Map(),
|
retryStateBySession: new Map(),
|
||||||
retryTimerBySession: new Map(),
|
retryTimerBySession: new Map(),
|
||||||
@@ -108,6 +110,7 @@ describe("executeCompact lock management", () => {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pluginConfig = OhMyOpenCodeConfigSchema.parse({})
|
||||||
fakeTimeouts = createFakeTimeouts()
|
fakeTimeouts = createFakeTimeouts()
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -124,7 +127,14 @@ describe("executeCompact lock management", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// when: Execute compaction successfully
|
// when: Execute compaction successfully
|
||||||
await executeCompact(sessionID, msg, autoCompactState, mockClient, directory)
|
await executeCompact(sessionID, msg, autoCompactState, mockClient, directory, pluginConfig)
|
||||||
|
|
||||||
|
expect(mockClient.session.summarize).toHaveBeenCalledWith(
|
||||||
|
expect.objectContaining({
|
||||||
|
path: { id: sessionID },
|
||||||
|
body: { providerID: "anthropic", modelID: "claude-opus-4-6", auto: true },
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
// then: Lock should be cleared
|
// then: Lock should be cleared
|
||||||
expect(autoCompactState.compactionInProgress.has(sessionID)).toBe(false)
|
expect(autoCompactState.compactionInProgress.has(sessionID)).toBe(false)
|
||||||
@@ -142,7 +152,14 @@ describe("executeCompact lock management", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// when: Execute compaction
|
// when: Execute compaction
|
||||||
await executeCompact(sessionID, msg, autoCompactState, mockClient, directory)
|
await executeCompact(sessionID, msg, autoCompactState, mockClient, directory, pluginConfig)
|
||||||
|
|
||||||
|
expect(mockClient.session.summarize).toHaveBeenCalledWith(
|
||||||
|
expect.objectContaining({
|
||||||
|
path: { id: sessionID },
|
||||||
|
body: { providerID: "anthropic", modelID: "claude-opus-4-6", auto: true },
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
// then: Lock should still be cleared despite exception
|
// then: Lock should still be cleared despite exception
|
||||||
expect(autoCompactState.compactionInProgress.has(sessionID)).toBe(false)
|
expect(autoCompactState.compactionInProgress.has(sessionID)).toBe(false)
|
||||||
@@ -153,7 +170,7 @@ describe("executeCompact lock management", () => {
|
|||||||
autoCompactState.compactionInProgress.add(sessionID)
|
autoCompactState.compactionInProgress.add(sessionID)
|
||||||
|
|
||||||
// when: Try to execute compaction
|
// when: Try to execute compaction
|
||||||
await executeCompact(sessionID, msg, autoCompactState, mockClient, directory)
|
await executeCompact(sessionID, msg, autoCompactState, mockClient, directory, pluginConfig)
|
||||||
|
|
||||||
// then: Toast should be shown with warning message
|
// then: Toast should be shown with warning message
|
||||||
expect(mockClient.tui.showToast).toHaveBeenCalledWith(
|
expect(mockClient.tui.showToast).toHaveBeenCalledWith(
|
||||||
@@ -181,7 +198,7 @@ describe("executeCompact lock management", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
//#when - Execute compaction (fixEmptyMessages will be called)
|
//#when - Execute compaction (fixEmptyMessages will be called)
|
||||||
await executeCompact(sessionID, msg, autoCompactState, mockClient, directory)
|
await executeCompact(sessionID, msg, autoCompactState, mockClient, directory, pluginConfig)
|
||||||
|
|
||||||
//#then - Lock should be cleared
|
//#then - Lock should be cleared
|
||||||
expect(autoCompactState.compactionInProgress.has(sessionID)).toBe(false)
|
expect(autoCompactState.compactionInProgress.has(sessionID)).toBe(false)
|
||||||
@@ -209,6 +226,7 @@ describe("executeCompact lock management", () => {
|
|||||||
autoCompactState,
|
autoCompactState,
|
||||||
mockClient,
|
mockClient,
|
||||||
directory,
|
directory,
|
||||||
|
pluginConfig,
|
||||||
experimental,
|
experimental,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -222,7 +240,7 @@ describe("executeCompact lock management", () => {
|
|||||||
autoCompactState.compactionInProgress.add(sessionID)
|
autoCompactState.compactionInProgress.add(sessionID)
|
||||||
|
|
||||||
// when: Try to execute compaction while lock is held
|
// when: Try to execute compaction while lock is held
|
||||||
await executeCompact(sessionID, msg, autoCompactState, mockClient, directory)
|
await executeCompact(sessionID, msg, autoCompactState, mockClient, directory, pluginConfig)
|
||||||
|
|
||||||
// then: Toast should be shown
|
// then: Toast should be shown
|
||||||
const toastCalls = (mockClient.tui.showToast as any).mock.calls
|
const toastCalls = (mockClient.tui.showToast as any).mock.calls
|
||||||
@@ -243,6 +261,7 @@ describe("executeCompact lock management", () => {
|
|||||||
autoCompactState.retryStateBySession.set(sessionID, {
|
autoCompactState.retryStateBySession.set(sessionID, {
|
||||||
attempt: 5,
|
attempt: 5,
|
||||||
lastAttemptTime: Date.now(),
|
lastAttemptTime: Date.now(),
|
||||||
|
firstAttemptTime: Date.now(),
|
||||||
})
|
})
|
||||||
autoCompactState.truncateStateBySession.set(sessionID, {
|
autoCompactState.truncateStateBySession.set(sessionID, {
|
||||||
truncateAttempt: 5,
|
truncateAttempt: 5,
|
||||||
@@ -254,7 +273,7 @@ describe("executeCompact lock management", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// when: Execute compaction
|
// when: Execute compaction
|
||||||
await executeCompact(sessionID, msg, autoCompactState, mockClient, directory)
|
await executeCompact(sessionID, msg, autoCompactState, mockClient, directory, pluginConfig)
|
||||||
|
|
||||||
// then: Should show failure toast
|
// then: Should show failure toast
|
||||||
const toastCalls = (mockClient.tui.showToast as any).mock.calls
|
const toastCalls = (mockClient.tui.showToast as any).mock.calls
|
||||||
@@ -279,7 +298,7 @@ describe("executeCompact lock management", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// when: Execute compaction
|
// when: Execute compaction
|
||||||
await executeCompact(sessionID, msg, autoCompactState, mockClient, directory)
|
await executeCompact(sessionID, msg, autoCompactState, mockClient, directory, pluginConfig)
|
||||||
|
|
||||||
// then: Lock should be cleared even if toast fails
|
// then: Lock should be cleared even if toast fails
|
||||||
expect(autoCompactState.compactionInProgress.has(sessionID)).toBe(false)
|
expect(autoCompactState.compactionInProgress.has(sessionID)).toBe(false)
|
||||||
@@ -297,7 +316,7 @@ describe("executeCompact lock management", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// when: Execute compaction
|
// when: Execute compaction
|
||||||
await executeCompact(sessionID, msg, autoCompactState, mockClient, directory)
|
await executeCompact(sessionID, msg, autoCompactState, mockClient, directory, pluginConfig)
|
||||||
|
|
||||||
// Wait for setTimeout callback
|
// Wait for setTimeout callback
|
||||||
await fakeTimeouts.advanceBy(600)
|
await fakeTimeouts.advanceBy(600)
|
||||||
@@ -324,7 +343,7 @@ describe("executeCompact lock management", () => {
|
|||||||
}))
|
}))
|
||||||
|
|
||||||
// when: Execute compaction
|
// when: Execute compaction
|
||||||
await executeCompact(sessionID, msg, autoCompactState, mockClient, directory)
|
await executeCompact(sessionID, msg, autoCompactState, mockClient, directory, pluginConfig)
|
||||||
|
|
||||||
// then: Truncation was attempted
|
// then: Truncation was attempted
|
||||||
expect(truncateSpy).toHaveBeenCalled()
|
expect(truncateSpy).toHaveBeenCalled()
|
||||||
@@ -372,7 +391,7 @@ describe("executeCompact lock management", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// when: Execute compaction
|
// when: Execute compaction
|
||||||
await executeCompact(sessionID, msg, autoCompactState, mockClient, directory)
|
await executeCompact(sessionID, msg, autoCompactState, mockClient, directory, pluginConfig)
|
||||||
|
|
||||||
// Wait for setTimeout callback
|
// Wait for setTimeout callback
|
||||||
await fakeTimeouts.advanceBy(600)
|
await fakeTimeouts.advanceBy(600)
|
||||||
|
|||||||
Reference in New Issue
Block a user