The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
`[SDK]` Execute logic function e2e test (#18351)
Paul Rastoin132a19f6884a
diff --git a/packages/twenty-sdk/src/cli/__tests__/apps/function-execute-app/__e2e__/function-execute.e2e-spec.ts b/packages/twenty-sdk/src/cli/__tests__/apps/function-execute-app/__e2e__/function-execute.e2e-spec.tsnew file mode 100644index 00000000000..29481a13c44--- /dev/null+++ b/packages/twenty-sdk/src/cli/__tests__/apps/function-execute-app/__e2e__/function-execute.e2e-spec.ts@@ -0,0 +1,79 @@+import { resolve } from 'path';+import { vi } from 'vitest';++import { appBuild } from '@/cli/public-operations/app-build';+import { appUninstall } from '@/cli/public-operations/app-uninstall';+import { functionExecute } from '@/cli/public-operations/function-execute';+import { ADD_NUMBERS_UNIVERSAL_IDENTIFIER } from '../src/logic-functions/add-numbers.function';++const APP_PATH = resolve(__dirname, '../');++describe('functionExecute E2E', () => {+ beforeAll(async () => {+ const buildResult = await appBuild({ appPath: APP_PATH });++ if (!buildResult.success) {+ throw new Error(+ `appBuild failed: ${buildResult.error.code} – ${buildResult.error.message}`,+ );+ }++ // Although appBuild uploads files before syncing the manifest, the server+ // may need a moment to make them readable by the execution engine.+ // Retry a dummy execution until the handler file becomes available.+ await vi.waitFor(+ async () => {+ const result = await functionExecute({+ appPath: APP_PATH,+ functionName: 'add-numbers',+ payload: { a: 0, b: 0 },+ });++ expect(result.success).toBe(true);+ },+ { timeout: 10_000, interval: 1_000 },+ );+ }, 60_000);++ afterAll(async () => {+ await appUninstall({ appPath: APP_PATH });+ }, 30_000);++ it('should execute a function by name and return the computed result', async () => {+ const result = await functionExecute({+ appPath: APP_PATH,+ functionName: 'add-numbers',+ payload: { a: 3, b: 7 },+ });++ expect(result).toMatchObject({+ success: true,+ data: { functionName: 'add-numbers', status: 'SUCCESS', data: 10 },+ });+ });++ it('should execute a function by universalIdentifier', async () => {+ const result = await functionExecute({+ appPath: APP_PATH,+ functionUniversalIdentifier: ADD_NUMBERS_UNIVERSAL_IDENTIFIER,+ payload: { a: 100, b: 200 },+ });++ expect(result).toMatchObject({+ success: true,+ data: { functionName: 'add-numbers', status: 'SUCCESS', data: 300 },+ });+ });++ it('should return FUNCTION_NOT_FOUND for a non-existent function name', async () => {+ const result = await functionExecute({+ appPath: APP_PATH,+ functionName: 'non-existent-function',+ });++ expect(result).toMatchObject({+ success: false,+ error: { code: 'FUNCTION_NOT_FOUND' },+ });+ });+});diff --git a/packages/twenty-sdk/src/cli/__tests__/apps/function-execute-app/application.config.ts b/packages/twenty-sdk/src/cli/__tests__/apps/function-execute-app/application.config.tsnew file mode 100644index 00000000000..5ec44ef59d4--- /dev/null+++ b/packages/twenty-sdk/src/cli/__tests__/apps/function-execute-app/application.config.ts@@ -0,0 +1,11 @@+import { defineApplication } from '@/sdk';++import { DEFAULT_ROLE_UNIVERSAL_IDENTIFIER } from './my.role';++export default defineApplication({+ universalIdentifier: '01e065d6-1b48-4107-86d8-b06f87420a4f',+ displayName: 'Function Execute Test App',+ description: 'A minimal app for testing function:execute',+ icon: 'IconFunction',+ defaultRoleUniversalIdentifier: DEFAULT_ROLE_UNIVERSAL_IDENTIFIER,+});diff --git a/packages/twenty-sdk/src/cli/__tests__/apps/function-execute-app/my.role.ts b/packages/twenty-sdk/src/cli/__tests__/apps/function-execute-app/my.role.tsnew file mode 100644index 00000000000..cf02d00129c--- /dev/null+++ b/packages/twenty-sdk/src/cli/__tests__/apps/function-execute-app/my.role.ts@@ -0,0 +1,18 @@+import { defineRole } from '@/sdk';++export const DEFAULT_ROLE_UNIVERSAL_IDENTIFIER =+ '9f992b6c-17e9-4381-bab5-b6150b574304';++export default defineRole({+ universalIdentifier: DEFAULT_ROLE_UNIVERSAL_IDENTIFIER,+ label: 'Default role',+ description: 'Default role for function execute test app',+ canReadAllObjectRecords: true,+ canUpdateAllObjectRecords: false,+ canSoftDeleteAllObjectRecords: false,+ canDestroyAllObjectRecords: false,+ canUpdateAllSettings: false,+ canBeAssignedToAgents: false,+ canBeAssignedToUsers: true,+ canBeAssignedToApiKeys: false,+});diff --git a/packages/twenty-sdk/src/cli/__tests__/apps/function-execute-app/package.json b/packages/twenty-sdk/src/cli/__tests__/apps/function-execute-app/package.jsonnew file mode 100644index 00000000000..47c10ecbaec--- /dev/null+++ b/packages/twenty-sdk/src/cli/__tests__/apps/function-execute-app/package.json@@ -0,0 +1,27 @@+{+ "name": "function-execute-app",+ "version": "0.1.0",+ "license": "MIT",+ "engines": {+ "node": "^24.5.0",+ "npm": "please-use-yarn",+ "yarn": ">=4.0.2"+ },+ "packageManager": "[email protected]",+ "scripts": {+ "app:dev": "twenty app:dev",+ "function:execute": "twenty function:execute",+ "app:uninstall": "twenty app:uninstall"+ },+ "dependencies": {+ "twenty-sdk": "latest"+ },+ "devDependencies": {+ "typescript": "^5.9.3",+ "@types/node": "^24.7.2",+ "@types/react": "^19.0.2",+ "react": "^19.0.2",+ "eslint": "^9.32.0",+ "typescript-eslint": "^8.50.0"+ }+}diff --git a/packages/twenty-sdk/src/cli/__tests__/apps/function-execute-app/src/logic-functions/add-numbers.function.ts b/packages/twenty-sdk/src/cli/__tests__/apps/function-execute-app/src/logic-functions/add-numbers.function.tsnew file mode 100644index 00000000000..e3ee6b30220--- /dev/null+++ b/packages/twenty-sdk/src/cli/__tests__/apps/function-execute-app/src/logic-functions/add-numbers.function.ts@@ -0,0 +1,24 @@+import { defineLogicFunction } from '@/sdk';++export const ADD_NUMBERS_UNIVERSAL_IDENTIFIER =+ 'f9e5589c-e951-4d99-85db-0a305ab53502';++const addNumbersHandler = (payload: { a: number; b: number }) => {+ const result = payload.a + payload.b;++ console.log(`Adding ${payload.a} + ${payload.b} = ${result}`);++ return result;+};++export default defineLogicFunction({+ universalIdentifier: ADD_NUMBERS_UNIVERSAL_IDENTIFIER,+ name: 'add-numbers',+ timeoutSeconds: 5,+ handler: addNumbersHandler,+ httpRouteTriggerSettings: {+ path: '/add-numbers',+ httpMethod: 'GET',+ isAuthRequired: false,+ },+});diff --git a/packages/twenty-sdk/src/cli/__tests__/apps/function-execute-app/tsconfig.json b/packages/twenty-sdk/src/cli/__tests__/apps/function-execute-app/tsconfig.jsonnew file mode 100644index 00000000000..b6520539e57--- /dev/null+++ b/packages/twenty-sdk/src/cli/__tests__/apps/function-execute-app/tsconfig.json@@ -0,0 +1,16 @@+{+ "compilerOptions": {+ "target": "ES2020",+ "module": "ESNext",+ "moduleResolution": "bundler",+ "strict": true,+ "esModuleInterop": true,+ "skipLibCheck": true,+ "jsx": "react-jsx",+ "baseUrl": ".",+ "paths": {+ "@/*": ["../../../../../src/*"]+ }+ },+ "include": ["**/*"]+}diff --git a/packages/twenty-sdk/src/cli/__tests__/constants/testConfig.ts b/packages/twenty-sdk/src/cli/__tests__/constants/testConfig.tsindex a6216bf6fb9..4f4b9786ac9 100644--- a/packages/twenty-sdk/src/cli/__tests__/constants/testConfig.ts+++ b/packages/twenty-sdk/src/cli/__tests__/constants/testConfig.ts@@ -1,7 +1,7 @@import { type TwentyConfig } from '@/cli/utilities/config/config-service';export const testConfig: TwentyConfig = {- apiUrl: 'http://apple.localhost:3000',+ apiUrl: 'http://localhost:3000',apiKey:'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyMDIwMjAyMC1lNmI1LTQ2ODAtOGEzMi1iODIwOTczNzE1NmIiLCJ1c2VySWQiOiIyMDIwMjAyMC1lNmI1LTQ2ODAtOGEzMi1iODIwOTczNzE1NmIiLCJ3b3Jrc3BhY2VJZCI6IjIwMjAyMDIwLTFjMjUtNGQwMi1iZjI1LTZhZWNjZjdlYTQxOSIsIndvcmtzcGFjZU1lbWJlcklkIjoiMjAyMDIwMjAtNDYzZi00MzViLTgyOGMtMTA3ZTAwN2EyNzExIiwidXNlcldvcmtzcGFjZUlkIjoiMjAyMDIwMjAtMWU3Yy00M2Q5LWE1ZGItNjg1YjUwNjlkODE2IiwidHlwZSI6IkFDQ0VTUyIsImF1dGhQcm92aWRlciI6InBhc3N3b3JkIiwiaWF0IjoxNzUxMjgxNzA0LCJleHAiOjIwNjY4NTc3MDR9.HMGqCsVlOAPVUBhKSGlD1X86VoHKt4LIUtET3CGIdik',};