mirror of
https://github.com/hanzoai/insights.git
synced 2026-10-30 16:15:31 +00:00
The tree was a debranded snapshot of upstream taken around 2026-02-20, and upstream has moved about five months since. This replaces it with upstream master, mechanically re-derived: bin/debrand does the rename, and the pipeline and squashed migrations are restored from the shipped tree. Not a merge. The previous import replaced the tree wholesale in a single commit, so there is no shared content history to merge against. Reproducing the rename on the new tree is what actually converges. What survives: .hanzo/workflows, the Dockerfiles and bin/docker* entrypoints, compose files, LLM.md, and insights/migrations. The squashed initial migration is load-bearing — the live database records it by name, so upstream's ~918 individual migrations cannot come back. Nine products are dropped as unreferenced. Twenty-one more that we do not run are kept anyway because the core app still imports them; unpicking those is a refactor and does not belong in a rebase. This base does not build yet, and the reasons are not in the rename: upstream now wants Python 3.13.13 against our 3.12.12 and Django 5.2 against our 4.2, and the dependency manifests still name upstream packages that we republish under our own names. Those are decisions, not renames.
217 lines
7.2 KiB
TypeScript
217 lines
7.2 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import type { ScopedCache } from '@/lib/cache/ScopedCache'
|
|
import { SessionManager } from '@/lib/SessionManager'
|
|
import type { State } from '@/tools/types'
|
|
|
|
vi.mock('uuid', () => ({
|
|
v7: vi.fn(() => 'test-uuid-12345'),
|
|
}))
|
|
|
|
describe('SessionManager', () => {
|
|
let mockCache: ScopedCache<State>
|
|
let sessionManager: SessionManager
|
|
|
|
beforeEach(() => {
|
|
mockCache = {
|
|
get: vi.fn(),
|
|
set: vi.fn(),
|
|
delete: vi.fn(),
|
|
clear: vi.fn(),
|
|
has: vi.fn(),
|
|
values: vi.fn(),
|
|
entries: vi.fn(),
|
|
keys: vi.fn(),
|
|
} as unknown as ScopedCache<State>
|
|
|
|
sessionManager = new SessionManager(mockCache)
|
|
vi.clearAllMocks()
|
|
})
|
|
|
|
describe('getSessionUuid', () => {
|
|
it('should return existing session uuid if it exists', async () => {
|
|
const sessionId = 'test-session-123'
|
|
const existingUuid = 'existing-uuid-456'
|
|
|
|
;(mockCache.get as any).mockResolvedValue({ uuid: existingUuid })
|
|
|
|
const result = await sessionManager.getSessionUuid(sessionId)
|
|
|
|
expect(result).toBe(existingUuid)
|
|
expect(mockCache.get).toHaveBeenCalledWith('session:test-session-123')
|
|
expect(mockCache.set).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('should create and return new session uuid if none exists', async () => {
|
|
const sessionId = 'test-session-123'
|
|
|
|
;(mockCache.get as any).mockResolvedValue(null)
|
|
|
|
const result = await sessionManager.getSessionUuid(sessionId)
|
|
|
|
expect(result).toBe('test-uuid-12345')
|
|
expect(mockCache.get).toHaveBeenCalledWith('session:test-session-123')
|
|
expect(mockCache.set).toHaveBeenCalledWith('session:test-session-123', {
|
|
uuid: 'test-uuid-12345',
|
|
})
|
|
})
|
|
|
|
it('should create new uuid if session exists but has no uuid', async () => {
|
|
const sessionId = 'test-session-123'
|
|
|
|
;(mockCache.get as any).mockResolvedValue({})
|
|
|
|
const result = await sessionManager.getSessionUuid(sessionId)
|
|
|
|
expect(result).toBe('test-uuid-12345')
|
|
expect(mockCache.set).toHaveBeenCalledWith('session:test-session-123', {
|
|
uuid: 'test-uuid-12345',
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('hasSession', () => {
|
|
it('should return true if session exists with uuid', async () => {
|
|
const sessionId = 'test-session-123'
|
|
|
|
;(mockCache.get as any).mockResolvedValue({ uuid: 'some-uuid' })
|
|
|
|
const result = await sessionManager.hasSession(sessionId)
|
|
|
|
expect(result).toBe(true)
|
|
expect(mockCache.get).toHaveBeenCalledWith('session:test-session-123')
|
|
})
|
|
|
|
it('should return false if session does not exist', async () => {
|
|
const sessionId = 'test-session-123'
|
|
|
|
;(mockCache.get as any).mockResolvedValue(null)
|
|
|
|
const result = await sessionManager.hasSession(sessionId)
|
|
|
|
expect(result).toBe(false)
|
|
expect(mockCache.get).toHaveBeenCalledWith('session:test-session-123')
|
|
})
|
|
|
|
it('should return false if session exists but has no uuid', async () => {
|
|
const sessionId = 'test-session-123'
|
|
|
|
;(mockCache.get as any).mockResolvedValue({})
|
|
|
|
const result = await sessionManager.hasSession(sessionId)
|
|
|
|
expect(result).toBe(false)
|
|
})
|
|
|
|
it('should return false if session exists with undefined uuid', async () => {
|
|
const sessionId = 'test-session-123'
|
|
|
|
;(mockCache.get as any).mockResolvedValue({ uuid: undefined })
|
|
|
|
const result = await sessionManager.hasSession(sessionId)
|
|
|
|
expect(result).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('removeSession', () => {
|
|
it('should delete session from cache', async () => {
|
|
const sessionId = 'test-session-123'
|
|
|
|
await sessionManager.removeSession(sessionId)
|
|
|
|
expect(mockCache.delete).toHaveBeenCalledWith('session:test-session-123')
|
|
})
|
|
|
|
it('should handle removal of non-existent session gracefully', async () => {
|
|
const sessionId = 'non-existent-session'
|
|
|
|
;(mockCache.delete as any).mockResolvedValue(undefined)
|
|
|
|
await sessionManager.removeSession(sessionId)
|
|
|
|
expect(mockCache.delete).toHaveBeenCalledWith('session:non-existent-session')
|
|
})
|
|
})
|
|
|
|
describe('clearAllSessions', () => {
|
|
it('should clear all sessions from cache', async () => {
|
|
await sessionManager.clearAllSessions()
|
|
|
|
expect(mockCache.clear).toHaveBeenCalled()
|
|
})
|
|
})
|
|
|
|
describe('_getKey', () => {
|
|
it('should format session key correctly', async () => {
|
|
const sessionId = 'test-session'
|
|
|
|
const key = await sessionManager._getKey(sessionId)
|
|
|
|
expect(key).toBe('session:test-session')
|
|
})
|
|
|
|
it('should handle special characters in session id', async () => {
|
|
const sessionId = 'test-session!@#$%^&*()'
|
|
|
|
const key = await sessionManager._getKey(sessionId)
|
|
|
|
expect(key).toBe('session:test-session!@#$%^&*()')
|
|
})
|
|
|
|
it('should handle empty session id', async () => {
|
|
const sessionId = ''
|
|
|
|
const key = await sessionManager._getKey(sessionId)
|
|
|
|
expect(key).toBe('session:')
|
|
})
|
|
})
|
|
|
|
describe('integration scenarios', () => {
|
|
it('should handle multiple sequential operations', async () => {
|
|
const sessionId = 'test-session'
|
|
|
|
;(mockCache.get as any).mockResolvedValueOnce(null)
|
|
;(mockCache.get as any).mockResolvedValueOnce({ uuid: 'test-uuid-12345' })
|
|
;(mockCache.get as any).mockResolvedValueOnce(null)
|
|
|
|
const uuid1 = await sessionManager.getSessionUuid(sessionId)
|
|
expect(uuid1).toBe('test-uuid-12345')
|
|
expect(mockCache.set).toHaveBeenCalled()
|
|
|
|
const hasSession = await sessionManager.hasSession(sessionId)
|
|
expect(hasSession).toBe(true)
|
|
|
|
await sessionManager.removeSession(sessionId)
|
|
expect(mockCache.delete).toHaveBeenCalled()
|
|
|
|
const hasSessionAfterRemove = await sessionManager.hasSession(sessionId)
|
|
expect(hasSessionAfterRemove).toBe(false)
|
|
})
|
|
|
|
it('should handle concurrent sessions', async () => {
|
|
const sessionId1 = 'session-1'
|
|
const sessionId2 = 'session-2'
|
|
|
|
;(mockCache.get as any).mockImplementation((key: string) => {
|
|
if (key === 'session:session-1') {
|
|
return Promise.resolve({ uuid: 'uuid-1' })
|
|
}
|
|
if (key === 'session:session-2') {
|
|
return Promise.resolve({ uuid: 'uuid-2' })
|
|
}
|
|
return Promise.resolve(null)
|
|
})
|
|
|
|
const [uuid1, uuid2] = await Promise.all([
|
|
sessionManager.getSessionUuid(sessionId1),
|
|
sessionManager.getSessionUuid(sessionId2),
|
|
])
|
|
|
|
expect(uuid1).toBe('uuid-1')
|
|
expect(uuid2).toBe('uuid-2')
|
|
})
|
|
})
|
|
})
|