diff --git a/test/commands/file/upload.test.ts b/test/commands/file/upload.test.ts index 286b0e2..bd0601d 100644 --- a/test/commands/file/upload.test.ts +++ b/test/commands/file/upload.test.ts @@ -1,4 +1,7 @@ import { describe, it, expect } from 'bun:test'; +import { mkdtempSync, rmSync, writeFileSync } from 'fs'; +import { tmpdir } from 'os'; +import { join } from 'path'; import { default as uploadCommand } from '../../../src/commands/file/upload'; const baseConfig = { @@ -27,6 +30,22 @@ const baseFlags = { async: false, }; +async function captureStdout(fn: () => Promise): Promise { + const originalWrite = process.stdout.write; + let output = ''; + process.stdout.write = ((chunk: string | Uint8Array) => { + output += typeof chunk === 'string' ? chunk : Buffer.from(chunk).toString('utf-8'); + return true; + }) as typeof process.stdout.write; + + try { + await fn(); + return output; + } finally { + process.stdout.write = originalWrite; + } +} + describe('file upload command', () => { it('has correct name', () => { expect(uploadCommand.name).toBe('file upload'); @@ -45,17 +64,22 @@ describe('file upload command', () => { }); it('shows dry-run output with file info', async () => { - let captured = ''; - const origWrite = process.stdout.write; - process.stdout.write = (chunk: any): any => { captured += String(chunk); return true; }; - - await uploadCommand.execute( - { ...baseConfig, dryRun: true }, - { ...baseFlags, dryRun: true, file: '/dev/null', purpose: 'vision' }, - ); - - process.stdout.write = origWrite; - expect(captured).toContain('/dev/null'); - expect(captured).toContain('vision'); + const tempDir = mkdtempSync(join(tmpdir(), 'mmx-upload-test-')); + const filePath = join(tempDir, 'fixture.bin'); + writeFileSync(filePath, 'fixture'); + + try { + const captured = await captureStdout(async () => { + await uploadCommand.execute( + { ...baseConfig, dryRun: true }, + { ...baseFlags, dryRun: true, file: filePath, purpose: 'vision' }, + ); + }); + + expect(captured).toContain(filePath); + expect(captured).toContain('vision'); + } finally { + rmSync(tempDir, { recursive: true, force: true }); + } }); -}); \ No newline at end of file +}); diff --git a/test/commands/music/generate.test.ts b/test/commands/music/generate.test.ts index e5d0b16..fdba485 100644 --- a/test/commands/music/generate.test.ts +++ b/test/commands/music/generate.test.ts @@ -1,4 +1,7 @@ import { describe, it, expect } from 'bun:test'; +import { mkdtempSync, rmSync, writeFileSync } from 'fs'; +import { tmpdir } from 'os'; +import { join } from 'path'; import { default as generateCommand } from '../../../src/commands/music/generate'; const baseConfig = { @@ -107,12 +110,20 @@ describe('music generate command', () => { }); it('rejects --instrumental with --lyrics-file', async () => { - await expect( - generateCommand.execute( - { ...baseConfig, dryRun: true }, - { ...baseFlags, prompt: 'Folk', instrumental: true, lyricsFile: '/dev/null' }, - ), - ).rejects.toThrow('Cannot use --instrumental with --lyrics'); + const tempDir = mkdtempSync(join(tmpdir(), 'mmx-lyrics-test-')); + const lyricsFile = join(tempDir, 'lyrics.txt'); + writeFileSync(lyricsFile, 'Hello'); + + try { + await expect( + generateCommand.execute( + { ...baseConfig, dryRun: true }, + { ...baseFlags, prompt: 'Folk', instrumental: true, lyricsFile }, + ), + ).rejects.toThrow('Cannot use --instrumental with --lyrics'); + } finally { + rmSync(tempDir, { recursive: true, force: true }); + } }); it('handles "无歌词" as instrumental', async () => {