Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import * as BunFileSystem from '@effect/platform-bun/BunFileSystem'; |
| 2 | import * as BunPath from '@effect/platform-bun/BunPath'; |
| 3 | import { describe, expect, it } from '@effect/vitest'; |
| 4 | import { ConfigProvider, Effect, Layer } from 'effect'; |
| 5 | import * as FileSystem from 'effect/FileSystem'; |
| 6 | import { TestClock } from 'effect/testing'; |
| 7 | import path from 'node:path'; |
| 8 | import * as tempy from 'tempy'; |
| 9 | import * as constants from 'src/constants'; |
| 10 | import { defaultNodeOs, NodeOs } from 'src/services/node-os'; |
| 11 | import { PENDING_LOGIN_FILE_NAME, PendingLoginSession } from 'src/services/pending-login-session'; |
| 12 | import { hostOnlyEnvConfigProvider } from 'test/__utils__/live-env-config-provider'; |
| 13 | |
| 14 | const STARTED = { |
| 15 | key: 'session-key', |
| 16 | loginUrl: 'https://dashboard.composio.dev/?cliKey=session-key', |
| 17 | expiresAt: '1970-01-01T00:10:00.000Z', |
| 18 | }; |
| 19 | |
| 20 | /** The real service over a fresh home directory, so the file lands in `<home>/.composio`. */ |
| 21 | const pendingLoginTest = () => { |
| 22 | const home = tempy.temporaryDirectory({ prefix: 'pending-login' }); |
| 23 | const file = path.join(home, constants.USER_COMPOSIO_DIR, PENDING_LOGIN_FILE_NAME); |
| 24 | const layer = Layer.mergeAll( |
| 25 | Layer.provide( |
| 26 | PendingLoginSession.Default, |
| 27 | Layer.mergeAll( |
| 28 | BunFileSystem.layer, |
| 29 | BunPath.layer, |
| 30 | Layer.succeed(NodeOs, NodeOs.of(defaultNodeOs({ homedir: home }))) |
| 31 | ) |
| 32 | ), |
| 33 | BunFileSystem.layer, |
| 34 | ConfigProvider.layer(hostOnlyEnvConfigProvider) |
| 35 | ); |
| 36 | return { file, layer }; |
| 37 | }; |
| 38 | |
| 39 | describe('PendingLoginSession', () => { |
| 40 | it.effect( |
| 41 | '[Given] a written login [Then] reads it back from a file only the user can read', |
| 42 | () => { |
| 43 | const { file, layer } = pendingLoginTest(); |
| 44 | return Effect.gen(function* () { |
| 45 | const pending = yield* PendingLoginSession; |
| 46 | const fs = yield* FileSystem.FileSystem; |
| 47 | |
| 48 | yield* pending.write(STARTED); |
| 49 | |
| 50 | expect(yield* pending.read).toEqual({ ...STARTED, cachedAt: '1970-01-01T00:00:00.000Z' }); |
| 51 | expect((yield* fs.stat(file)).mode & 0o777).toBe(0o600); |
| 52 | }).pipe(Effect.provide(layer)); |
| 53 | } |
| 54 | ); |
| 55 | |
| 56 | it.effect('[Given] no cached login [Then] fails as missing', () => { |
| 57 | const { layer } = pendingLoginTest(); |
| 58 | return Effect.gen(function* () { |
| 59 | const pending = yield* PendingLoginSession; |
| 60 | |
| 61 | const error = yield* Effect.flip(pending.read); |
| 62 | |
| 63 | expect(error).toMatchObject({ |
| 64 | _tag: 'commands/PendingLoginError', |
| 65 | reason: 'missing', |
| 66 | message: 'No pending login found. Run `composio login < /dev/null` first.', |
| 67 | }); |
| 68 | }).pipe(Effect.provide(layer)); |
| 69 | }); |
| 70 | |
| 71 | it.effect('[Given] a file that is not a cached login [Then] fails as invalid', () => { |
| 72 | const { file, layer } = pendingLoginTest(); |
| 73 | return Effect.gen(function* () { |
| 74 | const pending = yield* PendingLoginSession; |
| 75 | const fs = yield* FileSystem.FileSystem; |
| 76 | yield* fs.makeDirectory(path.dirname(file), { recursive: true }); |
| 77 | yield* fs.writeFileString(file, '{"key":"only-a-key"}'); |
| 78 | |
| 79 | const error = yield* Effect.flip(pending.read); |
| 80 | |
| 81 | expect(error).toMatchObject({ reason: 'invalid', message: 'Pending login cache is invalid' }); |
| 82 | }).pipe(Effect.provide(layer)); |
| 83 | }); |
| 84 | |
| 85 | it.effect( |
| 86 | '[Given] a login cached over ten minutes ago [Then] deletes it and fails as expired', |
| 87 | () => { |
| 88 | const { file, layer } = pendingLoginTest(); |
| 89 | return Effect.gen(function* () { |
| 90 | const pending = yield* PendingLoginSession; |
| 91 | const fs = yield* FileSystem.FileSystem; |
| 92 | yield* pending.write(STARTED); |
| 93 | |
| 94 | yield* TestClock.adjust('10 minutes'); |
| 95 | expect(yield* pending.read).toMatchObject({ key: STARTED.key }); |
| 96 | |
| 97 | yield* TestClock.adjust('1 millis'); |
| 98 | const error = yield* Effect.flip(pending.read); |
| 99 | |
| 100 | expect(error).toMatchObject({ |
| 101 | reason: 'expired', |
| 102 | message: 'Pending login expired. Run `composio login < /dev/null` again.', |
| 103 | }); |
| 104 | expect(yield* fs.exists(file)).toBe(false); |
| 105 | }).pipe(Effect.provide(layer)); |
| 106 | } |
| 107 | ); |
| 108 | |
| 109 | it.effect( |
| 110 | '[Given] a cached login [When] cleared twice [Then] the file is gone and both succeed', |
| 111 | () => { |
| 112 | const { file, layer } = pendingLoginTest(); |
| 113 | return Effect.gen(function* () { |
| 114 | const pending = yield* PendingLoginSession; |
| 115 | const fs = yield* FileSystem.FileSystem; |
| 116 | yield* pending.write(STARTED); |
| 117 | |
| 118 | yield* pending.clear; |
| 119 | yield* pending.clear; |
| 120 | |
| 121 | expect(yield* fs.exists(file)).toBe(false); |
| 122 | }).pipe(Effect.provide(layer)); |
| 123 | } |
| 124 | ); |
| 125 | }); |