File size: 4,016 Bytes
52efc7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { describe, it, expect, vi, beforeEach } from 'vitest';
import { exportSessionCommand } from './exportSessionCommand.js';
import * as fs from 'node:fs/promises';
import * as path from 'node:path';
import { SessionSelector } from '../../utils/sessionUtils.js';
import type { CommandContext } from './types.js';
import { Storage, type ConversationRecord } from '@google/gemini-cli-core';

vi.mock('node:fs/promises');
vi.mock('../../utils/sessionUtils.js');

describe('exportSessionCommand', () => {
  let mockContext: CommandContext;

  beforeEach(() => {
    vi.resetAllMocks();
    vi.spyOn(Storage.prototype, 'initialize').mockResolvedValue(undefined);
    vi.spyOn(Storage.prototype, 'getProjectTempDir').mockReturnValue(
      path.join(path.sep, 'tmp', 'mock-dir'),
    );
    mockContext = {
      services: {
        agentContext: {
          config: {
            sessionId: 'test-session-id',
            getSessionId: () => 'test-session-id',
            storage: new Storage(process.cwd()),
          },
        },
      },
      invocation: {
        args: '  export.json  ',
        name: 'export-session',
        raw: '/export-session export.json',
      },
      ui: {
        addItem: vi.fn(),
        setPendingItem: vi.fn(),
        pendingItem: null,
      },
    } as unknown as CommandContext;
  });

  it('should return error if no path is provided', async () => {
    mockContext.invocation!.args = '   ';
    const result = await exportSessionCommand.action!(mockContext, '');

    expect(result).toEqual({
      type: 'message',
      messageType: 'error',
      content: expect.stringContaining('Please provide a file path'),
    });
  });

  it('should return error if sessionId is missing', async () => {
    mockContext.services.agentContext!.config.getSessionId = () =>
      undefined as unknown as string;
    const result = await exportSessionCommand.action!(mockContext, '');

    expect(result).toEqual({
      type: 'message',
      messageType: 'error',
      content: 'No active session found to export.',
    });
  });

  it('should export the session successfully', async () => {
    const mockSessionData: ConversationRecord = {
      sessionId: 'test-session-id',
      messages: [],
      projectHash: 'hash',
      startTime: 'time',
      lastUpdated: 'time',
    };
    vi.mocked(SessionSelector.prototype.resolveSession).mockResolvedValue({
      sessionData: mockSessionData,
      sessionPath: path.join(
        path.sep,
        'tmp',
        'mock-dir',
        'chats',
        'session.jsonl',
      ),
      displayInfo: 'test',
    });

    vi.mocked(fs.access).mockRejectedValue(new Error('Not found'));

    const result = await exportSessionCommand.action!(mockContext, '');

    expect(result).toBeUndefined();
    expect(fs.writeFile).toHaveBeenCalledWith(
      path.resolve(process.cwd(), 'export.json'),
      JSON.stringify(mockSessionData, null, 2),
      'utf-8',
    );
    expect(mockContext.ui.setPendingItem).toHaveBeenCalledWith(
      expect.objectContaining({
        type: 'export_session',
        exportSession: { isPending: true },
      }),
    );
    expect(mockContext.ui.addItem).toHaveBeenCalledWith(
      expect.objectContaining({
        type: 'export_session',
        exportSession: {
          isPending: false,
          targetPath: expect.stringContaining('export.json'),
        },
      }),
      expect.any(Number),
    );
    expect(mockContext.ui.setPendingItem).toHaveBeenLastCalledWith(null);
  });

  it('should return error if resolveSession fails', async () => {
    vi.mocked(SessionSelector.prototype.resolveSession).mockRejectedValue(
      new Error('Session not found'),
    );

    const result = await exportSessionCommand.action!(mockContext, '');

    expect(result).toEqual({
      type: 'message',
      messageType: 'error',
      content: 'Failed to export session: Session not found',
    });
  });
});