🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
// @vitest-environment node
import { asc, eq } from 'drizzle-orm';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { getTestDB } from '../../../core/getTestDB';
import { channelMessageFiles, files } from '../../../schemas';
import type { LobeChatDatabase } from '../../../type';
import { ChannelExecutionRepository } from '..';
import { createChannelFixture, jobsOf, lease } from './testUtils';
const db: LobeChatDatabase = await getTestDB();
const fixture = createChannelFixture(db, 'channel-attachments');
const execution = new ChannelExecutionRepository(db);
beforeEach(fixture.setup);
afterEach(fixture.cleanup);
const createFile = async (name: string, fileType = 'text/plain', metadata?: object) => {
const [file] = await db
.insert(files)
.values({
fileType,
metadata,
name,
size: 1,
url: `files/${name}`,
userId: fixture.memberId,
workspaceId: fixture.workspaceId,
})
.returning();
return file.id;
};
const attachmentsOf = async (messageId: string) => {
const rows = await db
.select({ fileId: channelMessageFiles.fileId })
.from(channelMessageFiles)
.where(eq(channelMessageFiles.messageId, messageId))
.orderBy(asc(channelMessageFiles.sortOrder));
return rows.map((row) => row.fileId);
};
describe('channel message attachments', () => {
it('records attachments in the order sent and matches them on retry', async () => {
const { asMember, channel } = await fixture.createTeamChannel();
const agent = await fixture.addAgent(channel.id, 'Coder');
const [first, second] = [await createFile('b.txt'), await createFile('a.txt')];
const input = {
content: 'Review these',
fileIds: [first, second],
mentions: [agent.id],
requestKey: 'req-1',
routing: 'decision' as const,
};
const message = await asMember.send(channel.id, input);
expect(await attachmentsOf(message.id)).toEqual([first, second]);
await expect(asMember.send(channel.id, input)).resolves.toMatchObject({ id: message.id });
await expect(
asMember.send(channel.id, { ...input, fileIds: [second, first] }),
).rejects.toMatchObject({ code: 'CONFLICT' });
expect(await attachmentsOf(message.id)).toEqual([first, second]);
});
it('hands attachments to the claimed run, without files deleted since', async () => {
const { asMember, channel } = await fixture.createTeamChannel();
const agent = await fixture.addAgent(channel.id, 'Coder');
const [kept, deleted] = [await createFile('kept.txt'), await createFile('deleted.txt')];
const message = await asMember.send(channel.id, {
content: 'Review these',
fileIds: [kept, deleted],
mentions: [agent.id],
requestKey: 'req-1',
routing: 'decision',
});
await db.delete(files).where(eq(files.id, deleted));
const [job] = await jobsOf(db, message.id);
await execution.claim(channel.id, job.id, lease);
expect(await execution.input(channel.id, job.id)).toEqual([
expect.objectContaining({ fileIds: [kept], id: message.id }),
]);
});
// A message of only an image left its list row's preview blank.
it('previews a message without text by its first attachment type and count', async () => {
const { asMember, channel } = await fixture.createTeamChannel();
const [image, text] = [await createFile('shot.png', 'image/png'), await createFile('a.txt')];
await asMember.send(channel.id, {
content: '',
fileIds: [image, text],
requestKey: 'req-1',
routing: 'decision',
});
expect((await asMember.list()).find((item) => item.id === channel.id)).toMatchObject({
lastMessage: {
attachments: { count: 2, fileType: 'image/png' },
author: { id: fixture.memberId, type: 'user' },
content: '',
},
});
});
// Mobile sized an image's box only after fetching it, so the row grew and the feed jumped.
it("returns an image attachment's recorded pixel size and ThumbHash, and nothing else from its metadata", async () => {
const { asMember, channel } = await fixture.createTeamChannel();
const shot = await createFile('shot.png', 'image/png', {
filename: 'shot.png',
height: 2622,
path: 'files/private/shot.png',
ratio: 1206 / 2622,
thumbhash: 'HBkSHYSIeHiPiHh8eJd4eTN0EEQG',
width: 1206,
});
const legacy = await createFile('old.png', 'image/png', { path: 'files/old.png' });
const half = await createFile('half.png', 'image/png', { thumbhash: '<script>', width: 10 });
const text = await createFile('a.txt');
await asMember.send(channel.id, {
content: '',
fileIds: [shot, legacy, half, text],
requestKey: 'req-1',
routing: 'decision',
});
const { attachments } = await asMember.detail(channel.id);
const byId = new Map(attachments.map((file) => [file.id, file]));
expect(byId.get(shot)).toEqual({
fileType: 'image/png',
height: 2622,
id: shot,
name: 'shot.png',
size: 1,
thumbhash: 'HBkSHYSIeHiPiHh8eJd4eTN0EEQG',
url: 'files/shot.png',
width: 1206,
});
for (const id of [legacy, half, text]) {
expect(byId.get(id)).toMatchObject({ height: null, thumbhash: null, width: null });
}
});
});