Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
TypeScript agentic-aiagentsaiai-agentsaiagentsdeveloper-toolsfunction-callinggpt-4javascriptjsllmllmopsmcppythonremote-mcp-serverssetypescript
import { afterEach, beforeAll, describe, expect, it, vi } from '@effect/vitest';
import * as FileSystem from '@effect/platform/FileSystem';
import * as Path from '@effect/platform/Path';
import * as BunFileSystem from '@effect/platform-bun/BunFileSystem';
import * as BunPath from '@effect/platform-bun/BunPath';
import { ConfigProvider, Effect, Layer } from 'effect';
import { execSync } from 'node:child_process';
import * as tempy from 'tempy';
import { ComposioClientSingleton } from 'src/services/composio-clients';
import { APP_VERSION } from 'src/constants';
import { defaultNodeOs, NodeOs } from 'src/services/node-os';
import { extendConfigProvider } from 'src/services/config';
const withConfigLayer = (map: Map<string, string>, homedir: string) =>
Layer.mergeAll(
BunFileSystem.layer,
BunPath.layer,
Layer.succeed(NodeOs, defaultNodeOs({ homedir })),
Layer.setConfigProvider(extendConfigProvider(ConfigProvider.fromMap(map)))
);
const okResponse = () =>
new Response(JSON.stringify({ data: [] }), {
status: 200,
headers: { 'Content-Type': 'application/json' },
});
// Mirrors the module-private `cwdHash` in src/services/consumer-short-term-cache.ts
// (djb2Hash + base36): the seeded cache key below must be byte-identical to what
// the service computes for process.cwd() when it looks up the CLI session.
const cwdHash = (cwd: string): string => {
let hash = 5381;
for (let index = 0; index < cwd.length; index += 1) {
hash = (hash * 33) ^ cwd.charCodeAt(index);
}
return Math.abs(hash >>> 0).toString(36);
};
const writeCliSessionCache = (
homedir: string,
session: { readonly id: string; readonly expiresAt: string }
) =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const cacheDir = path.join(homedir, '.composio');
yield* fs.makeDirectory(cacheDir, { recursive: true });
yield* fs.writeFileString(
path.join(cacheDir, 'consumer-short-term-cache.json'),
JSON.stringify({
test: {
probablyMyCliSessionsByCwdHash: {
[cwdHash(process.cwd())]: session,
},
},
})
);
});
describe('ComposioClientSingleton headers', () => {
// Delete any real keychain entry so the subprocess keyring read
// inside ComposioUserContextLive (baked into
// ComposioClientSingleton.Default's dependencies) finds nothing
// and apiKey resolves to Option.none(). Without this, the test
// picks up real credentials and assertions on x-user-api-key fail.
beforeAll(() => {
try {
execSync(
'/usr/bin/security delete-generic-password -s com.composio.cli -a default 2>/dev/null',
{ stdio: 'ignore' }
);
} catch {
// Entry may not exist — fine.
}
});
afterEach(() => {
vi.restoreAllMocks();
vi.unstubAllEnvs();
});
it.effect('uses x-user-api-key and never x-api-key', () => {
const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue(okResponse());
const homedir = tempy.temporaryDirectory();
const configMap = new Map([
['COMPOSIO_USER_API_KEY', 'uak_from_user_env'],
['COMPOSIO_API_KEY', 'ak_should_be_ignored'],
['COMPOSIO_BASE_URL', 'https://backend.composio.dev'],
]);
return Effect.gen(function* () {
const client = yield* Effect.flatMap(ComposioClientSingleton, singleton => singleton.get());
yield* Effect.promise(() =>
client.tools
.list({ limit: 1, toolkit_versions: 'latest' })
.then(() => undefined)
.catch(() => undefined)
);
expect(fetchSpy).toHaveBeenCalledOnce();
const [, init] = fetchSpy.mock.calls[0]!;
const headers = new Headers((init as RequestInit).headers);
expect(headers.get('x-user-api-key')).toBe('uak_from_user_env');
expect(headers.has('x-api-key')).toBe(false);
expect(headers.get('x-framework')).toBe('cli');
expect(headers.get('x-source')).toBe('CLI');
expect(headers.get('x-runtime')).toBe('NODEJS');
expect(headers.get('x-sdk-version')).toBe(APP_VERSION);
expect(headers.get('x-cli-session-id')).toBeNull();
}).pipe(
Effect.provide(
Layer.provide(ComposioClientSingleton.Default, withConfigLayer(configMap, homedir))
)
);
});
// The session-id reader compares expiresAt against Clock.currentTimeMillis;
// the TestClock sits at epoch 0, so fixed instants around it are
// deterministically in the future or the past.
it.effect('includes the current cwd CLI session ID', () => {
const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue(okResponse());
const homedir = tempy.temporaryDirectory();
const configMap = new Map([
['COMPOSIO_USER_API_KEY', 'uak_from_user_env'],
['COMPOSIO_BASE_URL', 'https://backend.composio.dev'],
]);
return Effect.gen(function* () {
const path = yield* Path.Path;
// The session-id reader resolves its cache dir from COMPOSIO_CACHE_DIR
// (stubbed by the shared vitest setup) before falling back to homedir,
// so point it at the directory this test writes the fixture into.
vi.stubEnv('COMPOSIO_CACHE_DIR', path.join(homedir, '.composio'));
yield* writeCliSessionCache(homedir, {
id: 'cli_s_current',
expiresAt: '1970-01-01T00:01:00.000Z',
});
const client = yield* Effect.flatMap(ComposioClientSingleton, singleton => singleton.get());
yield* Effect.promise(() =>
client.tools
.list({ limit: 1, toolkit_versions: 'latest' })
.then(() => undefined)
.catch(() => undefined)
);
const [, init] = fetchSpy.mock.calls[0]!;
const headers = new Headers((init as RequestInit).headers);
expect(headers.get('x-cli-session-id')).toBe('cli_s_current');
}).pipe(
Effect.provide(
Layer.provideMerge(ComposioClientSingleton.Default, withConfigLayer(configMap, homedir))
)
);
});
it.effect('omits an expired cwd CLI session ID', () => {
const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue(okResponse());
const homedir = tempy.temporaryDirectory();
const configMap = new Map([
['COMPOSIO_USER_API_KEY', 'uak_from_user_env'],
['COMPOSIO_BASE_URL', 'https://backend.composio.dev'],
]);
return Effect.gen(function* () {
const path = yield* Path.Path;
vi.stubEnv('COMPOSIO_CACHE_DIR', path.join(homedir, '.composio'));
yield* writeCliSessionCache(homedir, {
id: 'cli_s_expired',
expiresAt: '1969-12-31T23:59:00.000Z',
});
const client = yield* Effect.flatMap(ComposioClientSingleton, singleton => singleton.get());
yield* Effect.promise(() =>
client.tools
.list({ limit: 1, toolkit_versions: 'latest' })
.then(() => undefined)
.catch(() => undefined)
);
const [, init] = fetchSpy.mock.calls[0]!;
const headers = new Headers((init as RequestInit).headers);
expect(headers.get('x-cli-session-id')).toBeNull();
}).pipe(
Effect.provide(
Layer.provideMerge(ComposioClientSingleton.Default, withConfigLayer(configMap, homedir))
)
);
});
it.effect('does not read COMPOSIO_API_KEY for user auth', () => {
const fetchSpy = vi.spyOn(globalThis, 'fetch').mockResolvedValue(okResponse());
const homedir = tempy.temporaryDirectory();
const configMap = new Map([
['COMPOSIO_API_KEY', 'ak_only'],
['COMPOSIO_BASE_URL', 'https://backend.composio.dev'],
]);
return Effect.gen(function* () {
const client = yield* Effect.flatMap(ComposioClientSingleton, singleton => singleton.get());
yield* Effect.promise(() =>
client.tools
.list({ limit: 1, toolkit_versions: 'latest' })
.then(() => undefined)
.catch(() => undefined)
);
expect(fetchSpy).toHaveBeenCalledOnce();
const [, init] = fetchSpy.mock.calls[0]!;
const headers = new Headers((init as RequestInit).headers);
expect(headers.get('x-user-api-key')).toBeNull();
expect(headers.has('x-api-key')).toBe(false);
expect(headers.get('x-source')).toBe('CLI');
}).pipe(
Effect.provide(
Layer.provide(ComposioClientSingleton.Default, withConfigLayer(configMap, homedir))
)
);
});
});