Skip to content

The agent that grows with you

fix(desktop): make canonical group attachments usable without submitting drafts

David Dudok de Witff08f87b532b
diff --git a/apps/desktop/src/plugins/hermes-bots/canonical-group-attachments.test.tsx b/apps/desktop/src/plugins/hermes-bots/canonical-group-attachments.test.tsx
new file mode 100644
index 00000000000..1ec9abb1565
--- /dev/null
+++ b/apps/desktop/src/plugins/hermes-bots/canonical-group-attachments.test.tsx
@@ -0,0 +1,69 @@
+import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
+import type { ComponentProps } from 'react'
+import { afterEach, expect, it, vi } from 'vitest'
+
+const request = vi.hoisted(() => vi.fn())
+vi.mock('@hermes/plugin-sdk', () => ({
+ host: { requestProfile: request },
+ Button: (props: ComponentProps<'button'>) => <button {...props} />
+}))
+vi.mock('./canonical-group-labels', () => ({
+ useCanonicalGroupLabels: () => ({ attachFiles: 'Attach files', download: 'Download',
+ removeAttachment: 'Remove', uploadFailed: 'Attachment failed' })
+}))
+
+import { CanonicalGroupAttachments } from './canonical-group-attachments'
+
+const binding = { connectionId: 'remote-owner', profile: 'reviewer', roomId: 'room' }
+const originalDesktop = window.hermesDesktop
+afterEach(() => { cleanup(); request.mockReset(); window.hermesDesktop = originalDesktop })
+
+it.each([32, 1_048_576])('uploads all %i bytes using the canonical owner method', async size => {
+ const bytes = Uint8Array.from({ length: size }, (_, index) => index % 256)
+ const file = new File([bytes], 'report.bin', { type: 'application/octet-stream' })
+ // jsdom lacks Blob.arrayBuffer; preserve its real FileReader implementation.
+ Object.defineProperty(file, 'arrayBuffer', { value: async () => bytes.buffer })
+ const uploaded = { attachment_id: 'file-one', kind: 'file', name: file.name, mime: file.type, size }
+ request.mockImplementation(async (route, method, params) => {
+ expect(route).toMatchObject({ connectionId: binding.connectionId, targetProfile: binding.profile })
+ if (method !== 'groups.attachment.upload') {throw new Error('Unknown gateway method')}
+ expect(params).toMatchObject({ profile: binding.profile, room_id: binding.roomId, name: file.name })
+ const decoded = Uint8Array.from(atob(params.data_base64), value => value.charCodeAt(0))
+ expect(Buffer.from(decoded).equals(Buffer.from(bytes))).toBe(true)
+
+ return { ...uploaded, sha256: 'receipt-digest', state: 'uploaded', created_at: 123,
+ idempotent: false, room_id: binding.roomId, authority: { gateway_id: 'owner', epoch: 1 } }
+ })
+ const changed = vi.fn()
+ const submit = vi.fn(event => event.preventDefault())
+ const view = render(<form onSubmit={submit}><CanonicalGroupAttachments attachments={[]} binding={binding} disabled={false} onChange={changed} /></form>)
+ fireEvent.click(screen.getByRole('button', { name: 'Attach files' }))
+ fireEvent.change(view.container.querySelector('input')!, { target: { files: [file] } })
+ await waitFor(() => expect(changed).toHaveBeenCalledWith([uploaded]), { timeout: 2000 })
+ expect(request).toHaveBeenCalledTimes(1)
+ expect(submit).not.toHaveBeenCalled()
+})
+
+it('downloads only the selected committed attachment from its captured owner', async () => {
+ const attachment = { attachment_id: 'file-one', event_id: 'event-one', kind: 'file', name: 'report.bin', mime: 'application/octet-stream' }
+ const save = vi.fn().mockResolvedValue(undefined)
+ window.hermesDesktop = { saveImageBuffer: save } as unknown as typeof window.hermesDesktop
+ request.mockImplementation(async (route, method, params) => {
+ expect(route).toMatchObject({ connectionId: binding.connectionId, targetProfile: binding.profile })
+ if (method !== 'groups.attachment.download') {throw new Error('Unknown gateway method')}
+ expect(params).toEqual({ profile: binding.profile, room_id: binding.roomId,
+ event_id: attachment.event_id, attachment_id: attachment.attachment_id })
+
+ return { ...attachment, data_base64: 'AAEC/w==' }
+ })
+ const submit = vi.fn(event => event.preventDefault())
+ const changed = vi.fn()
+ render(<form onSubmit={submit}><CanonicalGroupAttachments attachments={[attachment]} binding={binding} disabled={false} onChange={changed} /></form>)
+ fireEvent.click(screen.getByRole('button', { name: 'Download' }))
+ await waitFor(() => expect(save).toHaveBeenCalledWith(new Uint8Array([0, 1, 2, 255]), '.bin', attachment.name))
+ expect(request).toHaveBeenCalledTimes(1)
+ await waitFor(() => expect((screen.getByRole('button', { name: 'Remove' }) as HTMLButtonElement).disabled).toBe(false))
+ fireEvent.click(screen.getByRole('button', { name: 'Remove' }))
+ expect(changed).toHaveBeenCalledWith([])
+ expect(submit).not.toHaveBeenCalled()
+})
diff --git a/apps/desktop/src/plugins/hermes-bots/canonical-group-attachments.tsx b/apps/desktop/src/plugins/hermes-bots/canonical-group-attachments.tsx
index e96ec84377f..08165156d38 100644
--- a/apps/desktop/src/plugins/hermes-bots/canonical-group-attachments.tsx
+++ b/apps/desktop/src/plugins/hermes-bots/canonical-group-attachments.tsx
@@ -38,14 +38,21 @@ export function CanonicalGroupAttachments({ binding, attachments, onChange, disa
setBusy(true); setError('')
try {
- const data = await file.arrayBuffer()
+ const data = await new Promise<string>((resolve, reject) => {
+ const reader = new FileReader()
+ reader.onload = () => resolve(String(reader.result).split(',', 2)[1])
+ reader.onerror = () => reject(reader.error ?? new Error(labels.uploadFailed))
+ reader.readAsDataURL(file)
+ })
- const result = await canonicalGroupRequest<Attachment>(binding, 'bot.group.attachment.upload', {
+ const result = await canonicalGroupRequest<Attachment>(binding, 'groups.attachment.upload', {
room_id: binding.roomId, upload_id: crypto.randomUUID(), kind: kindFor(file), name: file.name,
- mime: file.type || 'application/octet-stream', data_base64: btoa(String.fromCharCode(...new Uint8Array(data)))
+ mime: file.type || 'application/octet-stream', data_base64: data
})
- onChange([...attachments, result])
+ // Upload receipts include storage metadata; Send accepts only the manifest.
+ const { attachment_id, kind, name, mime, size } = result
+ onChange([...attachments, { attachment_id, kind, name, mime, size }])
} catch (e) { setError(e instanceof Error ? e.message : String(e)) }
finally { setBusy(false) }
}
@@ -55,7 +62,7 @@ export function CanonicalGroupAttachments({ binding, attachments, onChange, disa
setBusy(true); setError('')
try {
- const result = await canonicalGroupRequest<DownloadedAttachment>(binding, 'bot.group.attachment.download', {
+ const result = await canonicalGroupRequest<DownloadedAttachment>(binding, 'groups.attachment.download', {
room_id: binding.roomId, event_id: attachment.event_id, attachment_id: attachment.attachment_id
})
@@ -69,10 +76,10 @@ export function CanonicalGroupAttachments({ binding, attachments, onChange, disa
<input hidden onChange={e => { const file = e.target.files?.[0];
if (file) {void upload(file);} e.currentTarget.value = '' }} ref={input} type="file" />
- <Button disabled={disabled || busy} onClick={() => input.current?.click()}>{labels.attachFiles}</Button>
+ <Button disabled={disabled || busy} onClick={() => input.current?.click()} type="button">{labels.attachFiles}</Button>
{attachments.map(a => <span className="flex items-center gap-1" key={a.attachment_id ?? a.name}>
- <span>{a.name}</span><Button disabled={disabled || busy} onClick={() => void download(a)}>{labels.download}</Button>
- <Button disabled={disabled || busy} onClick={() => onChange(attachments.filter(item => item !== a))}>{labels.removeAttachment}</Button>
+ <span>{a.name}</span><Button disabled={disabled || busy} onClick={() => void download(a)} type="button">{labels.download}</Button>
+ <Button disabled={disabled || busy} onClick={() => onChange(attachments.filter(item => item !== a))} type="button">{labels.removeAttachment}</Button>
</span>)}
{error && <span role="alert">{labels.uploadFailed}: {error}</span>}
</div>