/** * #1250: user-facing results go to stdout without a log prefix; diagnostics go to stderr and only * carry `[time][LEVEL] [CLI]` with --verbose; instance failures are classified, and an unexpected * one shows its log correlation id with --debug. */ import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { setCliFlags } from '../src/utils/cli-flags.js'; import { cliLogFormatter, logger } from '../src/utils/logger.js'; import { output } from '../src/utils/output.js'; import { describeHostFailure, findInstanceLogLines, reportHostFailure } from '../src/utils/host-error.js'; import { formatCommandRoute, manifestFeatureNames, packageAuthors, packagePersonName, readPackageCapabilities, } from '../src/utils/package-capabilities.js'; import { NpmViewError, npmViewFailure } from '../src/utils/plugin-registry.js'; const plugin = vi.hoisted(() => ({ info: undefined as unknown, search: {} as Record, queries: [] as string[] })); vi.mock('../src/utils/plugin-registry.js', async (importOriginal) => ({ ...await importOriginal(), npmView: () => { if (plugin.info instanceof Error) throw plugin.info; return plugin.info; }, fetchWeeklyDownloads: async () => null, npmSearch: (query: string) => { plugin.queries.push(query); const page = plugin.search[query]; if (page instanceof Error) throw page; return page ?? []; }, })); // The date is present when stdout is not a terminal (#1250), as under the test runner. const PREFIX = /\[(?:\d{4}-\d{2}-\d{2} )?\d{2}:\d{2}:\d{2}\.\d{3}\]\[(INFO|WARN|ERROR)\]/; let stdout: string[]; let stderr: string[]; let tmp: string; let prevCwd: string; beforeEach(() => { stdout = []; stderr = []; vi.spyOn(console, 'log').mockImplementation((...args: unknown[]) => { stdout.push(args.join(' ')); }); vi.spyOn(process.stderr, 'write').mockImplementation(((chunk: string) => { stderr.push(String(chunk)); return true; }) as never); prevCwd = process.cwd(); tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'zhin-cli-output-')); process.chdir(tmp); }); afterEach(() => { setCliFlags({ verbose: false, debug: false }); process.chdir(prevCwd); fs.rmSync(tmp, { recursive: true, force: true }); vi.restoreAllMocks(); vi.unstubAllGlobals(); }); describe('output and diagnostics', () => { it('writes results to stdout as-is and diagnostics to stderr without a prefix unless --verbose', () => { output.line('第一行', '第二行'); output.line(); expect(stdout).toEqual(['第一行\n第二行', '']); logger.info('诊断信息'); logger.warn('注意'); expect(stderr.join('')).toContain('诊断信息\n'); expect(stderr.join('')).not.toMatch(PREFIX); expect(stderr.join('')).not.toContain('[CLI]'); setCliFlags({ verbose: true }); logger.info('详细'); expect(stderr.at(-1)).toMatch(PREFIX); expect(stderr.at(-1)).toContain('[CLI] 详细'); // Nothing diagnostic reached stdout. expect(stdout).toEqual(['第一行\n第二行', '']); }); it('marks warnings and errors without the timestamp prefix', () => { const entry = (level: 'warn' | 'error' | 'info') => ({ level, name: 'CLI', message: 'm', timestamp: new Date(), args: [] }); expect(cliLogFormatter.format(entry('info'))).toBe('m'); expect(cliLogFormatter.format(entry('warn'))).toMatch(/⚠.* m$/); expect(cliLogFormatter.format(entry('error'))).toMatch(/✖.* m$/); }); }); describe('zhin packages info', () => { it('reads author and maintainers in every npm shape', () => { expect(packagePersonName('Jane Doe (https://x.dev)')).toBe('Jane Doe'); expect(packagePersonName({ name: 'Jane' })).toBe('Jane'); // npm search maintainers carry `username` (#1250 cli-search-author-unknown). expect(packagePersonName({ username: 'lc-cn' })).toBe('lc-cn'); expect(packagePersonName('')).toBeUndefined(); // Nested / unbalanced brackets never leave partial markup behind. expect(packagePersonName('Eve <