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.

5.9 KB
Raw Blame
import { execSync } from 'node:child_process';
import { readFileSync } from 'node:fs';
import path from 'node:path';

import { describe, expect, it } from 'vitest';
import { parse } from 'yaml';

const PKG_ROOT = path.join(__dirname, '..');

describe('generate-openapi', () => {
  it('openapi.yml stays in sync with the registered routes', () => {
    // Runs the generator in --check mode as a subprocess (the same command CI
    // and developers use), so the committed spec failing to match the routes
    // fails this test with the generator's own diagnostics.
    try {
      execSync('bun scripts/generate-openapi.ts --check', {
        cwd: PKG_ROOT,
        encoding: 'utf8',
        stdio: 'pipe',
      });
    } catch (error) {
      const execError = error as { stderr?: string; stdout?: string };
      throw new Error(
        `openapi.yml is out of date. Run \`bun generate:openapi\` in packages/openapi and commit the result.\n${execError.stderr ?? ''}${execError.stdout ?? ''}`,
        { cause: error },
      );
    }
  }, 120_000);

  it('committed spec is a valid OpenAPI 3.1 document with full route coverage', () => {
    const spec = parse(readFileSync(path.join(PKG_ROOT, 'openapi.yml'), 'utf8'));

    expect(spec.openapi).toBe('3.1.0');
    expect(spec.info.title).toBe('LobeHub API');
    expect(spec.components.securitySchemes.bearerAuth.scheme).toBe('bearer');
    expect(spec.components.schemas.Agent.additionalProperties).toBe(false);
    expect(spec.components.schemas.Agent.properties).not.toHaveProperty('clientId');
    expect(spec.components.schemas.Agent.properties).not.toHaveProperty('marketIdentifier');
    expect(spec.components.schemas.Agent.properties).not.toHaveProperty('userId');
    expect(spec.components.schemas.Agent.properties).not.toHaveProperty('workspaceId');
    expect(spec.components.schemas.Provider.properties).not.toHaveProperty('keyVaults');
    expect(spec.components.schemas.ChatResponse.additionalProperties).toBe(false);
    expect(spec.components.schemas.McpServer.properties).not.toHaveProperty('credentials');
    expect(spec.components.schemas.McpServer.properties).not.toHaveProperty('oidcConfig');
    expect(spec.components.schemas.ApiKey.properties.scopes.anyOf[0].items.enum).toEqual(
      expect.arrayContaining(['mcp:read', 'mcp:write', 'usage:read']),
    );
    expect(
      spec.paths['/api/v1/api-keys'].post.requestBody.content['application/json'].schema.properties
        .scopes.anyOf[0].items.enum,
    ).toEqual(expect.arrayContaining(['mcp:read', 'mcp:write', 'usage:read']));
    expect(spec.paths['/api/v1/chat'].post.responses['200'].content).toBeTruthy();
    expect(spec.paths['/api/v1/chat/translate'].post.responses['200'].content).toBeTruthy();
    expect(spec.paths['/api/v1/chat/generate-reply'].post.responses['200'].content).toBeTruthy();

    const operations = Object.values(spec.paths as Record<string, object>).flatMap((item) =>
      Object.keys(item).filter((method) =>
        ['delete', 'get', 'patch', 'post', 'put'].includes(method),
      ),
    );
    // Business endpoints + /health; grows as routes are added.
    expect(operations.length).toBeGreaterThanOrEqual(97);

    // Every response object must carry a description (required by OpenAPI).
    // Authenticated routes share 401/403/500; route-specific errors (404, 409,
    // …) are declared only where the handler can produce them.
    for (const [path, item] of Object.entries(spec.paths as Record<string, Record<string, any>>)) {
      for (const op of Object.values(item)) {
        if (!op.responses) continue;
        expect(
          op.responses['200']?.content ??
            op.responses['201']?.content ??
            op.responses['202']?.content,
        ).toBeTruthy();
        if (path !== '/api/v1/health') {
          expect(op.responses['401']?.content).toBeTruthy();
          expect(op.responses['403']?.content).toBeTruthy();
          expect(op.responses['500']?.content).toBeTruthy();
        }
        for (const response of Object.values(
          op.responses as Record<string, { description?: string }>,
        )) {
          expect(response.description).toBeTruthy();
          expect(response.description).not.toContain('schema pending');
        }
      }
    }
  });

  it('declares the success status the handler actually returns', () => {
    const spec = parse(readFileSync(path.join(PKG_ROOT, 'openapi.yml'), 'utf8'));

    const successStatuses = (method: string, path: string) =>
      Object.keys(spec.paths[path][method].responses).filter((status) => status.startsWith('2'));

    // Creating and enqueueing are not 200 responses; the document (and the SDK
    // generated from it) used to advertise 200 for all of them, so a
    // status-discriminating client branched on a status the runtime never sends.
    expect(successStatuses('post', '/api/v1/goals')).toEqual(['201']);
    expect(successStatuses('post', '/api/v1/tasks')).toEqual(['201']);
    expect(successStatuses('post', '/api/v1/signals/source-events')).toEqual(['202']);
    expect(successStatuses('post', '/api/v1/signals/trigger')).toEqual(['202']);
    expect(successStatuses('post', '/api/v1/tasks/{id}/runs')).toEqual(['202']);
    expect(successStatuses('post', '/api/v1/eval/runs')).toEqual(['202']);
    expect(successStatuses('post', '/api/v1/agents/{id}/duplicate')).toEqual(['201']);
    expect(successStatuses('get', '/api/v1/goals')).toEqual(['200']);

    // Exactly one success status per operation: a leftover placeholder 200 next
    // to the real 201/202 is the same wrong contract, just harder to spot.
    for (const [path, item] of Object.entries(
      spec.paths as Record<string, Record<string, { responses: Record<string, unknown> }>>,
    )) {
      for (const [method, op] of Object.entries(item)) {
        if (!['delete', 'get', 'patch', 'post', 'put'].includes(method)) continue;
        expect(
          Object.keys(op.responses).filter((status) => status.startsWith('2')),
          `${method.toUpperCase()} ${path}`,
        ).toHaveLength(1);
      }
    }
  });
});