From 0983e4e28872b70429797c66e97bb758c9a25c4c Mon Sep 17 00:00:00 2001 From: lucaronin Date: Sun, 17 May 2026 04:09:47 +0200 Subject: [PATCH] test: cover attachment preservation during tab switch --- src/hooks/useEditorTabSwap.test.ts | 37 ++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/hooks/useEditorTabSwap.test.ts b/src/hooks/useEditorTabSwap.test.ts index 3e3d427c..2ba66fba 100644 --- a/src/hooks/useEditorTabSwap.test.ts +++ b/src/hooks/useEditorTabSwap.test.ts @@ -682,6 +682,43 @@ describe('useEditorTabSwap raw mode sync', () => { ) }) + it('flushes unsaved file attachment blocks as portable links before switching notes', async () => { + const tabA = makeTab('a.md', 'Note A') + const tabB = makeTab('b.md', 'Note B') + const onContentChange = vi.fn() + const { docRef, mockEditor, rerender, result } = await createSwapHarness({ + initialProps: { tabs: [tabA, tabB], activeTabPath: 'a.md', rawMode: false, vaultPath: '/vault' }, + onContentChange, + }) + + docRef.current = [{ + type: 'file', + props: { + name: 'project brief.pdf', + url: '/vault/attachments/project brief.pdf', + }, + children: [], + }] + mockEditor.blocksToMarkdownLossy.mockClear() + mockEditor.blocksToMarkdownLossy.mockReturnValue('unreachable lossy markdown') + + act(() => { + result.current.handleEditorChange() + }) + expect(onContentChange).not.toHaveBeenCalled() + + act(() => { + rerender({ tabs: [tabA, tabB], activeTabPath: 'b.md', rawMode: false, vaultPath: '/vault' }) + }) + await act(async () => { await Promise.resolve() }) + + expect(onContentChange).toHaveBeenCalledWith( + 'a.md', + '---\ntitle: Note A\n---\n[project brief.pdf]()\n', + ) + expect(mockEditor.blocksToMarkdownLossy).not.toHaveBeenCalled() + }) + it('serializes rich inline math nodes back to Markdown on editor changes', async () => { vi.spyOn(document, 'querySelector').mockReturnValue({ scrollTop: 0 } as unknown as Element) vi.spyOn(window, 'requestAnimationFrame').mockImplementation((cb) => { cb(0); return 0 })