2026-04-05 17:14:34 +09:00
|
|
|
export {}
|
2026-04-03 23:06:35 +09:00
|
|
|
const { describe, expect, mock, test, afterAll } = require("bun:test")
|
2026-03-07 15:39:25 +09:00
|
|
|
|
2026-04-05 17:14:34 +09:00
|
|
|
mock.module("../../shared/opencode-storage-detection", () => ({
|
|
|
|
|
isSqliteBackend: () => true,
|
|
|
|
|
}))
|
2026-03-25 22:27:26 +01:00
|
|
|
|
2026-04-05 17:14:34 +09:00
|
|
|
afterAll(() => { mock.restore() })
|
2026-03-25 22:27:26 +01:00
|
|
|
|
2026-04-05 17:14:34 +09:00
|
|
|
const { getLastAgentFromSession } = await import("./session-last-agent")
|
2026-03-07 15:39:25 +09:00
|
|
|
|
2026-04-05 17:14:34 +09:00
|
|
|
describe("getLastAgentFromSession SQLite backend ordering", () => {
|
|
|
|
|
test("returns newest non-compaction agent using time.created and id tie-breaker", async () => {
|
|
|
|
|
// given
|
|
|
|
|
const client = {
|
|
|
|
|
session: {
|
|
|
|
|
messages: async () => ({
|
|
|
|
|
data: [
|
|
|
|
|
{ id: "msg_0001", info: { agent: "atlas", time: { created: 100 } } },
|
|
|
|
|
{ id: "msg_0003", info: { agent: "compaction", time: { created: 200 } } },
|
|
|
|
|
{ id: "msg_0002", info: { agent: "sisyphus-junior", time: { created: 100 } } },
|
|
|
|
|
],
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
}
|
2026-04-03 23:06:35 +09:00
|
|
|
|
2026-04-05 17:14:34 +09:00
|
|
|
// when
|
|
|
|
|
const result = await getLastAgentFromSession("ses_sqlite_last_agent", client as never)
|
2026-03-07 15:39:25 +09:00
|
|
|
|
2026-04-05 17:14:34 +09:00
|
|
|
// then
|
|
|
|
|
expect(result).toBe("sisyphus-junior")
|
|
|
|
|
})
|
2026-03-07 15:39:25 +09:00
|
|
|
|
2026-04-05 17:14:34 +09:00
|
|
|
test("handles equal timestamps with random-looking ids deterministically", async () => {
|
2026-03-07 15:39:25 +09:00
|
|
|
// given
|
2026-04-05 17:14:34 +09:00
|
|
|
const client = {
|
|
|
|
|
session: {
|
|
|
|
|
messages: async () => ({
|
|
|
|
|
data: [
|
|
|
|
|
{ id: "msg_a91f00ab", info: { agent: "atlas", time: { created: 100 } } },
|
|
|
|
|
{ id: "msg_f0e1d2c3", info: { agent: "compaction", time: { created: 200 } } },
|
|
|
|
|
{ id: "msg_d4c3b2a1", info: { agent: "sisyphus-junior", time: { created: 100 } } },
|
|
|
|
|
],
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
}
|
2026-03-07 15:39:25 +09:00
|
|
|
|
|
|
|
|
// when
|
2026-04-05 17:14:34 +09:00
|
|
|
const result = await getLastAgentFromSession("ses_sqlite_last_agent_equal_time", client as never)
|
2026-03-07 15:39:25 +09:00
|
|
|
|
|
|
|
|
// then
|
2026-04-05 17:14:34 +09:00
|
|
|
expect(result).toBe("sisyphus-junior")
|
2026-03-07 15:39:25 +09:00
|
|
|
})
|
|
|
|
|
|
2026-04-05 17:14:34 +09:00
|
|
|
test("returns null instead of throwing when SQLite message lookup fails", async () => {
|
2026-03-07 15:39:25 +09:00
|
|
|
// given
|
2026-04-05 17:14:34 +09:00
|
|
|
const client = {
|
|
|
|
|
session: {
|
|
|
|
|
messages: async () => {
|
|
|
|
|
throw new Error("sqlite lookup failed")
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
2026-03-07 15:39:25 +09:00
|
|
|
|
|
|
|
|
// when
|
2026-04-05 17:14:34 +09:00
|
|
|
const result = await getLastAgentFromSession("ses_sqlite_error", client as never)
|
2026-03-07 15:39:25 +09:00
|
|
|
|
|
|
|
|
// then
|
|
|
|
|
expect(result).toBeNull()
|
|
|
|
|
})
|
|
|
|
|
})
|