Skip to content

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

11.3 KB
Raw Blame
import { describe, it, expect, vi, beforeEach } from 'vitest';
import express from 'express';
import { request } from '../lib/testHelper.js';
import { errorMiddleware } from '../lib/errorHandler.js';

// Stub the service graph — this test verifies routing + the pagination envelope,
// not the DB-backed board logic (covered by moodBoard/db.test.js).
vi.mock('../services/moodBoard/index.js', () => ({
  listBoards: vi.fn(async () => []),
  listBoardNames: vi.fn(async () => []),
  getBoard: vi.fn(),
  createBoard: vi.fn(),
  updateBoard: vi.fn(),
  deleteBoard: vi.fn(),
  addBoardItem: vi.fn(),
  backfillGalleryPrompts: vi.fn(),
  updateBoardItem: vi.fn(),
  removeBoardItem: vi.fn(),
  linkPinterestBoard: vi.fn(),
  unlinkPinterestBoard: vi.fn(),
  syncPinterestBoard: vi.fn(),
  importXPost: vi.fn(),
  composeBoardCollage: vi.fn(),
  extractItemFrames: vi.fn(),
  renderBoardItem: vi.fn(),
}));

// The synthesis service pulls the aiProvider/promptRunner stack — stub it so
// this stays a routing test (service behavior is covered in its own suite).
vi.mock('../services/moodBoardStyleSynthesis.js', () => ({
  synthesizeBoardStyle: vi.fn(),
}));

vi.mock('../services/moodBoardCompositeStyle.js', () => ({
  composeBoardPrompt: vi.fn(),
}));

vi.mock('../services/moodBoard/generateMore.js', () => ({ startGenerateJob: vi.fn(), getGenerateJob: vi.fn() }));
import { startGenerateJob, getGenerateJob } from '../services/moodBoard/generateMore.js';

import * as svc from '../services/moodBoard/index.js';
import { synthesizeBoardStyle } from '../services/moodBoardStyleSynthesis.js';
import { composeBoardPrompt } from '../services/moodBoardCompositeStyle.js';
import moodBoardRoutes from './moodBoard.js';

const makeApp = () => {
  const app = express();
  app.use(express.json());
  app.use('/api/mood-boards', moodBoardRoutes);
  app.use(errorMiddleware);
  return app;
};

