|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * PIN — `os g` in a project that declares locales other than its source |
| 5 | + * locale prints ONE line after a successful write: the label was written in |
| 6 | + * the source locale only, run `os i18n check`, and where translations go. |
| 7 | + * |
| 8 | + * The maintainer's ruling: the blank template and the Starter ship every |
| 9 | + * platform locale; generators write the source language only; completeness is |
| 10 | + * `os i18n check`'s report. ⛔ No translation stub is written, because |
| 11 | + * coverage counts any string in a bundle as translated. |
| 12 | + * |
| 13 | + * Halves: |
| 14 | + * 1. The command, in-process (`Generate.run` in a sandbox project under this |
| 15 | + * package's `node_modules`, so the config's `@objectstack/spec` import |
| 16 | + * resolves to the workspace copy): one line with every platform locale |
| 17 | + * declared and no translation file written; no line in a single-locale |
| 18 | + * project; no line on `--dry-run`, which writes nothing. |
| 19 | + * 2. The reader: which declared locales count as "other" — the same readers |
| 20 | + * `os i18n check` uses (`sourceLocale` outranks `defaultLocale`, and the |
| 21 | + * users' `defaultLocale` is gated on a stack declaring `sourceLocale`). |
| 22 | + */ |
| 23 | + |
| 24 | +import { afterAll, afterEach, describe, expect, it, vi } from 'vitest'; |
| 25 | +import fs from 'node:fs'; |
| 26 | +import path from 'node:path'; |
| 27 | +import { fileURLToPath } from 'node:url'; |
| 28 | +import { PLATFORM_LOCALE_TAGS } from '@objectstack/spec/system'; |
| 29 | +import Generate from '../src/commands/generate'; |
| 30 | +import { declaredOtherLocales, generateLocaleHintLine } from '../src/utils/generate-locale-hint.js'; |
| 31 | + |
| 32 | +const HERE = path.dirname(fileURLToPath(import.meta.url)); |
| 33 | +const CLI_ROOT = path.resolve(HERE, '..'); |
| 34 | +const TMP_ROOT = fs.mkdtempSync(path.join(HERE, '..', 'node_modules', '.generate-locale-hint-')); |
| 35 | +const ORIGINAL_CWD = process.cwd(); |
| 36 | + |
| 37 | +afterEach(() => { |
| 38 | + process.chdir(ORIGINAL_CWD); |
| 39 | +}); |
| 40 | + |
| 41 | +afterAll(() => { |
| 42 | + fs.rmSync(TMP_ROOT, { recursive: true, force: true }); |
| 43 | +}); |
| 44 | + |
| 45 | +/** A project whose config declares `i18n` as given (nothing when omitted). */ |
| 46 | +function project(i18n?: Record<string, unknown>): string { |
| 47 | + const root = fs.mkdtempSync(path.join(TMP_ROOT, 'project-')); |
| 48 | + const i18nLine = i18n ? ` i18n: ${JSON.stringify(i18n)},\n` : ''; |
| 49 | + fs.writeFileSync( |
| 50 | + path.join(root, 'objectstack.config.ts'), |
| 51 | + `import { defineStack } from '@objectstack/spec'; |
| 52 | +
|
| 53 | +export default defineStack({ |
| 54 | + manifest: { |
| 55 | + id: 'com.example.locale-hint', |
| 56 | + namespace: 'locale_hint', |
| 57 | + version: '0.1.0', |
| 58 | + type: 'app', |
| 59 | + name: 'Locale Hint', |
| 60 | + }, |
| 61 | +${i18nLine}}); |
| 62 | +`, |
| 63 | + ); |
| 64 | + return root; |
| 65 | +} |
| 66 | + |
| 67 | +/** Every file under `root`, relative, sorted. */ |
| 68 | +function tree(root: string): string[] { |
| 69 | + const out: string[] = []; |
| 70 | + const walk = (dir: string): void => { |
| 71 | + for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { |
| 72 | + const full = path.join(dir, entry.name); |
| 73 | + if (entry.isDirectory()) walk(full); |
| 74 | + else out.push(path.relative(root, full)); |
| 75 | + } |
| 76 | + }; |
| 77 | + walk(root); |
| 78 | + return out.sort(); |
| 79 | +} |
| 80 | + |
| 81 | +/** Run `os g …` in `cwd`, in-process, and answer its stdout lines, uncoloured. */ |
| 82 | +async function generate(cwd: string, argv: string[]): Promise<string[]> { |
| 83 | + const lines: string[] = []; |
| 84 | + const logSpy = vi.spyOn(console, 'log').mockImplementation((...args: unknown[]) => { |
| 85 | + lines.push(args.map(String).join(' ')); |
| 86 | + }); |
| 87 | + process.chdir(cwd); |
| 88 | + try { |
| 89 | + await Generate.run(argv, { root: CLI_ROOT }); |
| 90 | + } finally { |
| 91 | + logSpy.mockRestore(); |
| 92 | + process.chdir(ORIGINAL_CWD); |
| 93 | + } |
| 94 | + // eslint-disable-next-line no-control-regex |
| 95 | + return lines.join('\n').replace(/\u001b\[[0-9;]*m/g, '').split('\n'); |
| 96 | +} |
| 97 | + |
| 98 | +const hintLines = (lines: string[]): string[] => lines.filter((l) => l.includes('i18n check')); |
| 99 | + |
| 100 | +describe('os g: one hint when the project declares other locales', () => { |
| 101 | + it('every platform locale declared: one line naming the source and `os i18n check`, and no stub written', async () => { |
| 102 | + const root = project({ defaultLocale: 'en', supportedLocales: [...PLATFORM_LOCALE_TAGS] }); |
| 103 | + const lines = await generate(root, ['object', 'note']); |
| 104 | + |
| 105 | + const hints = hintLines(lines); |
| 106 | + expect(hints).toHaveLength(1); |
| 107 | + expect(hints[0]).toContain('source locale (en) only'); |
| 108 | + expect(hints[0]).toContain('zh-Hans, zh-Hant, es and 7 more'); |
| 109 | + expect(hints[0]).toContain('`os i18n check`'); |
| 110 | + expect(hints[0]).toContain('`translations`'); |
| 111 | + |
| 112 | + // The scaffold and its barrel, and nothing else: no translation stub. |
| 113 | + expect(tree(root)).toEqual(['objectstack.config.ts', 'src/objects/index.ts', 'src/objects/note.object.ts']); |
| 114 | + const scaffold = fs.readFileSync(path.join(root, 'src/objects/note.object.ts'), 'utf8'); |
| 115 | + expect(scaffold).toContain("label: 'Note'"); |
| 116 | + }, 60_000); // the first in-process run pays the config loader's cold start |
| 117 | + |
| 118 | + it('a single-locale project: no line', async () => { |
| 119 | + const root = project({ defaultLocale: 'en', supportedLocales: ['en'] }); |
| 120 | + const lines = await generate(root, ['object', 'note']); |
| 121 | + expect(fs.existsSync(path.join(root, 'src/objects/note.object.ts'))).toBe(true); |
| 122 | + expect(hintLines(lines)).toEqual([]); |
| 123 | + }); |
| 124 | + |
| 125 | + it('a project with no i18n block: no line', async () => { |
| 126 | + const root = project(); |
| 127 | + const lines = await generate(root, ['object', 'note']); |
| 128 | + expect(fs.existsSync(path.join(root, 'src/objects/note.object.ts'))).toBe(true); |
| 129 | + expect(hintLines(lines)).toEqual([]); |
| 130 | + }); |
| 131 | + |
| 132 | + it('--dry-run writes nothing, so it says nothing about a written label', async () => { |
| 133 | + const root = project({ defaultLocale: 'en', supportedLocales: [...PLATFORM_LOCALE_TAGS] }); |
| 134 | + const lines = await generate(root, ['object', 'note', '--dry-run']); |
| 135 | + expect(tree(root)).toEqual(['objectstack.config.ts']); |
| 136 | + expect(hintLines(lines)).toEqual([]); |
| 137 | + }); |
| 138 | +}); |
| 139 | + |
| 140 | +describe('which declared locales are "other than the source"', () => { |
| 141 | + it('supportedLocales minus defaultLocale when no sourceLocale is declared', () => { |
| 142 | + expect(declaredOtherLocales({ i18n: { defaultLocale: 'en', supportedLocales: ['en', 'zh-Hans'] } })) |
| 143 | + .toEqual({ source: 'en', others: ['zh-Hans'] }); |
| 144 | + }); |
| 145 | + |
| 146 | + it('sourceLocale outranks defaultLocale, and the users\' defaultLocale counts as declared', () => { |
| 147 | + expect(declaredOtherLocales({ i18n: { sourceLocale: 'en', defaultLocale: 'zh-Hans', supportedLocales: ['en'] } })) |
| 148 | + .toEqual({ source: 'en', others: ['zh-Hans'] }); |
| 149 | + }); |
| 150 | + |
| 151 | + it('a stack whose only declared locale is the source answers nothing', () => { |
| 152 | + expect(declaredOtherLocales({ i18n: { defaultLocale: 'ja', supportedLocales: ['ja'] } })).toBeUndefined(); |
| 153 | + expect(declaredOtherLocales({})).toBeUndefined(); |
| 154 | + expect(declaredOtherLocales(undefined)).toBeUndefined(); |
| 155 | + }); |
| 156 | + |
| 157 | + it('names up to three locales, then counts the rest', () => { |
| 158 | + expect(generateLocaleHintLine({ source: 'en', others: ['de', 'fr'] })).toContain('also declares de, fr.'); |
| 159 | + }); |
| 160 | +}); |
0 commit comments