|
1 | 1 | import { describe, it, expect } from 'bun:test'; |
2 | 2 | import { default as generateCommand } from '../../../src/commands/image/generate'; |
3 | 3 |
|
| 4 | +const baseConfig = { |
| 5 | + apiKey: 'test-key', |
| 6 | + region: 'global' as const, |
| 7 | + baseUrl: 'https://api.mmx.io', |
| 8 | + output: 'text' as const, |
| 9 | + timeout: 10, |
| 10 | + verbose: false, |
| 11 | + quiet: false, |
| 12 | + noColor: true, |
| 13 | + yes: false, |
| 14 | + dryRun: false, |
| 15 | + nonInteractive: true, |
| 16 | + async: false, |
| 17 | +}; |
| 18 | + |
| 19 | +const baseFlags = { |
| 20 | + quiet: false, |
| 21 | + verbose: false, |
| 22 | + noColor: true, |
| 23 | + yes: false, |
| 24 | + dryRun: false, |
| 25 | + help: false, |
| 26 | + nonInteractive: true, |
| 27 | + async: false, |
| 28 | +}; |
| 29 | + |
4 | 30 | describe('image generate command', () => { |
5 | 31 | it('has correct name', () => { |
6 | 32 | expect(generateCommand.name).toBe('image generate'); |
7 | 33 | }); |
8 | 34 |
|
9 | 35 | it('requires prompt', async () => { |
10 | | - const config = { |
11 | | - apiKey: 'test-key', |
12 | | - region: 'global' as const, |
13 | | - baseUrl: 'https://api.mmx.io', |
14 | | - output: 'text' as const, |
15 | | - timeout: 10, |
16 | | - verbose: false, |
17 | | - quiet: false, |
18 | | - noColor: true, |
19 | | - yes: false, |
20 | | - dryRun: false, |
21 | | - nonInteractive: true, |
22 | | - async: false, |
23 | | - }; |
24 | | - |
25 | 36 | await expect( |
26 | | - generateCommand.execute(config, { |
27 | | - quiet: false, |
28 | | - verbose: false, |
29 | | - noColor: true, |
30 | | - yes: false, |
31 | | - dryRun: false, |
32 | | - help: false, |
33 | | - nonInteractive: true, |
34 | | - async: false, |
35 | | - }), |
| 37 | + generateCommand.execute(baseConfig, baseFlags), |
36 | 38 | ).rejects.toThrow('Missing required argument: --prompt'); |
37 | 39 | }); |
| 40 | + |
| 41 | + it('throws when width is provided without height', async () => { |
| 42 | + await expect( |
| 43 | + generateCommand.execute(baseConfig, { ...baseFlags, prompt: 'test', width: 1024 }), |
| 44 | + ).rejects.toThrow('--width requires --height'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('throws when height is provided without width', async () => { |
| 48 | + await expect( |
| 49 | + generateCommand.execute(baseConfig, { ...baseFlags, prompt: 'test', height: 1024 }), |
| 50 | + ).rejects.toThrow('--height requires --width'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('throws when width is below 512', async () => { |
| 54 | + await expect( |
| 55 | + generateCommand.execute(baseConfig, { ...baseFlags, prompt: 'test', width: 256, height: 256 }), |
| 56 | + ).rejects.toThrow('must be between 512 and 2048'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('throws when height is above 2048', async () => { |
| 60 | + await expect( |
| 61 | + generateCommand.execute(baseConfig, { ...baseFlags, prompt: 'test', width: 1024, height: 4096 }), |
| 62 | + ).rejects.toThrow('must be between 512 and 2048'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('throws when dimensions are not multiples of 8', async () => { |
| 66 | + await expect( |
| 67 | + generateCommand.execute(baseConfig, { ...baseFlags, prompt: 'test', width: 1025, height: 1024 }), |
| 68 | + ).rejects.toThrow('must be a multiple of 8'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('throws when --out is used with --n > 1', async () => { |
| 72 | + await expect( |
| 73 | + generateCommand.execute(baseConfig, { ...baseFlags, prompt: 'test', out: '/tmp/img.jpg', n: 3 }), |
| 74 | + ).rejects.toThrow('--out cannot be used with --n > 1'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('builds correct request body in dry-run', async () => { |
| 78 | + let captured = ''; |
| 79 | + const origLog = console.log; |
| 80 | + console.log = (msg: string) => { captured += msg; }; |
| 81 | + try { |
| 82 | + await generateCommand.execute( |
| 83 | + { ...baseConfig, dryRun: true, output: 'json' as const }, |
| 84 | + { ...baseFlags, dryRun: true, prompt: 'A cat', aspectRatio: '16:9', n: 2, seed: 42 }, |
| 85 | + ); |
| 86 | + } catch { /* dry-run may log or resolve */ } |
| 87 | + console.log = origLog; |
| 88 | + const parsed = JSON.parse(captured); |
| 89 | + expect(parsed.request.prompt).toBe('A cat'); |
| 90 | + expect(parsed.request.n).toBe(2); |
| 91 | + expect(parsed.request.seed).toBe(42); |
| 92 | + expect(parsed.request.model).toBe('image-01'); |
| 93 | + }); |
38 | 94 | }); |
0 commit comments