Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 04:45 UTC
| 1 | import { Effect, Layer, Option } from 'effect'; |
| 2 | import { CommandSurface } from 'src/commands/surface'; |
| 3 | import { ComposioClientSingleton } from 'src/services/composio-clients'; |
| 4 | import { ProjectContext } from 'src/services/project-context'; |
| 5 | import type { TerminalUI } from 'src/services/terminal-ui'; |
| 6 | import { ComposioUserContext } from 'src/services/user-context'; |
| 7 | import { cliExit } from 'test/__utils__/cli'; |
| 8 | import type { MockAccountRequest, MockRequestScope } from 'test/__utils__/layers/accounts'; |
| 9 | import { makeOrgProject, makeSessionInfo } from 'test/__utils__/models/account'; |
| 10 | import type { TestLiveInput } from 'test/__utils__/services/test-layer'; |
| 11 | import { recordRepositoryScopes } from 'test/__utils__/services/toolkits-repository-stub'; |
| 12 | |
| 13 | /** |
| 14 | * The developer project the `global-test-user-id` fixture's directory is set up for |
| 15 | * (`.composio/project.json`), as the org and project a request is scoped to. |
| 16 | */ |
| 17 | export const DIRECTORY_PROJECT_SCOPE = { orgId: 'org_test', projectId: 'proj_test' } as const; |
| 18 | |
| 19 | /** Another developer project in the same org, which `--project-name` names. */ |
| 20 | export const OTHER_DEVELOPER_PROJECT = makeOrgProject({ |
| 21 | id: 'proj_other', |
| 22 | name: 'other-project', |
| 23 | orgId: 'org_test', |
| 24 | }); |
| 25 | |
| 26 | export const OTHER_PROJECT_SCOPE = { |
| 27 | orgId: 'org_test', |
| 28 | projectId: OTHER_DEVELOPER_PROJECT.id, |
| 29 | } as const; |
| 30 | |
| 31 | /** |
| 32 | * `TestLive` input for a logged-in user (org `org_test`, global test user `global-default`) |
| 33 | * working in a directory set up for {@link DIRECTORY_PROJECT_SCOPE}, whose org also holds |
| 34 | * {@link OTHER_DEVELOPER_PROJECT}. |
| 35 | */ |
| 36 | export const inDeveloperProjectDirectory = { |
| 37 | fixture: 'global-test-user-id', |
| 38 | accountData: { projects: [OTHER_DEVELOPER_PROJECT] }, |
| 39 | } as const satisfies TestLiveInput; |
| 40 | |
| 41 | /** |
| 42 | * The developer project the backend acts on for a request that names none, as session info |
| 43 | * reports it for `org_test`: the newest developer project of that org. |
| 44 | */ |
| 45 | export const DEFAULT_PROJECT_SCOPE = { orgId: 'org_test', projectId: 'proj_newest' } as const; |
| 46 | |
| 47 | /** |
| 48 | * `TestLive` account data whose session info reports {@link DEFAULT_PROJECT_SCOPE}'s project in the |
| 49 | * org the request names (`org_test` when it names none), recording every account request in |
| 50 | * `requests`. |
| 51 | */ |
| 52 | export const defaultDeveloperProjectAccountData = (requests: Array<MockAccountRequest> = []) => ({ |
| 53 | // The org the request names (`x-org-id`), else `org_test`. |
| 54 | sessionInfo: (scope: MockRequestScope) => |
| 55 | makeSessionInfo({ |
| 56 | orgId: scope.orgId ?? DEFAULT_PROJECT_SCOPE.orgId, |
| 57 | projectNanoId: DEFAULT_PROJECT_SCOPE.projectId, |
| 58 | projectName: 'newest-project', |
| 59 | }), |
| 60 | onRequest: (request: MockAccountRequest) => { |
| 61 | requests.push(request); |
| 62 | }, |
| 63 | }); |
| 64 | |
| 65 | /** |
| 66 | * What a dev command that acts on a project's resources says on stderr, off a terminal, when it |
| 67 | * uses {@link DEFAULT_PROJECT_SCOPE} because the directory is set up for no project. |
| 68 | */ |
| 69 | export const NO_DIRECTORY_PROJECT_NOTICE = |
| 70 | 'Warning: No developer project is set up for this directory; using "newest-project" (proj_newest), the newest developer project in the org you are logged in to, instead. Run `composio dev init` to choose one, or pass --project-name.'; |
| 71 | |
| 72 | /** |
| 73 | * Runs the CLI with `args` while the repository records the scope of every project resource |
| 74 | * call, and returns how the run ended with those calls. `layer` replaces more of the test layer, |
| 75 | * such as the terminal. |
| 76 | */ |
| 77 | export const runRecordingRepositoryScopes = ( |
| 78 | args: ReadonlyArray<string>, |
| 79 | layer: Layer.Layer<TerminalUI> | undefined = undefined |
| 80 | ) => |
| 81 | Effect.gen(function* () { |
| 82 | const recorder = recordRepositoryScopes(); |
| 83 | const result = yield* cliExit(args).pipe( |
| 84 | Effect.provide(recorder.layer), |
| 85 | Effect.provide(layer ?? Layer.empty) |
| 86 | ); |
| 87 | return { ...result, calls: recorder.calls }; |
| 88 | }); |
| 89 | |
| 90 | /** Logs in to `org_test` without setting the working directory up for any project. */ |
| 91 | export const loginWithoutDeveloperProject = Effect.gen(function* () { |
| 92 | const userContext = yield* ComposioUserContext; |
| 93 | yield* userContext.login('test_api_key', 'org_test'); |
| 94 | }); |
| 95 | |
| 96 | /** |
| 97 | * What `resolveSurfaceProject('developer')` reads in a handler test, without argv or `TestLive`: |
| 98 | * the dev tree with no shared flags, a user logged in to `org_test`, and a working directory set |
| 99 | * up for {@link DIRECTORY_PROJECT_SCOPE}. Resolving that project sends no request, so the |
| 100 | * client singleton dies when asked for a client. |
| 101 | */ |
| 102 | export const DirectoryProjectTest = Layer.mergeAll( |
| 103 | CommandSurface.dev({ userId: Option.none(), projectName: Option.none() }), |
| 104 | Layer.succeed( |
| 105 | ComposioUserContext, |
| 106 | ComposioUserContext.of({ |
| 107 | data: { |
| 108 | apiKey: Option.some('uak_test'), |
| 109 | baseURL: 'https://mock.invalid', |
| 110 | webURL: 'https://platform.mock.invalid', |
| 111 | orgId: Option.some(DIRECTORY_PROJECT_SCOPE.orgId), |
| 112 | projectId: Option.none(), |
| 113 | testUserId: Option.none(), |
| 114 | }, |
| 115 | backendMismatch: Option.none(), |
| 116 | isLoggedIn: () => true, |
| 117 | logout: Effect.void, |
| 118 | login: () => Effect.void, |
| 119 | update: () => Effect.void, |
| 120 | }) |
| 121 | ), |
| 122 | Layer.succeed( |
| 123 | ProjectContext, |
| 124 | ProjectContext.of({ |
| 125 | resolve: Effect.succeedSome({ |
| 126 | orgId: DIRECTORY_PROJECT_SCOPE.orgId, |
| 127 | projectId: DIRECTORY_PROJECT_SCOPE.projectId, |
| 128 | projectName: Option.some('Developer Project'), |
| 129 | orgName: Option.none(), |
| 130 | email: Option.none(), |
| 131 | testUserId: Option.none(), |
| 132 | }), |
| 133 | }) |
| 134 | ), |
| 135 | Layer.succeed( |
| 136 | ComposioClientSingleton, |
| 137 | ComposioClientSingleton.of({ |
| 138 | get: () => Effect.die('the client singleton is not used in this test'), |
| 139 | getFor: () => Effect.die('the client singleton is not used in this test'), |
| 140 | getMetrics: () => Effect.succeed({ byteSize: 0, requests: 0 }), |
| 141 | }) |
| 142 | ) |
| 143 | ); |