Files
orca/config/scripts/localization-extraction-call-syntax.test.mjs
T
Neil 6607c54984 perf(ci): cut redundant tests and reuse expensive setup (#28669)
* perf(ci): reduce test overhead and reuse expensive build work

* test(ci): keep worker startup outside failure-case deadlines

* test: isolate push fixture advisory locks

* test(ci): preserve fixed drain retry allowance

* Align CI contracts with current triggers and guide content

* Stabilize consolidated reveal scenarios and retain behavioral CI contracts

* Fix upstream lint blockers while retaining behavior and assertions

* Classify global Jira preferences in the WSL migration inventory

* Align Jira test fixtures with startup and build safety checks

* Include package-test routing dependency in CI checkouts and fixtures

* Keep native stream classes intact in transcript dependency tests
2026-10-14 16:14:27 -07:00

100 lines
3.5 KiB
JavaScript

import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
import { createRequire } from 'node:module'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { findKeys } from 'i18next-cli'
import { describe, expect, it } from 'vitest'
const require = createRequire(import.meta.url)
const commonJsFindKeys = require('i18next-cli').findKeys
const calls = {
normal: "translate('probe.normal', 'Normal')",
block: "translate /* comment */ ('probe.block', 'Block')",
line: "translate\n// comment\n('probe.line', 'Line')",
member: "i18n./* member */t /* call */ ('probe.member', 'Member')",
optional: "translate?. /* optional */ ('probe.optional', 'Optional')",
generic: "translate /* generic */ <string>('probe.generic', 'Generic')",
dollar: "$api.t /* member */ ('probe.dollar', 'Dollar')",
unicode: "transl\\u0061te('probe.unicode', 'Unicode')"
}
describe.each([
['ES module', findKeys],
['CommonJS', commonJsFindKeys]
])('localization extraction through %s', (_name, extractKeys) => {
it('keeps keys in calls containing comments or escaped identifiers', async () => {
const directory = mkdtempSync(join(tmpdir(), 'orca-extraction-syntax-'))
try {
for (const [name, source] of Object.entries(calls)) {
writeFileSync(join(directory, `${name}.ts`), source)
}
const errors = []
const result = await extractKeys(
{
locales: ['en'],
extract: {
input: [join(directory, '*.ts').replaceAll('\\', '/')],
defaultNS: false,
functions: ['t', '*.t', 'translate'],
disablePlurals: true
}
},
undefined,
errors
)
expect(errors).toEqual([])
expect([...result.allKeys.values()].map((entry) => entry.key).sort()).toEqual(
Object.keys(calls)
.map((name) => `probe.${name}`)
.sort()
)
} finally {
rmSync(directory, { recursive: true, force: true })
}
})
it('preserves cross-file namespaces, source locations and fresh plugin transformations', async () => {
const directory = mkdtempSync(join(tmpdir(), 'orca-extraction-passes-'))
const file = join(directory, 'a-consumer.ts')
const config = {
locales: ['en'],
extract: {
input: [join(directory, '*.ts').replaceAll('\\', '/')],
functions: ['t', '*.t'],
disablePlurals: true
}
}
try {
writeFileSync(join(directory, 'z-constants.ts'), "export const NS = 'probe' as const\n")
const source = [
"import { NS } from './z-constants'",
'/* 🙂 */ const { t } = useTranslation(NS);',
"t('first', 'First')"
].join('\n')
writeFileSync(file, source)
const first = await extractKeys(config)
expect([...first.allKeys.values()]).toMatchObject([
{ key: 'first', ns: 'probe', locations: [{ file, line: 3, column: 2 }] }
])
writeFileSync(file, source.replace("'first'", "'updated'"))
const updated = await extractKeys(config)
expect([...updated.allKeys.values()].map((entry) => entry.key)).toEqual(['updated'])
const transformed = await extractKeys({
...config,
plugins: [
{
name: 'fixture-transform',
onLoad: (code) => code.replace("'updated'", "'transformed'")
}
]
})
expect([...transformed.allKeys.values()].map((entry) => entry.key)).toEqual(['transformed'])
} finally {
rmSync(directory, { recursive: true, force: true })
}
})
})