describe('mood-board routes', () => {
  beforeEach(() => vi.clearAllMocks());

  it('GET /names serves the picker projection rather than a board lookup', async () => {
    svc.listBoardNames.mockResolvedValueOnce([{ id: 'mb-1', name: 'A' }]);
    const res = await request(makeApp()).get('/api/mood-boards/names');
    expect(res.status).toBe(200);
    expect(res.body).toEqual([{ id: 'mb-1', name: 'A' }]);
    expect(svc.getBoard).not.toHaveBeenCalled();
  });

  describe('GET /', () => {
    it('returns the full boards array by default', async () => {
      svc.listBoards.mockResolvedValueOnce([{ id: 'mb-1', name: 'A' }]);
      const res = await request(makeApp()).get('/api/mood-boards');
      expect(res.status).toBe(200);
      expect(Array.isArray(res.body)).toBe(true);
      expect(res.body).toHaveLength(1);
    });

    it('returns a bounded envelope when pagination is requested', async () => {
      svc.listBoards.mockResolvedValueOnce(
        Array.from({ length: 5 }, (_, i) => ({ id: `mb-${i}`, name: `B${i}` }))
      );
      const res = await request(makeApp()).get('/api/mood-boards?limit=2&offset=1');
      expect(res.status).toBe(200);
      expect(res.body.items).toHaveLength(2);
      expect(res.body.items[0].id).toBe('mb-1');
      expect(res.body.total).toBe(5);
      expect(res.body.limit).toBe(2);
      expect(res.body.offset).toBe(1);
    });
  });

  describe('GET /:id', () => {
    it('returns 404 when the board is missing', async () => {
      svc.getBoard.mockResolvedValueOnce(null);
      const res = await request(makeApp()).get('/api/mood-boards/nope');
      expect(res.status).toBe(404);
    });

    it('returns the board when found', async () => {
      svc.getBoard.mockResolvedValueOnce({ id: 'mb-1', name: 'A' });
      const res = await request(makeApp()).get('/api/mood-boards/mb-1');
      expect(res.status).toBe(200);
      expect(res.body.id).toBe('mb-1');
    });
  });

  describe('POST /:id/synthesize-style (#4188 Phase 4)', () => {
    it('404s when the board is missing', async () => {
      svc.getBoard.mockResolvedValueOnce(null);
      const res = await request(makeApp()).post('/api/mood-boards/nope/synthesize-style').send({});
      expect(res.status).toBe(404);
      expect(synthesizeBoardStyle).not.toHaveBeenCalled();
    });

    it('passes the board and the validated style context to the service', async () => {
      const board = { id: 'mb-1', name: 'A', items: [] };
      svc.getBoard.mockResolvedValueOnce(board);
      synthesizeBoardStyle.mockResolvedValueOnce({ proposed: {}, diff: { hasChanges: false } });
      const res = await request(makeApp()).post('/api/mood-boards/mb-1/synthesize-style').send({
        styleNotes: 'current',
        influences: { embrace: ['a'], avoid: [] },
        locked: { influencesAvoid: true },
        providerId: 'ollama',
        model: 'qwen',
      });
      expect(res.status).toBe(200);
      expect(synthesizeBoardStyle).toHaveBeenCalledWith({
        board,
        styleNotes: 'current',
        influences: { embrace: ['a'], avoid: [] },
        locked: { influencesAvoid: true },
        providerId: 'ollama',
        model: 'qwen',
      });
    });

    it('400s on an unknown body key (strict schema)', async () => {
      const res = await request(makeApp()).post('/api/mood-boards/mb-1/synthesize-style').send({
        universeId: 'w-1',
      });
      expect(res.status).toBe(400);
      expect(synthesizeBoardStyle).not.toHaveBeenCalled();
    });
  });

  describe('POST /:id/compose-prompt', () => {
    it('404s when the board is missing', async () => {
      svc.getBoard.mockResolvedValueOnce(null);
      const res = await request(makeApp()).post('/api/mood-boards/nope/compose-prompt').send({});
      expect(res.status).toBe(404);
      expect(composeBoardPrompt).not.toHaveBeenCalled();
    });

    it('persists the composed style on the board', async () => {
      const board = { id: 'mb-1', name: 'A', items: [] };
      const style = { prompt: 'ink wash dusk', negativePrompt: null, rationale: 'tactile', analyzedItemCount: 1, providerId: 'ollama', model: 'qwen', composedAt: '2026-08-14T00:00:00.000Z' };
      svc.getBoard.mockResolvedValueOnce(board);
      composeBoardPrompt.mockResolvedValueOnce(style);
      svc.updateBoard.mockResolvedValueOnce({ ...board, style });
      const res = await request(makeApp()).post('/api/mood-boards/mb-1/compose-prompt').send({ providerId: 'ollama', model: 'qwen', effort: 'high' });
      expect(res.status).toBe(200);
      expect(composeBoardPrompt).toHaveBeenCalledWith({ board, providerId: 'ollama', model: 'qwen', effort: 'high' });
      expect(svc.updateBoard).toHaveBeenCalledWith('mb-1', { style });
      expect(res.body.style.prompt).toBe('ink wash dusk');
    });

    it('400s on an unknown body key', async () => {
      const res = await request(makeApp()).post('/api/mood-boards/mb-1/compose-prompt').send({ bogus: 'high' });
      expect(res.status).toBe(400);
      expect(composeBoardPrompt).not.toHaveBeenCalled();
    });
  });

  describe('POST /:id/backfill-prompts', () => {
    it('404s when the board is missing and returns the updated board otherwise', async () => {
      svc.backfillGalleryPrompts.mockResolvedValueOnce(null);
      expect((await request(makeApp()).post('/api/mood-boards/nope/backfill-prompts')).status).toBe(404);
      svc.backfillGalleryPrompts.mockResolvedValueOnce({ id: 'mb-1', items: [{ id: 'i1', caption: 'p' }] });
      const res = await request(makeApp()).post('/api/mood-boards/mb-1/backfill-prompts');
      expect(res.status).toBe(200);
      expect(res.body.items[0].caption).toBe('p');
    });
  });

  describe('POST /:id/x-post', () => {
    it('validates the body and passes it to the service', async () => {
      svc.importXPost.mockResolvedValueOnce({ board: { id: 'mb-1' }, added: 2 });
      const res = await request(makeApp()).post('/api/mood-boards/mb-1/x-post').send({ url: 'https://x.com/user/status/1' });
      expect(res.status).toBe(200);
      expect(res.body).toEqual({ board: { id: 'mb-1' }, added: 2 });
      expect(svc.importXPost).toHaveBeenCalledWith('mb-1', { url: 'https://x.com/user/status/1' });
    });

    it('400s on a non-http(s) url (schema gate)', async () => {
      const res = await request(makeApp()).post('/api/mood-boards/mb-1/x-post').send({ url: 'not a url' });
      expect(res.status).toBe(400);
      expect(svc.importXPost).not.toHaveBeenCalled();
    });
  });
  describe('collage + frame extraction', () => {
    it('applies collage defaults and rejects out-of-range frame counts', async () => {
      svc.composeBoardCollage.mockResolvedValueOnce({ filename: 'c.jpg' });
      const ok = await request(makeApp()).post('/api/mood-boards/mb-1/collage').send({});
      expect(ok.status).toBe(200);
      expect(svc.composeBoardCollage).toHaveBeenCalledWith('mb-1', { framesPerVideo: 3, addFramesToBoard: false, cellSize: 512 });
      const bad = await request(makeApp()).post('/api/mood-boards/mb-1/collage').send({ framesPerVideo: 99 });
      expect(bad.status).toBe(400);
    });

    it('takes a count or picked times for per-item frame extraction', async () => {
      svc.extractItemFrames.mockResolvedValueOnce({ added: 4 });
      const ok = await request(makeApp()).post('/api/mood-boards/mb-1/items/i1/extract-frames').send({ count: 4 });
      expect(ok.status).toBe(200);
      expect(svc.extractItemFrames).toHaveBeenCalledWith('mb-1', 'i1', { count: 4 });
      expect((await request(makeApp()).post('/api/mood-boards/mb-1/items/i1/extract-frames').send({})).status).toBe(400);
      expect((await request(makeApp()).post('/api/mood-boards/mb-1/items/i1/extract-frames').send({ times: [1.25, 3] })).status).toBe(200);
      expect(svc.extractItemFrames).toHaveBeenLastCalledWith('mb-1', 'i1', { times: [1.25, 3] });
      expect((await request(makeApp()).post('/api/mood-boards/mb-1/items/i1/extract-frames').send({ count: 2, times: [1] })).status).toBe(400);
    });

    it('queues a note render (202) on an optional backend pin', async () => {
      svc.renderBoardItem.mockResolvedValueOnce({ jobId: 'job-1', item: { id: 'i1' } });
      const ok = await request(makeApp()).post('/api/mood-boards/mb-1/items/i1/render').send({});
      expect(ok.status).toBe(202);
      expect(svc.renderBoardItem).toHaveBeenCalledWith('mb-1', 'i1', {});
      expect((await request(makeApp()).post('/api/mood-boards/mb-1/items/i1/render').send({ target: 'deck' })).status).toBe(400);
    });
  });
});


describe('mood-board generate routes', () => {
  it('validates bounded batches and forwards independent prompt and image choices', async () => {
    startGenerateJob.mockResolvedValue({ status: 'running' });
    const body = { requestId: '12345678-1234-4123-8123-123456789abc', providerId: 'writer', model: 'writer-model', render: { mode: 'grok', width: 1024, height: 576 } };
    const res = await request(makeApp()).post('/api/mood-boards/example/generate').send(body);
    expect(res.status).toBe(202);
    expect(startGenerateJob).toHaveBeenCalledWith('example', expect.objectContaining({ ...body, count: 1 }));
    startGenerateJob.mockClear();
    for (const invalid of [{ count: 5 }, { render: { width: 3840, height: 3840 } }, { render: { mode: 'external' } }, { requestId: '' }]) {
      expect((await request(makeApp()).post('/api/mood-boards/example/generate').send({ ...body, ...invalid })).status).toBe(400);
    }
    expect(startGenerateJob).not.toHaveBeenCalled();
    getGenerateJob.mockReturnValue({ status: 'queued', items: [] });
    expect((await request(makeApp()).get('/api/mood-boards/example/generate')).body.status).toBe('queued');
  });
});