test(hooks): update multiple hook test suites

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
YeonGyu-Kim
2026-04-10 15:53:32 +09:00
parent 4180a0ba0a
commit 8a4eacfd24
14 changed files with 316 additions and 259 deletions
@@ -6,7 +6,7 @@ import { randomUUID } from "node:crypto"
import type { PluginInput } from "@opencode-ai/plugin"
import { createAtlasHook } from "./atlas-hook"
import { clearBoulderState, writeBoulderState } from "../../features/boulder-state"
import { _resetForTesting, registerAgentName, setSessionAgent } from "../../features/claude-code-session-state"
import { _resetForTesting, clearSessionAgent, registerAgentName, setSessionAgent } from "../../features/claude-code-session-state"
// Force process isolation in CI runner (globalThis.setTimeout override conflicts with other atlas tests)
mock.module("../../shared/opencode-storage-detection", () => ({
@@ -401,7 +401,8 @@ describe("atlas background task retry", () => {
// when
await hook.handler({ event: { type: "session.idle", properties: { sessionID: descendantSessionID } } })
expect(capturedTimers.size).toBe(1)
descendantAgent = "sisyphus-junior"
descendantAgent = "prometheus"
clearSessionAgent(descendantSessionID)
backgroundRunning = false
await firePendingTimers()
+34 -18
View File
@@ -9,19 +9,6 @@ const testDirs: string[] = []
const TEST_STORAGE_ROOT = join(tmpdir(), `atlas-session-last-agent-${Date.now()}`)
const TEST_MESSAGE_STORAGE = join(TEST_STORAGE_ROOT, "message")
mock.module("../../shared/opencode-storage-detection", () => ({
isSqliteBackend: () => false,
}))
mock.module("../../shared/opencode-message-dir", () => ({
getMessageDir: (sessionID: string) => {
const directPath = join(TEST_MESSAGE_STORAGE, sessionID)
return require("node:fs").existsSync(directPath) ? directPath : null
},
}))
afterAll(() => { mock.restore() })
afterEach(() => {
while (testDirs.length > 0) {
const directory = testDirs.pop()
@@ -31,11 +18,17 @@ afterEach(() => {
}
})
async function importFreshSessionLastAgentModule(): Promise<typeof import("./session-last-agent")> {
return import(`./session-last-agent?test=${Date.now()}-${Math.random()}`)
}
function createTempMessageDir(sessionID: string): string {
const directory = mkdtempSync(join(tmpdir(), "atlas-session-last-agent-json-"))
testDirs.push(directory)
const messageDir = join(TEST_MESSAGE_STORAGE, sessionID)
rmSync(messageDir, { recursive: true, force: true })
mkdirSync(messageDir, { recursive: true })
testDirs.push(messageDir)
return messageDir
}
@@ -57,10 +50,20 @@ describe("getLastAgentFromSession JSON backend", () => {
time: { created: 50 },
}), "utf-8")
const { getLastAgentFromSession } = await import("./session-last-agent")
const { getLastAgentFromSession } = await importFreshSessionLastAgentModule()
// when
const result = await getLastAgentFromSession(sessionID)
const result = await getLastAgentFromSession(sessionID, undefined, {
isSqliteBackend: () => false,
getMessageDir: (targetSessionID: string) => {
const directPath = join(TEST_MESSAGE_STORAGE, targetSessionID)
return require("node:fs").existsSync(directPath) ? directPath : null
},
isCompactionMessage: (message: { agent?: unknown }) => {
return typeof message.agent === "string" && message.agent.toLowerCase() === "compaction"
},
hasCompactionPartInStorage: () => false,
})
// then
expect(result).toBe("atlas")
@@ -71,6 +74,7 @@ describe("getLastAgentFromSession JSON backend", () => {
const sessionID = "ses_json_compaction_marker"
const messageDir = createTempMessageDir(sessionID)
const compactionMessageID = "msg_test_atlas_compaction_marker"
const regularMessageID = `msg_${sessionID}_regular`
const partDir = join(PART_STORAGE, compactionMessageID)
testDirs.push(partDir)
writeFileSync(join(messageDir, "msg_0001.json"), JSON.stringify({
@@ -84,15 +88,27 @@ describe("getLastAgentFromSession JSON backend", () => {
}), "utf-8")
writeFileSync(join(messageDir, "msg_0002.json"), JSON.stringify({
id: "msg_0002",
id: regularMessageID,
agent: "sisyphus-junior",
time: { created: 100 },
}), "utf-8")
const { getLastAgentFromSession } = await import("./session-last-agent")
const { getLastAgentFromSession } = await importFreshSessionLastAgentModule()
// when
const result = await getLastAgentFromSession(sessionID)
const result = await getLastAgentFromSession(sessionID, undefined, {
isSqliteBackend: () => false,
getMessageDir: (targetSessionID: string) => {
const directPath = join(TEST_MESSAGE_STORAGE, targetSessionID)
return require("node:fs").existsSync(directPath) ? directPath : null
},
isCompactionMessage: (message: { agent?: unknown }) => {
return typeof message.agent === "string" && message.agent.toLowerCase() === "compaction"
},
hasCompactionPartInStorage: (messageID: string | undefined) => {
return messageID === compactionMessageID
},
})
// then
expect(result).toBe("sisyphus-junior")
@@ -1,11 +1,5 @@
export {}
const { describe, expect, mock, test, afterAll } = require("bun:test")
mock.module("../../shared/opencode-storage-detection", () => ({
isSqliteBackend: () => true,
}))
afterAll(() => { mock.restore() })
const { describe, expect, test } = require("bun:test")
const { getLastAgentFromSession } = await import("./session-last-agent")
@@ -25,7 +19,9 @@ describe("getLastAgentFromSession SQLite backend ordering", () => {
}
// when
const result = await getLastAgentFromSession("ses_sqlite_last_agent", client as never)
const result = await getLastAgentFromSession("ses_sqlite_last_agent", client as never, {
isSqliteBackend: () => true,
})
// then
expect(result).toBe("sisyphus-junior")
@@ -46,7 +42,9 @@ describe("getLastAgentFromSession SQLite backend ordering", () => {
}
// when
const result = await getLastAgentFromSession("ses_sqlite_last_agent_equal_time", client as never)
const result = await getLastAgentFromSession("ses_sqlite_last_agent_equal_time", client as never, {
isSqliteBackend: () => true,
})
// then
expect(result).toBe("sisyphus-junior")
@@ -70,7 +68,9 @@ describe("getLastAgentFromSession SQLite backend ordering", () => {
}
// when
const result = await getLastAgentFromSession("ses_sqlite_compaction_marker", client as never)
const result = await getLastAgentFromSession("ses_sqlite_compaction_marker", client as never, {
isSqliteBackend: () => true,
})
// then
expect(result).toBe("sisyphus")
@@ -87,7 +87,9 @@ describe("getLastAgentFromSession SQLite backend ordering", () => {
}
// when
const result = await getLastAgentFromSession("ses_sqlite_error", client as never)
const result = await getLastAgentFromSession("ses_sqlite_error", client as never, {
isSqliteBackend: () => true,
})
// then
expect(result).toBeNull()