|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; |
| 8 | +import * as fs from 'node:fs/promises'; |
| 9 | +import * as path from 'node:path'; |
| 10 | +import * as os from 'node:os'; |
| 11 | +import { TeleportService, Config, ChatRecordingService } from '@google/gemini-cli-core'; |
| 12 | + |
| 13 | +describe('Teleport E2E Integration', () => { |
| 14 | + let tmpDir: string; |
| 15 | + let machineA_Home: string; |
| 16 | + let machineB_Home: string; |
| 17 | + let projectDir: string; |
| 18 | + |
| 19 | + beforeEach(async () => { |
| 20 | + tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'gemini-teleport-e2e-')); |
| 21 | + machineA_Home = path.join(tmpDir, 'machineA'); |
| 22 | + machineB_Home = path.join(tmpDir, 'machineB'); |
| 23 | + projectDir = path.join(tmpDir, 'my-project'); |
| 24 | + |
| 25 | + await fs.mkdir(machineA_Home, { recursive: true }); |
| 26 | + await fs.mkdir(machineB_Home, { recursive: true }); |
| 27 | + await fs.mkdir(projectDir, { recursive: true }); |
| 28 | + }); |
| 29 | + |
| 30 | + afterEach(async () => { |
| 31 | + await fs.rm(tmpDir, { recursive: true, force: true }); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should round-trip a session between two simulated machines', async () => { |
| 35 | + // --- STEP 1: Setup Machine A and create a session --- |
| 36 | + const configA = new Config({ |
| 37 | + sessionId: 'session-123', |
| 38 | + targetDir: projectDir, |
| 39 | + cwd: projectDir, |
| 40 | + model: 'test-model', |
| 41 | + }); |
| 42 | + const machineA_TempDir = path.join(machineA_Home, 'tmp'); |
| 43 | + await fs.mkdir(machineA_TempDir, { recursive: true }); |
| 44 | + vi.spyOn(configA.storage, 'getProjectTempDir').mockReturnValue(machineA_TempDir); |
| 45 | + |
| 46 | + const recordingServiceA = new ChatRecordingService(configA); |
| 47 | + recordingServiceA.initialize(); |
| 48 | + |
| 49 | + recordingServiceA.recordMessage({ |
| 50 | + model: 'test-model', |
| 51 | + type: 'user', |
| 52 | + content: [{ text: 'Hello from Machine A' }] |
| 53 | + }); |
| 54 | + |
| 55 | + const realSessionId = configA.getSessionId(); |
| 56 | + const chatFilePathA = recordingServiceA.getConversationFilePath(); |
| 57 | + expect(chatFilePathA).not.toBeNull(); |
| 58 | + |
| 59 | + // --- STEP 2: Export from Machine A --- |
| 60 | + const teleportServiceA = new TeleportService(configA); |
| 61 | + const tarballPath = path.join(tmpDir, 'teleport.tar.gz'); |
| 62 | + await teleportServiceA.exportSession(realSessionId, tarballPath); |
| 63 | + |
| 64 | + // --- STEP 3: Setup Machine B and Import --- |
| 65 | + const configB = new Config({ |
| 66 | + sessionId: 'new-session', |
| 67 | + targetDir: projectDir, |
| 68 | + cwd: projectDir, |
| 69 | + model: 'test-model', |
| 70 | + }); |
| 71 | + const machineB_TempDir = path.join(machineB_Home, 'tmp'); |
| 72 | + await fs.mkdir(machineB_TempDir, { recursive: true }); |
| 73 | + vi.spyOn(configB.storage, 'getProjectTempDir').mockReturnValue(machineB_TempDir); |
| 74 | + |
| 75 | + const teleportServiceB = new TeleportService(configB); |
| 76 | + const importResult = await teleportServiceB.importSession(tarballPath); |
| 77 | + |
| 78 | + expect(importResult.sessionId).toBe(realSessionId); |
| 79 | + |
| 80 | + // --- STEP 4: Verify Machine B can "see" the session --- |
| 81 | + const chatFiles = await configB.storage.listProjectChatFiles(); |
| 82 | + expect(chatFiles.length).toBe(1); |
| 83 | + |
| 84 | + // storage.listProjectChatFiles returns relative paths |
| 85 | + const importedFile = path.join(machineB_TempDir, chatFiles[0].filePath); |
| 86 | + const conversationData = JSON.parse(await fs.readFile(importedFile, 'utf8')); |
| 87 | + |
| 88 | + const recordingServiceB = new ChatRecordingService(configB); |
| 89 | + recordingServiceB.initialize({ |
| 90 | + filePath: importedFile, |
| 91 | + conversation: conversationData |
| 92 | + }); |
| 93 | + |
| 94 | + const conversation = recordingServiceB.getConversation(); |
| 95 | + expect(conversation).not.toBeNull(); |
| 96 | + expect(conversation?.messages[0].content[0].text).toBe('Hello from Machine A'); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should handle encrypted sessions in E2E', async () => { |
| 100 | + const secret = 'password123'; |
| 101 | + const configA = new Config({ sessionId: 'enc-session', targetDir: projectDir, cwd: projectDir, model: 'm' }); |
| 102 | + const machineA_TempDir = path.join(machineA_Home, 'tmp'); |
| 103 | + await fs.mkdir(machineA_TempDir, { recursive: true }); |
| 104 | + vi.spyOn(configA.storage, 'getProjectTempDir').mockReturnValue(machineA_TempDir); |
| 105 | + |
| 106 | + const recordingServiceA = new ChatRecordingService(configA); |
| 107 | + recordingServiceA.initialize(); |
| 108 | + recordingServiceA.recordMessage({ model: 'm', type: 'user', content: [{ text: 'Encrypted message' }] }); |
| 109 | + |
| 110 | + const realSessionId = configA.getSessionId(); |
| 111 | + const teleportServiceA = new TeleportService(configA); |
| 112 | + const tarballPath = path.join(tmpDir, 'encrypted.tar.gz'); |
| 113 | + await teleportServiceA.exportSession(realSessionId, tarballPath, secret); |
| 114 | + |
| 115 | + // Machine B |
| 116 | + const configB = new Config({ sessionId: 'b', targetDir: projectDir, cwd: projectDir, model: 'm' }); |
| 117 | + const machineB_TempDir = path.join(machineB_Home, 'tmp'); |
| 118 | + await fs.mkdir(machineB_TempDir, { recursive: true }); |
| 119 | + vi.spyOn(configB.storage, 'getProjectTempDir').mockReturnValue(machineB_TempDir); |
| 120 | + |
| 121 | + const teleportServiceB = new TeleportService(configB); |
| 122 | + await teleportServiceB.importSession(tarballPath, secret); |
| 123 | + |
| 124 | + const chatFiles = await configB.storage.listProjectChatFiles(); |
| 125 | + const importedFile = path.join(machineB_TempDir, chatFiles[0].filePath); |
| 126 | + const conversationData = JSON.parse(await fs.readFile(importedFile, 'utf8')); |
| 127 | + |
| 128 | + const recordingServiceB = new ChatRecordingService(configB); |
| 129 | + recordingServiceB.initialize({ |
| 130 | + filePath: importedFile, |
| 131 | + conversation: conversationData |
| 132 | + }); |
| 133 | + |
| 134 | + const conversation = recordingServiceB.getConversation(); |
| 135 | + expect(conversation?.messages[0].content[0].text).toBe('Encrypted message'); |
| 136 | + }); |
| 137 | +}); |
0 commit comments