Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
import { describe, it, expect, vi, beforeEach } from 'vitest'
import EventEmitter from 'events'
// Mock all dependencies. Brain search runs through the REAL brainSearchIndex
// (issue #3506), so the seam mocked here is brainStorage's disk reader.
vi.mock('./brainStorage.js', () => ({
brainEvents: new EventEmitter(),
getAll: vi.fn().mockResolvedValue([]),
memoryRecencyMs: (record) => Date.parse(record?.updatedAt ?? '') || 0
}))
vi.mock('./memoryBM25.js', () => ({
searchBM25: vi.fn().mockResolvedValue([])
}))
vi.mock('./memoryBackend.js', () => ({
getMemories: vi.fn().mockResolvedValue({ memories: [] }),
ensureBackend: vi.fn().mockResolvedValue('file'),
hybridSearchMemories: vi.fn().mockResolvedValue({ memories: [] })
}))
vi.mock('./apps.js', () => ({
getAllApps: vi.fn().mockResolvedValue([])
}))
vi.mock('./history.js', () => ({
getHistory: vi.fn().mockResolvedValue({ entries: [] })
}))
vi.mock('./musicVideo/projects.js', () => ({
listProjects: vi.fn().mockResolvedValue([])
}))
import { fanOutSearch } from './search.js'
import { getAll } from './brainStorage.js'
import { __resetBrainSearchIndex, BRAIN_SEARCH_TYPES } from './brainSearchIndex.js'
import { searchBM25 } from './memoryBM25.js'
import { getMemories, ensureBackend, hybridSearchMemories } from './memoryBackend.js'
import { getAllApps } from './apps.js'
import { getHistory } from './history.js'
import { listProjects as listMusicVideoProjects } from './musicVideo/projects.js'
// Per-type record fixtures the mocked brainStorage.getAll serves.
let brainRecords = {}
const setBrainRecords = (type, records) => { brainRecords[type] = records }
describe('search service', () => {
beforeEach(() => {
vi.clearAllMocks()
brainRecords = {}
__resetBrainSearchIndex()
getAll.mockImplementation(async (type) => brainRecords[type] ?? [])
ensureBackend.mockResolvedValue('file')
})
describe('fanOutSearch', () => {
it('should return empty array when no sources match', async () => {
const results = await fanOutSearch('nonexistent')
expect(results).toEqual([])
})
it('should search across brain inbox entries', async () => {
setBrainRecords('inbox', [
{ id: 'i1', capturedText: 'Hello world from inbox' },
{ id: 'i2', capturedText: 'Another thought' }
])
const results = await fanOutSearch('hello')
const brainSource = results.find(s => s.id === 'brain')
expect(brainSource).toBeDefined()
expect(brainSource.results).toHaveLength(1)
expect(brainSource.results[0].id).toBe('i1')
expect(brainSource.results[0].type).toBe('inbox')
})
it('should search across people', async () => {
setBrainRecords('people', [
{ id: 'p1', name: 'John Doe', context: 'Friend from school' },
{ id: 'p2', name: 'Jane Smith', context: 'Colleague at work' }
])
const results = await fanOutSearch('john')
const brainSource = results.find(s => s.id === 'brain')
expect(brainSource).toBeDefined()
const personResult = brainSource.results.find(r => r.id === 'p1')
expect(personResult).toBeDefined()
expect(personResult.type).toBe('person')
expect(personResult.title).toBe('John Doe')
})
it('should search across projects', async () => {
setBrainRecords('projects', [
{ id: 'pr1', name: 'PortOS', notes: 'Personal operating system' }
])
const results = await fanOutSearch('portos')
const brainSource = results.find(s => s.id === 'brain')
expect(brainSource).toBeDefined()
const projectResult = brainSource.results.find(r => r.id === 'pr1')
expect(projectResult).toBeDefined()
expect(projectResult.type).toBe('project')
})
it('should search across ideas', async () => {
setBrainRecords('ideas', [
{ id: 'id1', title: 'AI assistant', oneLiner: 'Build a smart agent', notes: 'Use LLMs' }
])
const results = await fanOutSearch('agent')
const brainSource = results.find(s => s.id === 'brain')
expect(brainSource).toBeDefined()
const ideaResult = brainSource.results.find(r => r.id === 'id1')
expect(ideaResult).toBeDefined()
expect(ideaResult.type).toBe('idea')
})
it('should search across links', async () => {
setBrainRecords('links', [
{ id: 'l1', title: 'GitHub', url: 'https://github.com', description: 'Code hosting platform' }
])
const results = await fanOutSearch('github')
const brainSource = results.find(s => s.id === 'brain')
expect(brainSource).toBeDefined()
const linkResult = brainSource.results.find(r => r.id === 'l1')
expect(linkResult).toBeDefined()
expect(linkResult.type).toBe('link')
})
it('should search across threads and deep-link the record drawer', async () => {
setBrainRecords('threads', [
{ id: 'th1', title: 'Renew the domain', nextAction: 'Log into the registrar', notes: 'Expires soon' },
{ id: 'th2', title: 'Unrelated', nextAction: 'Nothing here', notes: '' }
])
const results = await fanOutSearch('registrar')
const brainSource = results.find(s => s.id === 'brain')
expect(brainSource.results.map(r => r.id)).toEqual(['th1'])
expect(brainSource.results[0]).toMatchObject({
type: 'thread',
title: 'Renew the domain',
url: '/brain/threads?thread=th1'
})
})
it('should search across admin items', async () => {
setBrainRecords('admin', [
{ id: 'ad1', title: 'Renew registration', notes: 'Due next month', nextAction: 'Book appointment' }
])
const results = await fanOutSearch('renew')
const brainSource = results.find(s => s.id === 'brain')
const adminResult = brainSource.results.find(r => r.id === 'ad1')
expect(adminResult).toBeDefined()
expect(adminResult.type).toBe('admin')
expect(adminResult.url).toBe('/brain/memory?type=admin&id=ad1')
})
it('should search across memory entries, newest first', async () => {
setBrainRecords('memories', [
{ id: 'me-old', title: 'Trip notes', content: 'older recollection', updatedAt: '2020-01-01T00:00:00.000Z' },
{ id: 'me-new', title: 'Trip recap', content: 'newer recollection', updatedAt: '2026-01-01T00:00:00.000Z' }
])
const results = await fanOutSearch('recollection')
const brainSource = results.find(s => s.id === 'brain')
const memoryEntries = brainSource.results.filter(r => r.type === 'memory-entry')
expect(memoryEntries.map(r => r.id)).toEqual(['me-new', 'me-old'])
})
it('should read each brain store from disk only once across repeated queries', async () => {
setBrainRecords('people', [{ id: 'p1', name: 'Ada Placeholder', context: 'colleague' }])
await fanOutSearch('ada')
const afterFirst = getAll.mock.calls.length
await fanOutSearch('ada')
await fanOutSearch('placeholder')
// One scan per brain source on the first query; every later keystroke is
// served from the in-memory projection (issue #3506).
expect(afterFirst).toBe(BRAIN_SEARCH_TYPES.length)
expect(getAll).toHaveBeenCalledTimes(BRAIN_SEARCH_TYPES.length)
})
it('should not re-scan a brain store that is legitimately empty', async () => {
await fanOutSearch('anything')
await fanOutSearch('anything else')
expect(getAll).toHaveBeenCalledTimes(BRAIN_SEARCH_TYPES.length)
})
it('should search apps', async () => {
getAllApps.mockResolvedValue([
{ id: 'a1', name: 'MortalLoom', description: 'Longevity tracker app' },
{ id: 'a2', name: 'PortOS', description: 'Personal OS' }
])
const results = await fanOutSearch('mortal')
const appsSource = results.find(s => s.id === 'apps')
expect(appsSource).toBeDefined()
expect(appsSource.results).toHaveLength(1)
expect(appsSource.results[0].id).toBe('a1')
expect(appsSource.results[0].type).toBe('app')
})
it('should search history', async () => {
getHistory.mockResolvedValue({
entries: [
{ id: 'h1', targetName: 'deploy', action: 'deployed app to production' },
{ id: 'h2', targetName: 'test', action: 'ran test suite' }
]
})
const results = await fanOutSearch('deploy')
const historySource = results.find(s => s.id === 'history')
expect(historySource).toBeDefined()
expect(historySource.results).toHaveLength(1)
expect(historySource.results[0].id).toBe('h1')
expect(historySource.results[0].type).toBe('history')
})
it('should search health metrics', async () => {
const results = await fanOutSearch('heart')
const healthSource = results.find(s => s.id === 'health')
expect(healthSource).toBeDefined()
expect(healthSource.results.length).toBeGreaterThan(0)
expect(healthSource.results[0].type).toBe('health-metric')
})
it('should match health metrics by display name', async () => {
const results = await fanOutSearch('steps')
const healthSource = results.find(s => s.id === 'health')
expect(healthSource).toBeDefined()
const stepsResult = healthSource.results.find(r => r.id === 'step_count')
expect(stepsResult).toBeDefined()
expect(stepsResult.title).toBe('Steps')
})
it('should match health metrics by key name', async () => {
const results = await fanOutSearch('hrv')
const healthSource = results.find(s => s.id === 'health')
expect(healthSource).toBeDefined()
const hrvResult = healthSource.results.find(r => r.id === 'hrv')
expect(hrvResult).toBeDefined()
})
it('should use BM25 search for file-based memory backend', async () => {
ensureBackend.mockResolvedValue('file')
searchBM25.mockResolvedValue([
{ id: 'mem1', score: 1.5 }
])
getMemories.mockResolvedValue({
memories: [
{ id: 'mem1', summary: 'Test memory about coding' }
]
})
const results = await fanOutSearch('coding')
const memorySource = results.find(s => s.id === 'memory')
expect(memorySource).toBeDefined()
expect(memorySource.results).toHaveLength(1)
expect(memorySource.results[0].type).toBe('memory')
})
it('should use hybrid search for postgres backend', async () => {
ensureBackend.mockResolvedValue('postgres')
hybridSearchMemories.mockResolvedValue({
memories: [
{ id: 'pgm1', summary: 'Postgres memory result' }
]
})
const results = await fanOutSearch('postgres')
const memorySource = results.find(s => s.id === 'memory')
expect(memorySource).toBeDefined()
expect(memorySource.results).toHaveLength(1)
expect(hybridSearchMemories).toHaveBeenCalledWith('postgres', null, { limit: 10 })
})
it('should handle failing adapters gracefully', async () => {
getAll.mockRejectedValue(new Error('DB error'))
searchBM25.mockRejectedValue(new Error('Index error'))
getAllApps.mockRejectedValue(new Error('App error'))
getHistory.mockRejectedValue(new Error('History error'))
// Should not throw, should return health results at minimum
const results = await fanOutSearch('test')
// Only health (synchronous) should have results, others fall back
expect(Array.isArray(results)).toBe(true)
})
it('should limit brain results to 8', async () => {
setBrainRecords('inbox', Array.from({ length: 20 }, (_, i) => ({
id: `inbox-${i}`,
capturedText: `test entry ${i}`
})))
const results = await fanOutSearch('test')
const brainSource = results.find(s => s.id === 'brain')
expect(brainSource.results.length).toBeLessThanOrEqual(8)
})
it('should only return non-empty sources', async () => {
// Only apps have results
getAllApps.mockResolvedValue([
{ id: 'a1', name: 'TestApp', description: 'A test app' }
])
const results = await fanOutSearch('testapp')
for (const source of results) {
expect(source.results.length).toBeGreaterThan(0)
}
})
it('should be case-insensitive', async () => {
getAllApps.mockResolvedValue([
{ id: 'a1', name: 'PortOS', description: 'Personal OS' }
])
const results = await fanOutSearch('PORTOS')
const appsSource = results.find(s => s.id === 'apps')
expect(appsSource).toBeDefined()
expect(appsSource.results).toHaveLength(1)
})
it('should search across music-video projects', async () => {
listMusicVideoProjects.mockResolvedValue([
{ id: 'mv-1', name: 'Cyberpunk Odyssey', concept: 'Sci-fi cyberpunk music video' },
{ id: 'mv-2', name: 'Acoustic Sunset', concept: 'Serene folk ballad' }
])
const results = await fanOutSearch('cyberpunk')
const mvSource = results.find(s => s.id === 'musicVideo')
expect(mvSource).toBeDefined()
expect(mvSource.results).toHaveLength(1)
expect(mvSource.results[0].id).toBe('mv-1')
expect(mvSource.results[0].title).toBe('Cyberpunk Odyssey')
expect(mvSource.results[0].url).toBe('/music-video/mv-1')
expect(mvSource.results[0].type).toBe('music-video')
})
it('should keep music-video matches when a concept is a structured object', async () => {
listMusicVideoProjects.mockResolvedValue([
{ id: 'mv-1', name: 'Example project', concept: { prompt: 'Synthetic concept', style: 'Synthetic style' } },
{ id: 'mv-2', name: 'Example legacy', concept: 'Legacy concept text' },
{ id: 'mv-3', name: 'Example empty', concept: {} },
{ id: 'mv-4', name: 'Example null', concept: null, description: 'Fallback description' }
])
const results = await fanOutSearch('example')
const mvSource = results.find(s => s.id === 'musicVideo')
expect(mvSource).toBeDefined()
expect(mvSource.results).toHaveLength(4)
const snippets = Object.fromEntries(mvSource.results.map(r => [r.id, r.snippet]))
expect(snippets['mv-1']).toBe('Synthetic concept')
expect(snippets['mv-2']).toBe('Legacy concept text')
expect(snippets['mv-3']).toBe('Example empty')
expect(snippets['mv-4']).toBe('Fallback description')
})
it('should cap music-video results at five when concepts are structured', async () => {
listMusicVideoProjects.mockResolvedValue(
Array.from({ length: 7 }, (_, i) => ({
id: `mv-${i}`,
name: `Example ${i}`,
concept: { prompt: `Concept ${i}`, style: 'Style' }
}))
)
const results = await fanOutSearch('example')
const mvSource = results.find(s => s.id === 'musicVideo')
expect(mvSource.results).toHaveLength(5)
expect(mvSource.results.every(r => typeof r.snippet === 'string')).toBe(true)
})
})
})