|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { |
| 3 | + sessionExportFilename, |
| 4 | + sessionExportMarkdown, |
| 5 | +} from '../../src/renderer/export-session'; |
| 6 | +import type { Entry } from '../../src/renderer/timeline'; |
| 7 | + |
| 8 | +const exportedAt = new Date('2026-11-08T01:02:03.000Z'); |
| 9 | + |
| 10 | +const bashDone: Extract<Entry, { kind: 'tool' }> = { |
| 11 | + key: 'tool-1', |
| 12 | + kind: 'tool', |
| 13 | + toolId: 'call_1', |
| 14 | + tool: 'bash', |
| 15 | + args: 'npm test', |
| 16 | + input: 'npm test', |
| 17 | + output: '3 passed', |
| 18 | + running: false, |
| 19 | + ok: true, |
| 20 | +}; |
| 21 | + |
| 22 | +describe('sessionExportMarkdown', () => { |
| 23 | + it('writes the full conversation with context and tool input', () => { |
| 24 | + const markdown = sessionExportMarkdown({ |
| 25 | + title: 'Fix the export', |
| 26 | + cwd: '/repo/app', |
| 27 | + model: 'anthropic/claude-test', |
| 28 | + exportedAt, |
| 29 | + entries: [ |
| 30 | + { key: 'u1', kind: 'user', text: 'please fix it' }, |
| 31 | + { |
| 32 | + key: 'a1', |
| 33 | + kind: 'assistant', |
| 34 | + text: 'On it.', |
| 35 | + thinking: 'check the tests first', |
| 36 | + streaming: false, |
| 37 | + }, |
| 38 | + bashDone, |
| 39 | + ], |
| 40 | + }); |
| 41 | + expect(markdown).toBe( |
| 42 | + [ |
| 43 | + '# Fix the export', |
| 44 | + '', |
| 45 | + 'Exported from pidex at 2026-11-08T01:02:03.000Z.', |
| 46 | + '', |
| 47 | + 'It ran in `/repo/app`.', |
| 48 | + '', |
| 49 | + 'It used `anthropic/claude-test`.', |
| 50 | + '', |
| 51 | + '## You', |
| 52 | + '', |
| 53 | + 'please fix it', |
| 54 | + '', |
| 55 | + '## Pi', |
| 56 | + '', |
| 57 | + 'On it.', |
| 58 | + '', |
| 59 | + '### Thinking', |
| 60 | + '', |
| 61 | + 'check the tests first', |
| 62 | + '', |
| 63 | + '## bash / npm test (Done)', |
| 64 | + '', |
| 65 | + '### Input', |
| 66 | + '', |
| 67 | + '```', |
| 68 | + 'npm test', |
| 69 | + '```', |
| 70 | + '', |
| 71 | + '### Output', |
| 72 | + '', |
| 73 | + '```', |
| 74 | + '3 passed', |
| 75 | + '```', |
| 76 | + '', |
| 77 | + ].join('\n'), |
| 78 | + ); |
| 79 | + }); |
| 80 | + |
| 81 | + it('marks failed and running tools with the card wording', () => { |
| 82 | + const failed = sessionExportMarkdown({ |
| 83 | + title: 'T', |
| 84 | + cwd: '', |
| 85 | + model: '', |
| 86 | + exportedAt, |
| 87 | + entries: [{ ...bashDone, key: 'f', ok: false, output: 'boom' }], |
| 88 | + }); |
| 89 | + expect(failed).toContain('## bash / npm test (Failed)'); |
| 90 | + const running = sessionExportMarkdown({ |
| 91 | + title: 'T', |
| 92 | + cwd: '', |
| 93 | + model: '', |
| 94 | + exportedAt, |
| 95 | + entries: [{ ...bashDone, key: 'r', running: true, output: '' }], |
| 96 | + }); |
| 97 | + expect(running).toContain('## bash / npm test (Running)'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('keeps an empty input and output readable', () => { |
| 101 | + const markdown = sessionExportMarkdown({ |
| 102 | + title: 'T', |
| 103 | + cwd: '', |
| 104 | + model: '', |
| 105 | + exportedAt, |
| 106 | + entries: [{ ...bashDone, key: 'e', input: '', output: '' }], |
| 107 | + }); |
| 108 | + expect(markdown).toContain('### Input\n\nNo input.'); |
| 109 | + expect(markdown).toContain('### Output\n\nNo output.'); |
| 110 | + }); |
| 111 | + |
| 112 | + it('widens the fence when the output holds backticks', () => { |
| 113 | + const markdown = sessionExportMarkdown({ |
| 114 | + title: 'T', |
| 115 | + cwd: '', |
| 116 | + model: '', |
| 117 | + exportedAt, |
| 118 | + entries: [ |
| 119 | + { ...bashDone, key: 'b', output: 'before\n```\ncode\n```\nafter' }, |
| 120 | + ], |
| 121 | + }); |
| 122 | + expect(markdown).toContain('\n````\nbefore\n```\ncode\n```\nafter\n````'); |
| 123 | + }); |
| 124 | + |
| 125 | + it('handles an empty selection and a blank title', () => { |
| 126 | + const markdown = sessionExportMarkdown({ |
| 127 | + title: ' ', |
| 128 | + cwd: '', |
| 129 | + model: '', |
| 130 | + exportedAt, |
| 131 | + entries: [], |
| 132 | + }); |
| 133 | + expect(markdown).toContain('# Untitled session'); |
| 134 | + expect(markdown).toContain('No messages in this selection.'); |
| 135 | + expect(markdown).not.toContain('It ran in'); |
| 136 | + expect(markdown).not.toContain('It used'); |
| 137 | + }); |
| 138 | +}); |
| 139 | + |
| 140 | +describe('sessionExportFilename', () => { |
| 141 | + it('builds the name from the title and export day', () => { |
| 142 | + expect(sessionExportFilename('Fix: the Bug!', exportedAt)).toBe( |
| 143 | + 'pidex-fix-the-bug-2026-11-08.md', |
| 144 | + ); |
| 145 | + }); |
| 146 | + |
| 147 | + it('falls back when the title has no usable words', () => { |
| 148 | + expect(sessionExportFilename('!!!', exportedAt)).toBe( |
| 149 | + 'pidex-session-2026-11-08.md', |
| 150 | + ); |
| 151 | + expect(sessionExportFilename('', exportedAt)).toBe( |
| 152 | + 'pidex-session-2026-11-08.md', |
| 153 | + ); |
| 154 | + }); |
| 155 | +}); |
0 commit comments