chore(#1): apply Prettier formatting to all source and test files
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { TelemetryClient } from '../src/client.js';
|
||||
import { TelemetryConfig } from '../src/config.js';
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
||||
import { TelemetryClient } from "../src/client.js";
|
||||
import { TelemetryConfig } from "../src/config.js";
|
||||
import {
|
||||
TaskCompletionEvent,
|
||||
TaskType,
|
||||
@@ -8,14 +8,17 @@ import {
|
||||
Harness,
|
||||
Provider,
|
||||
Outcome,
|
||||
} from '../src/types/events.js';
|
||||
import { PredictionQuery, PredictionResponse } from '../src/types/predictions.js';
|
||||
} from "../src/types/events.js";
|
||||
import {
|
||||
PredictionQuery,
|
||||
PredictionResponse,
|
||||
} from "../src/types/predictions.js";
|
||||
|
||||
function makeConfig(overrides: Partial<TelemetryConfig> = {}): TelemetryConfig {
|
||||
return {
|
||||
serverUrl: 'https://tel.example.com',
|
||||
apiKey: 'a'.repeat(64),
|
||||
instanceId: 'test-instance',
|
||||
serverUrl: "https://tel.example.com",
|
||||
apiKey: "a".repeat(64),
|
||||
instanceId: "test-instance",
|
||||
submitIntervalMs: 60_000,
|
||||
maxQueueSize: 100,
|
||||
batchSize: 10,
|
||||
@@ -25,17 +28,17 @@ function makeConfig(overrides: Partial<TelemetryConfig> = {}): TelemetryConfig {
|
||||
};
|
||||
}
|
||||
|
||||
function makeEvent(id = 'evt-1'): TaskCompletionEvent {
|
||||
function makeEvent(id = "evt-1"): TaskCompletionEvent {
|
||||
return {
|
||||
instance_id: 'test-instance',
|
||||
instance_id: "test-instance",
|
||||
event_id: id,
|
||||
schema_version: '1.0',
|
||||
schema_version: "1.0",
|
||||
timestamp: new Date().toISOString(),
|
||||
task_duration_ms: 5000,
|
||||
task_type: TaskType.IMPLEMENTATION,
|
||||
complexity: Complexity.MEDIUM,
|
||||
harness: Harness.CLAUDE_CODE,
|
||||
model: 'claude-3-opus',
|
||||
model: "claude-3-opus",
|
||||
provider: Provider.ANTHROPIC,
|
||||
estimated_input_tokens: 1000,
|
||||
estimated_output_tokens: 500,
|
||||
@@ -57,7 +60,7 @@ function makeEvent(id = 'evt-1'): TaskCompletionEvent {
|
||||
function makeQuery(): PredictionQuery {
|
||||
return {
|
||||
task_type: TaskType.IMPLEMENTATION,
|
||||
model: 'claude-3-opus',
|
||||
model: "claude-3-opus",
|
||||
provider: Provider.ANTHROPIC,
|
||||
complexity: Complexity.MEDIUM,
|
||||
};
|
||||
@@ -76,20 +79,20 @@ function makePredictionResponse(): PredictionResponse {
|
||||
metadata: {
|
||||
sample_size: 100,
|
||||
fallback_level: 0,
|
||||
confidence: 'high',
|
||||
confidence: "high",
|
||||
last_updated: new Date().toISOString(),
|
||||
cache_hit: false,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
describe('TelemetryClient', () => {
|
||||
describe("TelemetryClient", () => {
|
||||
let fetchSpy: ReturnType<typeof vi.fn>;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
fetchSpy = vi.fn();
|
||||
vi.stubGlobal('fetch', fetchSpy);
|
||||
vi.stubGlobal("fetch", fetchSpy);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -97,8 +100,8 @@ describe('TelemetryClient', () => {
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
describe('start/stop lifecycle', () => {
|
||||
it('should start and stop cleanly', async () => {
|
||||
describe("start/stop lifecycle", () => {
|
||||
it("should start and stop cleanly", async () => {
|
||||
const client = new TelemetryClient(makeConfig());
|
||||
|
||||
expect(client.isRunning).toBe(false);
|
||||
@@ -109,26 +112,26 @@ describe('TelemetryClient', () => {
|
||||
expect(client.isRunning).toBe(false);
|
||||
});
|
||||
|
||||
it('should be idempotent on start', () => {
|
||||
it("should be idempotent on start", () => {
|
||||
const client = new TelemetryClient(makeConfig());
|
||||
client.start();
|
||||
client.start(); // Should not throw or create double intervals
|
||||
expect(client.isRunning).toBe(true);
|
||||
});
|
||||
|
||||
it('should be idempotent on stop', async () => {
|
||||
it("should be idempotent on stop", async () => {
|
||||
const client = new TelemetryClient(makeConfig());
|
||||
await client.stop();
|
||||
await client.stop(); // Should not throw
|
||||
expect(client.isRunning).toBe(false);
|
||||
});
|
||||
|
||||
it('should flush events on stop', async () => {
|
||||
it("should flush events on stop", async () => {
|
||||
const client = new TelemetryClient(makeConfig());
|
||||
client.start();
|
||||
|
||||
client.track(makeEvent('e1'));
|
||||
client.track(makeEvent('e2'));
|
||||
client.track(makeEvent("e1"));
|
||||
client.track(makeEvent("e2"));
|
||||
expect(client.queueSize).toBe(2);
|
||||
|
||||
await client.stop();
|
||||
@@ -137,21 +140,21 @@ describe('TelemetryClient', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('track()', () => {
|
||||
it('should queue events', () => {
|
||||
describe("track()", () => {
|
||||
it("should queue events", () => {
|
||||
const client = new TelemetryClient(makeConfig());
|
||||
client.track(makeEvent('e1'));
|
||||
client.track(makeEvent('e2'));
|
||||
client.track(makeEvent("e1"));
|
||||
client.track(makeEvent("e2"));
|
||||
expect(client.queueSize).toBe(2);
|
||||
});
|
||||
|
||||
it('should silently drop events when disabled', () => {
|
||||
it("should silently drop events when disabled", () => {
|
||||
const client = new TelemetryClient(makeConfig({ enabled: false }));
|
||||
client.track(makeEvent());
|
||||
expect(client.queueSize).toBe(0);
|
||||
});
|
||||
|
||||
it('should never throw even on internal error', () => {
|
||||
it("should never throw even on internal error", () => {
|
||||
const errorFn = vi.fn();
|
||||
const client = new TelemetryClient(
|
||||
makeConfig({ onError: errorFn, maxQueueSize: 0 }),
|
||||
@@ -163,14 +166,14 @@ describe('TelemetryClient', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('predictions', () => {
|
||||
it('should return null for uncached prediction', () => {
|
||||
describe("predictions", () => {
|
||||
it("should return null for uncached prediction", () => {
|
||||
const client = new TelemetryClient(makeConfig());
|
||||
const result = client.getPrediction(makeQuery());
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('should return cached prediction after refresh', async () => {
|
||||
it("should return cached prediction after refresh", async () => {
|
||||
const predictionResponse = makePredictionResponse();
|
||||
fetchSpy.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
@@ -190,8 +193,8 @@ describe('TelemetryClient', () => {
|
||||
expect(result).toEqual(predictionResponse);
|
||||
});
|
||||
|
||||
it('should handle refresh error gracefully', async () => {
|
||||
fetchSpy.mockRejectedValueOnce(new Error('Network error'));
|
||||
it("should handle refresh error gracefully", async () => {
|
||||
fetchSpy.mockRejectedValueOnce(new Error("Network error"));
|
||||
|
||||
const errorFn = vi.fn();
|
||||
const client = new TelemetryClient(
|
||||
@@ -203,11 +206,11 @@ describe('TelemetryClient', () => {
|
||||
expect(errorFn).toHaveBeenCalledWith(expect.any(Error));
|
||||
});
|
||||
|
||||
it('should handle non-ok HTTP response on refresh', async () => {
|
||||
it("should handle non-ok HTTP response on refresh", async () => {
|
||||
fetchSpy.mockResolvedValueOnce({
|
||||
ok: false,
|
||||
status: 500,
|
||||
statusText: 'Internal Server Error',
|
||||
statusText: "Internal Server Error",
|
||||
});
|
||||
|
||||
const errorFn = vi.fn();
|
||||
@@ -220,14 +223,14 @@ describe('TelemetryClient', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('background flush', () => {
|
||||
it('should trigger flush on interval', async () => {
|
||||
describe("background flush", () => {
|
||||
it("should trigger flush on interval", async () => {
|
||||
const client = new TelemetryClient(
|
||||
makeConfig({ submitIntervalMs: 10_000 }),
|
||||
);
|
||||
client.start();
|
||||
|
||||
client.track(makeEvent('e1'));
|
||||
client.track(makeEvent("e1"));
|
||||
expect(client.queueSize).toBe(1);
|
||||
|
||||
// Advance past submit interval
|
||||
@@ -240,13 +243,13 @@ describe('TelemetryClient', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('flush error handling', () => {
|
||||
it('should re-enqueue events on submit failure', async () => {
|
||||
describe("flush error handling", () => {
|
||||
it("should re-enqueue events on submit failure", async () => {
|
||||
// Use non-dryRun mode to actually hit the submitter
|
||||
fetchSpy.mockResolvedValueOnce({
|
||||
ok: false,
|
||||
status: 500,
|
||||
statusText: 'Internal Server Error',
|
||||
statusText: "Internal Server Error",
|
||||
});
|
||||
|
||||
const errorFn = vi.fn();
|
||||
@@ -254,7 +257,7 @@ describe('TelemetryClient', () => {
|
||||
makeConfig({ dryRun: false, maxRetries: 0, onError: errorFn }),
|
||||
);
|
||||
|
||||
client.track(makeEvent('e1'));
|
||||
client.track(makeEvent("e1"));
|
||||
expect(client.queueSize).toBe(1);
|
||||
|
||||
// Start and trigger flush
|
||||
@@ -267,9 +270,9 @@ describe('TelemetryClient', () => {
|
||||
await client.stop();
|
||||
});
|
||||
|
||||
it('should handle onError callback that throws', async () => {
|
||||
it("should handle onError callback that throws", async () => {
|
||||
const throwingErrorFn = () => {
|
||||
throw new Error('Error handler broke');
|
||||
throw new Error("Error handler broke");
|
||||
};
|
||||
const client = new TelemetryClient(
|
||||
makeConfig({ onError: throwingErrorFn, enabled: false }),
|
||||
@@ -278,13 +281,15 @@ describe('TelemetryClient', () => {
|
||||
// This should not throw even though onError throws
|
||||
// Force an error path by calling track when disabled (no error),
|
||||
// but we can test via refreshPredictions
|
||||
fetchSpy.mockRejectedValueOnce(new Error('fail'));
|
||||
await expect(client.refreshPredictions([makeQuery()])).resolves.not.toThrow();
|
||||
fetchSpy.mockRejectedValueOnce(new Error("fail"));
|
||||
await expect(
|
||||
client.refreshPredictions([makeQuery()]),
|
||||
).resolves.not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('event builder', () => {
|
||||
it('should expose an event builder', () => {
|
||||
describe("event builder", () => {
|
||||
it("should expose an event builder", () => {
|
||||
const client = new TelemetryClient(makeConfig());
|
||||
expect(client.eventBuilder).toBeDefined();
|
||||
|
||||
@@ -293,7 +298,7 @@ describe('TelemetryClient', () => {
|
||||
task_type: TaskType.TESTING,
|
||||
complexity: Complexity.LOW,
|
||||
harness: Harness.AIDER,
|
||||
model: 'gpt-4',
|
||||
model: "gpt-4",
|
||||
provider: Provider.OPENAI,
|
||||
estimated_input_tokens: 100,
|
||||
estimated_output_tokens: 50,
|
||||
@@ -311,8 +316,8 @@ describe('TelemetryClient', () => {
|
||||
retry_count: 0,
|
||||
});
|
||||
|
||||
expect(event.instance_id).toBe('test-instance');
|
||||
expect(event.schema_version).toBe('1.0');
|
||||
expect(event.instance_id).toBe("test-instance");
|
||||
expect(event.schema_version).toBe("1.0");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user