66 lines
2.6 KiB
TypeScript
66 lines
2.6 KiB
TypeScript
import assert from 'node:assert/strict'
|
|
import test from 'node:test'
|
|
import { TripoProvider, TripoProviderError, mapTripoTask } from '../src/providers/tripo.js'
|
|
|
|
function response(body: unknown, status = 200) {
|
|
return new Response(JSON.stringify(body), { status, headers: { 'Content-Type': 'application/json' } })
|
|
}
|
|
|
|
test('Tripo adapter creates a text-to-model task with official endpoint shape', async () => {
|
|
const requests: Array<{ url: string; init?: RequestInit }> = []
|
|
const provider = new TripoProvider({
|
|
apiKey: 'test-tripo-key',
|
|
fetcher: async (url, init) => {
|
|
requests.push({ url: String(url), ...(init ? { init } : {}) })
|
|
return response({ code: 0, data: { task_id: 'task_abc123' } })
|
|
},
|
|
})
|
|
const taskId = await provider.createTextToModel({ prompt: 'a low-poly wooden stool' })
|
|
|
|
assert.equal(taskId, 'task_abc123')
|
|
assert.equal(requests[0]?.url, 'https://openapi.tripo3d.com/v3/generation/text-to-model')
|
|
assert.equal(requests[0]?.init?.headers && new Headers(requests[0].init.headers).get('Authorization'), 'Bearer test-tripo-key')
|
|
assert.deepEqual(JSON.parse(String(requests[0]?.init?.body)), { model: 'v3.1-20260211', texture: true, pbr: true, prompt: 'a low-poly wooden stool' })
|
|
})
|
|
|
|
test('Tripo task response maps to MOYI task view', () => {
|
|
const mapped = mapTripoTask({
|
|
task_id: 'task_abc123',
|
|
type: 'text_to_model',
|
|
status: 'success',
|
|
progress: 100,
|
|
output: { model_url: 'https://cdn.example/model.glb', rendered_image_url: 'https://cdn.example/preview.png' },
|
|
credits_consumed: 48,
|
|
})
|
|
|
|
assert.equal(mapped.status, 'succeeded')
|
|
assert.equal(mapped.output?.modelUrl, 'https://cdn.example/model.glb')
|
|
assert.equal(mapped.creditsConsumed, 48)
|
|
})
|
|
|
|
test('Tripo adapter accepts the UUID task identifiers returned by the live v3 API', async () => {
|
|
const provider = new TripoProvider({
|
|
apiKey: 'test-tripo-key',
|
|
fetcher: async () => response({
|
|
code: 0,
|
|
data: { task_id: '950caa18-7003-4435-880d-cbb9e10ec8b6', type: 'text_to_model', status: 'running', progress: 42 },
|
|
}),
|
|
})
|
|
|
|
const task = await provider.getTask('950caa18-7003-4435-880d-cbb9e10ec8b6')
|
|
assert.equal(task.status, 'running')
|
|
assert.equal(task.progress, 42)
|
|
})
|
|
|
|
test('Tripo adapter rejects provider failures without leaking credentials', async () => {
|
|
const provider = new TripoProvider({
|
|
apiKey: 'test-tripo-key',
|
|
fetcher: async () => response({ code: 1001, message: 'invalid api key' }, 401),
|
|
})
|
|
|
|
await assert.rejects(
|
|
provider.createTextToModel({ prompt: 'test' }),
|
|
(error: unknown) => error instanceof TripoProviderError && error.statusCode === 401 && !error.message.includes('test-tripo-key'),
|
|
)
|
|
})
|