|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import os from 'node:os'; |
| 4 | +import path from 'node:path'; |
| 5 | +import test from 'node:test'; |
| 6 | +import { createRequire } from 'node:module'; |
| 7 | + |
| 8 | +const require = createRequire(import.meta.url); |
| 9 | + |
| 10 | +test('detects infographic mode from payload and writes a stable output path', () => { |
| 11 | + const { detectMode } = require('../SKILLs/baoyu-image-studio/scripts/lib/promptBuilder.js'); |
| 12 | + const { buildOutputPath } = require('../SKILLs/baoyu-image-studio/scripts/lib/outputPaths.js'); |
| 13 | + |
| 14 | + const mode = detectMode({ |
| 15 | + mode: 'infographic', |
| 16 | + topic: 'UTXO 解释', |
| 17 | + bullets: ['定义', '流程', '风险'], |
| 18 | + }); |
| 19 | + const outputPath = buildOutputPath({ |
| 20 | + cwd: '/tmp/demo', |
| 21 | + mode, |
| 22 | + title: 'UTXO 解释', |
| 23 | + extension: '.png', |
| 24 | + now: () => 1700000000000, |
| 25 | + }); |
| 26 | + |
| 27 | + assert.equal(mode, 'infographic'); |
| 28 | + assert.match(outputPath, /utxo/i); |
| 29 | + assert.match(outputPath, /\.png$/); |
| 30 | + assert.match(outputPath, /baoyu-image-studio/); |
| 31 | +}); |
| 32 | + |
| 33 | +test('buildPrompt shapes infographic content with bullets and style hints', () => { |
| 34 | + const { buildPrompt } = require('../SKILLs/baoyu-image-studio/scripts/lib/promptBuilder.js'); |
| 35 | + |
| 36 | + const prompt = buildPrompt({ |
| 37 | + mode: 'infographic', |
| 38 | + topic: 'UTXO 是什么', |
| 39 | + bullets: ['定义', '交易流程', '双花风险'], |
| 40 | + style: 'clean', |
| 41 | + }); |
| 42 | + |
| 43 | + assert.match(prompt, /UTXO/); |
| 44 | + assert.match(prompt, /定义/); |
| 45 | + assert.match(prompt, /交易流程/); |
| 46 | + assert.match(prompt, /clean/i); |
| 47 | +}); |
| 48 | + |
| 49 | +test('providerResolver returns bridge providers before env-only providers', () => { |
| 50 | + const { resolveProviderConfig } = require('../SKILLs/baoyu-image-studio/scripts/lib/providerResolver.js'); |
| 51 | + |
| 52 | + const result = resolveProviderConfig({ |
| 53 | + env: { |
| 54 | + BAOYU_IMAGE_PROVIDER: 'openrouter', |
| 55 | + OPENROUTER_API_KEY: 'router-key', |
| 56 | + OPENROUTER_IMAGE_MODEL: 'google/gemini-3.1-flash-image-preview', |
| 57 | + ARK_API_KEY: 'ark-key', |
| 58 | + }, |
| 59 | + }); |
| 60 | + |
| 61 | + assert.equal(result.provider, 'openrouter'); |
| 62 | + assert.equal(result.model, 'google/gemini-3.1-flash-image-preview'); |
| 63 | +}); |
| 64 | + |
| 65 | +test('falls back to seedream when no bridge provider is configured but ARK_API_KEY exists', () => { |
| 66 | + const { resolveProviderConfig } = require('../SKILLs/baoyu-image-studio/scripts/lib/providerResolver.js'); |
| 67 | + |
| 68 | + const result = resolveProviderConfig({ |
| 69 | + env: { |
| 70 | + ARK_API_KEY: 'ark-key', |
| 71 | + SEEDREAM_IMAGE_MODEL: 'doubao-seedream-5-0-260128', |
| 72 | + }, |
| 73 | + }); |
| 74 | + |
| 75 | + assert.equal(result.provider, 'seedream'); |
| 76 | + assert.equal(result.model, 'doubao-seedream-5-0-260128'); |
| 77 | +}); |
| 78 | + |
| 79 | +test('all supported provider adapters export a generateImage function', () => { |
| 80 | + const { loadProviderAdapter } = require('../SKILLs/baoyu-image-studio/scripts/lib/providerResolver.js'); |
| 81 | + |
| 82 | + for (const providerId of ['openai', 'google', 'openrouter', 'dashscope', 'replicate', 'jimeng', 'seedream']) { |
| 83 | + const adapter = loadProviderAdapter(providerId); |
| 84 | + assert.equal(typeof adapter.generateImage, 'function', `${providerId} adapter should export generateImage`); |
| 85 | + } |
| 86 | +}); |
| 87 | + |
| 88 | +test('runWithPayload writes a local image file and returns execution summary', async () => { |
| 89 | + const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'baoyu-image-studio-test-')); |
| 90 | + const { runWithPayload } = require('../SKILLs/baoyu-image-studio/scripts/index.js'); |
| 91 | + |
| 92 | + const result = await runWithPayload( |
| 93 | + { |
| 94 | + title: 'Orange Space Cat', |
| 95 | + prompt: 'An orange cat floating in space', |
| 96 | + mode: 'cover', |
| 97 | + }, |
| 98 | + { |
| 99 | + cwd: tempRoot, |
| 100 | + env: { |
| 101 | + BAOYU_IMAGE_PROVIDER: 'openai', |
| 102 | + OPENAI_API_KEY: 'test-key', |
| 103 | + }, |
| 104 | + now: () => 1700000000000, |
| 105 | + adapters: { |
| 106 | + openai: { |
| 107 | + async generateImage() { |
| 108 | + return { |
| 109 | + bytes: Buffer.from('fake-image'), |
| 110 | + extension: '.png', |
| 111 | + mimeType: 'image/png', |
| 112 | + }; |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + ); |
| 118 | + |
| 119 | + assert.equal(result.mode, 'cover'); |
| 120 | + assert.equal(result.provider, 'openai'); |
| 121 | + assert.equal(path.extname(result.outputPath), '.png'); |
| 122 | + assert.ok(fs.existsSync(result.outputPath)); |
| 123 | + assert.equal(fs.readFileSync(result.outputPath, 'utf8'), 'fake-image'); |
| 124 | + assert.match(result.message, /orange space cat/i); |
| 125 | +}); |
| 126 | + |
| 127 | +test('runWithPayload throws an actionable error when no supported provider is available', async () => { |
| 128 | + const { runWithPayload } = require('../SKILLs/baoyu-image-studio/scripts/index.js'); |
| 129 | + |
| 130 | + await assert.rejects( |
| 131 | + () => |
| 132 | + runWithPayload( |
| 133 | + { |
| 134 | + prompt: 'Generate a cover image about UTXO', |
| 135 | + }, |
| 136 | + { |
| 137 | + cwd: '/tmp/demo', |
| 138 | + env: {}, |
| 139 | + }, |
| 140 | + ), |
| 141 | + /No supported image provider is available/i, |
| 142 | + ); |
| 143 | +}); |
0 commit comments