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

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

vi.mock('../services/videoDownload.js', () => ({
  startVideoDownload: vi.fn(async () => ({ jobId: 'job-1' })),
  attachDownloadSseClient: vi.fn(() => true),
  cancelVideoDownload: vi.fn(() => true),
  listDownloads: vi.fn(async () => [{ id: 'dl-1', filename: 'downloaded-dl-1.mp4', source: 'download' }]),
  deleteDownload: vi.fn(async () => ({ ok: true })),
}));

vi.mock('../services/ytdlpUpdate.js', () => ({
  getYtDlpUpdateStatus: vi.fn(async () => ({ installed: true, version: '2026.08.19', canUpdate: true })),
  updateYtDlp: vi.fn(async () => ({ success: true, version: '2026.08.19' })),
}));

import * as svc from '../services/videoDownload.js';
import * as ytdlpUpdate from '../services/ytdlpUpdate.js';
import { errorMiddleware } from '../lib/errorHandler.js';
import videoDownloadRoutes from './videoDownload.js';

function makeApp() {
  const app = express();
  app.use(express.json());
  app.use('/api/devtools/video-download', videoDownloadRoutes);
  app.use(errorMiddleware);
  return app;
}

describe('video download routes (#1946)', () => {
  let app;
  beforeEach(() => { app = makeApp(); vi.clearAllMocks(); });

  it('POST / starts a job for a valid URL', async () => {
    const url = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ';
    const r = await request(app).post('/api/devtools/video-download').send({ url });
    expect(r.status).toBe(202);
    expect(r.body).toEqual({ jobId: 'job-1' });
    expect(svc.startVideoDownload).toHaveBeenCalledWith(url);
  });

  it('POST / rejects a non-URL body before reaching the service', async () => {
    const r = await request(app).post('/api/devtools/video-download').send({ url: 'not a url' });
    expect(r.status).toBe(400);
    expect(svc.startVideoDownload).not.toHaveBeenCalled();
  });

  it('GET /downloads lists downloaded videos (not read as a jobId)', async () => {
    const r = await request(app).get('/api/devtools/video-download/downloads');
    expect(r.status).toBe(200);
    expect(r.body[0].id).toBe('dl-1');
    expect(svc.attachDownloadSseClient).not.toHaveBeenCalled();
  });

  it('GET /downloads returns a bounded envelope when pagination is requested', async () => {
    svc.listDownloads.mockResolvedValueOnce(
      Array.from({ length: 5 }, (_, i) => ({ id: `dl-${i}`, source: 'download' })),
    );
    const r = await request(app).get('/api/devtools/video-download/downloads?limit=2&offset=1');
    expect(r.status).toBe(200);
    expect(r.body).toEqual({
      items: [
        { id: 'dl-1', source: 'download' },
        { id: 'dl-2', source: 'download' },
      ],
      total: 5,
      limit: 2,
      offset: 1,
    });
  });

  it('DELETE /downloads/:id deletes a download', async () => {
    const r = await request(app).delete('/api/devtools/video-download/downloads/dl-1');
    expect(r.status).toBe(200);
    expect(r.body).toEqual({ ok: true });
    expect(svc.deleteDownload).toHaveBeenCalledWith('dl-1');
  });

  it('GET /:jobId/events 404s when the job is unknown', async () => {
    svc.attachDownloadSseClient.mockReturnValueOnce(false);
    const r = await request(app).get('/api/devtools/video-download/missing/events');
    expect(r.status).toBe(404);
  });

  it('POST /:jobId/cancel proxies to the service', async () => {
    const r = await request(app).post('/api/devtools/video-download/job-1/cancel');
    expect(r.status).toBe(200);
    expect(r.body).toEqual({ ok: true });
    expect(svc.cancelVideoDownload).toHaveBeenCalledWith('job-1');
  });

  // `yt-dlp` has to stay a literal first segment rather than falling into
  // `/:jobId/events`, the same collision `downloads` is guarded against above.
  it('GET /yt-dlp reports update status instead of being read as a jobId', async () => {
    const r = await request(app).get('/api/devtools/video-download/yt-dlp');
    expect(r.status).toBe(200);
    expect(r.body.version).toBe('2026.08.19');
    expect(svc.attachDownloadSseClient).not.toHaveBeenCalled();
  });

  it('POST /yt-dlp/update returns the update result', async () => {
    const r = await request(app).post('/api/devtools/video-download/yt-dlp/update');
    expect(r.status).toBe(200);
    expect(r.body).toEqual({ success: true, version: '2026.08.19' });
    expect(svc.cancelVideoDownload).not.toHaveBeenCalled();
  });

  // A refusal (Homebrew pin, missing binary, failed upgrade) is returned rather
  // than thrown by the service, so the route is the only place it can become an
  // error the page renders — a 200 here would report a failed update as done.
  it('POST /yt-dlp/update surfaces a refusal as an error, not a success', async () => {
    ytdlpUpdate.updateYtDlp.mockResolvedValueOnce({ success: false, error: 'yt-dlp is pinned in Homebrew.' });
    const r = await request(app).post('/api/devtools/video-download/yt-dlp/update');
    expect(r.status).toBe(502);
    expect(r.body.error ?? r.body.message).toContain('pinned');
  });
});