Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

3.3 KB
Raw Blame
import { Hono } from 'hono';
import { describe, expect, it, vi } from 'vitest';

import type * as PermissionCheck from '../middleware/permission-check';

// The route module pulls in the db graph (via the permission middleware) and the
// better-auth graph (via `requireAuth`) at import time; this test is only about
// which gates the route itself declares.
vi.mock('@/database/core/db-adaptor', () => ({ getServerDB: vi.fn() }));
vi.mock('@/database/models/rbac', () => ({
  RbacModel: class {
    hasAnyPermission = async () => true;
  },
}));
vi.mock('../middleware/auth', () => ({
  requireAuth: async (_c: any, next: any) => next(),
}));
vi.mock('../middleware/permission-check', async (importOriginal) => {
  const actual = await importOriginal<typeof PermissionCheck>();
  return { ...actual, requireAnyPermission: () => async (_c: any, next: any) => next() };
});
vi.mock('../controllers/eval.controller', () => ({
  EvalController: class {
    createRun(c: any) {
      return c.json({ data: { id: 'run-1' }, success: true });
    }
  },
}));

vi.mock('../services/eval-resource.service', () => ({
  EvalResourceService: class {
    async deleteBenchmark(id: string) {
      return { deleted: true, id };
    }
  },
}));

const { default: EvalRoutes } = await import('./eval.route');
const { successEnvelope } = await import('../common/openapi');
const { DeletedResourceSchema } = await import('../types/common.type');

/**
 * `/eval/runs` queues an internal run, which pre-creates real chat topics via
 * `TopicModel` and keeps writing topic/message state from the QStash workflow.
 * A key deliberately denied `chat:write` must not be able to persist chat
 * artifacts through this entry point — same boundary `/responses` enforces.
 */
describe('POST /eval/runs API-key scopes', () => {
  const requestAs = (apiKeyScopes: string[]) => {
    const app = new Hono();

    app.use('*', async (c, next) => {
      c.set('userId' as never, 'user-1' as never);
      c.set('authType' as never, 'apikey' as never);
      c.set('apiKeyScopes' as never, apiKeyScopes as never);
      await next();
    });
    app.route('/', EvalRoutes);

    return app.request('/runs', {
      body: JSON.stringify({ datasetId: 'dataset-1', targetAgentId: 'agent-1' }),
      headers: { 'content-type': 'application/json' },
      method: 'POST',
    });
  };

  it('rejects a key holding agent:write and model:invoke but not chat:write', async () => {
    const res = await requestAs(['agent:write', 'model:invoke']);

    expect(res.status).toBe(403);
  });

  it('accepts a key holding the full agent-run trio', async () => {
    const res = await requestAs(['eval:write', 'chat:write', 'model:invoke']);

    expect(res.status).toBe(200);
  });
});

describe('DELETE /eval/benchmarks/:id', () => {
  it('answers with the unified deleted-resource payload', async () => {
    const app = new Hono();
    app.use('*', async (c, next) => {
      c.set('userId' as never, 'user-1' as never);
      await next();
    });
    app.route('/', EvalRoutes);

    const res = await app.request('/benchmarks/bm-1', { method: 'DELETE' });
    const body = await res.json();

    expect(res.status).toBe(200);
    expect(body.data).toEqual({ deleted: true, id: 'bm-1' });
    expect(() => successEnvelope(DeletedResourceSchema).parse(body)).not.toThrow();
  });
});