The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import {
afterEach,
beforeEach,
describe,
expect,
it,
vi,
type MockInstance,
} from 'vitest';
import { runAgent } from '@/sdk/logic-function/agents/run-agent';
describe('runAgent', () => {
let fetchSpy: MockInstance<typeof fetch>;
beforeEach(() => {
process.env.TWENTY_API_URL = 'https://api.test';
process.env.TWENTY_APP_ACCESS_TOKEN = 'app-token';
fetchSpy = vi.spyOn(globalThis, 'fetch');
});
afterEach(() => {
delete process.env.TWENTY_API_URL;
delete process.env.TWENTY_APP_ACCESS_TOKEN;
fetchSpy.mockRestore();
});
it('POSTs the runAgent mutation to /metadata and returns the result', async () => {
const payload = {
threadId: 'thread-id',
runId: 'run-id',
status: 'COMPLETED',
result: { response: 'done' },
error: null,
success: true,
};
fetchSpy.mockResolvedValue(
new Response(JSON.stringify({ data: { runAgent: payload } }), {
status: 200,
}),
);
const result = await runAgent({
agentUniversalIdentifier: 'agent-uid',
prompt: 'Enrich record 123',
});
expect(result).toEqual(payload);
const [url, requestInit] = fetchSpy.mock.calls[0];
expect(url).toBe('https://api.test/metadata');
expect(requestInit).toEqual(
expect.objectContaining({
method: 'POST',
headers: expect.objectContaining({
Authorization: 'Bearer app-token',
}),
}),
);
const sentBody = JSON.parse(requestInit?.body as string);
expect(sentBody.query).toContain('runAgent(input: $input)');
expect(sentBody.query).toContain('runId');
expect(sentBody.variables).toEqual({
input: {
agentUniversalIdentifier: 'agent-uid',
prompt: 'Enrich record 123',
},
});
});
it('sends a string input as a single user message, with its thread', async () => {
fetchSpy.mockResolvedValue(
new Response(
JSON.stringify({
data: {
runAgent: {
threadId: 'thread-id',
status: 'COMPLETED',
result: { response: 'done' },
error: null,
success: true,
},
},
}),
{ status: 200 },
),
);
const result = await runAgent({
agentUniversalIdentifier: 'agent-uid',
input: 'What was the last touchpoint?',
additionalInstructions: 'Answer in Slack markdown.',
thread: { key: 'C123:1700000000.000100', title: 'Acme renewal' },
});
expect(result.threadId).toBe('thread-id');
const sentBody = JSON.parse(fetchSpy.mock.calls[0][1]?.body as string);
expect(sentBody.variables).toEqual({
input: {
agentUniversalIdentifier: 'agent-uid',
input: [{ role: 'user', content: 'What was the last touchpoint?' }],
additionalInstructions: 'Answer in Slack markdown.',
thread: { key: 'C123:1700000000.000100', title: 'Acme renewal' },
},
});
});
it('POSTs messages when provided instead of prompt', async () => {
const payload = {
threadId: 'thread-id',
status: 'COMPLETED',
result: { response: 'done' },
error: null,
success: true,
};
fetchSpy.mockResolvedValue(
new Response(JSON.stringify({ data: { runAgent: payload } }), {
status: 200,
}),
);
const messages = [
{ role: 'user' as const, content: 'Hello' },
{ role: 'assistant' as const, content: 'Hi' },
{ role: 'user' as const, content: 'Status?' },
];
const result = await runAgent({
agentUniversalIdentifier: 'agent-uid',
messages,
});
expect(result).toEqual(payload);
const sentBody = JSON.parse(fetchSpy.mock.calls[0][1]?.body as string);
expect(sentBody.variables).toEqual({
input: {
agentUniversalIdentifier: 'agent-uid',
messages,
},
});
});
it('surfaces GraphQL errors as a regular Error', async () => {
fetchSpy.mockResolvedValue(
new Response(
JSON.stringify({ errors: [{ message: 'Agent not found' }] }),
{ status: 200 },
),
);
await expect(
runAgent({ agentUniversalIdentifier: 'a', prompt: 'p' }),
).rejects.toThrow(/Agent not found/);
});
it('surfaces non-2xx HTTP responses as a regular Error', async () => {
fetchSpy.mockResolvedValue(
new Response('boom', { status: 500, statusText: 'Server Error' }),
);
await expect(
runAgent({ agentUniversalIdentifier: 'a', prompt: 'p' }),
).rejects.toThrow(/HTTP 500/);
});
it('throws when the runtime env vars are missing', async () => {
delete process.env.TWENTY_API_URL;
await expect(
runAgent({ agentUniversalIdentifier: 'a', prompt: 'p' }),
).rejects.toThrow(
'requires the app runtime env vars TWENTY_API_URL and TWENTY_APP_APPLICATION_ACCESS_TOKEN (or TWENTY_APP_ACCESS_TOKEN)',
);
expect(fetchSpy).not.toHaveBeenCalled();
});
});