Skip to content

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

5.1 KB
Raw Blame
/**
 * E2E test worker for @composio/core file operations in Cloudflare Workers.
 * Tests that file operations correctly throw errors in edge runtimes
 * and that the FileToolModifier.workerd.ts is properly loaded.
 */
import { Composio } from '@composio/core';
import { Hono } from 'hono';

type Bindings = {
  COMPOSIO_API_KEY: string;
  COMPOSIO_BASE_URL: string;
};

const app = new Hono<{ Bindings: Bindings }>();

/**
 * Default route - lists available test endpoints
 */
app.get('/', c => {
  return c.json({
    message: 'Composio Core Files E2E Test Worker',
    endpoints: [
      '/test/files/upload',
      '/test/files/download',
      '/test/auto-upload-disabled',
      '/test/default-config',
    ],
  });
});

/**
 * Test: Files upload operation (should fail in Cloudflare Workers)
 * Tests that composio.files.upload() throws an appropriate error in edge runtimes
 */
app.get('/test/files/upload', async c => {
  try {
    const composio = new Composio({
      apiKey: c.env.COMPOSIO_API_KEY,
      baseURL: c.env.COMPOSIO_BASE_URL,
    });

    // Attempt to call files.upload - should throw an error
    await composio.files.upload({
      file: 'https://example.com/test.pdf',
      toolSlug: 'test-tool',
      toolkitSlug: 'test-toolkit',
    });

    // If we get here, the test failed - upload should have thrown
    return c.json(
      {
        error: 'Expected files.upload() to throw an error in Cloudflare Workers, but it did not',
      },
      { status: 500 }
    );
  } catch (error) {
    const errorMessage = error instanceof Error ? error.message : String(error);
    return c.json({
      error: errorMessage,
    });
  }
});

/**
 * Test: Files download operation (should fail in Cloudflare Workers)
 * Tests that composio.files.download() throws an appropriate error in edge runtimes
 */
app.get('/test/files/download', async c => {
  try {
    const composio = new Composio({
      apiKey: c.env.COMPOSIO_API_KEY,
      baseURL: c.env.COMPOSIO_BASE_URL,
    });

    // Attempt to call files.download - should throw an error
    await composio.files.download({
      s3Url: 'https://s3.example.com/test.pdf',
      toolSlug: 'test-tool',
      mimeType: 'application/pdf',
    });

    // If we get here, the test failed - download should have thrown
    return c.json(
      {
        error: 'Expected files.download() to throw an error in Cloudflare Workers, but it did not',
      },
      { status: 500 }
    );
  } catch (error) {
    const errorMessage = error instanceof Error ? error.message : String(error);
    return c.json({
      error: errorMessage,
    });
  }
});

/**
 * Test: Composio initialization with automatic file upload/download disabled
 *
 * In workerd runtime (Cloudflare Workers), dangerouslyAllowAutoUploadDownloadFiles defaults to false.
 * This test verifies that the default configuration works correctly and that
 * Composio can be initialized successfully without file upload/download support.
 */
app.get('/test/auto-upload-disabled', async c => {
  try {
    // Explicitly keep automatic file handling off (default everywhere)
    const composio = new Composio({
      apiKey: c.env.COMPOSIO_API_KEY,
      baseURL: c.env.COMPOSIO_BASE_URL,
      dangerouslyAllowAutoUploadDownloadFiles: false,
    });

    return c.json({
      success: true,
      message:
        'Composio initialized successfully with dangerouslyAllowAutoUploadDownloadFiles: false',
      hasProvider: typeof composio.provider !== 'undefined',
      hasTools: typeof composio.tools !== 'undefined',
      hasFiles: typeof composio.files !== 'undefined',
      recommendation:
        'Use this configuration in edge runtimes to avoid FileToolModifier errors. ' +
        'Note that file upload/download operations will not be automatically handled.',
    });
  } catch (error) {
    return c.json(
      {
        success: false,
        error: error instanceof Error ? error.message : String(error),
      },
      { status: 500 }
    );
  }
});

/**
 * Test: Composio initialization with default configuration
 * 
 * This test verifies that Composio initializes correctly with the default
 * configuration for workerd runtime (dangerouslyAllowAutoUploadDownloadFiles: false by default).
 * No explicit configuration is provided, relying on the runtime defaults.
 */
app.get('/test/default-config', async c => {
  try {
    // Initialize Composio without explicitly setting dangerouslyAllowAutoUploadDownloadFiles
    // Should use the workerd default (false)
    const composio = new Composio({
      apiKey: c.env.COMPOSIO_API_KEY,
      baseURL: c.env.COMPOSIO_BASE_URL,
    });

    return c.json({
      success: true,
      message: 'Composio initialized successfully with default configuration',
      hasProvider: typeof composio.provider !== 'undefined',
      hasTools: typeof composio.tools !== 'undefined',
      hasFiles: typeof composio.files !== 'undefined',
      note: 'In workerd runtime, dangerouslyAllowAutoUploadDownloadFiles defaults to false',
    });
  } catch (error) {
    return c.json(
      {
        success: false,
        error: error instanceof Error ? error.message : String(error),
      },
      { status: 500 }
    );
  }
});

export default app;