import test from 'node:test' import assert from 'node:assert/strict' import { createApiHandler } from '../src/host/api.js' import { setupService } from './helpers.js' test('HTTP 处理器端到端', async () => { const { service } = await setupService() const handle = createApiHandler(service) let r = await handle('list') assert.ok(r.ok) assert.deepEqual(r.value.projects, []) r = await handle('open', { path: '/api', name: '接口项目' }) assert.ok(r.ok) const id = r.value.project.id r = await handle('enable', { projectId: id }) assert.ok(r.ok) assert.equal(r.value.project.status, 'ready') r = await handle('configure', { projectId: id, seatId: 'gongbu', model: { provider: 'deepseek', model: 'deepseek-chat' }, instructions: '说明' }) assert.ok(r.ok) assert.deepEqual(r.value.seat.model, { provider: 'deepseek', model: 'deepseek-chat' }) r = await handle('project', { projectId: id }) assert.ok(r.ok) assert.equal(r.value.project.seats.length, 10) r = await handle('models') assert.ok(r.ok) assert.ok(r.value.providers.length >= 1) r = await handle('nope') assert.ok(!r.ok) assert.equal(r.error.code, 'sansheng/invalid') r = await handle('project', {}) assert.ok(!r.ok) r = await handle('project', { projectId: 'missing' }) assert.ok(!r.ok) assert.equal(r.error.code, 'sansheng/not-found') }) test('头像:字符与缩放图片入项目状态,超限与坏格式拒绝,清空恢复默认', async () => { const { service, store } = await setupService() const handle = createApiHandler(service) const opened = await handle('open', { path: '/avatar-api', name: '头像项目' }) const projectId = opened.value.project.id const char = await handle('setAvatar', { projectId, seatId: 'zhongshu', char: '中' }) assert.equal(char.ok, true) assert.equal(char.value.avatar.type, 'char') assert.equal(store.get(projectId).avatars.zhongshu.char, '中') const tiny = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==' const image = await handle('setAvatar', { projectId, seatId: 'menxia', dataUrl: tiny }) assert.equal(image.ok, true) assert.equal(image.value.avatar.type, 'image') assert.equal(store.get(projectId).avatars.menxia.mime, 'image/png') const badMime = await handle('setAvatar', { projectId, seatId: 'shangshu', dataUrl: 'data:text/plain;base64,aGVsbG8=' }) assert.equal(badMime.ok, false) assert.equal(badMime.error.code, 'sansheng/avatar-invalid') const huge = 'data:image/png;base64,' + Buffer.alloc(130 * 1024).toString('base64') const tooBig = await handle('setAvatar', { projectId, seatId: 'shangshu', dataUrl: huge }) assert.equal(tooBig.ok, false) assert.equal(tooBig.error.code, 'sansheng/avatar-invalid') const tooLongChar = await handle('setAvatar', { projectId, seatId: 'zhongshu', char: '一二三四五六七八九' }) assert.equal(tooLongChar.ok, false) assert.equal(tooLongChar.error.code, 'sansheng/avatar-invalid') const cleared = await handle('clearAvatar', { projectId, seatId: 'zhongshu' }) assert.equal(cleared.ok, true) assert.equal(store.get(projectId).avatars.zhongshu, undefined) assert.equal((await service.project(projectId)).project.avatars.menxia.type, 'image') })