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

10.4 KB
Raw Blame
/**
 * Schema tests for mood board validation (issue #911). Locks the cross-field
 * item rules (image requires mediaKey|imageUrl; text requires non-empty text)
 * and the URL/media-key shape guards the route boundary relies on.
 */

import { describe, it, expect } from 'vitest';
import {
  moodBoardCreateSchema,
  moodBoardUpdateSchema,
  moodBoardStyleSchema,
  moodBoardItemCreateSchema,
  moodBoardItemUpdateSchema,
  moodBoardXPostImportSchema,
  isVideoItemMediaKey,
} from './moodBoardValidation.js';

describe('moodBoardCreateSchema', () => {
  it('accepts a name + optional description', () => {
    expect(moodBoardCreateSchema.parse({ name: 'Refs' }).name).toBe('Refs');
    expect(moodBoardCreateSchema.parse({ name: 'Refs', description: 'd' }).description).toBe('d');
  });
  it('rejects an empty name', () => {
    expect(moodBoardCreateSchema.safeParse({ name: '' }).success).toBe(false);
  });
  it('rejects unknown keys (strict)', () => {
    expect(moodBoardCreateSchema.safeParse({ name: 'x', items: [] }).success).toBe(false);
  });
});

describe('moodBoardUpdateSchema', () => {
  it('accepts a partial patch', () => {
    expect(moodBoardUpdateSchema.parse({ description: '' }).description).toBe('');
  });
  it('accepts a composite style and a poster filename, and null clears both', () => {
    const style = moodBoardStyleSchema.parse({
      prompt: 'ink wash dusk',
      negativePrompt: 'gloss',
      analyzedItemCount: 2,
      composedAt: '2026-08-14T00:00:00.000Z',
    });
    expect(style.prompt).toBe('ink wash dusk');
    const patch = moodBoardUpdateSchema.parse({ style, posterImageRef: 'poster.png' });
    expect(patch.posterImageRef).toBe('poster.png');
    expect(moodBoardUpdateSchema.parse({ style: null, posterImageRef: null })).toEqual({ style: null, posterImageRef: null });
  });
  it('rejects a poster path and a style with no prompt', () => {
    expect(moodBoardUpdateSchema.safeParse({ posterImageRef: '../x.png' }).success).toBe(false);
    expect(moodBoardUpdateSchema.safeParse({ posterImageRef: 'notes.txt' }).success).toBe(false);
    expect(moodBoardStyleSchema.safeParse({ prompt: '   ' }).success).toBe(false);
    expect(moodBoardStyleSchema.safeParse({ prompt: 'ok', extra: true }).success).toBe(false);
  });
});

describe('moodBoardXPostImportSchema', () => {
  it('accepts an http(s) URL', () => {
    expect(moodBoardXPostImportSchema.parse({ url: 'https://x.com/user/status/1' }).url).toBe('https://x.com/user/status/1');
  });
  it('rejects a non-http(s) value', () => {
    expect(moodBoardXPostImportSchema.safeParse({ url: 'not a url' }).success).toBe(false);
  });
  it('rejects an empty url', () => {
    expect(moodBoardXPostImportSchema.safeParse({ url: '' }).success).toBe(false);
  });
  it('rejects unknown keys (strict)', () => {
    expect(moodBoardXPostImportSchema.safeParse({ url: 'https://x.com/user/status/1', extra: true }).success).toBe(false);
  });
});

