@devries-cameron20 / composio Mirror · read-only
Fork 0

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

next
ts 135 lines 5.09 KB
Raw
1 /**
2 * The person model, without a container: what makes them approve, and when
3 * they say so.
4 */
5
6 import { describe, expect, it } from 'bun:test';
7 import {
8 CONSUMER_PROJECT,
9 CONSUMER_USER_ID,
10 DEVELOPER_PROJECT,
11 GLOBAL_TEST_USER_ID,
12 World,
13 } from '@e2e-tests/cli-fake-backend';
14 import { PERSON_REPLY, personFor } from './src/person';
15 import { SCRUBBED_BACKEND_URL, SCRUBBED_DASHBOARD_URL } from './src/scrub';
16
17 const start = () => {
18 const world = new World();
19 // Nothing completes by waiting, as in a journey.
20 world.linkCompletesAfterPolls = null;
21 const person = personFor(world, {
22 dashboard: SCRUBBED_DASHBOARD_URL,
23 backend: SCRUBBED_BACKEND_URL,
24 });
25 return { world, person };
26 };
27
28 const loginUrl = (id: string) => `${SCRUBBED_DASHBOARD_URL}?cliKey=${id}`;
29 const authorizationUrl = (id: string) => `${SCRUBBED_BACKEND_URL}/api/v3/s/${id}`;
30
31 describe('what the person approves', () => {
32 it('approves a login whose complete URL the agent wrote', () => {
33 const { world, person } = start();
34 const session = world.createLoginSession();
35 person.hear(`Open ${loginUrl(session.id)} to log in.`, false);
36 expect(session.status).toBe('linked');
37 });
38
39 it('reads a URL through markdown and trailing punctuation', () => {
40 const { world, person } = start();
41 const first = world.createLoginSession();
42 const second = world.createLoginSession();
43 person.hear(`[Log in](${loginUrl(first.id)}), or: **${loginUrl(second.id)}**.`, false);
44 expect([first.status, second.status]).toEqual(['linked', 'linked']);
45 });
46
47 it('does not approve a login from its id alone', () => {
48 const { world, person } = start();
49 const session = world.createLoginSession();
50 person.hear(`Debug session id: ${session.id}`, true);
51 expect(session.status).toBe('pending');
52 });
53
54 it('does not approve a login whose id is followed by more characters', () => {
55 const { world, person } = start();
56 const session = world.createLoginSession();
57 person.hear(`Open ${loginUrl(session.id)}0 to log in.`, true);
58 expect(session.status).toBe('pending');
59 });
60
61 it('does not approve an authorization page on another host', () => {
62 const { world, person } = start();
63 const connection = world.beginConnection({
64 projectLabel: DEVELOPER_PROJECT.label,
65 userId: GLOBAL_TEST_USER_ID,
66 toolkit: 'slack',
67 });
68 person.hear(`Authorize here: https://wrong.invalid/api/v3/s/${connection.id}`, true);
69 expect(connection.status).toBe('INITIATED');
70 });
71
72 it('does not recognise a URL written without its scheme', () => {
73 const { world, person } = start();
74 const session = world.createLoginSession();
75 person.hear(`Open ${loginUrl(session.id).replace('https://', '')}`, true);
76 expect(session.status).toBe('pending');
77 });
78 });
79
80 describe('when the person says they approved', () => {
81 it('answers an agent that stopped to wait, once', () => {
82 const { world, person } = start();
83 const connection = world.beginConnection({
84 projectLabel: CONSUMER_PROJECT.label,
85 userId: CONSUMER_USER_ID,
86 toolkit: 'slack',
87 });
88 const message = `Please authorize Slack: ${authorizationUrl(connection.id)}`;
89 expect(person.hear(message, true)).toBe(PERSON_REPLY);
90 expect(connection.status).toBe('ACTIVE');
91 // The agent thanks them and stops again without checking: nothing new to say.
92 expect(person.hear('Thanks, Slack is connected.', true)).toBeUndefined();
93 });
94
95 it('answers later when the URL came beside a command and the agent stops afterwards', () => {
96 const { world, person } = start();
97 const connection = world.beginConnection({
98 projectLabel: CONSUMER_PROJECT.label,
99 userId: CONSUMER_USER_ID,
100 toolkit: 'slack',
101 });
102 expect(person.hear(`Authorize: ${authorizationUrl(connection.id)}`, false)).toBeUndefined();
103 // A turn of unrelated commands, with nothing written.
104 expect(person.hear('', false)).toBeUndefined();
105 expect(person.hear('Tell me once you have authorized Slack.', true)).toBe(PERSON_REPLY);
106 });
107
108 it('stays quiet once a command of the agent saw the approval', () => {
109 const { world, person } = start();
110 const session = world.createLoginSession();
111 expect(person.hear(`Log in at ${loginUrl(session.id)}`, false)).toBeUndefined();
112 // `composio login --poll` in the same turn reads the linked session.
113 session.reportedLinked = true;
114 expect(person.hear('You are logged in.', true)).toBeUndefined();
115 });
116
117 it('says so after approving a page the CLI opened in their browser', () => {
118 const { world, person } = start();
119 const connection = world.beginConnection({
120 projectLabel: CONSUMER_PROJECT.label,
121 userId: CONSUMER_USER_ID,
122 toolkit: 'slack',
123 });
124 world.authorizeConnection(connection.id);
125 expect(person.hear('I opened the page in your browser. Tell me when done.', true)).toBe(
126 PERSON_REPLY
127 );
128 });
129
130 it('has nothing to say about a connection that existed before the agent started', () => {
131 const { world, person } = start();
132 world.connect(CONSUMER_PROJECT.label, CONSUMER_USER_ID, 'slack');
133 expect(person.hear('All done.', true)).toBeUndefined();
134 });
135 });