Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

4.4 KB
Raw Blame
import { describe, expect, it } from 'vitest';

/** Require every static production translation key in en.json, the fallback catalog. */

const enModules = import.meta.glob('../../../public/locales/en.json', { eager: true, import: 'default' });
const sourceModules = import.meta.glob('../../**/*.{ts,tsx}', {
  eager: true,
  import: 'default',
  query: '?raw',
});

const en = Object.values(enModules)[0] as Record<string, unknown>;

/** Top-level namespaces in en.json. Anchoring on these keeps the scan from matching arbitrary dotted strings. */
const NAMESPACES = Object.keys(en);

/** Dynamic prefixes are exempt only with readable defaultValue; command titles use their definition title. */
export const DYNAMIC_KEY_PREFIXES = ['commandPalette.commands.'];

const isTestModule = (path: string): boolean => /\.test\.|\.type-test\.|\.testing\./.test(path);

/** Flattens the nested catalog to dotted paths, keeping plural suffixes intact. */
const flattenCatalog = (value: unknown, prefix = '', keys = new Set<string>()): Set<string> => {
  if (typeof value !== 'object' || value === null) {
    return keys;
  }

  for (const [segment, child] of Object.entries(value as Record<string, unknown>)) {
    const path = prefix ? `${prefix}.${segment}` : segment;

    if (typeof child === 'object' && child !== null) {
      // An array is read whole with `t('x', { returnObjects: true })`, so its own path is a key too.
      if (Array.isArray(child)) {
        keys.add(path);
      }

      flattenCatalog(child, path, keys);
      continue;
    }

    keys.add(path);
    // i18next resolves `t('x', { count })` against `x_one` / `x_other`; index the stem too.
    const pluralMatch = /^(.*)_(?:zero|one|two|few|many|other)$/.exec(path);

    if (pluralMatch?.[1]) {
      keys.add(pluralMatch[1]);
    }
  }

  return keys;
};

const catalogKeys = flattenCatalog(en);

const KEY_PATTERN = '[A-Za-z0-9_]+(?:\\.[A-Za-z0-9_]+)+';
// `t('a.b')` — the translator call itself.
const T_CALL = new RegExp(String.raw`\bt\(\s*['"](${KEY_PATTERN})['"]`, 'g');
// `i18nKey="a.b"` — the <Trans> form.
const I18N_KEY_PROP = new RegExp(String.raw`\bi18nKey\s*=\s*['"](${KEY_PATTERN})['"]`, 'g');
// `labelKey: 'a.b'` / `titleKey: 'a.b'` — keys held in const maps and passed to `t()` indirectly.
const KEY_PROPERTY = new RegExp(String.raw`\b\w*[Kk]ey\s*:\s*['"](${KEY_PATTERN})['"]`, 'g');

// `t('a.b')` with no options: nothing can fill the value's `{{placeholders}}`.
const BARE_T_CALL = new RegExp(String.raw`\bt\(\s*['"](${KEY_PATTERN})['"]\s*\)`, 'g');

const readCatalogString = (key: string): string | undefined => {
  let value: unknown = en;

  for (const segment of key.split('.')) {
    value = typeof value === 'object' && value !== null ? (value as Record<string, unknown>)[segment] : undefined;
  }

  return typeof value === 'string' ? value : undefined;
};

const collectKeys = (source: string): string[] => {
  const found: string[] = [];

  for (const pattern of [T_CALL, I18N_KEY_PROP, KEY_PROPERTY]) {
    pattern.lastIndex = 0;

    for (const match of source.matchAll(pattern)) {
      const key = match[1];

      if (key && NAMESPACES.includes(key.split('.')[0] ?? '')) {
        found.push(key);
      }
    }
  }

  return found;
};

const usages = Object.entries(sourceModules)
  .filter(([path]) => !isTestModule(path))
  .flatMap(([path, source]) => collectKeys(source as string).map((key) => ({ key, path })));

describe('translation keys', () => {
  it('resolves every static key against the English catalog', () => {
    // A broken glob would make every assertion here vacuously pass.
    expect(usages.length).toBeGreaterThan(500);
    const missing = usages
      .filter(({ key }) => !catalogKeys.has(key))
      .filter(({ key }) => !DYNAMIC_KEY_PREFIXES.some((prefix) => key.startsWith(prefix)))
      .map(({ key, path }) => `${key} (${path.replace('../../', 'src/')})`);

    expect([...new Set(missing)].sort()).toEqual([]);
  });

  it('passes options wherever the English value interpolates', () => {
    // A key shared by an interpolating caller and a bare one renders the literal `{{name}}` at the bare call.
    const unfilled = Object.entries(sourceModules)
      .filter(([path]) => !isTestModule(path))
      .flatMap(([path, source]) =>
        [...(source as string).matchAll(BARE_T_CALL)].flatMap(([, key]) =>
          key && readCatalogString(key)?.includes('{{') ? [`${key} (${path.replace('../../', 'src/')})`] : []
        )
      );

    expect([...new Set(unfilled)].sort()).toEqual([]);
  });
});