The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import {
cp,
mkdir,
mkdtemp,
readFile,
readdir,
rm,
symlink,
writeFile,
} from 'node:fs/promises';
import { createRequire } from 'node:module';
import { tmpdir } from 'node:os';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { stop } from 'esbuild';
import { EMPTY_YARN_LOCK_CONTENT } from 'twenty-shared/application';
import { isDefined } from 'twenty-shared/utils';
import {
afterAll,
afterEach,
beforeAll,
describe,
expect,
it,
vi,
} from 'vitest';
import { buildTestAppWorker } from '@/app/__tests__/utils/build-test-app-worker';
import { buildAndValidateManifest } from '@/app/manifest/build-and-validate-manifest';
import { buildManifest } from '@/app/manifest/manifest-build';
import { buildPullEntities } from '@/app/pull/build-pull-entities';
import { writeDefineFile } from '@/app/pull/write-define-file';
import { createAppProject } from '@/app/create-app-project';
import { runAppWorker } from '@/app/run-app-worker';
import { compileApplicationTranslations } from '@/app/translations/compile-application-translations';
const launch = vi.hoisted(() => ({ modulePath: '', execArgv: [] as string[] }));
vi.mock('@/app/get-app-worker-launch', () => ({
getAppWorkerLaunch: () => launch,
}));
vi.mock('@/app/get-app-template-directory', () => ({
getAppTemplateDirectory: () =>
join(REPOSITORY_ROOT, 'packages/twenty-cli/app-template'),
}));
const REPOSITORY_ROOT = fileURLToPath(
new URL('../../../../../../', import.meta.url),
);
const FIXTURES_DIRECTORY = join(
REPOSITORY_ROOT,
'packages/twenty-apps/fixtures',
);
const FIXTURES = [
'minimal-app',
'rich-app',
'function-execute-app',
'shared-dependencies-app',
'invalid-app',
];
const json = (value: unknown): unknown => JSON.parse(JSON.stringify(value));
describe('CLI manifest fixtures and worker transport', () => {
let root: string;
const copyFixture = async (name: string) => {
const appPath = await mkdtemp(join(root, `${name}-`));
await cp(join(FIXTURES_DIRECTORY, name), appPath, {
recursive: true,
filter: (source) =>
!source
.split('/')
.some((part) => ['node_modules', '.twenty', 'dist'].includes(part)),
});
await symlink(join(root, 'node_modules'), join(appPath, 'node_modules'));
return appPath;
};
const compareManifest = async (appPath: string) => {
const expected = await buildAndValidateManifest(appPath).catch(
(error: unknown) => ({
thrown: error instanceof Error ? error.message : String(error),
}),
);
if ('thrown' in expected) {
await expect(buildAndValidateManifest(appPath)).rejects.toThrow(
expected.thrown,
);
const worker = await runAppWorker({
request: { type: 'buildManifest', appPath },
signal: new AbortController().signal,
});
expect(worker.result).toMatchObject({
success: false,
error: { code: 'MANIFEST_BUILD_FAILED', message: expected.thrown },
});
return { success: false as const, errors: [expected.thrown] };
}
const warnings: string[] = [];
const translations = expected.success
? await compileApplicationTranslations({
appPath,
onWarning: (message) => warnings.push(message),
})
: undefined;
const worker = await runAppWorker({
request: { type: 'buildManifest', appPath },
signal: new AbortController().signal,
});
expect(worker.isSnapshotHeld).toBe(false);
expect(worker.result).toEqual(
json(
expected.success
? {
success: true,
data: {
manifest: { ...expected.manifest, translations },
filePaths: expected.filePaths,
},
diagnostics: [...expected.warnings, ...warnings].map(
(message) => ({
severity: 'warning',
code: 'BUILD_WARNING',
message,
}),
),
}
: {
success: false,
error: {
code: 'MANIFEST_BUILD_FAILED',
message: expected.errors.join('\n'),
},
diagnostics: expected.errors.map((message) => ({
severity: 'error',
code: 'MANIFEST_BUILD_FAILED',
message,
})),
},
),
);
return expected;
};
beforeAll(async () => {
root = await mkdtemp(join(tmpdir(), 'twenty-manifest-parity-'));
const modules = join(root, 'node_modules');
const sdkPath = join(modules, 'twenty-sdk');
const sourceSdk = join(REPOSITORY_ROOT, 'packages/twenty-sdk');
const sdkRequire = createRequire(join(sourceSdk, 'package.json'));
await mkdir(join(modules, '@sniptt'), { recursive: true });
for (const name of [
'react',
'react-dom',
'@sniptt/guards',
'uuid',
'esbuild',
'typescript',
'tinyglobby',
'twenty-shared',
'twenty-client-sdk',
'twenty-ui',
]) {
await symlink(
name === 'uuid'
? dirname(sdkRequire.resolve('uuid/package.json'))
: join(REPOSITORY_ROOT, 'node_modules', name),
join(modules, name),
);
}
const packageJson = JSON.parse(
await readFile(join(sourceSdk, 'package.json'), 'utf8'),
);
const exports: Record<string, unknown> = {};
for (const name of [
'define',
'front-component',
'logic-function',
'billing',
'utils',
]) {
await cp(join(sourceSdk, 'dist', name), join(sdkPath, 'dist', name), {
recursive: true,
});
exports[`./${name}`] = packageJson.exports[`./${name}`];
}
await writeFile(
join(sdkPath, 'package.json'),
JSON.stringify({
name: packageJson.name,
version: packageJson.version,
engines: packageJson.engines,
exports,
}),
);
expect(() =>
createRequire(join(root, 'package.json')).resolve('twenty-sdk/build'),
).toThrow();
await buildTestAppWorker(join(root, 'cli'));
launch.modulePath = join(root, 'cli/app-worker.cjs');
}, 60000);
afterEach(() => vi.unstubAllEnvs());
afterAll(async () => {
await stop();
await rm(root, { recursive: true, force: true });
});
it('covers every repository fixture', async () => {
expect(
(await readdir(FIXTURES_DIRECTORY, { withFileTypes: true }))
.filter((entry) => entry.isDirectory())
.map((entry) => entry.name)
.sort(),
).toEqual([...FIXTURES].sort());
});
it.each(FIXTURES)(
'matches the manifest, file paths, errors and warnings for %s',
async (name) => {
const appPath = await copyFixture(name);
const result = await compareManifest(appPath);
expect(result.success).toBe(name !== 'invalid-app');
expect(
JSON.parse(JSON.stringify(result).replaceAll(appPath, '<app>')),
).toMatchSnapshot();
},
60000,
);
it('builds pulled definitions while preserving an external default role reference', async () => {
const appPath = await copyFixture('minimal-app');
const { manifest } = await buildManifest(appPath);
if (!manifest) throw new Error('Failed to build the minimal app fixture');
const { entities } = buildPullEntities({
manifest: { ...manifest, roles: [] },
});
const application = entities.find(
(entity) => entity.kind === 'application',
);
if (!application) throw new Error('Pull did not generate the application');
await writeFile(
join(appPath, 'application.config.ts'),
writeDefineFile(application).content,
);
await rm(join(appPath, 'my.role.ts'));
const result = await compareManifest(appPath);
expect(result.success).toBe(true);
if (result.success) {
expect(result.manifest.application.defaultRoleUniversalIdentifier).toBe(
manifest.application.defaultRoleUniversalIdentifier,
);
expect(result.manifest.roles).toEqual([]);
expect(result.filePaths.roles).toEqual([]);
}
}, 60000);
it('matches a freshly initialized app and its server requirement', async () => {
const appPath = join(root, 'fresh-app');
await createAppProject({
appDirectory: appPath,
appName: 'fresh-app',
appDisplayName: 'Fresh app',
appDescription: 'Parity fixture',
signal: new AbortController().signal,
});
await symlink(join(root, 'node_modules'), join(appPath, 'node_modules'));
await writeFile(join(appPath, 'yarn.lock'), EMPTY_YARN_LOCK_CONTENT);
const result = await compareManifest(appPath);
const packageJson = JSON.parse(
await readFile(join(appPath, 'package.json'), 'utf8'),
);
expect(result.success).toBe(true);
if (result.success)
expect(result.manifest.application.requiredServerVersionRange).toBe(
packageJson.engines.twenty,
);
}, 60000);
it('matches authored and compiled translations and malformed catalog warnings', async () => {
const appPath = await copyFixture('minimal-app');
await mkdir(join(appPath, 'locales/compiled'), { recursive: true });
await writeFile(
join(appPath, 'locales/fr-FR.json'),
JSON.stringify({
Company: 'Entreprise',
door: { Open: 'Ouvrir' },
Empty: '',
}),
);
await writeFile(
join(appPath, 'locales/compiled/fr-FR.json'),
JSON.stringify({ orphan: 'Conservé' }),
);
await writeFile(join(appPath, 'locales/de-DE.json'), '{');
await writeFile(join(appPath, 'locales/klingon.json'), '{}');
await compareManifest(appPath);
}, 60000);
it('matches duplicate identifier diagnostics and empty lockfile failures', async () => {
const appPath = await copyFixture('minimal-app');
const duplicate = join(appPath, 'duplicate.role.ts');
await cp(join(appPath, 'my.role.ts'), duplicate);
const duplicateResult = await compareManifest(appPath);
expect(duplicateResult.success).toBe(false);
if (!duplicateResult.success)
expect(duplicateResult.errors.join('\n')).toContain(
'Duplicate universal identifiers',
);
await rm(duplicate);
await writeFile(join(appPath, 'yarn.lock'), '');
const lockResult = await compareManifest(appPath);
expect(lockResult).toEqual({
success: false,
errors: [
'yarn.lock is empty. Run "yarn install" to regenerate it before building.',
],
});
}, 60000);
it('matches a workflow definition and its validation errors', async () => {
const appPath = await copyFixture('minimal-app');
const writeWorkflow = (stepType: string) =>
writeFile(
join(appPath, 'my.workflow.ts'),
`import { defineWorkflow } from 'twenty-sdk/define';
export default defineWorkflow({
universalIdentifier: 'e1e2e3e4-e5e6-4000-8000-000000000060',
name: 'My workflow',
version: {
trigger: {
universalIdentifier: 'e1e2e3e4-e5e6-4000-8000-000000000061',
type: 'MANUAL',
nextStepIds: ['e1e2e3e4-e5e6-4000-8000-000000000062'],
},
steps: [
{
universalIdentifier: 'e1e2e3e4-e5e6-4000-8000-000000000062',
name: 'Run my function',
type: '${stepType}',
logicFunctionUniversalIdentifier: 'e1e2e3e4-e5e6-4000-8000-000000000010',
input: {},
nextStepIds: [],
},
],
},
});
`,
);
await writeWorkflow('LOGIC_FUNCTION');
const result = await compareManifest(appPath);
expect(result.success).toBe(true);
if (result.success) {
expect(result.manifest.workflows).toHaveLength(1);
expect(result.filePaths.workflows).toEqual(['my.workflow.ts']);
}
await writeWorkflow('CODE');
const invalidResult = await compareManifest(appPath);
expect(invalidResult.success).toBe(false);
if (!invalidResult.success) {
expect(invalidResult.errors.join('\n')).toContain(
'Unsupported step type',
);
}
}, 60000);
it.each([undefined, false, true])(
'matches one health check with member opt-in %s and rejects a second declaration',
async (runOnUserPreferences) => {
const appPath = await copyFixture('minimal-app');
const writeHealthCheck = ({
fileName,
universalIdentifier,
runOnUserPreferences,
}: {
fileName: string;
universalIdentifier: string;
runOnUserPreferences?: boolean;
}) =>
writeFile(
join(appPath, fileName),
`import { defineHealthCheck } from 'twenty-sdk/define';
export default defineHealthCheck({
universalIdentifier: '${universalIdentifier}',
name: '${fileName}',
${isDefined(runOnUserPreferences) ? `runOnUserPreferences: ${runOnUserPreferences},` : ''}
handler: () => ({ status: 'OK' }),
});
`,
);
await writeHealthCheck({
fileName: 'health-check.ts',
universalIdentifier: 'e1e2e3e4-e5e6-4000-8000-000000000070',
runOnUserPreferences,
});
const result = await compareManifest(appPath);
expect(result.success).toBe(true);
if (result.success) {
const healthCheckLogicFunction = result.manifest.logicFunctions.find(
({ universalIdentifier }) =>
universalIdentifier === 'e1e2e3e4-e5e6-4000-8000-000000000070',
);
expect(healthCheckLogicFunction).toBeDefined();
expect(healthCheckLogicFunction).not.toHaveProperty(
'runOnUserPreferences',
);
expect(result.manifest.application.healthCheckLogicFunction).toEqual({
universalIdentifier: 'e1e2e3e4-e5e6-4000-8000-000000000070',
...(isDefined(runOnUserPreferences) ? { runOnUserPreferences } : {}),
});
}
await writeHealthCheck({
fileName: 'second-health-check.ts',
universalIdentifier: 'e1e2e3e4-e5e6-4000-8000-000000000071',
runOnUserPreferences: true,
});
const duplicateResult = await compareManifest(appPath);
expect(duplicateResult.success).toBe(false);
if (!duplicateResult.success) {
expect(duplicateResult.errors).toContain(
'Only one health check is allowed per application',
);
}
},
60000,
);
it('keeps evaluated source output and workspace credentials outside the manifest result', async () => {
const appPath = await copyFixture('minimal-app');
const applicationFiles = (await buildManifest(appPath)).filePaths
.application;
const filePath = join(appPath, applicationFiles[0]);
await writeFile(
filePath,
`console.log('manifest source output');
if (process.env.TWENTY_API_KEY) throw new Error('workspace credential leaked');
${await readFile(filePath, 'utf8')}`,
);
vi.stubEnv('TWENTY_API_KEY', 'must-not-reach-app');
const worker = await runAppWorker({
request: { type: 'buildManifest', appPath },
signal: new AbortController().signal,
});
expect(worker.result).toMatchObject({ success: true });
expect(JSON.stringify(worker.output)).toContain('manifest source output');
expect(JSON.stringify(worker.result)).not.toContain(
'manifest source output',
);
}, 60000);
});