Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

test(service): add tests for attachment kinds in chat service

Steven Enamakelacab07d0fe3f
diff --git a/app/src/services/__tests__/chatService.test.ts b/app/src/services/__tests__/chatService.test.ts
index 6d89ccaa9a..c30b0c8fb3 100644
--- a/app/src/services/__tests__/chatService.test.ts
+++ b/app/src/services/__tests__/chatService.test.ts
@@ -441,6 +441,28 @@ describe('chatService.subscribeChatEvents', () => {
).rejects.toThrow('invalid chat queue acknowledgement');
});
+ it.each([
+ { kind: 'unknown_media', media: { opaqueId: 'opaque-1' } },
+ { kind: 'image', media: { opaqueId: 'opaque-1', localCachePath: '/tmp/image.png' } },
+ { kind: 'video', media: { opaqueId: 'opaque-1' }, thumbnail: { path: '/tmp/thumbnail.png' } },
+ ])('rejects queued attachment authorities or unknown kinds: $kind', async attachment => {
+ const socket = createMockSocket();
+ bindMockSocket(socket);
+ mockCallCoreRpc.mockResolvedValue({
+ queued: true,
+ queue_mode: 'followup',
+ client_id: 'socket-1',
+ thread_id: 'thread-1',
+ request_id: 'request-1',
+ queue_depth: 1,
+ queued_attachments: [attachment],
+ });
+
+ await expect(
+ chatSendWithAck({ threadId: 'thread-1', message: '', queueMode: 'followup' })
+ ).rejects.toThrow('invalid chat queue acknowledgement');
+ });
+
it('forwards speak_reply, source, session_id when provided', async () => {
const socket = createMockSocket();
bindMockSocket(socket);
diff --git a/app/src/services/chatService.ts b/app/src/services/chatService.ts
index 9a0afe4232..4bd452d7b4 100644
--- a/app/src/services/chatService.ts
+++ b/app/src/services/chatService.ts
@@ -8,6 +8,7 @@
*/
import debug from 'debug';
+import type { QueuedChatAck } from '../types/threadWire.generated';
import {
type ArtifactFailedEvent,
ArtifactKind,
@@ -55,7 +56,6 @@ import {
} from './chatWire';
import { callCoreRpc } from './coreRpcClient';
import { socketService } from './socketService';
-import type { QueuedChatAck } from '../types/threadWire.generated';
const chatLog = debug('realtime:chat');
@@ -964,17 +964,20 @@ function isRecord(value: unknown): value is Record<string, unknown> {
}
function isQueuedAttachment(value: unknown): value is ContentPart {
- if (!isRecord(value) || typeof value.kind !== 'string') return false;
- const mediaKinds = new Set(['image', 'video', 'audio', 'file', 'sticker']);
- if (!mediaKinds.has(value.kind)) return true;
+ if (!isRecord(value) || !['image', 'video', 'audio', 'file'].includes(String(value.kind))) {
+ return false;
+ }
if (!isRecord(value.media)) return false;
const media = value.media;
- return (
- typeof media.opaqueId === 'string' &&
- media.opaqueId.length > 0 &&
- media.url == null &&
- media.path == null
- );
+ const hasNoAuthority = (reference: Record<string, unknown>) =>
+ reference.url == null && reference.path == null && reference.localCachePath == null;
+ const safeMedia =
+ typeof media.opaqueId === 'string' && media.opaqueId.length > 0 && hasNoAuthority(media);
+ if (!safeMedia) return false;
+ if (value.kind === 'video' && value.thumbnail != null) {
+ return isRecord(value.thumbnail) && hasNoAuthority(value.thumbnail);
+ }
+ return true;
}
function isQueuedChatAck(value: unknown): value is QueuedChatAck {
@@ -1027,10 +1030,7 @@ export async function chatSendWithAck(params: ChatSendParams): Promise<ChatSendA
if (!isQueuedChatAck(result)) {
throw new Error('Core returned an invalid chat queue acknowledgement');
}
- return {
- requestId: result.request_id,
- queuedAttachments: result.queued_attachments,
- };
+ return { requestId: result.request_id, queuedAttachments: result.queued_attachments };
}
export async function chatSend(params: ChatSendParams): Promise<string | undefined> {