import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'; import { tmpdir } from 'node:os'; import path from 'node:path'; import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import { designContext } from '../src/commands/design.js'; import { init } from '../src/commands/init.js'; import { install } from '../src/commands/install.js'; import { EXIT_NOT_INITIALIZED, EXIT_OK, EXIT_V3_REFRESH_FAILED, refreshStyle, } from '../src/commands/refresh-style.js'; describe('mancode refresh-style', () => { let dir: string; beforeEach(async () => { dir = await mkdtemp(path.join(tmpdir(), 'mancode-refresh-')); await mkdir(path.join(dir, '.git'), { recursive: true }); }); afterEach(async () => { await rm(dir, { recursive: true, force: true }); }); it('returns EXIT_NOT_INITIALIZED when mancode is not initialized', async () => { const code = await refreshStyle(dir); expect(code).toBe(EXIT_NOT_INITIALIZED); }); it('returns EXIT_OK on initialized project', async () => { await silentInit(dir); const code = await refreshStyle(dir); expect(code).toBe(EXIT_OK); }); it('skips scan for a project without detected UI assets', async () => { await silentInit(dir); const code = await refreshStyle(dir); expect(code).toBe(EXIT_OK); }); it('does not recommend Tailwind for a Flutter project', async () => { await writeFile( path.join(dir, 'pubspec.yaml'), 'name: example\ndependencies:\n flutter:\n sdk: flutter\n', 'utf-8', ); await silentInit(dir); const logs = await captureLog(() => refreshStyle(dir)); const output = logs.join('\n'); expect(output).toContain('匹配度: low'); expect(output).not.toContain('tailwind.config'); }); it('writes scanned tokens to style-tokens.json', async () => { // 创建前端项目 await writeFile( path.join(dir, 'package.json'), JSON.stringify({ name: 'test-frontend', dependencies: { react: '^18.0.0', tailwindcss: '^3.4.0', }, }), 'utf-8', ); await writeFile( path.join(dir, 'tailwind.config.js'), TAILWIND_CONFIG, 'utf-8', ); await silentInit(dir); await refreshStyle(dir); const tokensRaw = await readFile( path.join(dir, '.mancode', 'aesthetics', 'style-tokens.json'), 'utf-8', ); const tokens = JSON.parse(tokensRaw); expect(tokens.matchLevel).toBe('high'); expect(tokens.colors).toHaveProperty('primary', '#3b82f6'); expect(tokens.fonts.sans).toEqual(['Inter', 'system-ui', 'sans-serif']); expect(tokens.scopeRoot).toBe('.'); }); it('scans a repository-relative UI root without accepting traversal', async () => { await mkdir(path.join(dir, 'apps', 'web', 'src'), { recursive: true }); await writeFile( path.join(dir, 'apps', 'web', 'package.json'), JSON.stringify({ dependencies: { react: '^18.0.0', tailwindcss: '^3.4.0' }, }), 'utf8', ); await writeFile( path.join(dir, 'apps', 'web', 'tailwind.config.js'), TAILWIND_CONFIG, 'utf8', ); await silentInit(dir); expect(await refreshStyle(dir, { root: 'apps/web' })).toBe(EXIT_OK); const tokens = JSON.parse( await readFile( path.join(dir, '.mancode', 'aesthetics', 'style-tokens.json'), 'utf8', ), ); expect(tokens.scopeRoot).toBe('apps/web'); expect(await refreshStyle(dir, { root: '../outside' })).toBe( EXIT_V3_REFRESH_FAILED, ); }); it('overwrites previous tokens on re-scan', async () => { await writeFile( path.join(dir, 'package.json'), JSON.stringify({ name: 'test-frontend', dependencies: { react: '^18.0.0', tailwindcss: '^3.4.0', }, }), 'utf-8', ); await writeFile( path.join(dir, 'tailwind.config.js'), TAILWIND_CONFIG, 'utf-8', ); await silentInit(dir); // 第一次扫描 await refreshStyle(dir); const tokens1 = JSON.parse( await readFile( path.join(dir, '.mancode', 'aesthetics', 'style-tokens.json'), 'utf-8', ), ); // 修改 tailwind config await writeFile( path.join(dir, 'tailwind.config.js'), TAILWIND_CONFIG_V2, 'utf-8', ); // 第二次扫描 await refreshStyle(dir); const tokens2 = JSON.parse( await readFile( path.join(dir, '.mancode', 'aesthetics', 'style-tokens.json'), 'utf-8', ), ); expect(tokens1.colors).toHaveProperty('primary', '#3b82f6'); expect(tokens2.colors).toHaveProperty('primary', '#ff0000'); expect(tokens2.lastScanned).not.toBe(tokens1.lastScanned); }); it('warns when static non-Claude platform instructions need refresh', async () => { await writeFile( path.join(dir, 'package.json'), JSON.stringify({ name: 'test-frontend', dependencies: { react: '^18.0.0', tailwindcss: '^3.4.0', }, }), 'utf-8', ); await writeFile( path.join(dir, 'tailwind.config.js'), TAILWIND_CONFIG, 'utf-8', ); await silentInit(dir); await install(dir, 'codex'); await install(dir, 'cursor'); const logs = await captureLog(() => refreshStyle(dir)); const output = logs.join('\n'); expect(output).toContain('Non-Claude-Code platforms (codex, cursor)'); expect(output).toContain('mancode install --force'); }); it('recreates missing aesthetics directory before writing tokens', async () => { await writeFile( path.join(dir, 'package.json'), JSON.stringify({ name: 'test-frontend', dependencies: { react: '^18.0.0', tailwindcss: '^3.4.0', }, }), 'utf-8', ); await writeFile( path.join(dir, 'tailwind.config.js'), TAILWIND_CONFIG, 'utf-8', ); await silentInit(dir); await rm(path.join(dir, '.mancode', 'aesthetics'), { recursive: true, force: true, }); const code = await refreshStyle(dir); expect(code).toBe(EXIT_OK); const tokens = JSON.parse( await readFile( path.join(dir, '.mancode', 'aesthetics', 'style-tokens.json'), 'utf-8', ), ); expect(tokens.colors).toHaveProperty('primary', '#3b82f6'); }); it('clears a stale UI library while retaining detected native CSS assets', async () => { await silentInit(dir); const statePath = path.join(dir, '.mancode', 'state.json'); const state = JSON.parse(await readFile(statePath, 'utf-8')); await writeFile( statePath, `${JSON.stringify({ ...state, uiLibrary: 'Stale UI' }, null, 2)}\n`, 'utf-8', ); await writeFile(path.join(dir, 'index.html'), '
App
\n'); await writeFile( path.join(dir, 'globals.css'), ':root { --surface: #ffffff; }\n', ); await refreshStyle(dir); const refreshedState = JSON.parse(await readFile(statePath, 'utf-8')); const tokens = JSON.parse( await readFile( path.join(dir, '.mancode', 'aesthetics', 'style-tokens.json'), 'utf-8', ), ); expect(refreshedState.uiLibrary).toBe('None'); expect(tokens.uiLibrary).toBeNull(); expect(tokens.cssVariables.surface).toBe('#ffffff'); }); it('refreshes V3 project facts and keeps V3 style tokens checkout-local', async () => { await writeFile( path.join(dir, 'package.json'), JSON.stringify({ name: 'test-v3-frontend', dependencies: { react: '^18.0.0', tailwindcss: '^3.4.0' }, }), 'utf-8', ); await writeFile(path.join(dir, 'tailwind.config.js'), TAILWIND_CONFIG); await silentV3Init(dir); expect(await refreshStyle(dir)).toBe(EXIT_OK); const facts = JSON.parse( await readFile( path.join(dir, '.mancode', 'shared', 'context', 'project.json'), 'utf8', ), ); const tokens = JSON.parse( await readFile( path.join(dir, '.mancode', 'local', 'cache', 'style-tokens.json'), 'utf8', ), ); expect(facts).toMatchObject({ revision: 2, profile: { frameworks: ['React', 'Tailwind CSS'] }, }); expect(tokens.colors).toHaveProperty('primary', '#3b82f6'); await expect( readFile(path.join(dir, '.mancode', 'state.json'), 'utf8'), ).rejects.toThrow(); }); it('makes a scoped V3 UI scan applicable in design context', async () => { await mkdir(path.join(dir, 'apps', 'web'), { recursive: true }); await writeFile( path.join(dir, 'apps', 'web', 'package.json'), JSON.stringify({ dependencies: { react: '^18.0.0', tailwindcss: '^3.4.0' }, }), 'utf8', ); await writeFile( path.join(dir, 'apps', 'web', 'tailwind.config.js'), TAILWIND_CONFIG, 'utf8', ); await silentV3Init(dir); expect(await refreshStyle(dir, { root: 'apps/web' })).toBe(EXIT_OK); const logs = await captureLog(() => designContext(dir, { json: true })); expect(JSON.parse(String(logs.at(-1)))).toMatchObject({ applicable: true, reason: 'scoped_ui_assets_detected', project: { uiAssets: 'none' }, style: { scopeRoot: 'apps/web' }, }); }); }); const TAILWIND_CONFIG = `module.exports = { darkMode: 'class', theme: { extend: { colors: { primary: '#3b82f6', secondary: '#8b5cf6', }, fontFamily: { sans: ['Inter', 'system-ui', 'sans-serif'], }, }, }, }; `; const TAILWIND_CONFIG_V2 = `module.exports = { darkMode: 'class', theme: { extend: { colors: { primary: '#ff0000', accent: '#00ff00', }, fontFamily: { sans: ['Geist', 'sans-serif'], }, }, }, }; `; /** * 静默执行 init,吞掉 stdout/stderr 噪音。 */ async function silentInit(dir: string): Promise { const originalLog = console.log; const originalError = console.error; console.log = () => {}; console.error = () => {}; try { const code = await init(dir); if (code !== 0) { throw new Error(`silentInit failed: init exited with ${code}`); } } finally { console.log = originalLog; console.error = originalError; } } async function silentV3Init(dir: string): Promise { const originalLog = console.log; const originalError = console.error; console.log = () => {}; console.error = () => {}; try { const code = await init(dir, { v3: true }); if (code !== 0) { throw new Error(`silentV3Init failed: init exited with ${code}`); } } finally { console.log = originalLog; console.error = originalError; } } async function captureLog(fn: () => Promise): Promise { const originalLog = console.log; const logs: string[] = []; console.log = (...args: unknown[]) => logs.push(args.join(' ')); try { await fn(); } finally { console.log = originalLog; } return logs; }