|
| 1 | +/* eslint-disable @typescript-eslint/require-await */ |
| 2 | +import { CANCELLED, TelemetryClient } from '../client'; |
| 3 | +import { InMemorySink } from '../sinks/in-memory-sink'; |
| 4 | +import { describe, expect, it } from 'vitest'; |
| 5 | + |
| 6 | +describe('TelemetryClient', () => { |
| 7 | + describe('withCommandRun', () => { |
| 8 | + it('records success with returned attrs', async () => { |
| 9 | + const sink = new InMemorySink(); |
| 10 | + const client = new TelemetryClient(sink); |
| 11 | + |
| 12 | + await client.withCommandRun('update', async () => ({ check_only: true })); |
| 13 | + |
| 14 | + expect(sink.metrics).toHaveLength(1); |
| 15 | + expect(sink.metrics[0]!.attrs).toMatchObject({ |
| 16 | + command_group: 'update', |
| 17 | + command: 'update', |
| 18 | + exit_reason: 'success', |
| 19 | + check_only: 'true', |
| 20 | + }); |
| 21 | + }); |
| 22 | + |
| 23 | + it('accepts sync callbacks', async () => { |
| 24 | + const sink = new InMemorySink(); |
| 25 | + const client = new TelemetryClient(sink); |
| 26 | + |
| 27 | + await client.withCommandRun('telemetry.disable', () => ({})); |
| 28 | + |
| 29 | + expect(sink.metrics).toHaveLength(1); |
| 30 | + expect(sink.metrics[0]!.attrs).toMatchObject({ exit_reason: 'success' }); |
| 31 | + }); |
| 32 | + |
| 33 | + it('records failure and re-throws on error', async () => { |
| 34 | + const sink = new InMemorySink(); |
| 35 | + const client = new TelemetryClient(sink); |
| 36 | + |
| 37 | + await expect( |
| 38 | + client.withCommandRun('deploy', async () => { |
| 39 | + throw new Error('boom'); |
| 40 | + }) |
| 41 | + ).rejects.toThrow('boom'); |
| 42 | + |
| 43 | + expect(sink.metrics).toHaveLength(1); |
| 44 | + expect(sink.metrics[0]!.attrs).toMatchObject({ |
| 45 | + command_group: 'deploy', |
| 46 | + exit_reason: 'failure', |
| 47 | + error_name: 'UnknownError', |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + it('classifies PackagingError subclasses', async () => { |
| 52 | + const sink = new InMemorySink(); |
| 53 | + const client = new TelemetryClient(sink); |
| 54 | + |
| 55 | + class MissingDependencyError extends Error { |
| 56 | + constructor() { |
| 57 | + super('missing dep'); |
| 58 | + this.name = 'MissingDependencyError'; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + await expect( |
| 63 | + client.withCommandRun('deploy', async () => { |
| 64 | + throw new MissingDependencyError(); |
| 65 | + }) |
| 66 | + ).rejects.toThrow(); |
| 67 | + |
| 68 | + expect(sink.metrics[0]!.attrs).toMatchObject({ |
| 69 | + error_name: 'PackagingError', |
| 70 | + is_user_error: 'false', |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + it('marks credential errors as user errors', async () => { |
| 75 | + const sink = new InMemorySink(); |
| 76 | + const client = new TelemetryClient(sink); |
| 77 | + |
| 78 | + class AwsCredentialsError extends Error { |
| 79 | + constructor() { |
| 80 | + super('creds expired'); |
| 81 | + this.name = 'AwsCredentialsError'; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + await expect( |
| 86 | + client.withCommandRun('invoke', async () => { |
| 87 | + throw new AwsCredentialsError(); |
| 88 | + }) |
| 89 | + ).rejects.toThrow(); |
| 90 | + |
| 91 | + expect(sink.metrics[0]!.attrs).toMatchObject({ |
| 92 | + error_name: 'CredentialsError', |
| 93 | + is_user_error: 'true', |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + it('records duration as a non-negative integer', async () => { |
| 98 | + const sink = new InMemorySink(); |
| 99 | + const client = new TelemetryClient(sink); |
| 100 | + |
| 101 | + await client.withCommandRun('telemetry.disable', async () => { |
| 102 | + await new Promise(r => globalThis.setTimeout(r, 5)); |
| 103 | + return {}; |
| 104 | + }); |
| 105 | + |
| 106 | + expect(sink.metrics[0]!.value).toBeGreaterThanOrEqual(0); |
| 107 | + expect(Number.isInteger(sink.metrics[0]!.value)).toBe(true); |
| 108 | + }); |
| 109 | + |
| 110 | + it('converts boolean attrs to strings', async () => { |
| 111 | + const sink = new InMemorySink(); |
| 112 | + const client = new TelemetryClient(sink); |
| 113 | + |
| 114 | + await client.withCommandRun('update', async () => ({ check_only: true })); |
| 115 | + |
| 116 | + expect(sink.metrics[0]!.attrs.check_only).toBe('true'); |
| 117 | + }); |
| 118 | + |
| 119 | + it('silently drops invalid success payloads', async () => { |
| 120 | + const sink = new InMemorySink(); |
| 121 | + const client = new TelemetryClient(sink); |
| 122 | + |
| 123 | + // Missing required attrs for 'create' — should silently drop |
| 124 | + await client.withCommandRun( |
| 125 | + 'create', |
| 126 | + // @ts-expect-error — intentionally incomplete |
| 127 | + async () => ({ language: 'python' }) |
| 128 | + ); |
| 129 | + |
| 130 | + expect(sink.metrics).toHaveLength(0); |
| 131 | + }); |
| 132 | + |
| 133 | + it('records cancel when callback returns CANCELLED', async () => { |
| 134 | + const sink = new InMemorySink(); |
| 135 | + const client = new TelemetryClient(sink); |
| 136 | + |
| 137 | + await client.withCommandRun('deploy', () => CANCELLED); |
| 138 | + |
| 139 | + expect(sink.metrics).toHaveLength(1); |
| 140 | + expect(sink.metrics[0]!.attrs).toMatchObject({ |
| 141 | + command_group: 'deploy', |
| 142 | + exit_reason: 'cancel', |
| 143 | + }); |
| 144 | + }); |
| 145 | + }); |
| 146 | +}); |
0 commit comments