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.

16.1 KB
Raw Blame
import { describe, it } from '@effect/vitest';
import { assertEquals } from '@effect/vitest/utils';
import * as FileSystem from '@effect/platform/FileSystem';
import * as BunFileSystem from '@effect/platform-bun/BunFileSystem';
import * as BunPath from '@effect/platform-bun/BunPath';
import { ConfigProvider, Effect, Layer, Option, Data } from 'effect';
import * as tempy from 'tempy';
import { ComposioUserContext, rawComposioUserContextLive } from 'src/services/user-context';
import { defaultNodeOs, NodeOs } from 'src/services/node-os';
import { UserData, UserDataWithDefaults, userDataToJSON } from 'src/models/user-data';
import { extendConfigProvider } from 'src/services/config';
import { CliUserConfig } from 'src/models/cli-user-config';
import { ComposioCliUserConfig } from 'src/services/cli-user-config';
import { makeKeyringService, KeyringService } from '@composio/cli-keyring/effect';
import {
  type CredentialStore,
  type EntryModifiers,
  KeyringError,
  CredentialPersistence,
} from '@composio/cli-keyring';
import path from 'node:path';

const InMemoryKeyringLayer = (() => {
  const items = new Map<string, Uint8Array>();
  const key = (s: string, u: string) => `${s}\0${u}`;
  const store: CredentialStore = {
    id: 'memory',
    vendor: 'test',
    persistence: () => CredentialPersistence.ProcessOnly,
    async setSecret(s: string, u: string, secret: Uint8Array, _m: EntryModifiers) {
      items.set(key(s, u), new Uint8Array(secret));
    },
    async getSecret(s: string, u: string, _m: EntryModifiers) {
      const v = items.get(key(s, u));
      if (!v) throw new KeyringError({ kind: 'NoEntry' });
      return v;
    },
    async deleteCredential(s: string, u: string, _m: EntryModifiers) {
      if (!items.delete(key(s, u))) throw new KeyringError({ kind: 'NoEntry' });
    },
  };
  return Layer.succeed(KeyringService, makeKeyringService(store));
})();

const MockCliUserConfigLayer = Layer.succeed(
  ComposioCliUserConfig,
  ComposioCliUserConfig.of({
    data: {
      channel: 'beta',
      developerModeEnabled: true,
      developerDangerousCommandsEnabled: false,
      experimentalFeatures: {},
      artifactDirectory: undefined,
      experimentalSubagentTarget: 'auto',
      security: 'auto',
    },
    raw: CliUserConfig.make({
      developer: { enabled: true, destructiveActions: false },
      experimentalFeatures: {},
      artifactDirectory: Option.none(),
      experimentalSubagent: Option.none(),
      security: 'auto',
    }),
    channel: 'beta',
    isDevModeEnabled: () => true,
    areDeveloperDangerousCommandsEnabled: () => false,
    isExperimentalFeatureEnabled: () => true,
    update: () => Effect.void,
  })
);

const ComposioUserContextLive = Layer.provide(
  rawComposioUserContextLive,
  Layer.mergeAll(InMemoryKeyringLayer, MockCliUserConfigLayer)
);

