import { execFileSync, spawnSync } from 'node:child_process' import { cpSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs' import { tmpdir } from 'node:os' import path from 'node:path' import { afterEach, describe, expect, test } from 'vitest' import { parseToolOutput } from '../src/modules/doctor/rules/tooling.js' import { runDoctor } from '../src/modules/doctor/run.js' const packageRoot = path.resolve(import.meta.dirname, '..') const scripts = path.join(packageRoot, 'docs/agent/skills/iapl-audit/scripts') let dirs: string[] = [] function project(files: Record): string { const root = mkdtempSync(path.join(tmpdir(), 'ipal-polish-')) dirs.push(root) for (const [rel, content] of Object.entries(files)) { const file = path.join(root, rel) mkdirSync(path.dirname(file), { recursive: true }) writeFileSync(file, content) } return root } /** `file:line` of every finding of the check. */ function polish(files: Record): string[] { const root = project(files) const out = execFileSync(process.execPath, [path.join(scripts, 'polish-literals.mjs'), 'src'], { cwd: root, encoding: 'utf8' }) return out.split('\n').filter(Boolean).map((l) => l.match(/^(.+?:\d+):/)?.[1] ?? l) } const HELPER = "import { adminText, rowLabel } from '@intecion/ipal-kit/blocks'\n" afterEach(() => { for (const d of dirs) {rmSync(d, { force: true, recursive: true })} dirs = [] }) describe('hardcoded Polish text: the panel labels are not public text', () => { test('adminText() in a schema file, on one line and over several', () => { expect(polish({ 'src/collections/Pages.ts': `${HELPER}export const Pages = { slug: 'pages', fields: [ { name: 'title', type: 'text', label: adminText('Title', 'Tytuł') }, { name: 'heading', type: 'text', label: adminText( 'Heading', 'Nagłówek', ) }, ] }\n`, 'src/globals/Header.ts': `${HELPER}export const Header = { slug: 'header', label: adminText('Header', 'Nagłówek') }\n`, })).toEqual([]) }) test('{ en, pl } as label, singular, plural, description, placeholder, group, option label — one line and several', () => { expect(polish({ 'src/globals/Company.ts': `export const Company = { slug: 'company', admin: { group: { en: 'Company', pl: 'Dane firmy żółć' } }, fields: [ { name: 'areas', type: 'array', labels: { singular: { en: 'Area', pl: 'Obszar działania' }, plural: { en: 'Areas', pl: 'Obszary działania', } }, fields: [] }, { name: 'kind', type: 'select', label: { en: 'Social profiles', pl: 'Profile społecznościowe', }, admin: { description: { en: 'Where it shows', pl: 'Gdzie się wyświetla' }, placeholder: { en: 'Pick', pl: 'Wybierz coś ładnego ł' } }, options: [{ label: { en: 'Title', pl: 'Tytuł' }, value: 't' }] }, ] }\n`, })).toEqual([]) }) test('an aliased import, rowLabel fallback, a typed AdminText constant, a file importing Payload types', () => { expect(polish({ 'src/fields/title.ts': `import { adminText as t, rowLabel } from '@intecion/ipal-kit/blocks' export const a = { label: t('Title', 'Tytuł'), admin: { components: rowLabel('x', { en: 'Item', pl: 'Pozycja ż' }) } }\n`, 'src/lib/schema.ts': `import type { AdminText } from '@intecion/ipal-kit' import type { Field } from 'payload' const TITLE: AdminText = { en: 'Title', pl: 'Tytuł' } export const f: Field = { name: 'title', type: 'text', label: { en: 'Title', pl: 'Tytuł' } } export const g = TITLE\n`, })).toEqual([]) }) test('comments never count', () => { expect(polish({ 'src/components/A.tsx': "// 'Nagłówek'\n/* \"Tytuł\" */\nexport const A = () => null\n" })).toEqual([]) }) }) describe('hardcoded Polish text: public strings are still reported', () => { test('a public component with the same words, whichever form it uses', () => { expect(polish({ 'src/components/Hero.tsx': `${HELPER}export const Hero = () => (
<Multi caption={{ en: 'Social profiles', pl: 'Profile społecznościowe', }} /> </section> )\n`, })).toEqual(['src/components/Hero.tsx:3', 'src/components/Hero.tsx:4', 'src/components/Hero.tsx:8']) }) test('a block component next to its schema; a helper in lib without schema types', () => { expect(polish({ 'src/blocks/Hero/Component.tsx': "export const Hero = () => <h1 title=\"Nagłówek\" />\n", 'src/blocks/Hero/config.ts': `${HELPER}export const Hero = { slug: 'hero', label: adminText('Heading', 'Nagłówek') }\n`, 'src/lib/texts.ts': "export const labels = { label: { en: 'Title', pl: 'Tytuł' } }\n", })).toEqual(['src/blocks/Hero/Component.tsx:1', 'src/lib/texts.ts:1']) }) test('in a schema file: Polish only, another key, defaultValue, a lookalike helper, a client file', () => { expect(polish({ 'src/collections/Bad.ts': `${HELPER}export const Bad = { slug: 'bad', fields: [ { name: 'a', label: 'Tytuł' }, { name: 'b', label: { pl: 'Tytuł' } }, { name: 'c', heading: { en: 'Title', pl: 'Tytuł' } }, { name: 'd', defaultValue: 'Nagłówek' }, { name: 'e', defaultValue: adminText('Heading', 'Nagłówek') }, { name: 'f', defaultValue: { en: 'Heading', pl: 'Nagłówek', } }, ] }\n`, 'src/globals/Client.ts': `'use client'\n${HELPER}export const x = { label: adminText('Title', 'Tytuł') }\n`, 'src/globals/Lookalike.ts': "const adminText = (a: string, b: string) => b\nexport const x = { label: adminText('Title', 'Tytuł') }\n", })).toEqual([ 'src/collections/Bad.ts:3', 'src/collections/Bad.ts:4', 'src/collections/Bad.ts:5', 'src/collections/Bad.ts:6', 'src/collections/Bad.ts:7', 'src/collections/Bad.ts:10', 'src/globals/Client.ts:3', 'src/globals/Lookalike.ts:2', ]) }) test('adminText outside a schema file is reported; rowLabel only spares its { en, pl } fallback', () => { expect(polish({ 'src/fields/row.ts': `${HELPER}export const a = rowLabel('x', 'Pozycja ż')\nexport const b = rowLabel('Żółć', { en: 'Item', pl: 'Pozycja ż' })\n`, 'src/lib/text.ts': `${HELPER}export const t = adminText('Title', 'Tytuł')\n`, })).toEqual(['src/fields/row.ts:2', 'src/fields/row.ts:3', 'src/lib/text.ts:2']) }) }) describe('iapl-audit and iapl-doctor agree', () => { const files = { 'package.json': JSON.stringify({ name: 'examplepl-web' }), 'src/collections/Pages.ts': `${HELPER}export const Pages = { slug: 'pages', fields: [ { name: 'title', label: adminText('Title', 'Tytuł') }, { name: 'heading', label: { en: 'Heading', pl: 'Nagłówek', } }, ] }\n`, 'src/components/Public.tsx': `${HELPER}export const Public = () => <p title="Nagłówek">{adminText('Title', 'Tytuł')}</p>\n`, } test('audit.sh and the i18n-literals rule report the public component only', async () => { const root = project(files) const audit = spawnSync('bash', [path.join(scripts, 'audit.sh'), '--quick'], { cwd: root, encoding: 'utf8' }) const line = parseToolOutput(audit.stdout).find((l) => l.title === 'hardcoded Polish text in code') expect(line?.level).toBe('error') expect(line?.items).toEqual([`src/components/Public.tsx:2:export const Public = () => <p title="Nagłówek">{adminText('Title', 'Tytuł')}</p>`]) const report = await runDoctor({ offline: true, only: ['audit'], packageRoot, root, writeSeen: false }) const polishFindings = report.findings.filter((f) => f.rule === 'i18n-literals' && f.message.startsWith('hardcoded Polish')) expect(polishFindings.map((f) => f.file)).toEqual(['src/components/Public.tsx']) }) test('without TypeScript in reach the check still runs, as a plain grep', () => { const copy = project({ 'x.txt': '' }) cpSync(scripts, path.join(copy, 'scripts'), { recursive: true }) const root = project(files) const env = { ...process.env, NODE_PATH: '' } // pnpm's bin shims put its store on NODE_PATH const alone = spawnSync(process.execPath, [path.join(copy, 'scripts/polish-literals.mjs'), 'src'], { cwd: root, encoding: 'utf8', env }) expect(alone.status).toBe(3) const audit = spawnSync('bash', [path.join(copy, 'scripts/audit.sh'), '--quick'], { cwd: root, encoding: 'utf8', env }) expect(parseToolOutput(audit.stdout).find((l) => l.title === 'hardcoded Polish text in code')?.items.join('\n')).toContain('src/components/Public.tsx') }) test('audit.sh passes when only schema labels hold Polish', () => { const { 'src/components/Public.tsx': _, ...schemaOnly } = files const audit = spawnSync('bash', [path.join(scripts, 'audit.sh'), '--quick'], { cwd: project(schemaOnly), encoding: 'utf8' }) expect(parseToolOutput(audit.stdout).find((l) => l.title === 'hardcoded Polish text in code')).toBeUndefined() }) })