Files
BrowserOS/packages/browseros-agent/apps/server/tests/lib/db/index.test.ts
Nikhil c07d3d95d4 feat: add sqlite drizzle persistence (#919)
* feat: add drizzle agent schema

* feat: run sqlite drizzle migrations

* refactor: remove old sql identity dependency

* feat: store harness agents in sqlite

* build: package db migrations

* refactor: remove sqlite oauth token store

* feat: restore oauth token storage

* fix: handle empty install id

* chore: ignore server runtime state

* fix: address review feedback for PR 919
2026-05-02 15:19:57 -07:00

63 lines
1.7 KiB
TypeScript

/**
* @license
* Copyright 2025 BrowserOS
*/
import { afterEach, describe, expect, it } from 'bun:test'
import { existsSync, mkdtempSync } from 'node:fs'
import { rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { closeDb, initializeDb } from '../../../src/lib/db'
import { agentDefinitions } from '../../../src/lib/db/schema'
describe('database initialization', () => {
const tempDirs: string[] = []
afterEach(async () => {
closeDb()
await Promise.all(
tempDirs.map((dir) => rm(dir, { recursive: true, force: true })),
)
tempDirs.length = 0
})
it('creates the parent directory, opens sqlite, and runs migrations', () => {
const dir = mkTempDir()
const dbPath = join(dir, 'nested', 'browseros.sqlite')
const handle = initializeDb({ dbPath })
const rows = handle.db.select().from(agentDefinitions).all()
expect(existsSync(dbPath)).toBe(true)
expect(rows).toEqual([])
})
it('is idempotent when initialized twice for the same path', () => {
const dir = mkTempDir()
const dbPath = join(dir, 'browseros.sqlite')
const first = initializeDb({ dbPath })
const second = initializeDb({ dbPath })
expect(second).toBe(first)
})
it('fails clearly when an explicit migration directory is missing', () => {
const dir = mkTempDir()
expect(() =>
initializeDb({
dbPath: join(dir, 'browseros.sqlite'),
migrationsDir: join(dir, 'missing-migrations'),
}),
).toThrow(/Drizzle migrations directory not found/)
})
function mkTempDir(): string {
const dir = mkdtempSync(join(tmpdir(), 'browseros-db-test-'))
tempDirs.push(dir)
return dir
}
})