chore(#1): apply Prettier formatting to all source and test files
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:
2026-02-14 22:48:08 -06:00
parent 9df760cab2
commit 493bc72601
16 changed files with 283 additions and 262 deletions

View File

@@ -1,11 +1,11 @@
import { TelemetryConfig, ResolvedConfig, resolveConfig } from './config.js'; import { TelemetryConfig, ResolvedConfig, resolveConfig } from "./config.js";
import { EventQueue } from './queue.js'; import { EventQueue } from "./queue.js";
import { BatchSubmitter } from './submitter.js'; import { BatchSubmitter } from "./submitter.js";
import { PredictionCache } from './prediction-cache.js'; import { PredictionCache } from "./prediction-cache.js";
import { EventBuilder } from './event-builder.js'; import { EventBuilder } from "./event-builder.js";
import { TaskCompletionEvent } from './types/events.js'; import { TaskCompletionEvent } from "./types/events.js";
import { PredictionQuery, PredictionResponse } from './types/predictions.js'; import { PredictionQuery, PredictionResponse } from "./types/predictions.js";
import { BatchPredictionResponse } from './types/common.js'; import { BatchPredictionResponse } from "./types/common.js";
/** /**
* Main telemetry client. Queues task-completion events for background * Main telemetry client. Queues task-completion events for background
@@ -24,7 +24,9 @@ export class TelemetryClient {
this.config = resolveConfig(config); this.config = resolveConfig(config);
this.queue = new EventQueue(this.config.maxQueueSize); this.queue = new EventQueue(this.config.maxQueueSize);
this.submitter = new BatchSubmitter(this.config); this.submitter = new BatchSubmitter(this.config);
this.predictionCache = new PredictionCache(this.config.predictionCacheTtlMs); this.predictionCache = new PredictionCache(
this.config.predictionCacheTtlMs,
);
this._eventBuilder = new EventBuilder(this.config); this._eventBuilder = new EventBuilder(this.config);
} }
@@ -86,9 +88,9 @@ export class TelemetryClient {
try { try {
const response = await fetch(url, { const response = await fetch(url, {
method: 'POST', method: "POST",
headers: { headers: {
'Content-Type': 'application/json', "Content-Type": "application/json",
}, },
body: JSON.stringify({ queries }), body: JSON.stringify({ queries }),
signal: controller.signal, signal: controller.signal,

View File

@@ -46,7 +46,7 @@ const DEFAULT_ON_ERROR = (_error: Error): void => {
export function resolveConfig(config: TelemetryConfig): ResolvedConfig { export function resolveConfig(config: TelemetryConfig): ResolvedConfig {
return { return {
serverUrl: config.serverUrl.replace(/\/+$/, ''), serverUrl: config.serverUrl.replace(/\/+$/, ""),
apiKey: config.apiKey, apiKey: config.apiKey,
instanceId: config.instanceId, instanceId: config.instanceId,
enabled: config.enabled ?? true, enabled: config.enabled ?? true,

View File

@@ -1,4 +1,4 @@
import { ResolvedConfig } from './config.js'; import { ResolvedConfig } from "./config.js";
import { import {
Complexity, Complexity,
Harness, Harness,
@@ -8,7 +8,7 @@ import {
RepoSizeCategory, RepoSizeCategory,
TaskCompletionEvent, TaskCompletionEvent,
TaskType, TaskType,
} from './types/events.js'; } from "./types/events.js";
export interface EventBuilderParams { export interface EventBuilderParams {
task_duration_ms: number; task_duration_ms: number;
@@ -54,7 +54,7 @@ export class EventBuilder {
return { return {
instance_id: this.config.instanceId, instance_id: this.config.instanceId,
event_id: crypto.randomUUID(), event_id: crypto.randomUUID(),
schema_version: '1.0', schema_version: "1.0",
timestamp: new Date().toISOString(), timestamp: new Date().toISOString(),
...params, ...params,
}; };

View File

@@ -1,12 +1,12 @@
export { TelemetryClient } from './client.js'; export { TelemetryClient } from "./client.js";
export { EventBuilder } from './event-builder.js'; export { EventBuilder } from "./event-builder.js";
export { EventQueue } from './queue.js'; export { EventQueue } from "./queue.js";
export { BatchSubmitter } from './submitter.js'; export { BatchSubmitter } from "./submitter.js";
export { PredictionCache } from './prediction-cache.js'; export { PredictionCache } from "./prediction-cache.js";
export { resolveConfig } from './config.js'; export { resolveConfig } from "./config.js";
export type { TelemetryConfig, ResolvedConfig } from './config.js'; export type { TelemetryConfig, ResolvedConfig } from "./config.js";
export type { EventBuilderParams } from './event-builder.js'; export type { EventBuilderParams } from "./event-builder.js";
export type { SubmitResult } from './submitter.js'; export type { SubmitResult } from "./submitter.js";
// Re-export all types // Re-export all types
export { export {
@@ -17,7 +17,7 @@ export {
QualityGate, QualityGate,
Outcome, Outcome,
RepoSizeCategory, RepoSizeCategory,
} from './types/index.js'; } from "./types/index.js";
export type { export type {
TaskCompletionEvent, TaskCompletionEvent,
@@ -33,4 +33,4 @@ export type {
BatchEventResponse, BatchEventResponse,
BatchPredictionRequest, BatchPredictionRequest,
BatchPredictionResponse, BatchPredictionResponse,
} from './types/index.js'; } from "./types/index.js";

View File

@@ -1,4 +1,4 @@
import { PredictionQuery, PredictionResponse } from './types/predictions.js'; import { PredictionQuery, PredictionResponse } from "./types/predictions.js";
interface CacheEntry { interface CacheEntry {
response: PredictionResponse; response: PredictionResponse;

View File

@@ -1,4 +1,4 @@
import { TaskCompletionEvent } from './types/events.js'; import { TaskCompletionEvent } from "./types/events.js";
/** /**
* Bounded FIFO event queue. When the queue is full, the oldest events * Bounded FIFO event queue. When the queue is full, the oldest events

View File

@@ -1,8 +1,8 @@
import { ResolvedConfig } from './config.js'; import { ResolvedConfig } from "./config.js";
import { TaskCompletionEvent } from './types/events.js'; import { TaskCompletionEvent } from "./types/events.js";
import { BatchEventResponse } from './types/common.js'; import { BatchEventResponse } from "./types/common.js";
const SDK_VERSION = '0.1.0'; const SDK_VERSION = "0.1.0";
const USER_AGENT = `mosaic-telemetry-client-js/${SDK_VERSION}`; const USER_AGENT = `mosaic-telemetry-client-js/${SDK_VERSION}`;
export interface SubmitResult { export interface SubmitResult {
@@ -36,7 +36,7 @@ export class BatchSubmitter {
rejected: 0, rejected: 0,
results: events.map((e) => ({ results: events.map((e) => ({
event_id: e.event_id, event_id: e.event_id,
status: 'accepted' as const, status: "accepted" as const,
})), })),
}, },
}; };
@@ -68,7 +68,7 @@ export class BatchSubmitter {
return { return {
success: false, success: false,
error: lastError ?? new Error('Max retries exceeded'), error: lastError ?? new Error("Max retries exceeded"),
}; };
} }
@@ -84,19 +84,21 @@ export class BatchSubmitter {
try { try {
const response = await fetch(url, { const response = await fetch(url, {
method: 'POST', method: "POST",
headers: { headers: {
'Content-Type': 'application/json', "Content-Type": "application/json",
Authorization: `Bearer ${this.config.apiKey}`, Authorization: `Bearer ${this.config.apiKey}`,
'User-Agent': USER_AGENT, "User-Agent": USER_AGENT,
}, },
body: JSON.stringify({ events }), body: JSON.stringify({ events }),
signal: controller.signal, signal: controller.signal,
}); });
if (response.status === 429) { if (response.status === 429) {
const retryAfter = response.headers.get('Retry-After'); const retryAfter = response.headers.get("Retry-After");
const retryAfterMs = retryAfter ? parseInt(retryAfter, 10) * 1000 : 5000; const retryAfterMs = retryAfter
? parseInt(retryAfter, 10) * 1000
: 5000;
return { success: false, retryAfterMs }; return { success: false, retryAfterMs };
} }

View File

@@ -1,5 +1,5 @@
import { TaskCompletionEvent } from './events.js'; import { TaskCompletionEvent } from "./events.js";
import { PredictionQuery, PredictionResponse } from './predictions.js'; import { PredictionQuery, PredictionResponse } from "./predictions.js";
export interface BatchEventRequest { export interface BatchEventRequest {
events: TaskCompletionEvent[]; events: TaskCompletionEvent[];
@@ -7,7 +7,7 @@ export interface BatchEventRequest {
export interface BatchEventResult { export interface BatchEventResult {
event_id: string; event_id: string;
status: 'accepted' | 'rejected'; status: "accepted" | "rejected";
error?: string | null; error?: string | null;
} }

View File

@@ -1,67 +1,67 @@
export enum TaskType { export enum TaskType {
PLANNING = 'planning', PLANNING = "planning",
IMPLEMENTATION = 'implementation', IMPLEMENTATION = "implementation",
CODE_REVIEW = 'code_review', CODE_REVIEW = "code_review",
TESTING = 'testing', TESTING = "testing",
DEBUGGING = 'debugging', DEBUGGING = "debugging",
REFACTORING = 'refactoring', REFACTORING = "refactoring",
DOCUMENTATION = 'documentation', DOCUMENTATION = "documentation",
CONFIGURATION = 'configuration', CONFIGURATION = "configuration",
SECURITY_AUDIT = 'security_audit', SECURITY_AUDIT = "security_audit",
UNKNOWN = 'unknown', UNKNOWN = "unknown",
} }
export enum Complexity { export enum Complexity {
LOW = 'low', LOW = "low",
MEDIUM = 'medium', MEDIUM = "medium",
HIGH = 'high', HIGH = "high",
CRITICAL = 'critical', CRITICAL = "critical",
} }
export enum Harness { export enum Harness {
CLAUDE_CODE = 'claude_code', CLAUDE_CODE = "claude_code",
OPENCODE = 'opencode', OPENCODE = "opencode",
KILO_CODE = 'kilo_code', KILO_CODE = "kilo_code",
AIDER = 'aider', AIDER = "aider",
API_DIRECT = 'api_direct', API_DIRECT = "api_direct",
OLLAMA_LOCAL = 'ollama_local', OLLAMA_LOCAL = "ollama_local",
CUSTOM = 'custom', CUSTOM = "custom",
UNKNOWN = 'unknown', UNKNOWN = "unknown",
} }
export enum Provider { export enum Provider {
ANTHROPIC = 'anthropic', ANTHROPIC = "anthropic",
OPENAI = 'openai', OPENAI = "openai",
OPENROUTER = 'openrouter', OPENROUTER = "openrouter",
OLLAMA = 'ollama', OLLAMA = "ollama",
GOOGLE = 'google', GOOGLE = "google",
MISTRAL = 'mistral', MISTRAL = "mistral",
CUSTOM = 'custom', CUSTOM = "custom",
UNKNOWN = 'unknown', UNKNOWN = "unknown",
} }
export enum QualityGate { export enum QualityGate {
BUILD = 'build', BUILD = "build",
LINT = 'lint', LINT = "lint",
TEST = 'test', TEST = "test",
COVERAGE = 'coverage', COVERAGE = "coverage",
TYPECHECK = 'typecheck', TYPECHECK = "typecheck",
SECURITY = 'security', SECURITY = "security",
} }
export enum Outcome { export enum Outcome {
SUCCESS = 'success', SUCCESS = "success",
FAILURE = 'failure', FAILURE = "failure",
PARTIAL = 'partial', PARTIAL = "partial",
TIMEOUT = 'timeout', TIMEOUT = "timeout",
} }
export enum RepoSizeCategory { export enum RepoSizeCategory {
TINY = 'tiny', TINY = "tiny",
SMALL = 'small', SMALL = "small",
MEDIUM = 'medium', MEDIUM = "medium",
LARGE = 'large', LARGE = "large",
HUGE = 'huge', HUGE = "huge",
} }
export interface TaskCompletionEvent { export interface TaskCompletionEvent {

View File

@@ -7,7 +7,7 @@ export {
Outcome, Outcome,
RepoSizeCategory, RepoSizeCategory,
type TaskCompletionEvent, type TaskCompletionEvent,
} from './events.js'; } from "./events.js";
export { export {
type TokenDistribution, type TokenDistribution,
@@ -17,7 +17,7 @@ export {
type PredictionMetadata, type PredictionMetadata,
type PredictionResponse, type PredictionResponse,
type PredictionQuery, type PredictionQuery,
} from './predictions.js'; } from "./predictions.js";
export { export {
type BatchEventRequest, type BatchEventRequest,
@@ -25,4 +25,4 @@ export {
type BatchEventResponse, type BatchEventResponse,
type BatchPredictionRequest, type BatchPredictionRequest,
type BatchPredictionResponse, type BatchPredictionResponse,
} from './common.js'; } from "./common.js";

View File

@@ -1,4 +1,4 @@
import { Complexity, Provider, TaskType } from './events.js'; import { Complexity, Provider, TaskType } from "./events.js";
export interface TokenDistribution { export interface TokenDistribution {
p10: number; p10: number;
@@ -30,7 +30,7 @@ export interface PredictionData {
export interface PredictionMetadata { export interface PredictionMetadata {
sample_size: number; sample_size: number;
fallback_level: number; fallback_level: number;
confidence: 'none' | 'low' | 'medium' | 'high'; confidence: "none" | "low" | "medium" | "high";
last_updated: string | null; last_updated: string | null;
dimensions_matched?: Record<string, string | null> | null; dimensions_matched?: Record<string, string | null> | null;
fallback_note?: string | null; fallback_note?: string | null;

View File

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

View File

@@ -1,6 +1,6 @@
import { describe, it, expect, vi, afterEach } from 'vitest'; import { describe, it, expect, vi, afterEach } from "vitest";
import { EventBuilder } from '../src/event-builder.js'; import { EventBuilder } from "../src/event-builder.js";
import { ResolvedConfig } from '../src/config.js'; import { ResolvedConfig } from "../src/config.js";
import { import {
TaskType, TaskType,
Complexity, Complexity,
@@ -9,13 +9,13 @@ import {
Outcome, Outcome,
QualityGate, QualityGate,
RepoSizeCategory, RepoSizeCategory,
} from '../src/types/events.js'; } from "../src/types/events.js";
function makeConfig(): ResolvedConfig { function makeConfig(): ResolvedConfig {
return { return {
serverUrl: 'https://tel.example.com', serverUrl: "https://tel.example.com",
apiKey: 'a'.repeat(64), apiKey: "a".repeat(64),
instanceId: 'my-instance-uuid', instanceId: "my-instance-uuid",
enabled: true, enabled: true,
submitIntervalMs: 300_000, submitIntervalMs: 300_000,
maxQueueSize: 1000, maxQueueSize: 1000,
@@ -28,19 +28,19 @@ function makeConfig(): ResolvedConfig {
}; };
} }
describe('EventBuilder', () => { describe("EventBuilder", () => {
afterEach(() => { afterEach(() => {
vi.restoreAllMocks(); vi.restoreAllMocks();
}); });
it('should build a complete TaskCompletionEvent', () => { it("should build a complete TaskCompletionEvent", () => {
const builder = new EventBuilder(makeConfig()); const builder = new EventBuilder(makeConfig());
const event = builder.build({ const event = builder.build({
task_duration_ms: 15000, task_duration_ms: 15000,
task_type: TaskType.IMPLEMENTATION, task_type: TaskType.IMPLEMENTATION,
complexity: Complexity.HIGH, complexity: Complexity.HIGH,
harness: Harness.CLAUDE_CODE, harness: Harness.CLAUDE_CODE,
model: 'claude-3-opus', model: "claude-3-opus",
provider: Provider.ANTHROPIC, provider: Provider.ANTHROPIC,
estimated_input_tokens: 2000, estimated_input_tokens: 2000,
estimated_output_tokens: 1000, estimated_output_tokens: 1000,
@@ -49,37 +49,41 @@ describe('EventBuilder', () => {
estimated_cost_usd_micros: 100000, estimated_cost_usd_micros: 100000,
actual_cost_usd_micros: 110000, actual_cost_usd_micros: 110000,
quality_gate_passed: true, quality_gate_passed: true,
quality_gates_run: [QualityGate.BUILD, QualityGate.TEST, QualityGate.LINT], quality_gates_run: [
QualityGate.BUILD,
QualityGate.TEST,
QualityGate.LINT,
],
quality_gates_failed: [], quality_gates_failed: [],
context_compactions: 2, context_compactions: 2,
context_rotations: 1, context_rotations: 1,
context_utilization_final: 0.75, context_utilization_final: 0.75,
outcome: Outcome.SUCCESS, outcome: Outcome.SUCCESS,
retry_count: 0, retry_count: 0,
language: 'typescript', language: "typescript",
repo_size_category: RepoSizeCategory.MEDIUM, repo_size_category: RepoSizeCategory.MEDIUM,
}); });
expect(event.task_type).toBe(TaskType.IMPLEMENTATION); expect(event.task_type).toBe(TaskType.IMPLEMENTATION);
expect(event.complexity).toBe(Complexity.HIGH); expect(event.complexity).toBe(Complexity.HIGH);
expect(event.model).toBe('claude-3-opus'); expect(event.model).toBe("claude-3-opus");
expect(event.quality_gates_run).toEqual([ expect(event.quality_gates_run).toEqual([
QualityGate.BUILD, QualityGate.BUILD,
QualityGate.TEST, QualityGate.TEST,
QualityGate.LINT, QualityGate.LINT,
]); ]);
expect(event.language).toBe('typescript'); expect(event.language).toBe("typescript");
expect(event.repo_size_category).toBe(RepoSizeCategory.MEDIUM); expect(event.repo_size_category).toBe(RepoSizeCategory.MEDIUM);
}); });
it('should auto-generate event_id as UUID', () => { it("should auto-generate event_id as UUID", () => {
const builder = new EventBuilder(makeConfig()); const builder = new EventBuilder(makeConfig());
const event = builder.build({ const event = builder.build({
task_duration_ms: 1000, task_duration_ms: 1000,
task_type: TaskType.TESTING, task_type: TaskType.TESTING,
complexity: Complexity.LOW, complexity: Complexity.LOW,
harness: Harness.AIDER, harness: Harness.AIDER,
model: 'gpt-4', model: "gpt-4",
provider: Provider.OPENAI, provider: Provider.OPENAI,
estimated_input_tokens: 100, estimated_input_tokens: 100,
estimated_output_tokens: 50, estimated_output_tokens: 50,
@@ -108,7 +112,7 @@ describe('EventBuilder', () => {
task_type: TaskType.TESTING, task_type: TaskType.TESTING,
complexity: Complexity.LOW, complexity: Complexity.LOW,
harness: Harness.AIDER, harness: Harness.AIDER,
model: 'gpt-4', model: "gpt-4",
provider: Provider.OPENAI, provider: Provider.OPENAI,
estimated_input_tokens: 100, estimated_input_tokens: 100,
estimated_output_tokens: 50, estimated_output_tokens: 50,
@@ -129,8 +133,8 @@ describe('EventBuilder', () => {
expect(event.event_id).not.toBe(event2.event_id); expect(event.event_id).not.toBe(event2.event_id);
}); });
it('should auto-set timestamp to ISO 8601', () => { it("should auto-set timestamp to ISO 8601", () => {
const now = new Date('2026-02-07T10:00:00.000Z'); const now = new Date("2026-02-07T10:00:00.000Z");
vi.setSystemTime(now); vi.setSystemTime(now);
const builder = new EventBuilder(makeConfig()); const builder = new EventBuilder(makeConfig());
@@ -139,7 +143,7 @@ describe('EventBuilder', () => {
task_type: TaskType.DEBUGGING, task_type: TaskType.DEBUGGING,
complexity: Complexity.MEDIUM, complexity: Complexity.MEDIUM,
harness: Harness.OPENCODE, harness: Harness.OPENCODE,
model: 'claude-3-sonnet', model: "claude-3-sonnet",
provider: Provider.ANTHROPIC, provider: Provider.ANTHROPIC,
estimated_input_tokens: 500, estimated_input_tokens: 500,
estimated_output_tokens: 200, estimated_output_tokens: 200,
@@ -157,10 +161,10 @@ describe('EventBuilder', () => {
retry_count: 1, retry_count: 1,
}); });
expect(event.timestamp).toBe('2026-02-07T10:00:00.000Z'); expect(event.timestamp).toBe("2026-02-07T10:00:00.000Z");
}); });
it('should set instance_id from config', () => { it("should set instance_id from config", () => {
const config = makeConfig(); const config = makeConfig();
const builder = new EventBuilder(config); const builder = new EventBuilder(config);
const event = builder.build({ const event = builder.build({
@@ -168,7 +172,7 @@ describe('EventBuilder', () => {
task_type: TaskType.PLANNING, task_type: TaskType.PLANNING,
complexity: Complexity.LOW, complexity: Complexity.LOW,
harness: Harness.UNKNOWN, harness: Harness.UNKNOWN,
model: 'test-model', model: "test-model",
provider: Provider.UNKNOWN, provider: Provider.UNKNOWN,
estimated_input_tokens: 0, estimated_input_tokens: 0,
estimated_output_tokens: 0, estimated_output_tokens: 0,
@@ -186,17 +190,17 @@ describe('EventBuilder', () => {
retry_count: 0, retry_count: 0,
}); });
expect(event.instance_id).toBe('my-instance-uuid'); expect(event.instance_id).toBe("my-instance-uuid");
}); });
it('should set schema_version to 1.0', () => { it("should set schema_version to 1.0", () => {
const builder = new EventBuilder(makeConfig()); const builder = new EventBuilder(makeConfig());
const event = builder.build({ const event = builder.build({
task_duration_ms: 1000, task_duration_ms: 1000,
task_type: TaskType.REFACTORING, task_type: TaskType.REFACTORING,
complexity: Complexity.CRITICAL, complexity: Complexity.CRITICAL,
harness: Harness.KILO_CODE, harness: Harness.KILO_CODE,
model: 'gemini-pro', model: "gemini-pro",
provider: Provider.GOOGLE, provider: Provider.GOOGLE,
estimated_input_tokens: 3000, estimated_input_tokens: 3000,
estimated_output_tokens: 2000, estimated_output_tokens: 2000,
@@ -214,6 +218,6 @@ describe('EventBuilder', () => {
retry_count: 0, retry_count: 0,
}); });
expect(event.schema_version).toBe('1.0'); expect(event.schema_version).toBe("1.0");
}); });
}); });

View File

@@ -1,12 +1,15 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { PredictionCache } from '../src/prediction-cache.js'; import { PredictionCache } from "../src/prediction-cache.js";
import { PredictionQuery, PredictionResponse } from '../src/types/predictions.js'; import {
import { TaskType, Complexity, Provider } from '../src/types/events.js'; PredictionQuery,
PredictionResponse,
} from "../src/types/predictions.js";
import { TaskType, Complexity, Provider } from "../src/types/events.js";
function makeQuery(overrides: Partial<PredictionQuery> = {}): PredictionQuery { function makeQuery(overrides: Partial<PredictionQuery> = {}): PredictionQuery {
return { return {
task_type: TaskType.IMPLEMENTATION, task_type: TaskType.IMPLEMENTATION,
model: 'claude-3-opus', model: "claude-3-opus",
provider: Provider.ANTHROPIC, provider: Provider.ANTHROPIC,
complexity: Complexity.MEDIUM, complexity: Complexity.MEDIUM,
...overrides, ...overrides,
@@ -26,14 +29,14 @@ function makeResponse(sampleSize = 100): PredictionResponse {
metadata: { metadata: {
sample_size: sampleSize, sample_size: sampleSize,
fallback_level: 0, fallback_level: 0,
confidence: 'high', confidence: "high",
last_updated: new Date().toISOString(), last_updated: new Date().toISOString(),
cache_hit: false, cache_hit: false,
}, },
}; };
} }
describe('PredictionCache', () => { describe("PredictionCache", () => {
beforeEach(() => { beforeEach(() => {
vi.useFakeTimers(); vi.useFakeTimers();
}); });
@@ -42,13 +45,13 @@ describe('PredictionCache', () => {
vi.useRealTimers(); vi.useRealTimers();
}); });
it('should return null for cache miss', () => { it("should return null for cache miss", () => {
const cache = new PredictionCache(60_000); const cache = new PredictionCache(60_000);
const result = cache.get(makeQuery()); const result = cache.get(makeQuery());
expect(result).toBeNull(); expect(result).toBeNull();
}); });
it('should return cached prediction on hit', () => { it("should return cached prediction on hit", () => {
const cache = new PredictionCache(60_000); const cache = new PredictionCache(60_000);
const query = makeQuery(); const query = makeQuery();
const response = makeResponse(); const response = makeResponse();
@@ -59,7 +62,7 @@ describe('PredictionCache', () => {
expect(result).toEqual(response); expect(result).toEqual(response);
}); });
it('should return null when entry has expired', () => { it("should return null when entry has expired", () => {
const cache = new PredictionCache(60_000); // 60s TTL const cache = new PredictionCache(60_000); // 60s TTL
const query = makeQuery(); const query = makeQuery();
const response = makeResponse(); const response = makeResponse();
@@ -73,7 +76,7 @@ describe('PredictionCache', () => {
expect(cache.get(query)).toBeNull(); expect(cache.get(query)).toBeNull();
}); });
it('should differentiate queries by all fields', () => { it("should differentiate queries by all fields", () => {
const cache = new PredictionCache(60_000); const cache = new PredictionCache(60_000);
const query1 = makeQuery({ task_type: TaskType.IMPLEMENTATION }); const query1 = makeQuery({ task_type: TaskType.IMPLEMENTATION });
@@ -88,7 +91,7 @@ describe('PredictionCache', () => {
expect(cache.get(query2)?.metadata.sample_size).toBe(200); expect(cache.get(query2)?.metadata.sample_size).toBe(200);
}); });
it('should clear all entries', () => { it("should clear all entries", () => {
const cache = new PredictionCache(60_000); const cache = new PredictionCache(60_000);
cache.set(makeQuery(), makeResponse()); cache.set(makeQuery(), makeResponse());
cache.set(makeQuery({ task_type: TaskType.TESTING }), makeResponse()); cache.set(makeQuery({ task_type: TaskType.TESTING }), makeResponse());
@@ -99,7 +102,7 @@ describe('PredictionCache', () => {
expect(cache.get(makeQuery())).toBeNull(); expect(cache.get(makeQuery())).toBeNull();
}); });
it('should overwrite existing entry with same query', () => { it("should overwrite existing entry with same query", () => {
const cache = new PredictionCache(60_000); const cache = new PredictionCache(60_000);
const query = makeQuery(); const query = makeQuery();
@@ -110,7 +113,7 @@ describe('PredictionCache', () => {
expect(cache.get(query)?.metadata.sample_size).toBe(200); expect(cache.get(query)?.metadata.sample_size).toBe(200);
}); });
it('should clean expired entry on get', () => { it("should clean expired entry on get", () => {
const cache = new PredictionCache(60_000); const cache = new PredictionCache(60_000);
const query = makeQuery(); const query = makeQuery();

View File

@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest'; import { describe, it, expect } from "vitest";
import { EventQueue } from '../src/queue.js'; import { EventQueue } from "../src/queue.js";
import { import {
TaskType, TaskType,
Complexity, Complexity,
@@ -7,19 +7,19 @@ import {
Provider, Provider,
Outcome, Outcome,
TaskCompletionEvent, TaskCompletionEvent,
} from '../src/types/events.js'; } from "../src/types/events.js";
function makeEvent(id: string): TaskCompletionEvent { function makeEvent(id: string): TaskCompletionEvent {
return { return {
instance_id: 'test-instance', instance_id: "test-instance",
event_id: id, event_id: id,
schema_version: '1.0', schema_version: "1.0",
timestamp: new Date().toISOString(), timestamp: new Date().toISOString(),
task_duration_ms: 1000, task_duration_ms: 1000,
task_type: TaskType.IMPLEMENTATION, task_type: TaskType.IMPLEMENTATION,
complexity: Complexity.MEDIUM, complexity: Complexity.MEDIUM,
harness: Harness.CLAUDE_CODE, harness: Harness.CLAUDE_CODE,
model: 'claude-3-opus', model: "claude-3-opus",
provider: Provider.ANTHROPIC, provider: Provider.ANTHROPIC,
estimated_input_tokens: 1000, estimated_input_tokens: 1000,
estimated_output_tokens: 500, estimated_output_tokens: 500,
@@ -38,10 +38,10 @@ function makeEvent(id: string): TaskCompletionEvent {
}; };
} }
describe('EventQueue', () => { describe("EventQueue", () => {
it('should enqueue and drain events', () => { it("should enqueue and drain events", () => {
const queue = new EventQueue(10); const queue = new EventQueue(10);
const event = makeEvent('e1'); const event = makeEvent("e1");
queue.enqueue(event); queue.enqueue(event);
expect(queue.size).toBe(1); expect(queue.size).toBe(1);
@@ -49,102 +49,102 @@ describe('EventQueue', () => {
const drained = queue.drain(10); const drained = queue.drain(10);
expect(drained).toHaveLength(1); expect(drained).toHaveLength(1);
expect(drained[0].event_id).toBe('e1'); expect(drained[0].event_id).toBe("e1");
expect(queue.isEmpty).toBe(true); expect(queue.isEmpty).toBe(true);
}); });
it('should respect maxSize with FIFO eviction', () => { it("should respect maxSize with FIFO eviction", () => {
const queue = new EventQueue(3); const queue = new EventQueue(3);
queue.enqueue(makeEvent('e1')); queue.enqueue(makeEvent("e1"));
queue.enqueue(makeEvent('e2')); queue.enqueue(makeEvent("e2"));
queue.enqueue(makeEvent('e3')); queue.enqueue(makeEvent("e3"));
expect(queue.size).toBe(3); expect(queue.size).toBe(3);
// Adding a 4th should evict the oldest (e1) // Adding a 4th should evict the oldest (e1)
queue.enqueue(makeEvent('e4')); queue.enqueue(makeEvent("e4"));
expect(queue.size).toBe(3); expect(queue.size).toBe(3);
const drained = queue.drain(10); const drained = queue.drain(10);
expect(drained.map((e) => e.event_id)).toEqual(['e2', 'e3', 'e4']); expect(drained.map((e) => e.event_id)).toEqual(["e2", "e3", "e4"]);
}); });
it('should drain up to maxItems', () => { it("should drain up to maxItems", () => {
const queue = new EventQueue(10); const queue = new EventQueue(10);
queue.enqueue(makeEvent('e1')); queue.enqueue(makeEvent("e1"));
queue.enqueue(makeEvent('e2')); queue.enqueue(makeEvent("e2"));
queue.enqueue(makeEvent('e3')); queue.enqueue(makeEvent("e3"));
const drained = queue.drain(2); const drained = queue.drain(2);
expect(drained).toHaveLength(2); expect(drained).toHaveLength(2);
expect(drained.map((e) => e.event_id)).toEqual(['e1', 'e2']); expect(drained.map((e) => e.event_id)).toEqual(["e1", "e2"]);
expect(queue.size).toBe(1); expect(queue.size).toBe(1);
}); });
it('should remove drained items from the queue', () => { it("should remove drained items from the queue", () => {
const queue = new EventQueue(10); const queue = new EventQueue(10);
queue.enqueue(makeEvent('e1')); queue.enqueue(makeEvent("e1"));
queue.enqueue(makeEvent('e2')); queue.enqueue(makeEvent("e2"));
queue.drain(1); queue.drain(1);
expect(queue.size).toBe(1); expect(queue.size).toBe(1);
const remaining = queue.drain(10); const remaining = queue.drain(10);
expect(remaining[0].event_id).toBe('e2'); expect(remaining[0].event_id).toBe("e2");
}); });
it('should report isEmpty correctly', () => { it("should report isEmpty correctly", () => {
const queue = new EventQueue(5); const queue = new EventQueue(5);
expect(queue.isEmpty).toBe(true); expect(queue.isEmpty).toBe(true);
queue.enqueue(makeEvent('e1')); queue.enqueue(makeEvent("e1"));
expect(queue.isEmpty).toBe(false); expect(queue.isEmpty).toBe(false);
queue.drain(1); queue.drain(1);
expect(queue.isEmpty).toBe(true); expect(queue.isEmpty).toBe(true);
}); });
it('should report size correctly', () => { it("should report size correctly", () => {
const queue = new EventQueue(10); const queue = new EventQueue(10);
expect(queue.size).toBe(0); expect(queue.size).toBe(0);
queue.enqueue(makeEvent('e1')); queue.enqueue(makeEvent("e1"));
expect(queue.size).toBe(1); expect(queue.size).toBe(1);
queue.enqueue(makeEvent('e2')); queue.enqueue(makeEvent("e2"));
expect(queue.size).toBe(2); expect(queue.size).toBe(2);
queue.drain(1); queue.drain(1);
expect(queue.size).toBe(1); expect(queue.size).toBe(1);
}); });
it('should return empty array when draining empty queue', () => { it("should return empty array when draining empty queue", () => {
const queue = new EventQueue(5); const queue = new EventQueue(5);
const drained = queue.drain(10); const drained = queue.drain(10);
expect(drained).toEqual([]); expect(drained).toEqual([]);
}); });
it('should prepend events to the front of the queue', () => { it("should prepend events to the front of the queue", () => {
const queue = new EventQueue(10); const queue = new EventQueue(10);
queue.enqueue(makeEvent('e3')); queue.enqueue(makeEvent("e3"));
queue.prepend([makeEvent('e1'), makeEvent('e2')]); queue.prepend([makeEvent("e1"), makeEvent("e2")]);
expect(queue.size).toBe(3); expect(queue.size).toBe(3);
const drained = queue.drain(10); const drained = queue.drain(10);
expect(drained.map((e) => e.event_id)).toEqual(['e1', 'e2', 'e3']); expect(drained.map((e) => e.event_id)).toEqual(["e1", "e2", "e3"]);
}); });
it('should respect maxSize when prepending', () => { it("should respect maxSize when prepending", () => {
const queue = new EventQueue(3); const queue = new EventQueue(3);
queue.enqueue(makeEvent('e3')); queue.enqueue(makeEvent("e3"));
queue.enqueue(makeEvent('e4')); queue.enqueue(makeEvent("e4"));
// Only 1 slot available, so only first event should be prepended // Only 1 slot available, so only first event should be prepended
queue.prepend([makeEvent('e1'), makeEvent('e2')]); queue.prepend([makeEvent("e1"), makeEvent("e2")]);
expect(queue.size).toBe(3); expect(queue.size).toBe(3);
const drained = queue.drain(10); const drained = queue.drain(10);
expect(drained.map((e) => e.event_id)).toEqual(['e1', 'e3', 'e4']); expect(drained.map((e) => e.event_id)).toEqual(["e1", "e3", "e4"]);
}); });
}); });

View File

@@ -1,6 +1,6 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { BatchSubmitter } from '../src/submitter.js'; import { BatchSubmitter } from "../src/submitter.js";
import { ResolvedConfig } from '../src/config.js'; import { ResolvedConfig } from "../src/config.js";
import { import {
TaskCompletionEvent, TaskCompletionEvent,
TaskType, TaskType,
@@ -8,13 +8,13 @@ import {
Harness, Harness,
Provider, Provider,
Outcome, Outcome,
} from '../src/types/events.js'; } from "../src/types/events.js";
function makeConfig(overrides: Partial<ResolvedConfig> = {}): ResolvedConfig { function makeConfig(overrides: Partial<ResolvedConfig> = {}): ResolvedConfig {
return { return {
serverUrl: 'https://tel.example.com', serverUrl: "https://tel.example.com",
apiKey: 'a'.repeat(64), apiKey: "a".repeat(64),
instanceId: 'test-instance-id', instanceId: "test-instance-id",
enabled: true, enabled: true,
submitIntervalMs: 300_000, submitIntervalMs: 300_000,
maxQueueSize: 1000, maxQueueSize: 1000,
@@ -28,17 +28,17 @@ function makeConfig(overrides: Partial<ResolvedConfig> = {}): ResolvedConfig {
}; };
} }
function makeEvent(id = 'evt-1'): TaskCompletionEvent { function makeEvent(id = "evt-1"): TaskCompletionEvent {
return { return {
instance_id: 'test-instance-id', instance_id: "test-instance-id",
event_id: id, event_id: id,
schema_version: '1.0', schema_version: "1.0",
timestamp: new Date().toISOString(), timestamp: new Date().toISOString(),
task_duration_ms: 5000, task_duration_ms: 5000,
task_type: TaskType.IMPLEMENTATION, task_type: TaskType.IMPLEMENTATION,
complexity: Complexity.MEDIUM, complexity: Complexity.MEDIUM,
harness: Harness.CLAUDE_CODE, harness: Harness.CLAUDE_CODE,
model: 'claude-3-opus', model: "claude-3-opus",
provider: Provider.ANTHROPIC, provider: Provider.ANTHROPIC,
estimated_input_tokens: 1000, estimated_input_tokens: 1000,
estimated_output_tokens: 500, estimated_output_tokens: 500,
@@ -57,13 +57,13 @@ function makeEvent(id = 'evt-1'): TaskCompletionEvent {
}; };
} }
describe('BatchSubmitter', () => { describe("BatchSubmitter", () => {
let fetchSpy: ReturnType<typeof vi.fn>; let fetchSpy: ReturnType<typeof vi.fn>;
beforeEach(() => { beforeEach(() => {
vi.useFakeTimers(); vi.useFakeTimers();
fetchSpy = vi.fn(); fetchSpy = vi.fn();
vi.stubGlobal('fetch', fetchSpy); vi.stubGlobal("fetch", fetchSpy);
}); });
afterEach(() => { afterEach(() => {
@@ -71,11 +71,11 @@ describe('BatchSubmitter', () => {
vi.unstubAllGlobals(); vi.unstubAllGlobals();
}); });
it('should submit a batch successfully', async () => { it("should submit a batch successfully", async () => {
const responseBody = { const responseBody = {
accepted: 1, accepted: 1,
rejected: 0, rejected: 0,
results: [{ event_id: 'evt-1', status: 'accepted' }], results: [{ event_id: "evt-1", status: "accepted" }],
}; };
fetchSpy.mockResolvedValueOnce({ fetchSpy.mockResolvedValueOnce({
ok: true, ok: true,
@@ -91,13 +91,13 @@ describe('BatchSubmitter', () => {
expect(fetchSpy).toHaveBeenCalledTimes(1); expect(fetchSpy).toHaveBeenCalledTimes(1);
const [url, options] = fetchSpy.mock.calls[0]; const [url, options] = fetchSpy.mock.calls[0];
expect(url).toBe('https://tel.example.com/v1/events/batch'); expect(url).toBe("https://tel.example.com/v1/events/batch");
expect(options.method).toBe('POST'); expect(options.method).toBe("POST");
expect(options.headers['Authorization']).toBe(`Bearer ${'a'.repeat(64)}`); expect(options.headers["Authorization"]).toBe(`Bearer ${"a".repeat(64)}`);
}); });
it('should handle 429 with Retry-After header', async () => { it("should handle 429 with Retry-After header", async () => {
const headers = new Map([['Retry-After', '1']]); const headers = new Map([["Retry-After", "1"]]);
fetchSpy.mockResolvedValueOnce({ fetchSpy.mockResolvedValueOnce({
ok: false, ok: false,
status: 429, status: 429,
@@ -108,7 +108,7 @@ describe('BatchSubmitter', () => {
const responseBody = { const responseBody = {
accepted: 1, accepted: 1,
rejected: 0, rejected: 0,
results: [{ event_id: 'evt-1', status: 'accepted' }], results: [{ event_id: "evt-1", status: "accepted" }],
}; };
fetchSpy.mockResolvedValueOnce({ fetchSpy.mockResolvedValueOnce({
ok: true, ok: true,
@@ -129,23 +129,23 @@ describe('BatchSubmitter', () => {
expect(fetchSpy).toHaveBeenCalledTimes(2); expect(fetchSpy).toHaveBeenCalledTimes(2);
}); });
it('should handle 403 error', async () => { it("should handle 403 error", async () => {
fetchSpy.mockResolvedValueOnce({ fetchSpy.mockResolvedValueOnce({
ok: false, ok: false,
status: 403, status: 403,
statusText: 'Forbidden', statusText: "Forbidden",
}); });
const submitter = new BatchSubmitter(makeConfig({ maxRetries: 0 })); const submitter = new BatchSubmitter(makeConfig({ maxRetries: 0 }));
const result = await submitter.submit([makeEvent()]); const result = await submitter.submit([makeEvent()]);
expect(result.success).toBe(false); expect(result.success).toBe(false);
expect(result.error?.message).toContain('Forbidden'); expect(result.error?.message).toContain("Forbidden");
expect(result.error?.message).toContain('403'); expect(result.error?.message).toContain("403");
}); });
it('should retry on network error with backoff', async () => { it("should retry on network error with backoff", async () => {
fetchSpy.mockRejectedValueOnce(new Error('Network error')); fetchSpy.mockRejectedValueOnce(new Error("Network error"));
fetchSpy.mockResolvedValueOnce({ fetchSpy.mockResolvedValueOnce({
ok: true, ok: true,
status: 202, status: 202,
@@ -153,7 +153,7 @@ describe('BatchSubmitter', () => {
Promise.resolve({ Promise.resolve({
accepted: 1, accepted: 1,
rejected: 0, rejected: 0,
results: [{ event_id: 'evt-1', status: 'accepted' }], results: [{ event_id: "evt-1", status: "accepted" }],
}), }),
}); });
@@ -168,8 +168,8 @@ describe('BatchSubmitter', () => {
expect(fetchSpy).toHaveBeenCalledTimes(2); expect(fetchSpy).toHaveBeenCalledTimes(2);
}); });
it('should fail after max retries exhausted', async () => { it("should fail after max retries exhausted", async () => {
fetchSpy.mockRejectedValue(new Error('Network error')); fetchSpy.mockRejectedValue(new Error("Network error"));
const submitter = new BatchSubmitter(makeConfig({ maxRetries: 2 })); const submitter = new BatchSubmitter(makeConfig({ maxRetries: 2 }));
const submitPromise = submitter.submit([makeEvent()]); const submitPromise = submitter.submit([makeEvent()]);
@@ -179,12 +179,15 @@ describe('BatchSubmitter', () => {
const result = await submitPromise; const result = await submitPromise;
expect(result.success).toBe(false); expect(result.success).toBe(false);
expect(result.error?.message).toBe('Network error'); expect(result.error?.message).toBe("Network error");
}); });
it('should not call fetch in dryRun mode', async () => { it("should not call fetch in dryRun mode", async () => {
const submitter = new BatchSubmitter(makeConfig({ dryRun: true })); const submitter = new BatchSubmitter(makeConfig({ dryRun: true }));
const result = await submitter.submit([makeEvent('evt-1'), makeEvent('evt-2')]); const result = await submitter.submit([
makeEvent("evt-1"),
makeEvent("evt-2"),
]);
expect(result.success).toBe(true); expect(result.success).toBe(true);
expect(result.response?.accepted).toBe(2); expect(result.response?.accepted).toBe(2);
@@ -192,12 +195,14 @@ describe('BatchSubmitter', () => {
expect(fetchSpy).not.toHaveBeenCalled(); expect(fetchSpy).not.toHaveBeenCalled();
}); });
it('should handle request timeout via AbortController', async () => { it("should handle request timeout via AbortController", async () => {
fetchSpy.mockImplementation( fetchSpy.mockImplementation(
(_url: string, options: { signal: AbortSignal }) => (_url: string, options: { signal: AbortSignal }) =>
new Promise((_resolve, reject) => { new Promise((_resolve, reject) => {
options.signal.addEventListener('abort', () => { options.signal.addEventListener("abort", () => {
reject(new DOMException('The operation was aborted.', 'AbortError')); reject(
new DOMException("The operation was aborted.", "AbortError"),
);
}); });
}), }),
); );
@@ -211,6 +216,6 @@ describe('BatchSubmitter', () => {
const result = await submitPromise; const result = await submitPromise;
expect(result.success).toBe(false); expect(result.success).toBe(false);
expect(result.error?.message).toContain('aborted'); expect(result.error?.message).toContain("aborted");
}); });
}); });