The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import {
FIELD_ENUM_BINDINGS,
INDEX_ENUM_BINDINGS,
NAVIGATION_MENU_ITEM_ENUM_BINDINGS,
OBJECT_ENUM_BINDINGS,
PAGE_LAYOUT_ENUM_BINDINGS,
PAGE_LAYOUT_TAB_ENUM_BINDINGS,
ROLE_ENUM_BINDINGS,
VIEW_ENUM_BINDINGS,
VIEW_FIELD_ENUM_BINDINGS,
WORKFLOW_ENUM_BINDINGS,
writeDefineFile,
} from '@/app/pull/write-define-file';
import { SystemPermissionFlag } from 'twenty-shared/constants';
import { describe, expect, it } from 'vitest';
describe('writeDefineFile', () => {
it('should write an enum-valued property as a symbol and import it', () => {
const { content: file, requiredSdkExports } = writeDefineFile({
definer: 'defineField',
config: {
universalIdentifier: 'field-uid',
type: 'TEXT',
name: 'title',
},
enumBindings: FIELD_ENUM_BINDINGS,
});
expect(file).toContain(
"import { defineField, FieldType } from 'twenty-sdk/define';",
);
expect(requiredSdkExports).toEqual(['defineField', 'FieldType']);
expect(file).toContain('type: FieldType.TEXT,');
});
it('should look up an enum member whose name differs from its value', () => {
const { content: file } = writeDefineFile({
definer: 'defineField',
config: {
universalIdentifier: 'field-uid',
type: 'NUMBER',
universalSettings: { dataType: 'bigint' },
},
enumBindings: FIELD_ENUM_BINDINGS,
});
expect(file).toContain('dataType: NumberDataType.BIGINT,');
});
it('should leave a string that is not a member of the bound enum as a literal', () => {
const { content: file } = writeDefineFile({
definer: 'defineField',
config: { universalIdentifier: 'field-uid', type: 'NOT_A_FIELD_TYPE' },
enumBindings: FIELD_ENUM_BINDINGS,
});
expect(file).toContain("type: 'NOT_A_FIELD_TYPE',");
expect(file).toContain("import { defineField } from 'twenty-sdk/define';");
});
it('should only bind enum properties at the declared path', () => {
const { content: file } = writeDefineFile({
definer: 'defineIndex',
config: {
universalIdentifier: 'index-uid',
indexType: 'BTREE',
fields: [{ fieldUniversalIdentifier: 'field-uid', indexType: 'BTREE' }],
},
enumBindings: INDEX_ENUM_BINDINGS,
});
expect(file).toContain('indexType: IndexType.BTREE,');
expect(file).toContain("indexType: 'BTREE',");
});
it('should wrap the import statement when it grows past one line', () => {
const { content: file } = writeDefineFile({
definer: 'defineObject',
config: {
openRecordIn: 'USER_CHOICE',
fields: [
{
type: 'RELATION',
universalSettings: {
relationType: 'MANY_TO_ONE',
onDelete: 'SET_NULL',
},
},
],
},
enumBindings: OBJECT_ENUM_BINDINGS,
});
expect(file.startsWith('import {\n defineObject,\n')).toBe(true);
expect(file).toContain("} from 'twenty-sdk/define';");
expect(file).toContain('relationType: RelationType.MANY_TO_ONE,');
expect(file).toContain('onDelete: OnDeleteAction.SET_NULL,');
});
it('should quote a key that is not a valid identifier and escape strings', () => {
const { content: file } = writeDefineFile({
definer: 'defineApplication',
config: {
displayName: "Tim's app",
translations: { 'fr-FR': { greeting: 'Bonjour' } },
nothing: null,
empty: [],
},
});
expect(file).toContain("displayName: 'Tim\\'s app',");
expect(file).toContain("'fr-FR': {");
expect(file).toContain('nothing: null,');
expect(file).toContain('empty: [],');
});
it('should end with a single default export of the definer call', () => {
const { content: file } = writeDefineFile({
definer: 'defineObject',
config: { nameSingular: 'pet' },
});
expect(file).toBe(
"import { defineObject } from 'twenty-sdk/define';\n" +
'\n' +
'export default defineObject({\n' +
" nameSingular: 'pet',\n" +
'});\n',
);
});
it('should write every bound view property as its enum symbol and import each symbol on its own line', () => {
const { content: file } = writeDefineFile({
definer: 'defineView',
config: {
universalIdentifier: 'view-uid',
name: 'Companies by owner',
objectUniversalIdentifier: 'object-uid',
type: 'TABLE_WIDGET',
visibility: 'WORKSPACE',
openRecordIn: 'SIDE_PANEL',
calendarLayout: 'MONTH',
kanbanAggregateOperation: 'COUNT',
fields: [
{
universalIdentifier: 'view-field-uid',
fieldMetadataUniversalIdentifier: 'field-uid',
position: 0,
aggregateOperation: 'COUNT_UNIQUE_VALUES',
},
],
filters: [
{
universalIdentifier: 'view-filter-uid',
fieldMetadataUniversalIdentifier: 'field-uid',
operand: 'CONTAINS',
value: 'acme',
},
],
filterGroups: [
{
universalIdentifier: 'view-filter-group-uid',
logicalOperator: 'NOT',
},
],
sorts: [
{
universalIdentifier: 'view-sort-uid',
fieldMetadataUniversalIdentifier: 'field-uid',
direction: 'DESC',
},
],
},
enumBindings: VIEW_ENUM_BINDINGS,
});
expect(
file.startsWith(
'import {\n' +
' defineView,\n' +
' AggregateOperations,\n' +
' ViewCalendarLayout,\n' +
' ViewFilterGroupLogicalOperator,\n' +
' ViewFilterOperand,\n' +
' ViewOpenRecordIn,\n' +
' ViewSortDirection,\n' +
' ViewType,\n' +
' ViewVisibility,\n' +
"} from 'twenty-sdk/define';\n",
),
).toBe(true);
expect(file).toContain('type: ViewType.TABLE_WIDGET,');
expect(file).toContain('visibility: ViewVisibility.WORKSPACE,');
expect(file).toContain('openRecordIn: ViewOpenRecordIn.SIDE_PANEL,');
expect(file).toContain('calendarLayout: ViewCalendarLayout.MONTH,');
expect(file).toContain(
'kanbanAggregateOperation: AggregateOperations.COUNT,',
);
expect(file).toContain(
'aggregateOperation: AggregateOperations.COUNT_UNIQUE_VALUES,',
);
expect(file).toContain('operand: ViewFilterOperand.CONTAINS,');
expect(file).toContain(
'logicalOperator: ViewFilterGroupLogicalOperator.NOT,',
);
expect(file).toContain('direction: ViewSortDirection.DESC,');
});
it('should leave a filter operand that is not a member of ViewFilterOperand as a literal', () => {
const { content: file } = writeDefineFile({
definer: 'defineView',
config: {
universalIdentifier: 'view-uid',
filters: [
{
universalIdentifier: 'view-filter-uid',
fieldMetadataUniversalIdentifier: 'field-uid',
operand: 'contains',
value: 'acme',
},
],
},
enumBindings: VIEW_ENUM_BINDINGS,
});
expect(file).toContain("operand: 'contains',");
expect(file).toContain("import { defineView } from 'twenty-sdk/define';");
});
it('should not bind an operand that sits on the view itself rather than inside its filters', () => {
const { content: file } = writeDefineFile({
definer: 'defineView',
config: {
universalIdentifier: 'view-uid',
operand: 'CONTAINS',
},
enumBindings: VIEW_ENUM_BINDINGS,
});
expect(file).toContain("operand: 'CONTAINS',");
expect(file).toContain("import { defineView } from 'twenty-sdk/define';");
});
it('should write a standalone view field aggregate operation as an AggregateOperations member', () => {
const { content: file } = writeDefineFile({
definer: 'defineViewField',
config: {
universalIdentifier: 'view-field-uid',
viewUniversalIdentifier: 'view-uid',
fieldMetadataUniversalIdentifier: 'field-uid',
position: 0,
isVisible: true,
aggregateOperation: 'MIN',
},
enumBindings: VIEW_FIELD_ENUM_BINDINGS,
});
expect(file).toContain('aggregateOperation: AggregateOperations.MIN,');
expect(file).toContain(
"import { defineViewField, AggregateOperations } from 'twenty-sdk/define';",
);
});
it('should write every bound page layout property as its enum symbol and import each symbol on its own line', () => {
const { content: file } = writeDefineFile({
definer: 'definePageLayout',
config: {
universalIdentifier: 'page-layout-uid',
name: 'Pet dashboard',
type: 'DASHBOARD',
tabs: [
{
universalIdentifier: 'tab-uid',
title: 'Charts',
position: 0,
layoutMode: 'GRID',
widgets: [
{
universalIdentifier: 'chart-widget-uid',
title: 'Age by status',
type: 'GRAPH',
objectUniversalIdentifier: 'object-uid',
position: {
layoutMode: 'GRID',
row: 0,
column: 0,
rowSpan: 4,
columnSpan: 6,
},
configuration: {
configurationType: 'BAR_CHART',
aggregateFieldMetadataUniversalIdentifier: 'age-field-uid',
aggregateOperation: 'AVG',
primaryAxisGroupByFieldMetadataUniversalIdentifier:
'status-field-uid',
primaryAxisDateGranularity: 'MONTH',
secondaryAxisGroupByFieldMetadataUniversalIdentifier: null,
groupMode: 'STACKED',
},
},
{
universalIdentifier: 'fields-widget-uid',
title: 'Fields',
type: 'FIELDS',
position: { layoutMode: 'VERTICAL_LIST', index: 1 },
configuration: {
configurationType: 'FIELDS',
viewUniversalIdentifier: null,
fieldDisplayMode: 'CARD',
},
},
],
},
],
},
enumBindings: PAGE_LAYOUT_ENUM_BINDINGS,
});
expect(
file.startsWith(
'import {\n' +
' definePageLayout,\n' +
' AggregateOperations,\n' +
' ObjectRecordGroupByDateGranularity,\n' +
' PageLayoutTabLayoutMode,\n' +
' PageLayoutType,\n' +
' WidgetType,\n' +
"} from 'twenty-sdk/define';\n",
),
).toBe(true);
expect(file).toContain('type: PageLayoutType.DASHBOARD,');
expect(file).toContain('layoutMode: PageLayoutTabLayoutMode.GRID,');
expect(file).toContain('type: WidgetType.GRAPH,');
expect(file).toContain('type: WidgetType.FIELDS,');
expect(file).toContain(
'layoutMode: PageLayoutTabLayoutMode.VERTICAL_LIST,',
);
expect(file).toContain('aggregateOperation: AggregateOperations.AVG,');
expect(file).toContain(
'primaryAxisDateGranularity: ObjectRecordGroupByDateGranularity.MONTH,',
);
expect(file).toContain("configurationType: 'BAR_CHART',");
expect(file).toContain("groupMode: 'STACKED',");
expect(file).toContain("fieldDisplayMode: 'CARD',");
expect(file).toContain(
'secondaryAxisGroupByFieldMetadataUniversalIdentifier: null,',
);
expect(file).toContain('viewUniversalIdentifier: null,');
});
it('should not bind a layout mode that sits on the page layout itself rather than on its tabs', () => {
const { content: file } = writeDefineFile({
definer: 'definePageLayout',
config: { universalIdentifier: 'page-layout-uid', layoutMode: 'GRID' },
enumBindings: PAGE_LAYOUT_ENUM_BINDINGS,
});
expect(file).toContain("layoutMode: 'GRID',");
expect(file).toContain(
"import { definePageLayout } from 'twenty-sdk/define';",
);
});
it('should write a standalone page layout tab with its widgets under the tab enum bindings', () => {
const { content: file } = writeDefineFile({
definer: 'definePageLayoutTab',
config: {
universalIdentifier: 'tab-uid',
pageLayoutUniversalIdentifier: 'page-layout-uid',
title: 'Extra',
position: 60,
layoutMode: 'VERTICAL_LIST',
widgets: [
{
universalIdentifier: 'widget-uid',
title: 'Docs',
type: 'IFRAME',
position: { layoutMode: 'VERTICAL_LIST', index: 0 },
configuration: {
configurationType: 'IFRAME',
url: 'https://example.com/docs',
},
},
],
},
enumBindings: PAGE_LAYOUT_TAB_ENUM_BINDINGS,
});
expect(file).toBe(
'import {\n' +
' definePageLayoutTab,\n' +
' PageLayoutTabLayoutMode,\n' +
' WidgetType,\n' +
"} from 'twenty-sdk/define';\n" +
'\n' +
'export default definePageLayoutTab({\n' +
" universalIdentifier: 'tab-uid',\n" +
" pageLayoutUniversalIdentifier: 'page-layout-uid',\n" +
" title: 'Extra',\n" +
' position: 60,\n' +
' layoutMode: PageLayoutTabLayoutMode.VERTICAL_LIST,\n' +
' widgets: [\n' +
' {\n' +
" universalIdentifier: 'widget-uid',\n" +
" title: 'Docs',\n" +
' type: WidgetType.IFRAME,\n' +
' position: {\n' +
' layoutMode: PageLayoutTabLayoutMode.VERTICAL_LIST,\n' +
' index: 0,\n' +
' },\n' +
' configuration: {\n' +
" configurationType: 'IFRAME',\n" +
" url: 'https://example.com/docs',\n" +
' },\n' +
' },\n' +
' ],\n' +
'});\n',
);
});
it('should write a nameless object navigation menu item with its type as a NavigationMenuItemType member', () => {
const { content: file } = writeDefineFile({
definer: 'defineNavigationMenuItem',
config: {
universalIdentifier: 'ad199fde-d4f4-4634-a94c-95b52fa32567',
type: 'OBJECT',
position: 8,
targetObjectUniversalIdentifier: '5c5822f2-9efa-4cb7-8101-98f5c25765b9',
},
enumBindings: NAVIGATION_MENU_ITEM_ENUM_BINDINGS,
});
expect(file).toBe(
'import {\n' +
' defineNavigationMenuItem,\n' +
' NavigationMenuItemType,\n' +
"} from 'twenty-sdk/define';\n" +
'\n' +
'export default defineNavigationMenuItem({\n' +
" universalIdentifier: 'ad199fde-d4f4-4634-a94c-95b52fa32567',\n" +
' type: NavigationMenuItemType.OBJECT,\n' +
' position: 8,\n' +
" targetObjectUniversalIdentifier: '5c5822f2-9efa-4cb7-8101-98f5c25765b9',\n" +
'});\n',
);
});
it('should write a folder navigation menu item with its name and icon', () => {
const { content: file } = writeDefineFile({
definer: 'defineNavigationMenuItem',
config: {
universalIdentifier: 'navigation-menu-item-uid',
type: 'FOLDER',
position: 2,
name: 'Operations',
icon: 'IconFolder',
},
enumBindings: NAVIGATION_MENU_ITEM_ENUM_BINDINGS,
});
expect(file).toBe(
'import {\n' +
' defineNavigationMenuItem,\n' +
' NavigationMenuItemType,\n' +
"} from 'twenty-sdk/define';\n" +
'\n' +
'export default defineNavigationMenuItem({\n' +
" universalIdentifier: 'navigation-menu-item-uid',\n" +
' type: NavigationMenuItemType.FOLDER,\n' +
' position: 2,\n' +
" name: 'Operations',\n" +
" icon: 'IconFolder',\n" +
'});\n',
);
});
it('should write the row-level predicates, predicate groups and system permission flags of a role as enum members', () => {
const { content: file } = writeDefineFile({
definer: 'defineRole',
config: {
universalIdentifier: 'role-uid',
label: 'Auditor',
rowLevelPermissionPredicateGroups: [
{
universalIdentifier: 'predicate-group-uid',
objectUniversalIdentifier: 'object-uid',
logicalOperator: 'OR',
},
],
rowLevelPermissionPredicates: [
{
universalIdentifier: 'predicate-uid',
objectUniversalIdentifier: 'object-uid',
fieldUniversalIdentifier: 'field-uid',
operand: 'CONTAINS',
value: 'Rex',
predicateGroupUniversalIdentifier: 'predicate-group-uid',
},
],
permissionFlagUniversalIdentifiers: [
SystemPermissionFlag.WORKSPACE,
'application-permission-flag-uid',
],
},
enumBindings: ROLE_ENUM_BINDINGS,
});
expect(
file.startsWith(
'import {\n' +
' defineRole,\n' +
' RowLevelPermissionPredicateGroupLogicalOperator,\n' +
' RowLevelPermissionPredicateOperand,\n' +
' SystemPermissionFlag,\n' +
"} from 'twenty-sdk/define';\n",
),
).toBe(true);
expect(file).toContain(
'logicalOperator: RowLevelPermissionPredicateGroupLogicalOperator.OR,',
);
expect(file).toContain(
'operand: RowLevelPermissionPredicateOperand.CONTAINS,',
);
expect(file).toContain('SystemPermissionFlag.WORKSPACE');
expect(file).toContain("'application-permission-flag-uid'");
});
it('should write a permission flag with its permission type as a literal', () => {
const { content: file } = writeDefineFile({
definer: 'definePermissionFlag',
config: {
universalIdentifier: 'permission-flag-uid',
key: 'EXPORT_PETS',
label: 'Export pets',
permissionType: 'settings',
},
});
expect(file).toBe(
"import { definePermissionFlag } from 'twenty-sdk/define';\n" +
'\n' +
'export default definePermissionFlag({\n' +
" universalIdentifier: 'permission-flag-uid',\n" +
" key: 'EXPORT_PETS',\n" +
" label: 'Export pets',\n" +
" permissionType: 'settings',\n" +
'});\n',
);
});
it('should write workflow filter operands and form field types as enum members, keeping logical operators and record pickers literal', () => {
const stepFilters = {
stepFilterGroups: [{ id: 'group', logicalOperator: 'AND' }],
stepFilters: [
{
id: 'filter',
type: 'TEXT',
stepOutputKey: '{{trigger.properties.after.status}}',
operand: 'IS',
value: 'open',
stepFilterGroupId: 'group',
},
],
};
const { content: file, requiredSdkExports } = writeDefineFile({
definer: 'defineWorkflow',
config: {
universalIdentifier: 'workflow-uid',
name: 'Triage',
version: {
trigger: {
universalIdentifier: 'trigger-uid',
type: 'DATABASE_EVENT',
nextStepIds: ['find-uid'],
settings: {
action: 'updated',
filter: stepFilters,
objectUniversalIdentifier: 'object-uid',
},
},
steps: [
{
universalIdentifier: 'find-uid',
name: 'Find',
type: 'FIND_RECORDS',
nextStepIds: ['filter-uid'],
input: {
filter: {
recordFilterGroups: [{ id: 'root', logicalOperator: 'OR' }],
recordFilters: [
{
fieldMetadataUniversalIdentifier: 'field-uid',
operand: 'IS_NOT_EMPTY',
},
],
},
objectUniversalIdentifier: 'object-uid',
},
},
{
universalIdentifier: 'filter-uid',
name: 'Filter',
type: 'FILTER',
nextStepIds: ['form-uid'],
input: stepFilters,
},
{
universalIdentifier: 'form-uid',
name: 'Form',
type: 'FORM',
nextStepIds: [],
input: [
{ id: 'name', label: 'Name', name: 'name', type: 'TEXT' },
{
id: 'ticket',
label: 'Ticket',
name: 'ticket',
settings: { objectUniversalIdentifier: 'object-uid' },
type: 'RECORD',
},
],
},
],
},
},
enumBindings: WORKFLOW_ENUM_BINDINGS,
});
expect(requiredSdkExports).toEqual([
'defineWorkflow',
'FieldType',
'ViewFilterOperand',
]);
expect(file.match(/operand: ViewFilterOperand\.IS,/g)).toHaveLength(2);
expect(file).toContain('operand: ViewFilterOperand.IS_NOT_EMPTY,');
expect(file).toContain("logicalOperator: 'AND',");
expect(file).toContain("logicalOperator: 'OR',");
expect(file).toContain('type: FieldType.TEXT,');
expect(file).toContain("type: 'RECORD',");
expect(file).toContain("type: 'FIND_RECORDS',");
expect(file).toContain("type: 'DATABASE_EVENT',");
});
});