describe('ComposioUserContext', () => {
  const withMapConfigProvider = (map: Map<string, string>) =>
    Layer.setConfigProvider(extendConfigProvider(ConfigProvider.fromMap(map)));

  describe('[When] no `~/.composio/user_data.json` config file exists', () => {
    describe('[When] no dynamic `Config` is set', () => {
      it.scoped('[Then] it contains default user data', () => {
        const cwd = tempy.temporaryDirectory();
        const map = new Map([]) satisfies Map<string, string>;

        const NodeOsTest = Layer.succeed(NodeOs, defaultNodeOs({ homedir: cwd }));
        const ComposioUserContextTest = Layer.provideMerge(
          ComposioUserContextLive,
          Layer.mergeAll(BunFileSystem.layer, BunPath.layer, NodeOsTest, withMapConfigProvider(map))
        );

        return Effect.gen(function* () {
          const ctx = yield* ComposioUserContext;

          const expectedUserData = UserDataWithDefaults.make({
            apiKey: Option.none(),
            baseURL: 'https://backend.composio.dev',
            webURL: 'https://dashboard.composio.dev/',
            orgId: Option.none(),
            projectId: Option.none(),
            testUserId: Option.none(),
          });
          assertEquals(Data.struct(ctx.data), Data.struct(expectedUserData));
          assertEquals(ctx.isLoggedIn(), false);
        }).pipe(Effect.provide(ComposioUserContextTest));
      });
    });

    describe('[When] dynamic `APP_CONFIG` is set', () => {
      it.scoped('[Then] is logged in', () => {
        const cwd = tempy.temporaryDirectory();
        const map = new Map([
          ['COMPOSIO_USER_API_KEY', 'api_key'],
          ['COMPOSIO_BASE_URL', 'https://test.composio.localhost'],
        ]) satisfies Map<string, string>;

        const NodeOsTest = Layer.succeed(NodeOs, defaultNodeOs({ homedir: cwd }));
        const ComposioUserContextTest = Layer.provideMerge(
          ComposioUserContextLive,
          Layer.mergeAll(BunFileSystem.layer, BunPath.layer, NodeOsTest, withMapConfigProvider(map))
        );

        return Effect.gen(function* () {
          const ctx = yield* ComposioUserContext;

          const expectedUserData = UserDataWithDefaults.make({
            apiKey: Option.some('api_key'),
            baseURL: 'https://test.composio.localhost',
            webURL: 'https://dashboard.composio.dev/',
            orgId: Option.none(),
            projectId: Option.none(),
            testUserId: Option.none(),
          });
          assertEquals(Data.struct(ctx.data), Data.struct(expectedUserData));
          assertEquals(ctx.isLoggedIn(), true);
        }).pipe(Effect.provide(ComposioUserContextTest));
      });

      it.scoped('[Then] COMPOSIO_API_KEY alone does not authenticate user context', () => {
        const cwd = tempy.temporaryDirectory();
        const map = new Map([['COMPOSIO_API_KEY', 'legacy_api_key']]) satisfies Map<string, string>;

        const NodeOsTest = Layer.succeed(NodeOs, defaultNodeOs({ homedir: cwd }));
        const ComposioUserContextTest = Layer.provideMerge(
          ComposioUserContextLive,
          Layer.mergeAll(BunFileSystem.layer, BunPath.layer, NodeOsTest, withMapConfigProvider(map))
        );

        return Effect.gen(function* () {
          const ctx = yield* ComposioUserContext;
          assertEquals(ctx.isLoggedIn(), false);
          assertEquals(Option.getOrUndefined(ctx.data.apiKey), undefined);
        }).pipe(Effect.provide(ComposioUserContextTest));
      });
    });
  });

  describe('[When] `~/.composio/user_data.json` config file exists', () => {
    describe('[When] no dynamic `Config` is set', () => {
      // Note: this test only passes when using `it`, not `it.scoped`
      it('[Then] it reflects the config file', () => {
        const cwd = tempy.temporaryDirectory();
        const map = new Map([]) satisfies Map<string, string>;

        const NodeOsTest = Layer.succeed(NodeOs, defaultNodeOs({ homedir: cwd }));
        const ComposioUserContextTest = Layer.provideMerge(
          ComposioUserContextLive,
          Layer.mergeAll(BunFileSystem.layer, BunPath.layer, NodeOsTest, withMapConfigProvider(map))
        );

        return Effect.gen(function* () {
          const expectedUserData = UserData.make({
            apiKey: Option.some('api_key'),
            baseURL: Option.some('https://test.composio.localhost'),
            webURL: Option.some('https://dashboard.composio.dev/'),
            orgId: Option.none(),
            projectId: Option.none(),
            testUserId: Option.none(),
          });
          const userDataAsJson = yield* userDataToJSON(expectedUserData);

          const fs = yield* FileSystem.FileSystem;
          const userDataPath = path.join(cwd, '.composio', 'user_data.json');
          yield* fs.makeDirectory(path.join(cwd, '.composio'), { recursive: true });
          yield* fs.writeFileString(userDataPath, userDataAsJson);
          yield* fs.chmod(userDataPath, 0o644);
          assertEquals((yield* fs.stat(userDataPath)).mode & 0o777, 0o644);

          const ctx = yield* ComposioUserContext;
          assertEquals(
            Data.struct(ctx.data),
            Data.struct({
              ...expectedUserData,
              baseURL: expectedUserData.baseURL.pipe(Option.getOrUndefined),
              webURL: expectedUserData.webURL.pipe(Option.getOrUndefined),
            })
          );
          assertEquals(ctx.isLoggedIn(), true);
          assertEquals(yield* fs.readFileString(userDataPath, 'utf8'), userDataAsJson);
          assertEquals((yield* fs.stat(userDataPath)).mode & 0o777, 0o600);
        }).pipe(Effect.provide(ComposioUserContextTest));
      });
    });

    describe('[When] dynamic `APP_CONFIG` is set', () => {
      it.scoped('[Then] it overrides the config file', () => {
        const cwd = tempy.temporaryDirectory();
        const map = new Map([['COMPOSIO_USER_API_KEY', 'api_key']]) satisfies Map<string, string>;

        const NodeOsTest = Layer.succeed(NodeOs, defaultNodeOs({ homedir: cwd }));
        const ComposioUserContextTest = Layer.provideMerge(
          ComposioUserContextLive,
          Layer.mergeAll(BunFileSystem.layer, BunPath.layer, NodeOsTest, withMapConfigProvider(map))
        );

        return Effect.gen(function* () {
          const expectedUserData = UserData.make({
            apiKey: Option.some('api_key'),
            baseURL: Option.none(),
            webURL: Option.some('https://dashboard.composio.dev/'),
            orgId: Option.none(),
            projectId: Option.none(),
            testUserId: Option.none(),
          });
          const userDataAsJson = yield* userDataToJSON(expectedUserData);

          const fs = yield* FileSystem.FileSystem;
          yield* fs.makeDirectory(path.join(cwd, '.composio'), { recursive: true });
          yield* fs.writeFileString(path.join(cwd, '.composio', 'user_data.json'), userDataAsJson);

          const ctx = yield* ComposioUserContext;

          assertEquals(
            Data.struct(ctx.data),
            Data.struct({
              ...expectedUserData,
              baseURL: 'https://backend.composio.dev',
              webURL: expectedUserData.webURL.pipe(Option.getOrUndefined),
            })
          );
          assertEquals(ctx.isLoggedIn(), true);
        }).pipe(Effect.provide(ComposioUserContextTest));
      });
    });

    describe('[When] the file is empty', () => {
      it('[Then] it falls back to defaults and overwrites the file', () => {
        const cwd = tempy.temporaryDirectory();
        const map = new Map([]) satisfies Map<string, string>;

        const NodeOsTest = Layer.succeed(NodeOs, defaultNodeOs({ homedir: cwd }));
        const ComposioUserContextTest = Layer.provideMerge(
          ComposioUserContextLive,
          Layer.mergeAll(BunFileSystem.layer, BunPath.layer, NodeOsTest, withMapConfigProvider(map))
        );

        return Effect.gen(function* () {
          const fs = yield* FileSystem.FileSystem;
          yield* fs.makeDirectory(path.join(cwd, '.composio'), { recursive: true });
          // Write an empty file
          yield* fs.writeFileString(path.join(cwd, '.composio', 'user_data.json'), '');

          // Should NOT throw — should fall back to defaults
          const ctx = yield* ComposioUserContext;

          const expectedUserData = UserDataWithDefaults.make({
            apiKey: Option.none(),
            baseURL: 'https://backend.composio.dev',
            webURL: 'https://dashboard.composio.dev/',
            orgId: Option.none(),
            projectId: Option.none(),
            testUserId: Option.none(),
          });
          assertEquals(Data.struct(ctx.data), Data.struct(expectedUserData));
          assertEquals(ctx.isLoggedIn(), false);

          // The corrupted file should have been overwritten with valid defaults
          const contents = yield* fs.readFileString(
            path.join(cwd, '.composio', 'user_data.json'),
            'utf8'
          );
          const parsed = JSON.parse(contents);
          assertEquals(parsed.api_key, null);
        }).pipe(Effect.provide(ComposioUserContextTest));
      });
    });

    describe('[When] the file contains invalid JSON', () => {
      it('[Then] it falls back to defaults and overwrites the file', () => {
        const cwd = tempy.temporaryDirectory();
        const map = new Map([]) satisfies Map<string, string>;

        const NodeOsTest = Layer.succeed(NodeOs, defaultNodeOs({ homedir: cwd }));
        const ComposioUserContextTest = Layer.provideMerge(
          ComposioUserContextLive,
          Layer.mergeAll(BunFileSystem.layer, BunPath.layer, NodeOsTest, withMapConfigProvider(map))
        );

        return Effect.gen(function* () {
          const fs = yield* FileSystem.FileSystem;
          yield* fs.makeDirectory(path.join(cwd, '.composio'), { recursive: true });
          // Write corrupted JSON
          yield* fs.writeFileString(
            path.join(cwd, '.composio', 'user_data.json'),
            '{not valid json!!!'
          );

          // Should NOT throw — should fall back to defaults
          const ctx = yield* ComposioUserContext;

          const expectedUserData = UserDataWithDefaults.make({
            apiKey: Option.none(),
            baseURL: 'https://backend.composio.dev',
            webURL: 'https://dashboard.composio.dev/',
            orgId: Option.none(),
            projectId: Option.none(),
            testUserId: Option.none(),
          });
          assertEquals(Data.struct(ctx.data), Data.struct(expectedUserData));
          assertEquals(ctx.isLoggedIn(), false);
        }).pipe(Effect.provide(ComposioUserContextTest));
      });
    });

    describe('[When] the file contains valid JSON but wrong schema', () => {
      it('[Then] it falls back to defaults and overwrites the file', () => {
        const cwd = tempy.temporaryDirectory();
        const map = new Map([]) satisfies Map<string, string>;

        const NodeOsTest = Layer.succeed(NodeOs, defaultNodeOs({ homedir: cwd }));
        const ComposioUserContextTest = Layer.provideMerge(
          ComposioUserContextLive,
          Layer.mergeAll(BunFileSystem.layer, BunPath.layer, NodeOsTest, withMapConfigProvider(map))
        );

        return Effect.gen(function* () {
          const fs = yield* FileSystem.FileSystem;
          yield* fs.makeDirectory(path.join(cwd, '.composio'), { recursive: true });
          // Write valid JSON but with wrong schema (api_key should be string|null, not number)
          yield* fs.writeFileString(
            path.join(cwd, '.composio', 'user_data.json'),
            JSON.stringify({ api_key: 12345, unknown_field: true })
          );

          // Should NOT throw — should fall back to defaults
          const ctx = yield* ComposioUserContext;

          const expectedUserData = UserDataWithDefaults.make({
            apiKey: Option.none(),
            baseURL: 'https://backend.composio.dev',
            webURL: 'https://dashboard.composio.dev/',
            orgId: Option.none(),
            projectId: Option.none(),
            testUserId: Option.none(),
          });
          assertEquals(Data.struct(ctx.data), Data.struct(expectedUserData));
          assertEquals(ctx.isLoggedIn(), false);
        }).pipe(Effect.provide(ComposioUserContextTest));
      });
    });

    describe('[When] the file is corrupted but env USER_API_KEY is set', () => {
      it('[Then] it falls back to defaults but preserves env USER_API_KEY', () => {
        const cwd = tempy.temporaryDirectory();
        const map = new Map([
          ['COMPOSIO_USER_API_KEY', 'env_api_key'],
          ['COMPOSIO_API_KEY', 'legacy_api_key_should_be_ignored'],
        ]) satisfies Map<string, string>;

        const NodeOsTest = Layer.succeed(NodeOs, defaultNodeOs({ homedir: cwd }));
        const ComposioUserContextTest = Layer.provideMerge(
          ComposioUserContextLive,
          Layer.mergeAll(BunFileSystem.layer, BunPath.layer, NodeOsTest, withMapConfigProvider(map))
        );

        return Effect.gen(function* () {
          const fs = yield* FileSystem.FileSystem;
          yield* fs.makeDirectory(path.join(cwd, '.composio'), { recursive: true });
          yield* fs.writeFileString(path.join(cwd, '.composio', 'user_data.json'), '');

          const ctx = yield* ComposioUserContext;

          // Despite corrupted file, env USER_API_KEY should still work
          assertEquals(ctx.isLoggedIn(), true);
          assertEquals(Option.getOrUndefined(ctx.data.apiKey), 'env_api_key');
        }).pipe(Effect.provide(ComposioUserContextTest));
      });
    });
  });
});