83 lines
3.2 KiB
TypeScript
83 lines
3.2 KiB
TypeScript
/** @jest-environment node */
|
|
import { prisma } from '@/lib/prisma'
|
|
import { emptyCreation } from '@/lib/desktop-client/creation.mjs'
|
|
import { desktopExecutionModels } from '@/lib/desktop-client/models'
|
|
import { createCreationTask } from '@/lib/desktop-client/tasks'
|
|
|
|
jest.mock('@/lib/prisma', () => ({ prisma: { $transaction: jest.fn() } }))
|
|
jest.mock('@/lib/desktop-client/models', () => ({ desktopExecutionModels: jest.fn() }))
|
|
|
|
const auth = { id: 'session', account: { id: 'owner' } }
|
|
const projectId = 'project'
|
|
const digest = 'a'.repeat(64)
|
|
const screen = { id: 'hero', width: 1440, height: 1800 }
|
|
const creation = emptyCreation()
|
|
const document = {
|
|
...creation,
|
|
version: 2,
|
|
id: projectId,
|
|
name: '本地创作工程',
|
|
screens: [{ ...screen, layers: [] }],
|
|
plan: {
|
|
...creation.plan,
|
|
status: 'confirmed',
|
|
screens: [{ id: screen.id, name: '首屏', module: '首屏', image: true, title: '', body: '', imagePrompt: '' }],
|
|
confirmedRevision: 0,
|
|
},
|
|
}
|
|
const task = {
|
|
id: '12345678-1234-1234-1234-123456789abc',
|
|
stage: 'image',
|
|
model: {
|
|
id: 'image-model', key: 'image-model', fingerprint: digest, baseUrl: 'https://api.vip98k.com/v1',
|
|
protocol: { id: 'openai-image-generate', path: '/v1/images/generations', headers: {}, supportsImageInput: false },
|
|
},
|
|
document,
|
|
screenId: screen.id,
|
|
input: { hash: digest, prompt: '生成首屏', screenHash: digest, planHash: digest },
|
|
keyHash: digest,
|
|
}
|
|
|
|
test.each([
|
|
{ name: '已同步的创作项目', storedDocument: document },
|
|
{
|
|
name: '云端文档与任务快照不一致的轻量项目',
|
|
storedDocument: {
|
|
version: 3,
|
|
id: projectId,
|
|
name: '轻量工程',
|
|
native: { format: 'photocraft', schemaVersion: 1, documentSource: null, files: [] },
|
|
screens: [screen],
|
|
},
|
|
},
|
|
])('$name可以登记生图任务', async ({ storedDocument }) => {
|
|
;(desktopExecutionModels as jest.Mock).mockResolvedValue({ models: [{ ...task.model, stage: 'image' }] })
|
|
const query = jest.fn(async (sql: string, ...args: string[]) => {
|
|
if (sql.includes('pg_advisory_xact_lock')) return []
|
|
if (sql.startsWith('SELECT id FROM desktop_workspace_projects')) {
|
|
expect(sql).toContain('owner_id=$2 AND deleted_at IS NULL')
|
|
expect(args).toEqual([projectId, auth.account.id])
|
|
return [{ id: projectId, document: storedDocument }]
|
|
}
|
|
if (sql.startsWith('SELECT COUNT')) return [{ count: 0 }]
|
|
if (sql.startsWith('SELECT') && sql.includes('FROM desktop_creation_tasks')) return []
|
|
if (sql.startsWith('INSERT INTO desktop_creation_tasks')) {
|
|
expect(args.slice(0, 3)).toEqual([task.id, projectId, auth.account.id])
|
|
return [{ payload: JSON.parse(args[3]), revision: 1, sessionId: args[4], leaseMode: 'execute' }]
|
|
}
|
|
throw new Error(`Unexpected query: ${sql}`)
|
|
})
|
|
;(prisma.$transaction as jest.Mock).mockImplementation(action => action({
|
|
$queryRawUnsafe: query,
|
|
$executeRawUnsafe: jest.fn().mockResolvedValue(0),
|
|
}))
|
|
|
|
await expect(createCreationTask(auth, projectId, task)).resolves.toMatchObject({
|
|
task: { id: task.id, status: 'queued', document },
|
|
revision: 1,
|
|
leasedHere: true,
|
|
leaseMode: 'execute',
|
|
leaseToken: expect.any(String),
|
|
})
|
|
})
|