describe('moodBoardItemCreateSchema', () => {
  it('accepts an image item with imageUrl', () => {
    expect(moodBoardItemCreateSchema.safeParse({ type: 'image', imageUrl: 'https://x/y.png' }).success).toBe(true);
  });
  it('accepts an image item with an app-path imageUrl', () => {
    expect(moodBoardItemCreateSchema.safeParse({ type: 'image', imageUrl: '/data/images/a.png' }).success).toBe(true);
  });
  it('accepts an image item with a >2048-char data:image/*;base64 imageUrl', () => {
    const dataUrl = `data:image/webp;base64,${'A'.repeat(4000)}`;
    expect(moodBoardItemCreateSchema.safeParse({ type: 'image', imageUrl: dataUrl }).success).toBe(true);
  });
  it('still rejects an http(s) imageUrl longer than 2048 characters', () => {
    const longHttpUrl = `https://example.com/${'a'.repeat(2040)}.png`;
    expect(moodBoardItemCreateSchema.safeParse({ type: 'image', imageUrl: longHttpUrl }).success).toBe(false);
  });
  it('rejects non-raster or non-base64 data: URLs', () => {
    expect(moodBoardItemCreateSchema.safeParse({ type: 'image', imageUrl: 'data:image/svg+xml;base64,PHN2Zz4=' }).success).toBe(false);
    expect(moodBoardItemCreateSchema.safeParse({ type: 'image', imageUrl: 'data:text/html;base64,PGh0bWw+' }).success).toBe(false);
    expect(moodBoardItemCreateSchema.safeParse({ type: 'image', imageUrl: 'data:image/png,iVBORw0KGgo=' }).success).toBe(false);
    expect(moodBoardItemCreateSchema.safeParse({ type: 'image', imageUrl: 'data:image/webp;charset=utf-8;base64,AAAA' }).success).toBe(false);
  });
  it('accepts whitespace inside a data URL and rejects a payload past the 12MB ceiling', () => {
    const payloadLen = Math.ceil((12 * 1024 * 1024 * 4) / 3);
    const payload = 'A'.repeat(payloadLen);
    let wrapped = '';
    let offset = 0;
    for (let breaks = 0; breaks < 21; breaks += 1) {
      wrapped += `${payload.slice(offset, offset + 76)}\r\n`;
      offset += 76;
    }
    wrapped += payload.slice(offset);
    const dataUrl = `data:image/webp;base64,${wrapped}`;
    expect(dataUrl.length).toBeGreaterThan(payloadLen + 64);
    expect(moodBoardItemCreateSchema.safeParse({ type: 'image', imageUrl: dataUrl }).success).toBe(true);
    const over = moodBoardItemCreateSchema.safeParse({ type: 'image', imageUrl: `data:image/webp;base64,${payload}AAAA` });
    expect(over.success).toBe(false);
    expect(over.error.issues[0].message).toBe('Image data URL exceeds the 12MB limit');
  });
  it('accepts an image item with a mediaKey', () => {
    expect(moodBoardItemCreateSchema.safeParse({ type: 'image', mediaKey: 'image:a.png' }).success).toBe(true);
  });
  it('rejects an image item with neither mediaKey nor imageUrl', () => {
    expect(moodBoardItemCreateSchema.safeParse({ type: 'image' }).success).toBe(false);
  });
  it('rejects a bad imageUrl scheme', () => {
    expect(moodBoardItemCreateSchema.safeParse({ type: 'image', imageUrl: 'javascript:alert(1)' }).success).toBe(false);
  });
  it('rejects a protocol-relative imageUrl', () => {
    expect(moodBoardItemCreateSchema.safeParse({ type: 'image', imageUrl: '//evil.com/x.png' }).success).toBe(false);
  });
  it('rejects a malformed mediaKey', () => {
    expect(moodBoardItemCreateSchema.safeParse({ type: 'image', mediaKey: 'no-colon' }).success).toBe(false);
  });
  it('accepts a text item with text', () => {
    expect(moodBoardItemCreateSchema.safeParse({ type: 'text', text: 'note' }).success).toBe(true);
  });
  it('rejects a text item with blank text', () => {
    expect(moodBoardItemCreateSchema.safeParse({ type: 'text', text: '   ' }).success).toBe(false);
  });
  it('accepts a video item with a video mediaKey (#4188)', () => {
    expect(moodBoardItemCreateSchema.safeParse({ type: 'video', mediaKey: 'video:upload-ab12cd34.mp4' }).success).toBe(true);
  });
  it('accepts a video item with a poster imageUrl alongside the mediaKey', () => {
    expect(moodBoardItemCreateSchema.safeParse({
      type: 'video', mediaKey: 'video:a.mp4', imageUrl: '/data/video-thumbnails/a.jpg',
    }).success).toBe(true);
  });
  it('rejects a video item without a mediaKey', () => {
    expect(moodBoardItemCreateSchema.safeParse({ type: 'video', imageUrl: '/data/video-thumbnails/a.jpg' }).success).toBe(false);
  });
  it('rejects a video item whose mediaKey is not kind video', () => {
    expect(moodBoardItemCreateSchema.safeParse({ type: 'video', mediaKey: 'image:a.png' }).success).toBe(false);
  });
  it('rejects a video item whose ref is a bare id (no extension) — it would 404 as a filename', () => {
    expect(moodBoardItemCreateSchema.safeParse({ type: 'video', mediaKey: 'video:job-123' }).success).toBe(false);
  });
  it('accepts the full embedded-upload prompt without truncation', () => {
    const prompt = 'p'.repeat(16000);
    expect(moodBoardItemCreateSchema.parse({ type: 'image', mediaKey: 'image:upload.png', prompt }).prompt).toBe(prompt);
    expect(moodBoardItemCreateSchema.safeParse({ type: 'image', mediaKey: 'image:upload.png', prompt: prompt + 'x' }).success).toBe(false);
  });
  it('accepts an image item with prompt alongside imageUrl', () => {
    const item = moodBoardItemCreateSchema.parse({ type: 'image', imageUrl: 'https://x/y.png', prompt: 'a prompt' });
    expect(item.prompt).toBe('a prompt');
  });
});

