feat(observabilidade): wrapper SQLite com schema, upsert e filtros
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest'
|
||||
import { mkdtempSync } from 'fs'
|
||||
import { tmpdir } from 'os'
|
||||
import { join } from 'path'
|
||||
import { openSessionsDb } from '../services/sessions/db.js'
|
||||
import type { SessionMeta } from '../types/session.js'
|
||||
|
||||
function sampleMeta(overrides: Partial<SessionMeta> = {}): SessionMeta {
|
||||
return {
|
||||
session_id: 's1',
|
||||
project_path: '/tmp/project',
|
||||
project_slug: 'project',
|
||||
jsonl_path: '/tmp/project/s1.jsonl',
|
||||
started_at: '2026-04-23T10:00:00Z',
|
||||
ended_at: '2026-04-23T10:30:00Z',
|
||||
duration_sec: 1800,
|
||||
event_count: 50,
|
||||
user_messages: 5,
|
||||
assistant_msgs: 10,
|
||||
tool_calls: 20,
|
||||
first_prompt: 'olá',
|
||||
tools_used: ['Bash', 'Read'],
|
||||
skills_invoked: ['brainstorming'],
|
||||
outcome: 'completed',
|
||||
permission_mode: 'default',
|
||||
file_size: 10000,
|
||||
indexed_at: '2026-04-23T10:31:00Z',
|
||||
...overrides,
|
||||
}
|
||||
}
|
||||
|
||||
describe('sessions db', () => {
|
||||
let dbPath: string
|
||||
beforeEach(() => {
|
||||
const dir = mkdtempSync(join(tmpdir(), 'obs-db-'))
|
||||
dbPath = join(dir, 'sessions.db')
|
||||
})
|
||||
|
||||
it('cria schema, faz upsert e query', () => {
|
||||
const db = openSessionsDb(dbPath)
|
||||
db.upsertSession(sampleMeta())
|
||||
const rows = db.listSessions({ days: 30 })
|
||||
expect(rows).toHaveLength(1)
|
||||
expect(rows[0].session_id).toBe('s1')
|
||||
expect(rows[0].tools_used).toEqual(['Bash', 'Read'])
|
||||
})
|
||||
|
||||
it('upsert substitui registo existente', () => {
|
||||
const db = openSessionsDb(dbPath)
|
||||
db.upsertSession(sampleMeta({ event_count: 50 }))
|
||||
db.upsertSession(sampleMeta({ event_count: 75 }))
|
||||
const rows = db.listSessions({ days: 30 })
|
||||
expect(rows).toHaveLength(1)
|
||||
expect(rows[0].event_count).toBe(75)
|
||||
})
|
||||
|
||||
it('filtra por projecto e tool', () => {
|
||||
const db = openSessionsDb(dbPath)
|
||||
db.upsertSession(sampleMeta({ session_id: 'a', jsonl_path: '/tmp/a.jsonl', project_slug: 'alpha', tools_used: ['Bash'] }))
|
||||
db.upsertSession(sampleMeta({ session_id: 'b', jsonl_path: '/tmp/b.jsonl', project_slug: 'beta', tools_used: ['Read'] }))
|
||||
expect(db.listSessions({ project: 'alpha' })).toHaveLength(1)
|
||||
expect(db.listSessions({ tool: 'Read' })).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('devolve contagem total', () => {
|
||||
const db = openSessionsDb(dbPath)
|
||||
db.upsertSession(sampleMeta({ session_id: 'a', jsonl_path: '/tmp/a.jsonl' }))
|
||||
db.upsertSession(sampleMeta({ session_id: 'b', jsonl_path: '/tmp/b.jsonl' }))
|
||||
expect(db.countSessions({})).toBe(2)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user