|
1 | 1 | /** @vitest-environment jsdom */ |
2 | 2 | import { beforeEach, expect, it } from 'vitest' |
| 3 | +import { MAX_CHAT_CONTEXTS } from '@/lib/mothership/chat/context-limits' |
3 | 4 | import { STREAM_STORAGE_KEY } from '@/lib/mothership/constants' |
4 | | -import { readQueuedSendHandoffState, writeQueuedSendHandoffState } from './send-handoff' |
| 5 | +import { |
| 6 | + readQueuedSendHandoffState, |
| 7 | + writeQueuedSendHandoffState, |
| 8 | +} from '@/app/workspace/[workspaceId]/home/hooks/send-handoff' |
5 | 9 |
|
6 | 10 | beforeEach(() => sessionStorage.clear()) |
7 | 11 | const seed = () => ({ |
@@ -33,3 +37,76 @@ it('rejects invalid levels and preserves an explicit Build mode', () => { |
33 | 37 | writeQueuedSendHandoffState({ ...seed(), requestMode: 'agent' }) |
34 | 38 | expect(readQueuedSendHandoffState()).toMatchObject({ requestMode: 'agent' }) |
35 | 39 | }) |
| 40 | + |
| 41 | +it('keeps lost-context evidence when a recovered handoff is written again', () => { |
| 42 | + const validContext = { kind: 'docs' as const, label: 'Docs' } |
| 43 | + writeQueuedSendHandoffState({ |
| 44 | + ...seed(), |
| 45 | + contexts: [ |
| 46 | + validContext, |
| 47 | + { |
| 48 | + kind: 'browser_tab', |
| 49 | + tabId: 'tab-a', |
| 50 | + label: 'Browser', |
| 51 | + selection: { text: 'Selected passage', title: 'x'.repeat(513) }, |
| 52 | + }, |
| 53 | + ], |
| 54 | + }) |
| 55 | + const recovered = readQueuedSendHandoffState() |
| 56 | + expect(recovered).toMatchObject({ contexts: [validContext], hasUnavailableContexts: true }) |
| 57 | + if (!recovered) throw new Error('Expected a recoverable handoff') |
| 58 | + writeQueuedSendHandoffState(recovered) |
| 59 | + expect(readQueuedSendHandoffState()).toMatchObject({ |
| 60 | + contexts: [validContext], |
| 61 | + hasUnavailableContexts: true, |
| 62 | + }) |
| 63 | +}) |
| 64 | + |
| 65 | +it.each([null, {}, 'invalid'])( |
| 66 | + 'keeps the message for review when stored contexts are malformed (%s)', |
| 67 | + (contexts) => { |
| 68 | + sessionStorage.setItem( |
| 69 | + `${STREAM_STORAGE_KEY}:queued-send-handoff`, |
| 70 | + JSON.stringify({ ...seed(), contexts }) |
| 71 | + ) |
| 72 | + expect(readQueuedSendHandoffState()).toMatchObject({ |
| 73 | + message: 'Search', |
| 74 | + hasUnavailableContexts: true, |
| 75 | + }) |
| 76 | + } |
| 77 | +) |
| 78 | + |
| 79 | +it.each([100, 101])( |
| 80 | + 'recovers %s contexts within the send limit without losing review state', |
| 81 | + (count) => { |
| 82 | + const contexts = Array.from({ length: count }, (_, index) => ({ |
| 83 | + kind: 'file' as const, |
| 84 | + fileId: `file-${index}`, |
| 85 | + label: `File ${index}`, |
| 86 | + })) |
| 87 | + writeQueuedSendHandoffState({ ...seed(), contexts }) |
| 88 | + const recovered = readQueuedSendHandoffState() |
| 89 | + expect(recovered?.contexts).toEqual(contexts.slice(0, MAX_CHAT_CONTEXTS)) |
| 90 | + expect(recovered?.message).toBe('Search') |
| 91 | + expect(recovered?.hasUnavailableContexts).toBe(count > MAX_CHAT_CONTEXTS ? true : undefined) |
| 92 | + if (!recovered) throw new Error('Expected a recoverable handoff') |
| 93 | + writeQueuedSendHandoffState(recovered) |
| 94 | + expect(readQueuedSendHandoffState()).toEqual(recovered) |
| 95 | + } |
| 96 | +) |
| 97 | + |
| 98 | +it('keeps a full limit of valid contexts when an earlier stored context is invalid', () => { |
| 99 | + const contexts = Array.from({ length: MAX_CHAT_CONTEXTS }, (_, index) => ({ |
| 100 | + kind: 'file' as const, |
| 101 | + fileId: `file-${index}`, |
| 102 | + label: `File ${index}`, |
| 103 | + })) |
| 104 | + sessionStorage.setItem( |
| 105 | + `${STREAM_STORAGE_KEY}:queued-send-handoff`, |
| 106 | + JSON.stringify({ |
| 107 | + ...seed(), |
| 108 | + contexts: [{ kind: 'workspace', label: 'Missing address' }, ...contexts], |
| 109 | + }) |
| 110 | + ) |
| 111 | + expect(readQueuedSendHandoffState()).toMatchObject({ contexts, hasUnavailableContexts: true }) |
| 112 | +}) |
0 commit comments