|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import fs from 'fs-extra'; |
| 3 | +import { TelemetryClient } from '../../src/telemetry/TelemetryClient.js'; |
| 4 | + |
| 5 | +const { tempRoot } = vi.hoisted(() => ({ |
| 6 | + tempRoot: `/tmp/autohand-telemetry-client-${process.pid}`, |
| 7 | +})); |
| 8 | + |
| 9 | +vi.mock('../../src/constants.js', () => ({ |
| 10 | + AUTOHAND_PATHS: { |
| 11 | + telemetry: `${tempRoot}/telemetry`, |
| 12 | + }, |
| 13 | + AUTOHAND_FILES: { |
| 14 | + telemetryQueue: `${tempRoot}/telemetry/queue.json`, |
| 15 | + sessionSyncQueue: `${tempRoot}/telemetry/session-sync-queue.json`, |
| 16 | + deviceId: `${tempRoot}/device-id`, |
| 17 | + }, |
| 18 | +})); |
| 19 | + |
| 20 | +describe('TelemetryClient session sync', () => { |
| 21 | + beforeEach(async () => { |
| 22 | + await fs.remove(tempRoot); |
| 23 | + vi.stubGlobal('fetch', vi.fn(async (input: RequestInfo | URL) => { |
| 24 | + const url = String(input); |
| 25 | + if (url.endsWith('/health')) { |
| 26 | + return new Response('ok', { status: 200 }); |
| 27 | + } |
| 28 | + return new Response(JSON.stringify({ id: 'history-1' }), { status: 200 }); |
| 29 | + })); |
| 30 | + }); |
| 31 | + |
| 32 | + afterEach(async () => { |
| 33 | + vi.unstubAllGlobals(); |
| 34 | + await fs.remove(tempRoot); |
| 35 | + }); |
| 36 | + |
| 37 | + it('does not upload session snapshots without a logged-in auth token', async () => { |
| 38 | + const client = new TelemetryClient({ |
| 39 | + enabled: false, |
| 40 | + enableSessionSync: true, |
| 41 | + apiBaseUrl: 'https://api.example.test', |
| 42 | + }); |
| 43 | + |
| 44 | + const result = await client.uploadSession({ |
| 45 | + sessionId: 'session-1', |
| 46 | + messages: [{ role: 'user', content: 'hello' }], |
| 47 | + }); |
| 48 | + |
| 49 | + expect(result).toEqual({ success: false, error: 'Login required for session sync' }); |
| 50 | + expect(fetch).not.toHaveBeenCalledWith( |
| 51 | + 'https://api.example.test/v1/history', |
| 52 | + expect.anything() |
| 53 | + ); |
| 54 | + }); |
| 55 | + |
| 56 | + it('uploads session snapshots with the user auth token even when telemetry events are disabled', async () => { |
| 57 | + const client = new TelemetryClient({ |
| 58 | + enabled: false, |
| 59 | + enableSessionSync: true, |
| 60 | + apiBaseUrl: 'https://api.example.test', |
| 61 | + authToken: 'auth-token-123', |
| 62 | + clientVersion: '0.8.2', |
| 63 | + }); |
| 64 | + |
| 65 | + const result = await client.uploadSession({ |
| 66 | + sessionId: 'session-1', |
| 67 | + messages: [{ role: 'user', content: 'hello' }], |
| 68 | + }); |
| 69 | + |
| 70 | + expect(result).toEqual({ success: true, id: 'history-1' }); |
| 71 | + expect(fetch).toHaveBeenCalledWith( |
| 72 | + 'https://api.example.test/v1/history', |
| 73 | + expect.objectContaining({ |
| 74 | + method: 'POST', |
| 75 | + headers: expect.objectContaining({ |
| 76 | + Authorization: 'Bearer auth-token-123', |
| 77 | + 'X-CLI-Version': '0.8.2', |
| 78 | + }), |
| 79 | + }) |
| 80 | + ); |
| 81 | + }); |
| 82 | +}); |
0 commit comments