|
| 1 | +import { describe, expect, mock, test } from 'bun:test' |
| 2 | + |
| 3 | +import { exportConversation } from '../export-conversation' |
| 4 | +import { IS_FREEBUFF } from '../../utils/constants' |
| 5 | + |
| 6 | +import type { ExportDeps } from '../export-conversation' |
| 7 | +import type { ChatMessage, ContentBlock } from '../../types/chat' |
| 8 | + |
| 9 | +// The default filename prefix follows the product branding, like the |
| 10 | +// transcript header in serializeConversation. |
| 11 | +const PRODUCT = IS_FREEBUFF ? 'freebuff' : 'codebuff' |
| 12 | + |
| 13 | +const msg = ( |
| 14 | + variant: ChatMessage['variant'], |
| 15 | + opts: { content?: string; blocks?: ContentBlock[] } = {}, |
| 16 | +): ChatMessage => ({ |
| 17 | + id: `${variant}-${opts.content ?? 'x'}`, |
| 18 | + variant, |
| 19 | + content: opts.content ?? '', |
| 20 | + blocks: opts.blocks, |
| 21 | + timestamp: '2026-06-25T00:00:00.000Z', |
| 22 | +}) |
| 23 | + |
| 24 | +const makeDeps = () => { |
| 25 | + const writes: Array<{ filePath: string; data: string }> = [] |
| 26 | + const deps: ExportDeps = { |
| 27 | + getProjectRoot: () => '/project', |
| 28 | + getCurrentChatId: () => '2026-06-25T00-00-00.000Z', |
| 29 | + fileExists: () => false, |
| 30 | + writeFile: async (filePath, data) => { |
| 31 | + writes.push({ filePath, data }) |
| 32 | + }, |
| 33 | + } |
| 34 | + return { deps, writes } |
| 35 | +} |
| 36 | + |
| 37 | +describe('exportConversation', () => { |
| 38 | + test('defaults to <product>-chat-<chatId>.md in the project root', async () => { |
| 39 | + const { deps, writes } = makeDeps() |
| 40 | + const result = await exportConversation( |
| 41 | + [msg('user', { content: 'hi' })], |
| 42 | + '', |
| 43 | + deps, |
| 44 | + ) |
| 45 | + |
| 46 | + expect(result).toMatchObject({ |
| 47 | + ok: true, |
| 48 | + filePath: `/project/${PRODUCT}-chat-2026-06-25T00-00-00.000Z.md`, |
| 49 | + format: 'markdown', |
| 50 | + }) |
| 51 | + expect(writes).toHaveLength(1) |
| 52 | + expect(writes[0].data).toContain('## User') |
| 53 | + expect(writes[0].data).toContain('hi') |
| 54 | + }) |
| 55 | + |
| 56 | + test('resolves a relative filename against the project root', async () => { |
| 57 | + const { deps, writes } = makeDeps() |
| 58 | + const result = await exportConversation( |
| 59 | + [msg('user', { content: 'hi' })], |
| 60 | + ' notes/chat.md ', |
| 61 | + deps, |
| 62 | + ) |
| 63 | + expect(result).toMatchObject({ |
| 64 | + ok: true, |
| 65 | + filePath: '/project/notes/chat.md', |
| 66 | + }) |
| 67 | + expect(writes[0].filePath).toBe('/project/notes/chat.md') |
| 68 | + }) |
| 69 | + |
| 70 | + test('refuses a path that escapes the project root', async () => { |
| 71 | + const { deps, writes } = makeDeps() |
| 72 | + // path.resolve honors both of these; without the containment check they |
| 73 | + // reach anything on disk. |
| 74 | + for (const escape of ['../outside.md', '/etc/hosts']) { |
| 75 | + const result = await exportConversation( |
| 76 | + [msg('user', { content: 'hi' })], |
| 77 | + escape, |
| 78 | + deps, |
| 79 | + ) |
| 80 | + expect(result).toMatchObject({ ok: false }) |
| 81 | + if (!result.ok) expect(result.error).toContain('project root') |
| 82 | + } |
| 83 | + expect(writes).toHaveLength(0) |
| 84 | + }) |
| 85 | + |
| 86 | + test('refuses to overwrite an existing file', async () => { |
| 87 | + const { deps, writes } = makeDeps() |
| 88 | + deps.fileExists = () => true |
| 89 | + // The atomic write renames over the target, so '/export README.md' would |
| 90 | + // otherwise destroy the file with no prompt and no backup. |
| 91 | + const result = await exportConversation( |
| 92 | + [msg('user', { content: 'hi' })], |
| 93 | + 'README.md', |
| 94 | + deps, |
| 95 | + ) |
| 96 | + expect(result).toMatchObject({ ok: false }) |
| 97 | + if (!result.ok) expect(result.error).toContain('already exists') |
| 98 | + expect(writes).toHaveLength(0) |
| 99 | + }) |
| 100 | + |
| 101 | + test('a .json filename exports the raw messages as parseable JSON', async () => { |
| 102 | + const { deps, writes } = makeDeps() |
| 103 | + const messages = [msg('user', { content: 'hello json' })] |
| 104 | + const result = await exportConversation(messages, 'out.json', deps) |
| 105 | + |
| 106 | + expect(result).toMatchObject({ ok: true, format: 'json' }) |
| 107 | + const parsed = JSON.parse(writes[0].data) |
| 108 | + expect(parsed).toHaveLength(1) |
| 109 | + expect(parsed[0].content).toBe('hello json') |
| 110 | + }) |
| 111 | + |
| 112 | + test('.json export survives cyclic tool output instead of throwing', async () => { |
| 113 | + const { deps, writes } = makeDeps() |
| 114 | + const cyclic: Record<string, unknown> = { note: 'loop' } |
| 115 | + cyclic.self = cyclic |
| 116 | + const messages = [ |
| 117 | + msg('ai', { |
| 118 | + blocks: [ |
| 119 | + { |
| 120 | + type: 'tool', |
| 121 | + toolCallId: 'call-1', |
| 122 | + toolName: 'read_files' as never, |
| 123 | + input: cyclic, |
| 124 | + output: 'ok', |
| 125 | + }, |
| 126 | + ], |
| 127 | + }), |
| 128 | + ] |
| 129 | + |
| 130 | + const result = await exportConversation(messages, 'out.json', deps) |
| 131 | + expect(result).toMatchObject({ ok: true, format: 'json' }) |
| 132 | + expect(writes[0].data).toContain('[Circular]') |
| 133 | + }) |
| 134 | + |
| 135 | + test('markdown export has no clipboard ceiling — nothing is trimmed', async () => { |
| 136 | + const { deps, writes } = makeDeps() |
| 137 | + // Far past the /copy OSC 52 budget (22 KB); with a maxBytes this would be |
| 138 | + // replaced by an omission note. |
| 139 | + const huge = 'x'.repeat(200_000) |
| 140 | + const messages = [ |
| 141 | + msg('ai', { |
| 142 | + blocks: [ |
| 143 | + { |
| 144 | + type: 'tool', |
| 145 | + toolCallId: 'call-1', |
| 146 | + toolName: 'read_files' as never, |
| 147 | + input: { path: 'big.txt' }, |
| 148 | + output: huge, |
| 149 | + }, |
| 150 | + ], |
| 151 | + }), |
| 152 | + ] |
| 153 | + |
| 154 | + const result = await exportConversation(messages, '', deps) |
| 155 | + expect(result).toMatchObject({ ok: true }) |
| 156 | + expect(writes[0].data).toContain(huge) |
| 157 | + expect(writes[0].data).not.toContain('omitted') |
| 158 | + }) |
| 159 | + |
| 160 | + test('reports write failures instead of throwing', async () => { |
| 161 | + const { deps } = makeDeps() |
| 162 | + deps.writeFile = mock(async () => { |
| 163 | + throw new Error('EACCES: permission denied') |
| 164 | + }) |
| 165 | + const result = await exportConversation( |
| 166 | + [msg('user', { content: 'hi' })], |
| 167 | + '', |
| 168 | + deps, |
| 169 | + ) |
| 170 | + expect(result).toMatchObject({ ok: false }) |
| 171 | + if (!result.ok) expect(result.error).toContain('EACCES') |
| 172 | + }) |
| 173 | + |
| 174 | + test('reports a missing project root instead of throwing', async () => { |
| 175 | + const { deps } = makeDeps() |
| 176 | + deps.getProjectRoot = () => { |
| 177 | + throw new Error('Project root not set') |
| 178 | + } |
| 179 | + const result = await exportConversation( |
| 180 | + [msg('user', { content: 'hi' })], |
| 181 | + '', |
| 182 | + deps, |
| 183 | + ) |
| 184 | + expect(result).toMatchObject({ ok: false }) |
| 185 | + if (!result.ok) expect(result.error).toContain('Project root not set') |
| 186 | + }) |
| 187 | +}) |
0 commit comments