The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import { randomUUID } from 'node:crypto';
import { http, HttpResponse, passthrough, type RequestHandler } from 'msw';
import {
type AppConnection,
type RunAgentInput,
type RunAgentResult,
} from 'twenty-sdk/logic-function';
import { isDefined } from 'twenty-sdk/utils';
import { type AppKeyValueScope } from 'src/__tests__/types/app-key-value-scope.type';
import {
type AppRuntimeAgentResult,
type AppRuntimeMock,
} from 'src/__tests__/types/app-runtime-mock.type';
// The app runtime services (`kv`, `listConnections`, `getConnection`,
// `runAgent`) are GraphQL calls the server only answers for an
// application-scoped token, which a test workspace API key is not. They are
// answered here instead, with the same semantics the server implements, while
// every other request to /metadata and /graphql reaches the real server.
const buildKeyValueStoreKey = (key: string, scope: AppKeyValueScope): string =>
scope === 'SERVER' ? `SERVER:${key}` : `WORKSPACE:${key}`;
const readOperationName = (query: string): string | undefined =>
query.match(/(?:query|mutation)\s+(\w+)/)?.[1];
const graphqlError = (message: string) =>
HttpResponse.json({ errors: [{ message }] });
type AppRuntimeMockOptions = {
apiUrl: string;
workspaceId: string;
connections?: AppConnection[];
};
type StoredKeyValueEntry = {
value: unknown;
workspaceId: string | null;
};
const DEFAULT_AGENT_RESULT: AppRuntimeAgentResult = {
success: true,
result: { response: 'Agent answer.' },
error: null,
};
const completeAgentResult = ({
threadId = randomUUID(),
...agentResult
}: AppRuntimeAgentResult): RunAgentResult => ({
threadId,
status: agentResult.success ? 'COMPLETED' : 'FAILED',
...agentResult,
});
export const createAppRuntimeMock = ({
apiUrl,
workspaceId,
connections = [],
}: AppRuntimeMockOptions): AppRuntimeMock => {
let appConnections = connections;
let keyValueStore = new Map<string, StoredKeyValueEntry>();
let agentRuns: RunAgentInput[] = [];
let runAgentImplementation: (
input: RunAgentInput,
) => AppRuntimeAgentResult = () => DEFAULT_AGENT_RESULT;
const getKeyValue = (key: string, scope: AppKeyValueScope): unknown => {
const entry = keyValueStore.get(buildKeyValueStoreKey(key, scope));
return isDefined(entry) ? entry.value : null;
};
// SERVER keys are a cross-workspace claim registry: the stored value is
// always the claiming workspace id, and only that workspace can overwrite
// or delete the key.
const claimServerKey = (key: string) => {
const storeKey = buildKeyValueStoreKey(key, 'SERVER');
const existingEntry = keyValueStore.get(storeKey);
if (!isDefined(existingEntry)) {
keyValueStore.set(storeKey, { value: workspaceId, workspaceId: null });
return { key, value: workspaceId, scope: 'SERVER' };
}
if (existingEntry.value !== workspaceId) {
return null;
}
return { key, value: existingEntry.value, scope: 'SERVER' };
};
const setKeyValue = (
key: string,
value: unknown,
scope: AppKeyValueScope,
) => {
if (scope === 'SERVER') {
return claimServerKey(key);
}
keyValueStore.set(buildKeyValueStoreKey(key, 'WORKSPACE'), {
value,
workspaceId,
});
return { key, value, scope };
};
const deleteKeyValue = (key: string, scope: AppKeyValueScope): boolean => {
const storeKey = buildKeyValueStoreKey(key, scope);
const entry = keyValueStore.get(storeKey);
if (!isDefined(entry)) {
return false;
}
if (scope === 'SERVER' && entry.value !== workspaceId) {
return false;
}
keyValueStore.delete(storeKey);
return true;
};
const handleOperation = (
operationName: string,
variables: Record<string, unknown>,
) => {
switch (operationName) {
case 'ListAppConnections': {
const filter = (variables.filter ?? {}) as { providerName?: string };
return HttpResponse.json({
data: {
appConnections: appConnections.filter(
(connection) =>
filter.providerName === undefined ||
connection.providerName === filter.providerName,
),
},
});
}
case 'GetAppConnection': {
const connection = appConnections.find(
(candidate) => candidate.id === variables.id,
);
if (!isDefined(connection)) {
return graphqlError(`App connection ${variables.id} not found`);
}
return HttpResponse.json({ data: { appConnection: connection } });
}
case 'GetAppKeyValue': {
const key = String(variables.key);
const scope = (variables.scope ?? 'WORKSPACE') as AppKeyValueScope;
const storedValue = getKeyValue(key, scope);
return HttpResponse.json({
data: {
appKeyValue:
storedValue === null ? null : { key, value: storedValue, scope },
},
});
}
case 'SetAppKeyValue': {
const input = variables.input as {
key: string;
value: unknown;
scope?: AppKeyValueScope;
};
const result = setKeyValue(
input.key,
input.value,
input.scope ?? 'WORKSPACE',
);
if (!isDefined(result)) {
return graphqlError(
`Server key "${input.key}" is already claimed by another workspace`,
);
}
return HttpResponse.json({ data: { setAppKeyValue: result } });
}
case 'DeleteAppKeyValue':
return HttpResponse.json({
data: {
deleteAppKeyValue: deleteKeyValue(
String(variables.key),
(variables.scope ?? 'WORKSPACE') as AppKeyValueScope,
),
},
});
case 'RunAgent': {
const input = variables.input as RunAgentInput;
agentRuns.push(input);
return HttpResponse.json({
data: {
runAgent: completeAgentResult(runAgentImplementation(input)),
},
});
}
default:
return passthrough();
}
};
const handlers: RequestHandler[] = [
http.post(`${apiUrl}/metadata`, async ({ request }) => {
const body = (await request.clone().json()) as {
query?: string;
variables?: Record<string, unknown>;
};
const operationName = readOperationName(body.query ?? '');
if (!isDefined(operationName)) {
return passthrough();
}
return handleOperation(operationName, body.variables ?? {});
}),
];
return {
handlers,
workspaceId,
setConnections: (nextConnections: AppConnection[]): void => {
appConnections = nextConnections;
},
setAgentResult: (
implementation:
| AppRuntimeAgentResult
| ((input: RunAgentInput) => AppRuntimeAgentResult),
): void => {
runAgentImplementation =
typeof implementation === 'function'
? implementation
: () => implementation;
},
get agentRuns(): RunAgentInput[] {
return agentRuns;
},
get lastAgentMessages(): NonNullable<RunAgentInput['messages']> {
return agentRuns[agentRuns.length - 1]?.messages ?? [];
},
getKeyValue,
// Test-only back door that writes the store directly, unlike `kv.set`,
// which goes through the claim rules above. Seeding a SERVER key takes the
// claiming workspace id as its value, so a test can hand a team claim to
// another workspace.
seedKeyValue: (
key: string,
value: unknown,
scope: AppKeyValueScope = 'WORKSPACE',
): void => {
keyValueStore.set(buildKeyValueStoreKey(key, scope), {
value,
workspaceId: scope === 'SERVER' ? null : workspaceId,
});
},
reset: (): void => {
appConnections = connections;
keyValueStore = new Map();
agentRuns = [];
runAgentImplementation = () => DEFAULT_AGENT_RESULT;
},
};
};