mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-24 04:47:03 +00:00
* feat(web): persist and send voice input message across session navigation (#1435)
* fix(web): address bot review findings for voice send across session switch (#1435)
* fix(web): resolve teardown, inactive session, send error recovery and mention serialization findings (#1435)
* fix(web): preserve draft initialText on error and handle async cross-session permission lookup (#1435)
* fix(web): preserve session draft on background failure and send via composer delivery policy (#1435)
* fix(web): send dictation directly via API and fix standard dictation language and recording support (#1435)
* trigger CI rerun for voice dictation session switch fixes (#1435)
* fix(web): prevent failed voice sends from clobbering replacement composer text (#1435)
* fix(web): prevent zero-byte recording failure from clobbering replacement composer draft (#1435)
* fix(web): prevent unmount voice transcription, clear draft on send success, and clear partial transcript (#1435)
* fix(web): stop button transcribes into composer without auto-sending; only send button triggers stopAndSend (#1435)
* ci: rerun CI for flaky claudeRemote test timeout (#1435)
* ci: retry flaky claudeRemote test (#1435)
* fix(web): guard dictation send path against attachments/schedule/connecting and protect newer drafts from late overwrite (#1435)
* fix(web): preserve Pi steer delivery mode for background dictation sends (#1435)
* fix(web): include active state in handleSend dependency array (#1435)
* fix(voice): surface MediaRecorder failures instead of transcribing partial audio
A MediaRecorder error can still be followed by dataavailable and stop
with partial bytes, so the previous onstop-only flow could transcribe
and auto-send corrupt audio. Track recorder.onerror and short-circuit
the stop handler: restore the draft and surface an error instead of
transcribing. Add a regression test for error -> dataavailable -> stop.
* fix(web): resume inactive sessions for voice dictation direct-sends
A voice dictation direct-send (mic -> send) posts the transcribed message
straight to the session, bypassing the text pipeline's inactive-session
resume. The hub rejects message POSTs to inactive sessions with 409
session_inactive, so sending to an archived session always failed (while
reopening it first made the same send succeed).
- stopAndSend now accepts { resolveSessionId, onSessionResolved } and
resolves the target session after transcription, before the POST
- HappyComposer allows direct voice sends on inactive sessions when a
resolver is provided (instead of downgrading to a plain stop), and
threads SessionPage's canonical resolveSessionId + handleSessionResolved
(the same ones the text send path uses) into the dictation hooks
- after the message is delivered to the resumed session, the UI
navigates/seeds the live session; failed sends keep the existing
draft-restore + error surfacing
- realtime dictation sends apply the same resolution
* fix(web): satisfy HappyChatContextValue in markdown-a test helper
Upstream 51ae260a added the required showSessionSummaryInChat field to
HappyChatContextValue but missed the markdown-a test helper, so web
typecheck (part of CI) fails at the upstream tip. Provide the field like
the sibling test helpers do.
* fix(web): expose a Send action during connected dictation
The visible composer button becomes Stop-only while dictation is
connected, so mouse/touch users could never reach the session-bound
stopAndSend path in handleSend (keyboard Enter was the only entry
point). Add an explicit Send button next to Stop when dictation is
connected; it routes through onSend('default') so the transcript is
captured to the target session and survives navigation (per #1435),
while Stop keeps its stop-without-sending semantics.
Addresses HAPI bot review on #1436.
* fix(web): hide dictation Send button when direct send is not eligible
The Send button shown during connected dictation only performs a real
send when handleSend's direct-send path runs; with attachments, a
pending schedule, or scratchlist mode it silently falls back to
dictation.toggle() (stop + transcribe). Gate the button on the same
eligibility conditions as handleSend so a visible Send control always
actually submits.
Addresses HAPI bot review (Minor) on #1436.
* fix(web): drop superseded markdown-a test context fix
Upstream merged its own showSessionSummaryInChat fix for the markdown-a
test helper; the duplicate property added for the pre-merge CI break is
no longer needed.
* fix(web): gate dictation direct-send on full attachment state and guard late navigation
- dictationDirectSendEligible now uses blocksScheduling (visible + hidden
persisted + still-hydrating attachment drafts) instead of only the
visible attachments array. An inactive composer keeps persisted
attachment blobs out of the visible array, so the previous check could
expose direct Send, resume the session, and POST only the voice text,
stranding the blobs on the obsolete source session. handleSend's
canDirectSend and the Send-button gate share one helper so they can
never disagree.
- The voice send survives unmount by design (#1435); a send that finishes
after the operator left the source session no longer navigates them
back. handleVoiceSessionResolved only navigates when still on the
source session's page.
Addresses HAPI bot review (Major + Minor) on #1436.
* fix(web): read live route state when a background voice send completes
The previous guard captured the render-time pathname; once the source
SessionPage unmounts, that captured value still points at the source
route, so a voice send that finishes after the operator left would still
navigate them back. Read router.state.location.pathname at callback time
instead (the router instance is stable; its state is always current).
Addresses HAPI bot review (Minor) on #1436.
* fix(web): recover post-resume send failures under the resumed session id
When a voice dictation send fails after the inactive session was already
resumed, the catch path persisted the retryable transcript under the
archived source session id — which the hub has superseded — so the
recovery was invisible on the live session. Save the draft under the
resumed id, and fire onSessionResolved so the operator is taken to the
live session where the retryable text lives. Applies to both standard
and realtime dictation.
Addresses HAPI bot review (Major) on #1436.
* fix(web): snapshot resumed-session draft before the post-resume send
The previous recovery guard read getDraft(targetSessionId) inside the
catch — after the send already rejected — so it compared the current
draft to itself and always overwrote text the operator typed into the
resumed composer while the request was in flight. Snapshot the resumed
session's draft at resolve time instead, so a newer draft is never
clobbered. Applies to both standard and realtime dictation.
Addresses HAPI bot review (Major) on #1436.
* fix(web): preserve both drafts when a post-resume send fails
When the operator typed a newer draft into the resumed composer while
the voice send was in flight and the send then failed, the recovery
guard correctly refused to overwrite the newer draft — but that dropped
the failed voice text entirely. Merge instead: append the failed message
after the newer draft so both remain recoverable. Applies to both
standard and realtime dictation.
Addresses HAPI bot review (Major) on #1436.
* fix(web): merge live composer text into failed voice-send recovery
Failure recovery previously read only the persisted draft store, but
while the composer is mounted its edits live in memory (session storage
is written only on unmount). If the operator typed replacement text
while transcription or delivery was pending, recovery either skipped the
composer restore (persisting only the voice text, which the later unmount
then overwrote) or clobbered the replacement. Recovery now reads the live
composer text when mounted and appends the failed voice text after it,
reseating the composer to the merged draft; the unmounted case merges a
persisted draft written since the snapshot. Applies to recording,
zero-byte, transcription, send, and realtime failure paths in both
dictation implementations.
Addresses HAPI bot review (Major) on #1436.
* fix(web): preserve dictation audio constraints
* fix(web): transfer follow-up drafts after voice resume
* fix(web): merge source drafts on failed voice resume
* Preserve resumed voice drafts and replace recovered composer text
Sample destination/source draft merges at persistence time; use a replacement callback for realtime recovery. Six regressions fail before the fix; 66 focused tests, all five package suites, typecheck and 16 Playwright checks pass.
* Recover delayed voice failures into the current composer mount
Resolve live recovery targets by session identity and update the draft ref synchronously before later unmount. Both standard/realtime remount regressions fail before the fix. 85 focused tests, full package suites, typecheck and 16 Playwright checks pass.
* fix: preserve live composer drafts after background voice success
Use shared transfer sampling for standard and realtime sends; update the mounted destination before navigation and unmount. Verified focused voice/transfer tests, typecheck, all five full package suites (CLI rerun serially), and 16 browser checks.
* fix: retain resumed voice follow-ups and destination attachments
Verified focused regressions, root typecheck, all five complete package suites (environment-sensitive failures rerun serially), and 16 browser checks.
* fix: preserve independent destination drafts during voice recovery
Cross-session recovery always retains destination text and independently merges source follow-ups. Remove destination baseline replacement from both hooks. Verified 72 focused regressions, root typecheck, all five full package suites (Hub rerun serially), and 16 browser checks.
* fix: preserve voice recovery drafts before composer hydration
Expose persisted draft text until hydration and update the text ref synchronously when restoring. Cover both voice modes with a held animation frame and intentional empty text after hydration. Verified 94 focused checks, full package suites, root typecheck, and 16 browser checks.
* fix(web): retain ElevenLabs finalization across navigation
* fix(web): preserve new same-text drafts across pending voice sends
* fix(web): refresh voice send callback after draft hydration
* fix(web): resolve voice destinations from current session activity
* fix(web): give voice direct-sends a localId so agent echoes dedupe
Voice dictation, realtime dictation and the cross-session voice tool sent
through api.sendMessage with localId null. Shared Codex then minted a
random clientUserMessageId for the turn and echoed the user message back
with it; the hub dedupes only on (session, localId), so every voice
message was stored and shown twice.
Route all voice sends through sendVoiceMessage, which mints the same
local-* id useSendMessage uses.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1035 lines
42 KiB
TypeScript
1035 lines
42 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
getDraft: vi.fn(),
|
|
saveDraft: vi.fn(),
|
|
clearDraft: vi.fn(),
|
|
getDraftAttachments: vi.fn(),
|
|
getRestoredUploadMetadata: vi.fn(),
|
|
saveDraftAttachments: vi.fn(),
|
|
moveDraftAttachments: vi.fn(async (
|
|
_source: string,
|
|
_target: string,
|
|
resolveAttachments: () => Array<{ id: string; file: File }>,
|
|
) => resolveAttachments()),
|
|
}))
|
|
|
|
vi.mock('@/lib/composer-drafts', () => ({
|
|
getDraft: mocks.getDraft,
|
|
saveDraft: mocks.saveDraft,
|
|
clearDraft: mocks.clearDraft,
|
|
}))
|
|
vi.mock('@/lib/composer-attachment-drafts', () => ({
|
|
getDraftAttachments: mocks.getDraftAttachments,
|
|
getRestoredUploadMetadata: mocks.getRestoredUploadMetadata,
|
|
saveDraftAttachments: mocks.saveDraftAttachments,
|
|
moveDraftAttachments: mocks.moveDraftAttachments,
|
|
}))
|
|
|
|
import {
|
|
attachmentDraftRevision,
|
|
clearComposerDraftSnapshot,
|
|
composerDraftWasHandedOff,
|
|
forgetComposerDraftHandoff,
|
|
handoffComposerDraft,
|
|
persistInactiveComposerAttachments,
|
|
setComposerDraftSnapshot,
|
|
transferComposerDraft,
|
|
transferComposerDraftThenNavigate,
|
|
updateComposerDraftTextSnapshot,
|
|
} from './composer-draft-transfer'
|
|
|
|
function resetSession(sessionId: string): void {
|
|
clearComposerDraftSnapshot(sessionId)
|
|
forgetComposerDraftHandoff(sessionId)
|
|
}
|
|
|
|
function resetMoveDraftMock(): void {
|
|
mocks.moveDraftAttachments.mockReset()
|
|
mocks.moveDraftAttachments.mockImplementation(async (
|
|
_source: string,
|
|
_target: string,
|
|
resolveAttachments: () => Array<{ id: string; file: File }>,
|
|
) => resolveAttachments())
|
|
}
|
|
|
|
function expectMovedAttachments(
|
|
sourceSessionId: string,
|
|
targetSessionId: string,
|
|
expected: unknown,
|
|
): void {
|
|
expect(mocks.moveDraftAttachments).toHaveBeenCalledWith(
|
|
sourceSessionId,
|
|
targetSessionId,
|
|
expect.any(Function),
|
|
)
|
|
const resolve = mocks.moveDraftAttachments.mock.calls.find(
|
|
(call) => call[0] === sourceSessionId && call[1] === targetSessionId,
|
|
)?.[2] as (() => unknown) | undefined
|
|
expect(resolve?.()).toEqual(expected)
|
|
}
|
|
|
|
describe('transferComposerDraft', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
resetMoveDraftMock()
|
|
resetSession('old-live')
|
|
resetSession('new-live')
|
|
resetSession('old-stored')
|
|
resetSession('new-stored')
|
|
resetSession('old-empty')
|
|
resetSession('new-empty')
|
|
resetSession('old-pending')
|
|
resetSession('new-pending')
|
|
resetSession('source-a')
|
|
resetSession('target-a')
|
|
})
|
|
|
|
it('preserves target upload metadata while transferring source attachments', async () => {
|
|
const source = new File(['source'], 'source.txt')
|
|
const target = new File(['target'], 'target.txt')
|
|
setComposerDraftSnapshot('source-a', 'source', [{ id: 'source-file', file: source, path: '/old/path', uploadSessionId: 'source-a' }])
|
|
mocks.getDraftAttachments.mockResolvedValueOnce([target])
|
|
mocks.getRestoredUploadMetadata.mockReturnValueOnce({ id: 'target-file', path: '/target/path', uploadSessionId: 'target-a' })
|
|
await transferComposerDraft('source-a', 'target-a', [], { preserveTargetAttachments: true })
|
|
expectMovedAttachments('source-a', 'target-a', [
|
|
{ id: 'target-file', file: target, path: '/target/path', uploadSessionId: 'target-a', previewUrl: undefined },
|
|
{ id: 'source-file', file: source, path: undefined, uploadSessionId: undefined },
|
|
])
|
|
})
|
|
|
|
it('does not move attachments when reading the preserved target fails', async () => {
|
|
setComposerDraftSnapshot('source-a', 'source', [])
|
|
mocks.getDraftAttachments.mockRejectedValueOnce(new Error('storage unavailable'))
|
|
await expect(transferComposerDraft('source-a', 'target-a', [], { preserveTargetAttachments: true }))
|
|
.rejects.toThrow('storage unavailable')
|
|
expect(mocks.moveDraftAttachments).not.toHaveBeenCalled()
|
|
expect(composerDraftWasHandedOff('source-a')).toBe(false)
|
|
})
|
|
|
|
it('prefers the live composer snapshot when reopening the visible session', async () => {
|
|
const file = new File(['draft'], 'draft.txt')
|
|
setComposerDraftSnapshot('old-live', 'latest text', [{ id: 'a1', file }])
|
|
|
|
await transferComposerDraft('old-live', 'new-live')
|
|
|
|
expect(mocks.saveDraft).toHaveBeenCalledWith('new-live', 'latest text')
|
|
expectMovedAttachments('old-live', 'new-live', [{ id: 'a1', file }])
|
|
expect(mocks.clearDraft).toHaveBeenCalledWith('old-live')
|
|
expect(mocks.saveDraftAttachments).not.toHaveBeenCalled()
|
|
expect(mocks.getDraftAttachments).not.toHaveBeenCalled()
|
|
expect(composerDraftWasHandedOff('old-live')).toBe(true)
|
|
})
|
|
|
|
it('drops session-scoped upload metadata for a session-list reopen', async () => {
|
|
const file = new File(['draft'], 'draft.txt')
|
|
mocks.getDraft.mockReturnValue('persisted text')
|
|
mocks.getDraftAttachments.mockResolvedValue([file])
|
|
mocks.getRestoredUploadMetadata.mockReturnValue({
|
|
id: 'uploaded-1',
|
|
path: '/tmp/uploaded-1',
|
|
previewUrl: 'blob:preview',
|
|
uploadSessionId: 'old-stored',
|
|
})
|
|
|
|
await transferComposerDraft('old-stored', 'new-stored')
|
|
|
|
expect(mocks.saveDraft).toHaveBeenCalledWith('new-stored', 'persisted text')
|
|
expectMovedAttachments('old-stored', 'new-stored', [{
|
|
id: 'uploaded-1',
|
|
file,
|
|
path: undefined,
|
|
previewUrl: undefined,
|
|
uploadSessionId: undefined,
|
|
}])
|
|
expect(mocks.clearDraft).toHaveBeenCalledWith('old-stored')
|
|
})
|
|
|
|
it('does not resurrect persisted text when the live composer is empty', async () => {
|
|
mocks.getDraft.mockReturnValue('stale persisted text')
|
|
setComposerDraftSnapshot('old-empty', '', [])
|
|
|
|
await transferComposerDraft('old-empty', 'new-empty')
|
|
|
|
expect(mocks.saveDraft).toHaveBeenCalledWith('new-empty', '')
|
|
expectMovedAttachments('old-empty', 'new-empty', [])
|
|
})
|
|
|
|
it('falls back to persisted attachments after an inactive empty live snapshot is cleared', async () => {
|
|
const file = new File(['kept'], 'kept.txt')
|
|
mocks.getDraft.mockReturnValue('typed while inactive')
|
|
mocks.getDraftAttachments.mockResolvedValue([file])
|
|
mocks.getRestoredUploadMetadata.mockReturnValue({
|
|
id: 'kept-1',
|
|
path: '/tmp/kept',
|
|
uploadSessionId: 'old-empty',
|
|
})
|
|
setComposerDraftSnapshot('old-empty', 'typed while inactive', [])
|
|
clearComposerDraftSnapshot('old-empty')
|
|
|
|
await transferComposerDraft('old-empty', 'new-empty')
|
|
|
|
expect(mocks.saveDraft).toHaveBeenCalledWith('new-empty', 'typed while inactive')
|
|
expectMovedAttachments('old-empty', 'new-empty', [{
|
|
id: 'kept-1',
|
|
file,
|
|
path: undefined,
|
|
previewUrl: undefined,
|
|
uploadSessionId: undefined,
|
|
}])
|
|
})
|
|
|
|
it('merges an in-flight pending attachment that is not in the live snapshot yet', async () => {
|
|
const existing = new File(['old'], 'old.txt')
|
|
const pending = new File(['new'], 'new.txt')
|
|
setComposerDraftSnapshot('old-pending', 'typed', [{ id: 'a1', file: existing }])
|
|
|
|
await transferComposerDraft('old-pending', 'new-pending', [{
|
|
id: 'a2',
|
|
file: pending,
|
|
previewUrl: 'data:text/plain;base64,bmV3',
|
|
}])
|
|
|
|
expectMovedAttachments('old-pending', 'new-pending', [
|
|
{ id: 'a1', file: existing },
|
|
{
|
|
id: 'a2',
|
|
file: pending,
|
|
previewUrl: 'data:text/plain;base64,bmV3',
|
|
path: undefined,
|
|
uploadSessionId: undefined,
|
|
},
|
|
])
|
|
})
|
|
|
|
it('does not let a previously visited target snapshot replace the source draft', async () => {
|
|
const sourceFile = new File(['source'], 'source.txt')
|
|
const staleTargetFile = new File(['stale'], 'stale.txt')
|
|
mocks.getDraft.mockImplementation((sessionId: string) => (
|
|
sessionId === 'old-stored' ? 'source text' : 'stale target text'
|
|
))
|
|
mocks.getDraftAttachments.mockImplementation(async (sessionId: string) => (
|
|
sessionId === 'old-stored' ? [sourceFile] : [staleTargetFile]
|
|
))
|
|
mocks.getRestoredUploadMetadata.mockImplementation((file: File) => (
|
|
file === sourceFile
|
|
? { id: 'source-1', path: '/tmp/source', uploadSessionId: 'old-stored' }
|
|
: { id: 'stale-1', path: '/tmp/stale', uploadSessionId: 'new-stored' }
|
|
))
|
|
setComposerDraftSnapshot('new-stored', 'visited earlier', [{ id: 'stale-1', file: staleTargetFile }])
|
|
|
|
await transferComposerDraft('old-stored', 'new-stored')
|
|
|
|
expect(mocks.saveDraft).toHaveBeenCalledWith('new-stored', 'source text')
|
|
expectMovedAttachments('old-stored', 'new-stored', [{
|
|
id: 'source-1',
|
|
file: sourceFile,
|
|
path: undefined,
|
|
previewUrl: undefined,
|
|
uploadSessionId: undefined,
|
|
}])
|
|
})
|
|
|
|
it('blocks source re-persist after a durable handoff', async () => {
|
|
const file = new File(['draft'], 'draft.txt')
|
|
setComposerDraftSnapshot('old-live', 'latest text', [{ id: 'a1', file }])
|
|
|
|
await transferComposerDraft('old-live', 'new-live')
|
|
await persistInactiveComposerAttachments('old-live', 'resurrect?', [{ id: 'a1', file }])
|
|
|
|
expect(composerDraftWasHandedOff('old-live')).toBe(true)
|
|
expect(mocks.saveDraftAttachments).not.toHaveBeenCalled()
|
|
})
|
|
})
|
|
|
|
describe('handoffComposerDraft', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
resetMoveDraftMock()
|
|
resetSession('source-a')
|
|
resetSession('target-a')
|
|
mocks.getDraft.mockReturnValue('hello')
|
|
mocks.getDraftAttachments.mockResolvedValue([])
|
|
})
|
|
|
|
it('passes every concurrent in-flight file into one navigable handoff', async () => {
|
|
const file1 = new File(['one'], 'one.txt')
|
|
const file2 = new File(['two'], 'two.txt')
|
|
const onNavigable = vi.fn().mockResolvedValue(undefined)
|
|
|
|
const first = handoffComposerDraft('source-a', 'target-a', { id: 'p1', file: file1 }, onNavigable)
|
|
const second = handoffComposerDraft('source-a', 'target-a', { id: 'p2', file: file2 }, onNavigable)
|
|
|
|
await Promise.all([first, second])
|
|
|
|
expect(onNavigable).toHaveBeenCalledOnce()
|
|
expect(onNavigable).toHaveBeenCalledWith('target-a')
|
|
const resolve = mocks.moveDraftAttachments.mock.calls.at(-1)?.[2] as (() => Array<{ id: string }>)
|
|
expect(resolve().map((item) => item.id).sort()).toEqual(['p1', 'p2'])
|
|
})
|
|
|
|
it('appends a staggered file onto the target after the first handoff completes', async () => {
|
|
const file1 = new File(['one'], 'one.txt')
|
|
const file2 = new File(['two'], 'two.txt')
|
|
const onNavigable = vi.fn().mockResolvedValue(undefined)
|
|
|
|
await handoffComposerDraft('source-a', 'target-a', { id: 'p1', file: file1 }, onNavigable)
|
|
await handoffComposerDraft('source-a', 'target-a', { id: 'p2', file: file2 }, onNavigable)
|
|
|
|
expect(onNavigable).toHaveBeenCalledOnce()
|
|
const savedAttachments = mocks.saveDraftAttachments.mock.calls.at(-1)?.[1] as Array<{ id: string }>
|
|
expect(savedAttachments.map((item) => item.id).sort()).toEqual(['p1', 'p2'])
|
|
})
|
|
|
|
it('keeps upload metadata when appending onto the same target session', async () => {
|
|
const uploaded = new File(['one'], 'one.txt')
|
|
const late = new File(['two'], 'two.txt')
|
|
const onNavigable = vi.fn().mockResolvedValue(undefined)
|
|
|
|
// First handoff establishes source→target mapping.
|
|
await handoffComposerDraft('source-a', 'target-a', { id: 'p1', file: uploaded }, onNavigable)
|
|
// Simulate the target composer having finished uploading p1.
|
|
setComposerDraftSnapshot('target-a', 'hello', [{
|
|
id: 'p1',
|
|
file: uploaded,
|
|
path: '/uploads/one.txt',
|
|
uploadSessionId: 'target-a',
|
|
}])
|
|
await handoffComposerDraft('source-a', 'target-a', { id: 'p2', file: late }, onNavigable)
|
|
|
|
const savedAttachments = mocks.saveDraftAttachments.mock.calls.at(-1)?.[1] as Array<{
|
|
id: string
|
|
path?: string
|
|
uploadSessionId?: string
|
|
}>
|
|
expect(savedAttachments).toEqual(expect.arrayContaining([
|
|
expect.objectContaining({
|
|
id: 'p1',
|
|
path: '/uploads/one.txt',
|
|
uploadSessionId: 'target-a',
|
|
}),
|
|
expect.objectContaining({ id: 'p2', file: late }),
|
|
]))
|
|
})
|
|
|
|
it('drops a cancelled pending id from the source snapshot at save time', async () => {
|
|
const kept = new File(['kept'], 'kept.txt')
|
|
const cancelled = new File(['gone'], 'gone.txt')
|
|
setComposerDraftSnapshot('source-a', 'typed', [
|
|
{ id: 'kept-1', file: kept },
|
|
{ id: 'gone-1', file: cancelled },
|
|
])
|
|
const onNavigable = vi.fn().mockResolvedValue(undefined)
|
|
|
|
await handoffComposerDraft(
|
|
'source-a',
|
|
'target-a',
|
|
{
|
|
id: 'gone-1',
|
|
file: cancelled,
|
|
isCancelled: () => true,
|
|
},
|
|
onNavigable,
|
|
)
|
|
|
|
expect(onNavigable).toHaveBeenCalledOnce()
|
|
expectMovedAttachments('source-a', 'target-a', [
|
|
expect.objectContaining({ id: 'kept-1', file: kept }),
|
|
])
|
|
})
|
|
|
|
it('re-samples isCancelled after an async gap before writing the transfer', async () => {
|
|
const kept = new File(['kept'], 'kept.txt')
|
|
const cancelled = new File(['gone'], 'gone.txt')
|
|
setComposerDraftSnapshot('source-a', 'typed', [
|
|
{ id: 'kept-1', file: kept },
|
|
{ id: 'gone-1', file: cancelled },
|
|
])
|
|
let cancelledNow = false
|
|
const onNavigable = vi.fn().mockResolvedValue(undefined)
|
|
|
|
const handoff = handoffComposerDraft(
|
|
'source-a',
|
|
'target-a',
|
|
{
|
|
id: 'gone-1',
|
|
file: cancelled,
|
|
isCancelled: () => cancelledNow,
|
|
},
|
|
onNavigable,
|
|
)
|
|
// Cancel after enqueue, before the handoff's setTimeout(0) transfer samples.
|
|
cancelledNow = true
|
|
await handoff
|
|
|
|
expect(onNavigable).toHaveBeenCalledOnce()
|
|
expectMovedAttachments('source-a', 'target-a', [
|
|
expect.objectContaining({ id: 'kept-1', file: kept }),
|
|
])
|
|
})
|
|
|
|
it('re-samples cancellation after the mocked draft-write drain', async () => {
|
|
const kept = new File(['kept'], 'kept.txt')
|
|
const cancelled = new File(['gone'], 'gone.txt')
|
|
setComposerDraftSnapshot('source-a', 'typed', [
|
|
{ id: 'kept-1', file: kept },
|
|
{ id: 'gone-1', file: cancelled },
|
|
])
|
|
let cancelledNow = false
|
|
let releaseDrain!: () => void
|
|
const drainGate = new Promise<void>((resolve) => {
|
|
releaseDrain = resolve
|
|
})
|
|
mocks.moveDraftAttachments.mockImplementation(async (
|
|
_source: string,
|
|
_target: string,
|
|
resolveAttachments: () => Array<{ id: string; file: File }>,
|
|
) => {
|
|
await drainGate
|
|
return resolveAttachments()
|
|
})
|
|
|
|
const transfer = transferComposerDraft('source-a', 'target-a', [{
|
|
id: 'gone-1',
|
|
file: cancelled,
|
|
isCancelled: () => cancelledNow,
|
|
}])
|
|
cancelledNow = true
|
|
releaseDrain()
|
|
await transfer
|
|
|
|
expectMovedAttachments('source-a', 'target-a', [
|
|
expect.objectContaining({ id: 'kept-1', file: kept }),
|
|
])
|
|
expect(composerDraftWasHandedOff('source-a')).toBe(true)
|
|
})
|
|
|
|
it('re-samples composer text after the awaited attachment move', async () => {
|
|
const file = new File(['draft'], 'draft.txt')
|
|
setComposerDraftSnapshot('source-a', 'before wait', [{ id: 'a1', file }])
|
|
let releaseDrain!: () => void
|
|
const drainGate = new Promise<void>((resolve) => {
|
|
releaseDrain = resolve
|
|
})
|
|
mocks.moveDraftAttachments.mockImplementation(async (
|
|
_source: string,
|
|
_target: string,
|
|
resolveAttachments: () => Array<{ id: string; file: File }>,
|
|
) => {
|
|
await drainGate
|
|
return resolveAttachments()
|
|
})
|
|
|
|
const transfer = transferComposerDraft('source-a', 'target-a')
|
|
updateComposerDraftTextSnapshot('source-a', 'typed during wait')
|
|
releaseDrain()
|
|
await transfer
|
|
|
|
expect(mocks.saveDraft).toHaveBeenCalledWith('target-a', 'typed during wait')
|
|
expect(composerDraftWasHandedOff('source-a')).toBe(true)
|
|
})
|
|
|
|
it('prefers the send-time textOverride over a cleared inactive draft', async () => {
|
|
const file = new File(['draft'], 'draft.txt')
|
|
setComposerDraftSnapshot('source-a', 'submitted hello', [{ id: 'a1', file }])
|
|
let releaseDrain!: () => void
|
|
const drainGate = new Promise<void>((resolve) => {
|
|
releaseDrain = resolve
|
|
})
|
|
mocks.moveDraftAttachments.mockImplementation(async (
|
|
_source: string,
|
|
_target: string,
|
|
resolveAttachments: () => Array<{ id: string; file: File }>,
|
|
) => {
|
|
await drainGate
|
|
return resolveAttachments()
|
|
})
|
|
|
|
const transfer = transferComposerDraft(
|
|
'source-a',
|
|
'target-a',
|
|
[],
|
|
{ textOverride: 'submitted hello' },
|
|
)
|
|
// assistant-ui cleared the composer while resumeSession awaited.
|
|
updateComposerDraftTextSnapshot('source-a', '')
|
|
releaseDrain()
|
|
await transfer
|
|
|
|
expect(mocks.saveDraft).toHaveBeenCalledWith('target-a', 'submitted hello')
|
|
expect(composerDraftWasHandedOff('source-a')).toBe(true)
|
|
})
|
|
|
|
it('samples text overrides after the attachment transfer completes', async () => {
|
|
setComposerDraftSnapshot('source-a', 'source follow-up', [])
|
|
let targetDraft = 'old destination'
|
|
let releaseDrain!: () => void
|
|
const drainGate = new Promise<void>(resolve => { releaseDrain = resolve })
|
|
mocks.moveDraftAttachments.mockImplementation(async (
|
|
_source: string,
|
|
_target: string,
|
|
resolveAttachments: () => Array<{ id: string; file: File }>,
|
|
) => {
|
|
await drainGate
|
|
return resolveAttachments()
|
|
})
|
|
const transfer = transferComposerDraft('source-a', 'target-a', [], {
|
|
textOverride: sampled => `${targetDraft} ${sampled}`,
|
|
})
|
|
targetDraft = 'latest destination'
|
|
releaseDrain()
|
|
await transfer
|
|
expect(mocks.saveDraft).toHaveBeenCalledWith('target-a', 'latest destination source follow-up')
|
|
})
|
|
|
|
it('applies textOverride on same-id resume when attachments stay put', async () => {
|
|
mocks.getDraft.mockReturnValue('')
|
|
await transferComposerDraft(
|
|
'session-same',
|
|
'session-same',
|
|
[],
|
|
{ textOverride: 'submitted hello' },
|
|
)
|
|
expect(mocks.saveDraft).toHaveBeenCalledWith('session-same', 'submitted hello')
|
|
expect(mocks.moveDraftAttachments).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('keeps IndexedDB-only attachments when typing during a blocked move', async () => {
|
|
const stored = new File(['kept'], 'kept.txt')
|
|
mocks.getDraft.mockReturnValue('before')
|
|
mocks.getDraftAttachments.mockResolvedValue([stored])
|
|
mocks.getRestoredUploadMetadata.mockReturnValue({ id: 'stored-1' })
|
|
let releaseDrain!: () => void
|
|
const drainGate = new Promise<void>((resolve) => {
|
|
releaseDrain = resolve
|
|
})
|
|
let movedDuringDrain: Array<{ id: string; file: File }> | undefined
|
|
mocks.moveDraftAttachments.mockImplementation(async (
|
|
source: string,
|
|
target: string,
|
|
resolveAttachments: () => Array<{ id: string; file: File }>,
|
|
) => {
|
|
if (source === target) {
|
|
return resolveAttachments()
|
|
}
|
|
await drainGate
|
|
movedDuringDrain = resolveAttachments()
|
|
return movedDuringDrain
|
|
})
|
|
|
|
const transfer = transferComposerDraft('source-a', 'target-a')
|
|
// Let transfer install the barrier and enter the blocked move.
|
|
await Promise.resolve()
|
|
await Promise.resolve()
|
|
updateComposerDraftTextSnapshot('source-a', 'typed during wait')
|
|
releaseDrain()
|
|
await transfer
|
|
|
|
expect(mocks.saveDraft).toHaveBeenCalledWith('target-a', 'typed during wait')
|
|
expect(movedDuringDrain).toEqual([
|
|
expect.objectContaining({ id: 'stored-1', file: stored }),
|
|
])
|
|
expect(composerDraftWasHandedOff('source-a')).toBe(true)
|
|
})
|
|
|
|
it('drops attachments cancelled while the durable move is in flight', async () => {
|
|
const kept = new File(['kept'], 'kept.txt')
|
|
const doomed = new File(['doomed'], 'doomed.txt')
|
|
setComposerDraftSnapshot('source-a', 'typed', [{ id: 'kept-1', file: kept }])
|
|
let cancelled = false
|
|
let releaseMove!: () => void
|
|
const moveGate = new Promise<void>((resolve) => {
|
|
releaseMove = resolve
|
|
})
|
|
mocks.moveDraftAttachments.mockImplementation(async (
|
|
source: string,
|
|
target: string,
|
|
resolveAttachments: () => Array<{ id: string; file: File }>,
|
|
) => {
|
|
if (source === target) {
|
|
return resolveAttachments()
|
|
}
|
|
const resolved = resolveAttachments()
|
|
await moveGate
|
|
return resolved
|
|
})
|
|
|
|
const transfer = transferComposerDraft('source-a', 'target-a', [
|
|
{ id: 'doomed-1', file: doomed, isCancelled: () => cancelled },
|
|
])
|
|
await Promise.resolve()
|
|
await Promise.resolve()
|
|
cancelled = true
|
|
releaseMove()
|
|
await transfer
|
|
|
|
const correctiveResolvers = mocks.moveDraftAttachments.mock.calls
|
|
.filter((call) => call[0] === call[1])
|
|
.map((call) => call[2] as () => Array<{ id: string }>)
|
|
expect(correctiveResolvers.length).toBeGreaterThanOrEqual(1)
|
|
expect(correctiveResolvers.at(-1)?.().map((item) => item.id)).toEqual(['kept-1'])
|
|
expect(composerDraftWasHandedOff('source-a')).toBe(true)
|
|
})
|
|
|
|
it('stabilizes multi-file cancellation across the corrective write', async () => {
|
|
const fileA = new File(['a'], 'a.txt')
|
|
const fileB = new File(['b'], 'b.txt')
|
|
setComposerDraftSnapshot('source-a', 'typed', [])
|
|
let cancelA = false
|
|
let cancelB = false
|
|
let correctiveCalls = 0
|
|
let releaseCorrective!: () => void
|
|
const correctiveGate = new Promise<void>((resolve) => {
|
|
releaseCorrective = resolve
|
|
})
|
|
mocks.moveDraftAttachments.mockImplementation(async (
|
|
source: string,
|
|
target: string,
|
|
resolveAttachments: () => Array<{ id: string; file: File }>,
|
|
) => {
|
|
if (source === target) {
|
|
correctiveCalls += 1
|
|
if (correctiveCalls === 1) {
|
|
await correctiveGate
|
|
}
|
|
return resolveAttachments()
|
|
}
|
|
const resolved = resolveAttachments()
|
|
cancelA = true
|
|
return resolved
|
|
})
|
|
|
|
const transfer = transferComposerDraft('source-a', 'target-a', [
|
|
{ id: 'a1', file: fileA, isCancelled: () => cancelA },
|
|
{ id: 'b1', file: fileB, isCancelled: () => cancelB },
|
|
])
|
|
await Promise.resolve()
|
|
await Promise.resolve()
|
|
// First corrective is blocked; cancel the sibling mid-write.
|
|
cancelB = true
|
|
releaseCorrective()
|
|
await transfer
|
|
|
|
expect(correctiveCalls).toBeGreaterThanOrEqual(2)
|
|
const lastCorrective = mocks.moveDraftAttachments.mock.calls
|
|
.filter((call) => call[0] === call[1])
|
|
.at(-1)?.[2] as (() => Array<{ id: string }>) | undefined
|
|
expect(lastCorrective?.().map((item) => item.id)).toEqual([])
|
|
expect(composerDraftWasHandedOff('source-a')).toBe(true)
|
|
})
|
|
|
|
it('does not move when the source IndexedDB attachment read fails', async () => {
|
|
clearComposerDraftSnapshot('source-a')
|
|
forgetComposerDraftHandoff('source-a')
|
|
mocks.getDraft.mockReturnValue('typed')
|
|
mocks.getDraftAttachments.mockRejectedValue(new Error('IndexedDB read failed'))
|
|
|
|
await expect(transferComposerDraft('source-a', 'target-a')).rejects.toThrow(/IndexedDB read failed/)
|
|
|
|
expect(mocks.moveDraftAttachments).not.toHaveBeenCalled()
|
|
expect(mocks.clearDraft).not.toHaveBeenCalled()
|
|
expect(composerDraftWasHandedOff('source-a')).toBe(false)
|
|
// Text is independently recoverable under the resumed id even when
|
|
// attachment storage fails closed.
|
|
expect(mocks.saveDraft).toHaveBeenCalledWith('target-a', 'typed')
|
|
})
|
|
|
|
it('still leaves target text after navigate when attachment read fails', async () => {
|
|
clearComposerDraftSnapshot('source-a')
|
|
forgetComposerDraftHandoff('source-a')
|
|
mocks.getDraft.mockReturnValue('typed')
|
|
mocks.getDraftAttachments.mockRejectedValue(new Error('IndexedDB read failed'))
|
|
const navigate = vi.fn().mockResolvedValue(undefined)
|
|
|
|
await transferComposerDraftThenNavigate('source-a', 'target-a', navigate)
|
|
|
|
expect(navigate).toHaveBeenCalledOnce()
|
|
expect(mocks.saveDraft).toHaveBeenCalledWith('target-a', 'typed')
|
|
expect(mocks.moveDraftAttachments).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('still navigates when the local durable move rejects', async () => {
|
|
const file = new File(['draft'], 'draft.txt')
|
|
setComposerDraftSnapshot('source-a', 'typed', [{ id: 'a1', file }])
|
|
mocks.moveDraftAttachments.mockRejectedValue(new Error('quota exceeded'))
|
|
const onNavigable = vi.fn().mockResolvedValue(undefined)
|
|
|
|
await handoffComposerDraft(
|
|
'source-a',
|
|
'target-a',
|
|
{ id: 'p1', file },
|
|
onNavigable,
|
|
)
|
|
|
|
expect(onNavigable).toHaveBeenCalledWith('target-a')
|
|
expect(composerDraftWasHandedOff('source-a')).toBe(false)
|
|
expect(mocks.saveDraft).toHaveBeenCalledWith('target-a', 'typed')
|
|
})
|
|
|
|
it('navigates after resume even when transferComposerDraftThenNavigate hits a move failure', async () => {
|
|
const file = new File(['draft'], 'draft.txt')
|
|
setComposerDraftSnapshot('source-a', 'typed', [{ id: 'a1', file }])
|
|
mocks.moveDraftAttachments.mockRejectedValue(new Error('quota exceeded'))
|
|
const navigate = vi.fn().mockResolvedValue(undefined)
|
|
|
|
await transferComposerDraftThenNavigate('source-a', 'target-a', navigate)
|
|
|
|
expect(navigate).toHaveBeenCalledOnce()
|
|
expect(composerDraftWasHandedOff('source-a')).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('updateComposerDraftTextSnapshot', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
resetMoveDraftMock()
|
|
resetSession('session-inactive')
|
|
})
|
|
|
|
it('updates text without writing attachment blobs', () => {
|
|
const file = new File(['blob'], 'big.bin')
|
|
setComposerDraftSnapshot('session-inactive', 'before', [{ id: 'a1', file }])
|
|
|
|
updateComposerDraftTextSnapshot('session-inactive', 'after keystroke')
|
|
|
|
expect(mocks.saveDraft).toHaveBeenCalledWith('session-inactive', 'after keystroke')
|
|
expect(mocks.saveDraftAttachments).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('keeps attachment revision stable across text-only changes', () => {
|
|
const file = new File(['blob'], 'big.bin')
|
|
const drafts = [{ id: 'a1', file, path: '/tmp/a', uploadSessionId: 's1' }]
|
|
expect(attachmentDraftRevision(drafts)).toBe(attachmentDraftRevision([
|
|
{ id: 'a1', file: new File(['other'], 'other.bin'), path: '/tmp/a', uploadSessionId: 's1' },
|
|
]))
|
|
expect(attachmentDraftRevision(drafts)).not.toBe(attachmentDraftRevision([
|
|
{ id: 'a1', file, path: '/tmp/b', uploadSessionId: 's1' },
|
|
]))
|
|
})
|
|
})
|
|
|
|
describe('persistInactiveComposerAttachments', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
resetMoveDraftMock()
|
|
resetSession('session-inactive')
|
|
resetSession('old-pending')
|
|
resetSession('new-pending')
|
|
})
|
|
|
|
it('clears the live snapshot without touching stored files when nothing is visible', async () => {
|
|
setComposerDraftSnapshot('session-inactive', 'stale', [])
|
|
mocks.getDraftAttachments.mockResolvedValue([new File(['kept'], 'kept.txt')])
|
|
|
|
await persistInactiveComposerAttachments('session-inactive', 'typed', [])
|
|
|
|
expect(mocks.saveDraft).toHaveBeenCalledWith('session-inactive', 'typed')
|
|
expect(mocks.saveDraftAttachments).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('does not publish a partial snapshot when the merge read fails', async () => {
|
|
const picked = new File(['b'], 'b.txt')
|
|
mocks.getDraftAttachments.mockImplementation(async (
|
|
_sessionId: string,
|
|
options?: { throwOnError?: boolean },
|
|
) => {
|
|
if (options?.throwOnError) throw new Error('idb merge read failed')
|
|
return []
|
|
})
|
|
|
|
await expect(persistInactiveComposerAttachments('old-pending', 'typed', [
|
|
{ id: 'b1', file: picked },
|
|
])).rejects.toThrow(/idb merge read failed/)
|
|
|
|
expect(mocks.saveDraftAttachments).not.toHaveBeenCalled()
|
|
expect(mocks.getDraftAttachments).toHaveBeenCalledWith('old-pending', { throwOnError: true })
|
|
|
|
// Transfer must not see a live-only-B snapshot and skip the strict read.
|
|
mocks.getDraft.mockReturnValue('typed')
|
|
await expect(transferComposerDraft('old-pending', 'new-pending')).rejects.toThrow()
|
|
expect(mocks.moveDraftAttachments).not.toHaveBeenCalled()
|
|
expect(composerDraftWasHandedOff('old-pending')).toBe(false)
|
|
})
|
|
|
|
it('merges a newly selected file into the hidden stored draft', async () => {
|
|
const stored = new File(['a'], 'a.txt')
|
|
const picked = new File(['b'], 'b.txt')
|
|
mocks.getDraftAttachments.mockResolvedValue([stored])
|
|
mocks.getRestoredUploadMetadata.mockReturnValue({
|
|
id: 'stored-a',
|
|
path: '/tmp/a',
|
|
uploadSessionId: 'session-inactive',
|
|
})
|
|
|
|
await persistInactiveComposerAttachments('session-inactive', 'typed', [{
|
|
id: 'picked-b',
|
|
file: picked,
|
|
}])
|
|
|
|
expect(mocks.saveDraftAttachments).toHaveBeenCalledWith('session-inactive', [
|
|
expect.objectContaining({ id: 'stored-a', file: stored }),
|
|
expect.objectContaining({ id: 'picked-b', file: picked }),
|
|
])
|
|
})
|
|
|
|
it('removes a previously visible failed pick from storage when the operator clears it', async () => {
|
|
const storedA = new File(['a'], 'a.txt')
|
|
const pickedB = new File(['b'], 'b.txt')
|
|
mocks.getDraftAttachments
|
|
.mockResolvedValueOnce([storedA])
|
|
.mockResolvedValueOnce([storedA, pickedB])
|
|
mocks.getRestoredUploadMetadata.mockImplementation((file: File) => {
|
|
if (file === storedA) {
|
|
return { id: 'stored-a', path: '/tmp/a', uploadSessionId: 'session-inactive' }
|
|
}
|
|
if (file === pickedB) {
|
|
return { id: 'picked-b' }
|
|
}
|
|
return undefined
|
|
})
|
|
|
|
await persistInactiveComposerAttachments('session-inactive', 'typed', [{
|
|
id: 'picked-b',
|
|
file: pickedB,
|
|
}])
|
|
await persistInactiveComposerAttachments('session-inactive', 'typed', [])
|
|
|
|
expect(mocks.saveDraftAttachments).toHaveBeenLastCalledWith('session-inactive', [
|
|
expect.objectContaining({ id: 'stored-a', file: storedA }),
|
|
])
|
|
})
|
|
|
|
it('serializes concurrent persist calls so an older read cannot overwrite a newer merge', async () => {
|
|
const storedA = new File(['a'], 'a.txt')
|
|
const pickedB = new File(['b'], 'b.txt')
|
|
const pickedC = new File(['c'], 'c.txt')
|
|
let releaseFirstRead!: () => void
|
|
const firstReadGate = new Promise<void>((resolve) => {
|
|
releaseFirstRead = resolve
|
|
})
|
|
let readCount = 0
|
|
mocks.getDraftAttachments.mockImplementation(async () => {
|
|
readCount += 1
|
|
if (readCount === 1) {
|
|
await firstReadGate
|
|
return [storedA]
|
|
}
|
|
return [storedA]
|
|
})
|
|
mocks.getRestoredUploadMetadata.mockReturnValue({
|
|
id: 'stored-a',
|
|
path: '/tmp/a',
|
|
uploadSessionId: 'session-inactive',
|
|
})
|
|
|
|
const first = persistInactiveComposerAttachments('session-inactive', 'first', [{
|
|
id: 'picked-b',
|
|
file: pickedB,
|
|
}])
|
|
const second = persistInactiveComposerAttachments('session-inactive', 'second', [{
|
|
id: 'picked-c',
|
|
file: pickedC,
|
|
}])
|
|
releaseFirstRead()
|
|
await Promise.all([first, second])
|
|
|
|
expect(mocks.saveDraftAttachments.mock.calls.at(-1)?.[1]).toEqual([
|
|
expect.objectContaining({ id: 'stored-a', file: storedA }),
|
|
expect.objectContaining({ id: 'picked-c', file: pickedC }),
|
|
])
|
|
})
|
|
|
|
it('awaits a pending inactive persist before transferComposerDraft reads storage', async () => {
|
|
const storedA = new File(['a'], 'a.txt')
|
|
const pickedB = new File(['b'], 'b.txt')
|
|
let releaseRead!: () => void
|
|
const readGate = new Promise<void>((resolve) => {
|
|
releaseRead = resolve
|
|
})
|
|
mocks.getDraft.mockReturnValue('typed')
|
|
mocks.getDraftAttachments.mockImplementation(async () => {
|
|
await readGate
|
|
return [storedA]
|
|
})
|
|
mocks.getRestoredUploadMetadata.mockImplementation((file: File) => {
|
|
if (file === storedA) {
|
|
return { id: 'stored-a', path: '/tmp/a' }
|
|
}
|
|
return { id: 'picked-b' }
|
|
})
|
|
|
|
const persist = persistInactiveComposerAttachments('old-pending', 'typed', [{
|
|
id: 'picked-b',
|
|
file: pickedB,
|
|
}])
|
|
const transfer = transferComposerDraft('old-pending', 'new-pending')
|
|
releaseRead()
|
|
await Promise.all([persist, transfer])
|
|
|
|
expectMovedAttachments('old-pending', 'new-pending', expect.arrayContaining([
|
|
expect.objectContaining({ id: 'stored-a' }),
|
|
expect.objectContaining({ id: 'picked-b' }),
|
|
]))
|
|
})
|
|
|
|
it('routes attachment edits during an in-flight move to the target without recreating the source', async () => {
|
|
const kept = new File(['kept'], 'kept.txt')
|
|
const removed = new File(['gone'], 'gone.txt')
|
|
setComposerDraftSnapshot('old-pending', 'typed', [
|
|
{ id: 'kept-1', file: kept },
|
|
{ id: 'gone-1', file: removed },
|
|
])
|
|
let releaseMove!: () => void
|
|
const moveGate = new Promise<void>((resolve) => {
|
|
releaseMove = resolve
|
|
})
|
|
mocks.moveDraftAttachments.mockImplementation(async (_source, _target, resolveAttachments) => {
|
|
await moveGate
|
|
return resolveAttachments()
|
|
})
|
|
|
|
const transfer = transferComposerDraft('old-pending', 'new-pending')
|
|
await Promise.resolve()
|
|
await persistInactiveComposerAttachments('old-pending', 'typed', [{ id: 'kept-1', file: kept }])
|
|
releaseMove()
|
|
await transfer
|
|
|
|
expect(mocks.saveDraftAttachments.mock.calls.filter((call) => call[0] === 'old-pending')).toEqual([])
|
|
// Late edit triggers an awaited same-target corrective move, not a fire-and-forget save.
|
|
expect(mocks.moveDraftAttachments).toHaveBeenCalledWith(
|
|
'new-pending',
|
|
'new-pending',
|
|
expect.any(Function),
|
|
)
|
|
const corrective = mocks.moveDraftAttachments.mock.calls.find(
|
|
(call) => call[0] === 'new-pending' && call[1] === 'new-pending',
|
|
)?.[2] as (() => Array<{ id: string }>) | undefined
|
|
expect(corrective?.().map((item) => item.id)).toEqual(['kept-1'])
|
|
expect(composerDraftWasHandedOff('old-pending')).toBe(true)
|
|
expect(mocks.clearDraft).toHaveBeenCalledWith('old-pending')
|
|
})
|
|
|
|
it('does not mark a durable handoff when the corrective target write rejects', async () => {
|
|
const kept = new File(['kept'], 'kept.txt')
|
|
const removed = new File(['gone'], 'gone.txt')
|
|
setComposerDraftSnapshot('old-pending', 'typed', [
|
|
{ id: 'kept-1', file: kept },
|
|
{ id: 'gone-1', file: removed },
|
|
])
|
|
let releaseMove!: () => void
|
|
const moveGate = new Promise<void>((resolve) => {
|
|
releaseMove = resolve
|
|
})
|
|
let correctiveCalls = 0
|
|
mocks.moveDraftAttachments.mockImplementation(async (source, target, resolveAttachments) => {
|
|
if (source === target) {
|
|
correctiveCalls += 1
|
|
throw new Error('quota exceeded')
|
|
}
|
|
await moveGate
|
|
return resolveAttachments()
|
|
})
|
|
|
|
const transfer = transferComposerDraft('old-pending', 'new-pending')
|
|
await Promise.resolve()
|
|
await persistInactiveComposerAttachments('old-pending', 'typed', [{ id: 'kept-1', file: kept }])
|
|
releaseMove()
|
|
await expect(transfer).rejects.toThrow(/quota exceeded/)
|
|
|
|
expect(correctiveCalls).toBe(1)
|
|
expect(composerDraftWasHandedOff('old-pending')).toBe(false)
|
|
expect(mocks.clearDraft).not.toHaveBeenCalledWith('old-pending')
|
|
})
|
|
|
|
it('repeats the corrective target write until late edits stabilize', async () => {
|
|
const kept = new File(['kept'], 'kept.txt')
|
|
setComposerDraftSnapshot('old-pending', 'before', [{ id: 'kept-1', file: kept }])
|
|
let releaseCorrective!: () => void
|
|
const correctiveGate = new Promise<void>((resolve) => {
|
|
releaseCorrective = resolve
|
|
})
|
|
let correctiveCalls = 0
|
|
mocks.moveDraftAttachments.mockImplementation(async (source, target, resolveAttachments) => {
|
|
if (source === target) {
|
|
correctiveCalls += 1
|
|
if (correctiveCalls === 1) {
|
|
await correctiveGate
|
|
}
|
|
return resolveAttachments()
|
|
}
|
|
return resolveAttachments()
|
|
})
|
|
|
|
const transfer = transferComposerDraft('old-pending', 'new-pending')
|
|
await Promise.resolve()
|
|
await persistInactiveComposerAttachments('old-pending', 'mid', [{ id: 'kept-1', file: kept }])
|
|
// First corrective is blocked; a later text edit must force another pass.
|
|
updateComposerDraftTextSnapshot('old-pending', 'final typed')
|
|
releaseCorrective()
|
|
await transfer
|
|
|
|
expect(correctiveCalls).toBeGreaterThanOrEqual(2)
|
|
expect(mocks.saveDraft).toHaveBeenCalledWith('new-pending', 'final typed')
|
|
expect(composerDraftWasHandedOff('old-pending')).toBe(true)
|
|
})
|
|
|
|
it('buffers a persist that races the first transfer await onto the target', async () => {
|
|
const kept = new File(['kept'], 'kept.txt')
|
|
const removed = new File(['gone'], 'gone.txt')
|
|
setComposerDraftSnapshot('old-pending', 'typed', [
|
|
{ id: 'kept-1', file: kept },
|
|
{ id: 'gone-1', file: removed },
|
|
])
|
|
let releasePersistFlush!: () => void
|
|
const persistFlushGate = new Promise<void>((resolve) => {
|
|
releasePersistFlush = resolve
|
|
})
|
|
// Make awaitInactivePersist wait so the barrier is installed first.
|
|
mocks.getDraftAttachments.mockImplementation(async () => {
|
|
await persistFlushGate
|
|
return []
|
|
})
|
|
// Start a no-op queued persist so awaitInactivePersist has work.
|
|
const flushPersist = persistInactiveComposerAttachments('old-pending', 'typed', [
|
|
{ id: 'kept-1', file: kept },
|
|
{ id: 'gone-1', file: removed },
|
|
])
|
|
|
|
const transfer = transferComposerDraft('old-pending', 'new-pending')
|
|
await Promise.resolve()
|
|
// Barrier is up; this edit must land on pending.latest, not IndexedDB source.
|
|
await persistInactiveComposerAttachments('old-pending', 'typed', [{ id: 'kept-1', file: kept }])
|
|
releasePersistFlush()
|
|
await flushPersist
|
|
await transfer
|
|
|
|
expect(mocks.saveDraftAttachments.mock.calls.filter((call) => call[0] === 'old-pending')).toEqual([])
|
|
expectMovedAttachments('old-pending', 'new-pending', [
|
|
expect.objectContaining({ id: 'kept-1', file: kept }),
|
|
])
|
|
})
|
|
|
|
it('does not treat remounted empty visible state as removal of stored failed picks', async () => {
|
|
const { resetInactiveComposerAttachmentVisibility } = await import('./composer-draft-transfer')
|
|
const storedA = new File(['a'], 'a.txt')
|
|
const pickedB = new File(['b'], 'b.txt')
|
|
mocks.getDraftAttachments.mockResolvedValue([storedA, pickedB])
|
|
mocks.getRestoredUploadMetadata.mockImplementation((file: File) => {
|
|
if (file === storedA) return { id: 'stored-a' }
|
|
if (file === pickedB) return { id: 'picked-b' }
|
|
return undefined
|
|
})
|
|
|
|
await persistInactiveComposerAttachments('session-inactive', 'typed', [{
|
|
id: 'picked-b',
|
|
file: pickedB,
|
|
}])
|
|
// Remount inactive: visibility tracking resets before the empty hydrate persist.
|
|
resetInactiveComposerAttachmentVisibility('session-inactive')
|
|
mocks.saveDraftAttachments.mockClear()
|
|
await persistInactiveComposerAttachments('session-inactive', 'typed', [])
|
|
|
|
expect(mocks.saveDraftAttachments).not.toHaveBeenCalled()
|
|
})
|
|
})
|