Files
opencode-manager/frontend/src/stores/composerDraftStore.test.ts
T
ioplex dabe750920 fix: key auth rate limiting on the socket address only and clear sent drafts after unmount
- auth: drop the AUTH_TRUSTED_PROXY_HEADERS opt-in. Configured headers were
  trusted from any peer, so direct access to the published port could still
  rotate them. Better Auth now reads only the Manager-set socket address
  header; the env, compose forwarding, docs and tests are removed.
- composer: a send that completes after the composer unmounted (session
  switched mid-send) left the submitted text in the per-session draft store.
  Sends now complete through mutateAsync and clear the stored draft only if
  it still matches what was submitted, so newer edits are kept.
2026-10-23 12:49:53 -04:00

75 lines
2.8 KiB
TypeScript

import { describe, it, expect, beforeEach } from 'vitest'
import { useComposerDraftStore, type ComposerDraft } from './composerDraftStore'
const createDraft = (prompt: string): ComposerDraft => ({
prompt,
isBashMode: false,
attachedFiles: new Map(),
imageAttachments: [],
})
describe('useComposerDraftStore', () => {
beforeEach(() => {
useComposerDraftStore.setState({ drafts: {} })
})
it('clears the stored draft when it still holds the submitted content', () => {
const submitted = createDraft('sent message')
useComposerDraftStore.getState().saveDraft('session-1', submitted)
useComposerDraftStore.getState().clearDraftIfUnchanged('session-1', submitted)
expect(useComposerDraftStore.getState().drafts['session-1']).toBeUndefined()
})
it('keeps text typed after the submit', () => {
const submitted = createDraft('sent message')
useComposerDraftStore.getState().saveDraft('session-1', submitted)
const edited = createDraft('sent message and more')
useComposerDraftStore.getState().saveDraft('session-1', edited)
useComposerDraftStore.getState().clearDraftIfUnchanged('session-1', submitted)
expect(useComposerDraftStore.getState().getDraft('session-1')).toBe(edited)
})
it('keeps attachments replaced after the submit', () => {
const submitted = createDraft('sent message')
useComposerDraftStore.getState().saveDraft('session-1', submitted)
const edited = { ...submitted, attachedFiles: new Map([['file', { path: '/tmp/file', name: 'file' }]]) }
useComposerDraftStore.getState().saveDraft('session-1', edited)
useComposerDraftStore.getState().clearDraftIfUnchanged('session-1', submitted)
expect(useComposerDraftStore.getState().getDraft('session-1')).toBe(edited)
})
it('keeps the draft of another session', () => {
const submitted = createDraft('sent from session 1')
const other = createDraft('draft for session 2')
useComposerDraftStore.getState().saveDraft('session-1', submitted)
useComposerDraftStore.getState().saveDraft('session-2', other)
useComposerDraftStore.getState().clearDraftIfUnchanged('session-1', submitted)
expect(useComposerDraftStore.getState().getDraft('session-2')).toBe(other)
})
it('keeps the submitted attachments when asked to', () => {
const submitted: ComposerDraft = {
prompt: '!ls',
isBashMode: true,
attachedFiles: new Map([['file', { path: '/tmp/file', name: 'file' }]]),
imageAttachments: [],
}
useComposerDraftStore.getState().saveDraft('session-1', submitted)
useComposerDraftStore.getState().clearDraftIfUnchanged('session-1', submitted, { keepAttachments: true })
const draft = useComposerDraftStore.getState().getDraft('session-1')
expect(draft.prompt).toBe('')
expect(draft.isBashMode).toBe(false)
expect(draft.attachedFiles).toBe(submitted.attachedFiles)
})
})