describe('isVideoItemMediaKey', () => {
  it('accepts a filename-with-extension video key', () => {
    expect(isVideoItemMediaKey('video:upload-ab12cd34.mp4')).toBe(true);
    expect(isVideoItemMediaKey('video:clip.webm')).toBe(true);
  });
  it('rejects non-video kinds, extension-less refs, and traversal shapes', () => {
    expect(isVideoItemMediaKey('image:a.png')).toBe(false);
    expect(isVideoItemMediaKey('video:job-123')).toBe(false);
    expect(isVideoItemMediaKey('video:..')).toBe(false);
    expect(isVideoItemMediaKey('video:a/b.mp4')).toBe(false);
    expect(isVideoItemMediaKey('video:a\\b.mp4')).toBe(false);
    expect(isVideoItemMediaKey(null)).toBe(false);
    expect(isVideoItemMediaKey('')).toBe(false);
  });
});

describe('moodBoardItemUpdateSchema', () => {
  it('accepts a caption-only patch', () => {
    expect(moodBoardItemUpdateSchema.parse({ caption: 'c' }).caption).toBe('c');
  });
  it('accepts a prompt patch', () => {
    expect(moodBoardItemUpdateSchema.parse({ prompt: 'new prompt' }).prompt).toBe('new prompt');
  });
  it('accepts a null caption (clear)', () => {
    expect(moodBoardItemUpdateSchema.parse({ caption: null }).caption).toBeNull();
  });
  it('accepts a full and a minimal analysis patch (#4188 Phase 3)', () => {
    const full = moodBoardItemUpdateSchema.parse({
      analysis: {
        prompt: 'a moody castle at dusk',
        negativePrompt: 'blurry',
        rationale: 'gothic look',
        providerId: 'openai',
        model: 'gpt-4o',
        analyzedAt: '2026-08-14T00:00:00.000Z',
      },
    });
    expect(full.analysis.prompt).toBe('a moody castle at dusk');
    expect(moodBoardItemUpdateSchema.parse({ analysis: { prompt: 'p' } }).analysis.prompt).toBe('p');
  });
  it('accepts a null analysis (clear)', () => {
    expect(moodBoardItemUpdateSchema.parse({ analysis: null }).analysis).toBeNull();
  });
  it('rejects an analysis without a prompt, with unknown keys, or with a non-ISO analyzedAt', () => {
    expect(() => moodBoardItemUpdateSchema.parse({ analysis: {} })).toThrow();
    expect(() => moodBoardItemUpdateSchema.parse({ analysis: { prompt: '   ' } })).toThrow();
    expect(() => moodBoardItemUpdateSchema.parse({ analysis: { prompt: 'p', extra: true } })).toThrow();
    expect(() => moodBoardItemUpdateSchema.parse({ analysis: { prompt: 'p', analyzedAt: 'yesterday' } })).toThrow();
  });
});