|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Qwen Team |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import * as path from 'node:path'; |
| 8 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 9 | + |
| 10 | +const { |
| 11 | + mockLoadSession, |
| 12 | + mockCollectSessionData, |
| 13 | + mockNormalizeSessionData, |
| 14 | + mockToHtml, |
| 15 | + mockToMarkdown, |
| 16 | + mockToJson, |
| 17 | + mockToJsonl, |
| 18 | + mockGenerateExportFilename, |
| 19 | + mockWriteFile, |
| 20 | + mockShowSaveDialog, |
| 21 | +} = vi.hoisted(() => ({ |
| 22 | + mockLoadSession: vi.fn(), |
| 23 | + mockCollectSessionData: vi.fn(), |
| 24 | + mockNormalizeSessionData: vi.fn(), |
| 25 | + mockToHtml: vi.fn(), |
| 26 | + mockToMarkdown: vi.fn(), |
| 27 | + mockToJson: vi.fn(), |
| 28 | + mockToJsonl: vi.fn(), |
| 29 | + mockGenerateExportFilename: vi.fn(), |
| 30 | + mockWriteFile: vi.fn(), |
| 31 | + mockShowSaveDialog: vi.fn(), |
| 32 | +})); |
| 33 | + |
| 34 | +vi.mock('@qwen-code/qwen-code-core', () => { |
| 35 | + class SessionService { |
| 36 | + constructor(_cwd: string) {} |
| 37 | + |
| 38 | + async loadSession(_sessionId: string) { |
| 39 | + return mockLoadSession(); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + return { |
| 44 | + SessionService, |
| 45 | + }; |
| 46 | +}); |
| 47 | + |
| 48 | +vi.mock('@qwen-code/qwen-code/export', () => ({ |
| 49 | + collectSessionData: mockCollectSessionData, |
| 50 | + normalizeSessionData: mockNormalizeSessionData, |
| 51 | + toHtml: mockToHtml, |
| 52 | + toMarkdown: mockToMarkdown, |
| 53 | + toJson: mockToJson, |
| 54 | + toJsonl: mockToJsonl, |
| 55 | + generateExportFilename: mockGenerateExportFilename, |
| 56 | +})); |
| 57 | + |
| 58 | +vi.mock('node:fs/promises', () => ({ |
| 59 | + writeFile: mockWriteFile, |
| 60 | +})); |
| 61 | + |
| 62 | +vi.mock('vscode', () => ({ |
| 63 | + Uri: { |
| 64 | + file: (fsPath: string) => ({ fsPath }), |
| 65 | + }, |
| 66 | + window: { |
| 67 | + showSaveDialog: mockShowSaveDialog, |
| 68 | + }, |
| 69 | +})); |
| 70 | + |
| 71 | +import { |
| 72 | + exportSessionToFile, |
| 73 | + parseExportSlashCommand, |
| 74 | +} from './sessionExportService.js'; |
| 75 | + |
| 76 | +describe('sessionExportService', () => { |
| 77 | + beforeEach(() => { |
| 78 | + vi.clearAllMocks(); |
| 79 | + |
| 80 | + mockLoadSession.mockResolvedValue({ |
| 81 | + conversation: { |
| 82 | + sessionId: 'session-1', |
| 83 | + startTime: '2025-01-01T00:00:00Z', |
| 84 | + messages: [], |
| 85 | + }, |
| 86 | + }); |
| 87 | + mockCollectSessionData.mockResolvedValue({ |
| 88 | + sessionId: 'session-1', |
| 89 | + startTime: '2025-01-01T00:00:00Z', |
| 90 | + messages: [], |
| 91 | + }); |
| 92 | + mockNormalizeSessionData.mockImplementation((data) => data); |
| 93 | + mockToHtml.mockReturnValue('<html>export</html>'); |
| 94 | + mockToMarkdown.mockReturnValue('# export'); |
| 95 | + mockToJson.mockReturnValue('{"ok":true}'); |
| 96 | + mockToJsonl.mockReturnValue('{"ok":true}'); |
| 97 | + mockGenerateExportFilename.mockImplementation( |
| 98 | + (format: string) => `qwen-export.${format}`, |
| 99 | + ); |
| 100 | + }); |
| 101 | + |
| 102 | + describe('parseExportSlashCommand', () => { |
| 103 | + it('returns null for non-export input', () => { |
| 104 | + expect(parseExportSlashCommand('hello')).toBeNull(); |
| 105 | + expect(parseExportSlashCommand('/model')).toBeNull(); |
| 106 | + }); |
| 107 | + |
| 108 | + it('requires an explicit subcommand for bare /export', () => { |
| 109 | + expect(() => parseExportSlashCommand('/export')).toThrow( |
| 110 | + "Command '/export' requires a subcommand.", |
| 111 | + ); |
| 112 | + expect(() => parseExportSlashCommand('/export ')).toThrow( |
| 113 | + "Command '/export' requires a subcommand.", |
| 114 | + ); |
| 115 | + }); |
| 116 | + |
| 117 | + it('returns the requested export format', () => { |
| 118 | + expect(parseExportSlashCommand('/export html')).toBe('html'); |
| 119 | + expect(parseExportSlashCommand('/export md')).toBe('md'); |
| 120 | + expect(parseExportSlashCommand('/export JSON')).toBe('json'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('rejects unsupported export arguments', () => { |
| 124 | + expect(() => parseExportSlashCommand('/export csv')).toThrow( |
| 125 | + 'Unsupported /export format. Use /export html, /export md, /export json, or /export jsonl.', |
| 126 | + ); |
| 127 | + expect(() => parseExportSlashCommand('/export md extra')).toThrow( |
| 128 | + 'Unsupported /export format. Use /export html, /export md, /export json, or /export jsonl.', |
| 129 | + ); |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + describe('exportSessionToFile', () => { |
| 134 | + it('writes the exported session to the user-chosen path', async () => { |
| 135 | + const chosenPath = path.join('/workspace', 'qwen-export.html'); |
| 136 | + mockShowSaveDialog.mockResolvedValue({ fsPath: chosenPath }); |
| 137 | + |
| 138 | + const result = await exportSessionToFile({ |
| 139 | + sessionId: 'session-1', |
| 140 | + cwd: '/workspace', |
| 141 | + format: 'html', |
| 142 | + }); |
| 143 | + |
| 144 | + expect(mockCollectSessionData).toHaveBeenCalledWith( |
| 145 | + expect.objectContaining({ sessionId: 'session-1' }), |
| 146 | + expect.anything(), |
| 147 | + ); |
| 148 | + expect(mockNormalizeSessionData).toHaveBeenCalled(); |
| 149 | + expect(mockToHtml).toHaveBeenCalled(); |
| 150 | + expect(mockShowSaveDialog).toHaveBeenCalledWith( |
| 151 | + expect.objectContaining({ |
| 152 | + title: 'Export Session as HTML', |
| 153 | + }), |
| 154 | + ); |
| 155 | + expect(mockWriteFile).toHaveBeenCalledWith( |
| 156 | + chosenPath, |
| 157 | + '<html>export</html>', |
| 158 | + 'utf-8', |
| 159 | + ); |
| 160 | + expect(result).toEqual({ |
| 161 | + filename: 'qwen-export.html', |
| 162 | + uri: { fsPath: chosenPath }, |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + it('returns null when the user cancels the save dialog', async () => { |
| 167 | + mockShowSaveDialog.mockResolvedValue(undefined); |
| 168 | + |
| 169 | + const result = await exportSessionToFile({ |
| 170 | + sessionId: 'session-1', |
| 171 | + cwd: '/workspace', |
| 172 | + format: 'html', |
| 173 | + }); |
| 174 | + |
| 175 | + expect(result).toBeNull(); |
| 176 | + expect(mockWriteFile).not.toHaveBeenCalled(); |
| 177 | + }); |
| 178 | + |
| 179 | + it('throws when the target session cannot be loaded', async () => { |
| 180 | + mockLoadSession.mockResolvedValue(undefined); |
| 181 | + |
| 182 | + await expect( |
| 183 | + exportSessionToFile({ |
| 184 | + sessionId: 'missing-session', |
| 185 | + cwd: '/workspace', |
| 186 | + format: 'json', |
| 187 | + }), |
| 188 | + ).rejects.toThrow('No active session found to export.'); |
| 189 | + }); |
| 190 | + }); |
| 191 | +}); |
0 commit comments