POST /api/assets/generate wraps Flux/Krea generate with sticker/panel/icon presets; GET /api/assets/{id} returns status and PNG URLs using the existing poll path.
Co-authored-by: Cursor <cursoragent@cursor.com>
79 lines
3.0 KiB
JavaScript
79 lines
3.0 KiB
JavaScript
import test from 'node:test'
|
|
import assert from 'node:assert/strict'
|
|
import { readFileSync, writeFileSync, unlinkSync } from 'node:fs'
|
|
import { join } from 'node:path'
|
|
import { tmpdir } from 'node:os'
|
|
import { pathToFileURL } from 'node:url'
|
|
import ts from 'typescript'
|
|
|
|
const stub = `
|
|
export const IMAGE_V2_ENGINES = ['flux', 'krea']
|
|
export function parseImageV2Engine(raw, fallback = 'flux') {
|
|
const value = String(raw || '').trim().toLowerCase()
|
|
return IMAGE_V2_ENGINES.includes(value) ? value : fallback
|
|
}
|
|
export function clampImageV2Size(width, height) {
|
|
const sides = [768, 1024, 1280]
|
|
const snap = (raw, fallback) => {
|
|
const value = Number(raw)
|
|
if (!Number.isFinite(value) || value <= 0) return fallback
|
|
return sides.reduce((best, side) => Math.abs(side - value) < Math.abs(best - value) ? side : best, sides[0])
|
|
}
|
|
return { width: snap(width, 1024), height: snap(height, 1024), aspect: null }
|
|
}
|
|
`
|
|
const assetSrc = readFileSync(new URL('../utils/assetPack.ts', import.meta.url), 'utf8')
|
|
.replace(/from '~\/utils\/imageV2'/g, "from './imageV2-stub.mjs'")
|
|
|
|
const dir = join(tmpdir(), `asset-pack-${process.pid}`)
|
|
const { mkdirSync, rmSync } = await import('node:fs')
|
|
mkdirSync(dir, { recursive: true })
|
|
const stubPath = join(dir, 'imageV2-stub.mjs')
|
|
const assetPath = join(dir, 'assetPack.mjs')
|
|
writeFileSync(stubPath, stub)
|
|
writeFileSync(assetPath, ts.transpileModule(assetSrc, {
|
|
compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.ESNext }
|
|
}).outputText)
|
|
|
|
const {
|
|
buildAssetPackPrompt,
|
|
buildAssetPackNegative,
|
|
assetPackSize,
|
|
parseAssetPackEngine,
|
|
parseAssetPackStyle,
|
|
ASSET_PACK_DEFAULT_NEGATIVE
|
|
} = await import(pathToFileURL(assetPath).href)
|
|
rmSync(dir, { recursive: true, force: true })
|
|
|
|
test('sticker prompt expands with die-cut / transparent-friendly cues', () => {
|
|
const prompt = buildAssetPackPrompt({
|
|
prompt: 'cheerful runner mascot',
|
|
style: 'sticker',
|
|
transparentFriendly: true
|
|
})
|
|
assert.match(prompt, /die-cut sticker/i)
|
|
assert.match(prompt, /cheerful runner mascot/)
|
|
assert.match(prompt, /plain|chroma|keying/i)
|
|
assert.ok(!/Sonic|Mario|Minecraft/i.test(prompt))
|
|
})
|
|
|
|
test('default negative blocks trademarked names and NSFW', () => {
|
|
const neg = buildAssetPackNegative()
|
|
assert.equal(neg, ASSET_PACK_DEFAULT_NEGATIVE)
|
|
assert.match(neg, /Sonic/)
|
|
assert.match(neg, /Mario/)
|
|
assert.match(neg, /Minecraft/)
|
|
assert.match(neg, /NSFW/i)
|
|
assert.match(buildAssetPackNegative('extra clutter'), /extra clutter/)
|
|
})
|
|
|
|
test('engine and style defaults; sizes snap to allowed sides', () => {
|
|
assert.equal(parseAssetPackEngine(undefined), 'flux')
|
|
assert.equal(parseAssetPackEngine('krea'), 'krea')
|
|
assert.equal(parseAssetPackStyle('panel_bg'), 'panel_bg')
|
|
assert.deepEqual(assetPackSize('sticker'), { width: 1024, height: 1024, aspect: null })
|
|
assert.deepEqual(assetPackSize('icon'), { width: 768, height: 768, aspect: null })
|
|
assert.equal(assetPackSize('panel_bg').width, 1280)
|
|
assert.equal(assetPackSize('sticker', 1000, 900).width, 1024)
|
|
})
|