/** * Command palette search (GET /api/command/search): the memory facet searches * personal memory and the active workspace's memory through the memory contract. */ import { describe, it, expect, beforeEach, afterEach } from 'vitest'; import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import Fastify, { type FastifyInstance } from 'fastify'; import { createInMemoryMemoryStore, PERSONAL_SCOPE, workspaceScope, type MemoryStore, } from '@waggle/memory-contract'; import { commandRoutes } from '../../src/local/routes/command.js'; describe('GET /api/command/search — memory facet', () => { let dataDir: string; let memory: MemoryStore; let server: FastifyInstance; async function boot(activeWorkspaceId: string | null): Promise { server = Fastify({ logger: false }); server.decorate('localConfig', { dataDir } as never); server.decorate('memory', memory); server.decorate('workspaceManager', { list: () => [] } as never); server.decorate('agentState', { activeWorkspaceId, commandRegistry: { search: () => [], list: () => [] }, } as never); await server.register(commandRoutes); } async function memoryTitles(url: string): Promise { const res = await server.inject({ method: 'GET', url }); expect(res.statusCode).toBe(200); return (res.json().results as Array<{ type: string; title: string }>) .filter((r) => r.type === 'memory') .map((r) => r.title) .sort(); } beforeEach(async () => { dataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-command-search-')); memory = createInMemoryMemoryStore(); await memory.remember({ scope: PERSONAL_SCOPE, text: 'Personal budget review notes' }); await memory.remember({ scope: workspaceScope('alpha'), text: 'Alpha budget decision' }); await memory.remember({ scope: workspaceScope('beta'), text: 'Beta budget draft' }); }); afterEach(async () => { await server.close(); fs.rmSync(dataDir, { recursive: true, force: true }); }); it('searches personal memory and the active workspace, not other workspaces', async () => { await boot('alpha'); expect(await memoryTitles('/api/command/search?q=budget')) .toEqual(['Alpha budget decision', 'Personal budget review notes']); }); it('narrows to one scope when asked', async () => { await boot('alpha'); expect(await memoryTitles('/api/command/search?q=budget&scope=personal')) .toEqual(['Personal budget review notes']); expect(await memoryTitles('/api/command/search?q=budget&scope=workspace')) .toEqual(['Alpha budget decision']); }); it('searches only personal memory when no workspace is active', async () => { await boot(null); expect(await memoryTitles('/api/command/search?q=budget')) .toEqual(['Personal budget review notes']); expect(await memoryTitles('/api/command/search?q=budget&scope=workspace')).toEqual([]); }); });