Files
hapi/web/src/lib/clearDraftsAfterSend.test.ts
T
AnanovoandGitHub b2ec09bd0c fix(web): preserve composer attachments across session switches (#1110)
* fix(web): preserve composer attachments across session switches

Persist composer files per session and restore completed uploads without
uploading them again. Clear attachment drafts alongside text after send and
cover restoration, isolation, and adapter reuse with regression tests.

Fixes #465

* fix(web): keep cleared attachment drafts tombstoned

Retain an empty in-memory cache entry until the queued IndexedDB delete completes so a fast remount cannot restore stale files. Add regression coverage for the clear/remount race.

* fix(web): defer attachment restore for inactive sessions

Only restore or clear attachment drafts while the session attachment adapter is available. Preserve saved files when an inactive session mounts without attachment support and add regression coverage.
2026-07-24 10:55:42 +08:00

46 lines
1.8 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
vi.mock('@/lib/composer-drafts', () => ({
clearDraft: vi.fn(),
}))
vi.mock('@/lib/composer-attachment-drafts', () => ({
clearDraftAttachments: vi.fn(),
}))
import { clearDraft } from '@/lib/composer-drafts'
import { clearDraftAttachments } from '@/lib/composer-attachment-drafts'
import { clearDraftsAfterSend } from './clearDraftsAfterSend'
const mockClearDraft = vi.mocked(clearDraft)
const mockClearDraftAttachments = vi.mocked(clearDraftAttachments)
describe('clearDraftsAfterSend', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('clears the sent session draft', () => {
clearDraftsAfterSend('session-A', 'session-A')
expect(mockClearDraft).toHaveBeenCalledWith('session-A')
expect(mockClearDraft).toHaveBeenCalledTimes(1)
expect(mockClearDraftAttachments).toHaveBeenCalledWith('session-A')
})
it('clears both drafts when session was resolved to a different ID', () => {
clearDraftsAfterSend('resolved-B', 'session-A')
expect(mockClearDraft).toHaveBeenCalledWith('resolved-B')
expect(mockClearDraft).toHaveBeenCalledWith('session-A')
expect(mockClearDraft).toHaveBeenCalledTimes(2)
expect(mockClearDraftAttachments).toHaveBeenCalledWith('resolved-B')
expect(mockClearDraftAttachments).toHaveBeenCalledWith('session-A')
expect(mockClearDraftAttachments).toHaveBeenCalledTimes(2)
})
it('only clears sent session when route session is null', () => {
clearDraftsAfterSend('session-A', null)
expect(mockClearDraft).toHaveBeenCalledWith('session-A')
expect(mockClearDraft).toHaveBeenCalledTimes(1)
expect(mockClearDraftAttachments).toHaveBeenCalledWith('session-A')
})
})