import { afterEach, describe, expect, it } from 'vitest'; import Fastify, { type FastifyInstance } from 'fastify'; import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import { documentRoutes } from '../../src/local/routes/documents.js'; import { pinRoutes } from '../../src/local/routes/pins.js'; /** UF-064: pins and documents follow the server's data directory, not ~/.waggle. */ describe('pins and documents storage', () => { let server: FastifyInstance; let dataDir: string; async function buildServer(): Promise { dataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-pins-docs-')); const instance = Fastify({ logger: false }); instance.decorate('localConfig', { dataDir } as never); await instance.register(pinRoutes); await instance.register(documentRoutes); await instance.ready(); return instance; } afterEach(async () => { await server?.close(); fs.rmSync(dataDir, { recursive: true, force: true }); }); it('writes pins.json and documents.json under the configured data directory', async () => { server = await buildServer(); const pin = await server.inject({ method: 'POST', url: '/api/workspaces/ws-one/pins', payload: { messageContent: 'Keep this', messageRole: 'assistant' }, }); expect(pin.statusCode).toBeLessThan(300); const doc = await server.inject({ method: 'POST', url: '/api/workspaces/ws-one/documents', payload: { name: 'plan', path: '/tmp/plan.md' }, }); expect(doc.statusCode).toBeLessThan(300); expect(fs.existsSync(path.join(dataDir, 'workspaces', 'ws-one', 'pins.json'))).toBe(true); expect(fs.existsSync(path.join(dataDir, 'workspaces', 'ws-one', 'documents.json'))).toBe(true); const list = await server.inject({ method: 'GET', url: '/api/workspaces/ws-one/pins' }); expect(list.json().pins).toHaveLength(1); }); });