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 | /** |
| 2 | * FFI-backend roundtrip test — exercises the real `bun:ffi` |
| 3 | * `SecItem*` path against the live macOS keychain. |
| 4 | * |
| 5 | * Gated twice: the test only runs when |
| 6 | * |
| 7 | * - we are in Bun (`globalThis.Bun !== undefined`), AND |
| 8 | * - `COMPOSIO_KEYRING_E2E=1` is set in the env. |
| 9 | * |
| 10 | * The first gate keeps the file inert when vitest runs under Node |
| 11 | * (where importing `bun:ffi` would crash at module load). The second |
| 12 | * keeps CI and the default `pnpm test` run from touching the user's |
| 13 | * real keychain. |
| 14 | * |
| 15 | * Run locally with: |
| 16 | * |
| 17 | * COMPOSIO_KEYRING_E2E=1 bun --bun x vitest run test/ffi.test.ts |
| 18 | */ |
| 19 | |
| 20 | import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 21 | import { randomUUID } from 'node:crypto'; |
| 22 | import { Entry } from '../src/core/entry'; |
| 23 | import { KeyringError } from '../src/core/errors'; |
| 24 | import { setDefaultStore, unsetDefaultStore } from '../src/core/store'; |
| 25 | import type { CredentialStore } from '../src/core/store'; |
| 26 | |
| 27 | const underBun = typeof (globalThis as { Bun?: unknown }).Bun !== 'undefined'; |
| 28 | const enabled = underBun && process.env.COMPOSIO_KEYRING_E2E === '1'; |
| 29 | const suite = enabled ? describe : describe.skip; |
| 30 | |
| 31 | suite('ffi: MacOSSecurityFFIStore roundtrip', () => { |
| 32 | let store: CredentialStore; |
| 33 | |
| 34 | beforeAll(async () => { |
| 35 | // Import dynamically — never touch this module from Node. |
| 36 | const mod = await import('../src/stores/macos-security-ffi'); |
| 37 | store = new mod.MacOSSecurityFFIStore(); |
| 38 | setDefaultStore(store); |
| 39 | }); |
| 40 | |
| 41 | afterAll(() => { |
| 42 | unsetDefaultStore(); |
| 43 | }); |
| 44 | |
| 45 | it('reports the expected id and vendor', () => { |
| 46 | expect(store.id).toBe('macos-security-ffi'); |
| 47 | expect(store.vendor).toContain('bun:ffi'); |
| 48 | }); |
| 49 | |
| 50 | it('roundtrips a password', async () => { |
| 51 | const entry = new Entry(`com.composio.cli.ffi.${randomUUID()}`, 'default'); |
| 52 | try { |
| 53 | const password = `ffi-${randomUUID()}`; |
| 54 | await entry.setPassword(password); |
| 55 | expect(await entry.getPassword()).toBe(password); |
| 56 | } finally { |
| 57 | try { |
| 58 | await entry.deleteCredential(); |
| 59 | } catch { |
| 60 | /* best-effort cleanup */ |
| 61 | } |
| 62 | } |
| 63 | }); |
| 64 | |
| 65 | it('overwrites on repeated setPassword without duplicate error', async () => { |
| 66 | const entry = new Entry(`com.composio.cli.ffi.${randomUUID()}`, 'default'); |
| 67 | try { |
| 68 | await entry.setPassword('first'); |
| 69 | await entry.setPassword('second'); |
| 70 | await entry.setPassword('third'); |
| 71 | expect(await entry.getPassword()).toBe('third'); |
| 72 | } finally { |
| 73 | try { |
| 74 | await entry.deleteCredential(); |
| 75 | } catch { |
| 76 | /* best-effort cleanup */ |
| 77 | } |
| 78 | } |
| 79 | }); |
| 80 | |
| 81 | it('throws NoEntry for a missing credential', async () => { |
| 82 | const entry = new Entry(`com.composio.cli.ffi.nonexistent.${randomUUID()}`, 'default'); |
| 83 | await expect(entry.getPassword()).rejects.toSatisfy( |
| 84 | (err: unknown) => err instanceof KeyringError && err.kind === 'NoEntry' |
| 85 | ); |
| 86 | }); |
| 87 | |
| 88 | it('roundtrips arbitrary binary bytes', async () => { |
| 89 | const entry = new Entry(`com.composio.cli.ffi.bin.${randomUUID()}`, 'default'); |
| 90 | try { |
| 91 | const bytes = new Uint8Array(256); |
| 92 | for (let i = 0; i < 256; i++) bytes[i] = i; |
| 93 | await entry.setSecret(bytes); |
| 94 | const got = await entry.getSecret(); |
| 95 | expect(Array.from(got)).toEqual(Array.from(bytes)); |
| 96 | } finally { |
| 97 | try { |
| 98 | await entry.deleteCredential(); |
| 99 | } catch { |
| 100 | /* best-effort cleanup */ |
| 101 | } |
| 102 | } |
| 103 | }); |
| 104 | |
| 105 | it('delete then read throws NoEntry', async () => { |
| 106 | const entry = new Entry(`com.composio.cli.ffi.del.${randomUUID()}`, 'default'); |
| 107 | await entry.setPassword('x'); |
| 108 | await entry.deleteCredential(); |
| 109 | await expect(entry.getPassword()).rejects.toSatisfy( |
| 110 | (err: unknown) => err instanceof KeyringError && err.kind === 'NoEntry' |
| 111 | ); |
| 112 | }); |
| 113 | }); |