Files
hapi/shared/src/sessionOrder.test.ts
T
weishu 01dabad204 fix(sessions): stabilize ordering by latest user input
Persist and backfill the last user-message timestamp, and use it for deterministic session ordering across web and native clients while preserving manual pins.

Remove the fixed-order setting and guard ordering against transcript echoes, replayed messages, and stale REST/SSE updates. Add regression coverage and regenerate the SSE fixture.
2026-10-29 12:47:52 +08:00

41 lines
2.1 KiB
TypeScript

import { describe, expect, it } from 'bun:test'
import { compareSessionSummaries, compareSessionsByOrderKeyDesc, getSessionOrderKey } from './sessionOrder'
describe('user-activity session order', () => {
it('uses original user-message time, or creation before any user input', () => {
expect(getSessionOrderKey({ id: 'a', createdAt: 10, lastUserMessageAt: 20 })).toBe(20)
expect(getSessionOrderKey({ id: 'a', createdAt: 10 })).toBe(10)
})
it('ignores output, connection and permission changes', () => {
const sessions = [
{ id: 'old', createdAt: 10, lastUserMessageAt: 100, updatedAt: 900, active: true, pendingRequestsCount: 5 },
{ id: 'new', createdAt: 20, lastUserMessageAt: 200, updatedAt: 200, active: false, pendingRequestsCount: 0 }
]
expect(sessions.sort(compareSessionSummaries).map(s => s.id)).toEqual(['new', 'old'])
sessions[1]!.updatedAt = 10_000
expect(sessions.sort(compareSessionSummaries).map(s => s.id)).toEqual(['new', 'old'])
sessions[1]!.lastUserMessageAt = 300
expect(sessions.sort(compareSessionSummaries).map(s => s.id)).toEqual(['old', 'new'])
})
it('keeps explicit global and project pins ahead of user recency', () => {
const sessions = [
{ id: 'new', createdAt: 500 },
{ id: 'project', createdAt: 200, pinned: true },
{ id: 'global', createdAt: 100, globalPinned: true }
]
expect(sessions.sort(compareSessionSummaries).map(s => s.id)).toEqual(['global', 'project', 'new'])
})
it('uses creation and id to make timestamp ties independent of input order', () => {
const sessions = [
{ id: 'b', createdAt: 20, lastUserMessageAt: 100 },
{ id: 'a', createdAt: 20, lastUserMessageAt: 100 },
{ id: 'c', createdAt: 10, lastUserMessageAt: 100 }
]
expect([...sessions].sort(compareSessionsByOrderKeyDesc).map(s => s.id)).toEqual(['a', 'b', 'c'])
expect([...sessions].reverse().sort(compareSessionsByOrderKeyDesc).map(s => s.id)).toEqual(['a', 'b', 'c'])
})
})