The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import { splitPulledTranslations } from '@/app/pull/split-pulled-translations';
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { type Manifest } from 'twenty-shared/application';
import { generateMessageId } from 'twenty-shared/i18n';
import { afterEach, describe, expect, it } from 'vitest';
const PET_LABEL_ID = generateMessageId('Pet', 'objectMetadata.labelSingular');
const AGE_LABEL_ID = generateMessageId('Age', 'fieldMetadata.label');
const VIEW_NAME_ID = generateMessageId('All pets', 'view.name');
const buildManifest = (overrides: Record<string, unknown>): Manifest =>
({
application: {},
objects: [
{
universalIdentifier: 'pet',
nameSingular: 'pet',
namePlural: 'pets',
labelSingular: 'Pet',
labelPlural: 'Pets',
description: 'A pet',
fields: [
{
universalIdentifier: 'age',
name: 'age',
label: 'Age',
description: '',
type: 'NUMBER',
},
],
},
],
fields: [],
indexes: [],
views: [
{
universalIdentifier: 'all-pets',
name: 'All pets',
objectUniversalIdentifier: 'pet',
},
],
viewFields: [],
...overrides,
}) as unknown as Manifest;
const createdDirectories: string[] = [];
const createDirectory = async () => {
const directory = await mkdtemp(join(tmpdir(), 'split-pulled-translations-'));
createdDirectories.push(directory);
return directory;
};
describe('splitPulledTranslations', () => {
afterEach(async () => {
await Promise.all(
createdDirectories
.splice(0)
.map((directory) => rm(directory, { recursive: true, force: true })),
);
});
it('should write entries whose source is a pulled label in the authoring format and keep the rest compiled', async () => {
const catalogs = await splitPulledTranslations({
manifest: buildManifest({
translations: {
'fr-FR': {
[PET_LABEL_ID]: 'Animal',
[AGE_LABEL_ID]: 'Âge',
[VIEW_NAME_ID]: 'Tous les animaux',
zzzzzz: 'orphan',
},
},
}),
frontComponentSourcePaths: [],
});
expect(catalogs).toEqual([
{
locale: 'fr-FR',
authored: {
'fieldMetadata.label': { Age: 'Âge' },
'objectMetadata.labelSingular': { Pet: 'Animal' },
'view.name': { 'All pets': 'Tous les animaux' },
},
compiled: { zzzzzz: 'orphan' },
},
]);
});
it('should decode the strings of the front components it is given', async () => {
const componentPath = join(
await createDirectory(),
'card.front-component.tsx',
);
await writeFile(
componentPath,
`
import { t, Trans } from 'twenty-sdk/front-component';
const Component = () => {
const label = t('No content yet');
const verb = t({ message: 'Open', context: 'door' });
return <Trans context="card">Untitled</Trans>;
};
export default defineFrontComponent({ component: Component });
`,
);
const catalogs = await splitPulledTranslations({
manifest: buildManifest({
translations: {
'de-DE': {
[generateMessageId('No content yet')]: 'Noch kein Inhalt',
[generateMessageId('Open', 'door')]: 'Öffnen',
[generateMessageId('Untitled', 'card')]: 'Ohne Titel',
},
},
}),
frontComponentSourcePaths: [componentPath],
});
expect(catalogs).toEqual([
{
locale: 'de-DE',
authored: {
'No content yet': 'Noch kein Inhalt',
card: { Untitled: 'Ohne Titel' },
door: { Open: 'Öffnen' },
},
compiled: {},
},
]);
});
it('should keep an entry compiled when the label it was translated from has changed', async () => {
const staleId = generateMessageId('Kitten', 'objectMetadata.labelSingular');
const catalogs = await splitPulledTranslations({
manifest: buildManifest({
translations: { 'fr-FR': { [staleId]: 'Chaton' } },
}),
frontComponentSourcePaths: [],
});
expect(catalogs).toEqual([
{ locale: 'fr-FR', authored: {}, compiled: { [staleId]: 'Chaton' } },
]);
});
it('should ignore locales the platform does not support, including path-shaped keys', async () => {
const catalogs = await splitPulledTranslations({
manifest: buildManifest({
translations: {
'fr-FR': { [PET_LABEL_ID]: 'Animal' },
klingon: { [PET_LABEL_ID]: 'targh' },
'../../escape': { [PET_LABEL_ID]: 'nope' },
},
}),
frontComponentSourcePaths: [],
});
expect(catalogs.map(({ locale }) => locale)).toEqual(['fr-FR']);
});
it('should ignore a locale whose catalog is not a map of strings', async () => {
const catalogs = await splitPulledTranslations({
manifest: buildManifest({
translations: {
'fr-FR': 'Animal',
'de-DE': ['Haustier'],
'es-ES': { [PET_LABEL_ID]: 7 },
'it-IT': { [PET_LABEL_ID]: 'Animale' },
},
}),
frontComponentSourcePaths: [],
});
expect(catalogs.map(({ locale }) => locale)).toEqual(['it-IT']);
});
it('should order locales and return nothing for an export without translations', async () => {
const catalogs = await splitPulledTranslations({
manifest: buildManifest({
translations: {
'fr-FR': { [PET_LABEL_ID]: 'Animal' },
'de-DE': { [PET_LABEL_ID]: 'Haustier' },
},
}),
frontComponentSourcePaths: [],
});
expect(catalogs.map(({ locale }) => locale)).toEqual(['de-DE', 'fr-FR']);
expect(
await splitPulledTranslations({
manifest: buildManifest({}),
frontComponentSourcePaths: [],
}),
).toEqual([]);
});
it('keeps prototype-shaped message contexts as own catalog properties', async () => {
const componentPath = join(await createDirectory(), 'card.tsx');
await writeFile(
componentPath,
`import { t } from 'twenty-sdk/front-component';
export const label = t({ message: 'Review fixture', context: '__proto__' });`,
);
const catalogs = await splitPulledTranslations({
manifest: buildManifest({
translations: {
'fr-FR': {
[generateMessageId('Review fixture', '__proto__')]: 'Exemple',
},
},
}),
frontComponentSourcePaths: [componentPath],
});
expect(Object.hasOwn(catalogs[0].authored, '__proto__')).toBe(true);
expect(catalogs[0].authored.__proto__).toEqual({
'Review fixture': 'Exemple',
});
expect(Object.hasOwn(Object.prototype, 'Review fixture')).toBe(false);
expect(JSON.stringify(catalogs[0].authored)).toContain('__proto__');
});
});