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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -7,7 +7,7 @@ export {
Outcome,
RepoSizeCategory,
type TaskCompletionEvent,
} from './events.js';
} from "./events.js";
export {
type TokenDistribution,
@@ -17,7 +17,7 @@ export {
type PredictionMetadata,
type PredictionResponse,
type PredictionQuery,
} from './predictions.js';
} from "./predictions.js";
export {
type BatchEventRequest,
@@ -25,4 +25,4 @@ export {
type BatchEventResponse,
type BatchPredictionRequest,
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 {
p10: number;
@@ -30,7 +30,7 @@ export interface PredictionData {
export interface PredictionMetadata {
sample_size: number;
fallback_level: number;
confidence: 'none' | 'low' | 'medium' | 'high';
confidence: "none" | "low" | "medium" | "high";
last_updated: string | null;
dimensions_matched?: Record<string, string | null> | null;
fallback_note?: string | null;

View File

@@ -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");
});
});
});

View File

@@ -1,6 +1,6 @@
import { describe, it, expect, vi, afterEach } from 'vitest';
import { EventBuilder } from '../src/event-builder.js';
import { ResolvedConfig } from '../src/config.js';
import { describe, it, expect, vi, afterEach } from "vitest";
import { EventBuilder } from "../src/event-builder.js";
import { ResolvedConfig } from "../src/config.js";
import {
TaskType,
Complexity,
@@ -9,13 +9,13 @@ import {
Outcome,
QualityGate,
RepoSizeCategory,
} from '../src/types/events.js';
} from "../src/types/events.js";
function makeConfig(): ResolvedConfig {
return {
serverUrl: 'https://tel.example.com',
apiKey: 'a'.repeat(64),
instanceId: 'my-instance-uuid',
serverUrl: "https://tel.example.com",
apiKey: "a".repeat(64),
instanceId: "my-instance-uuid",
enabled: true,
submitIntervalMs: 300_000,
maxQueueSize: 1000,
@@ -28,19 +28,19 @@ function makeConfig(): ResolvedConfig {
};
}
describe('EventBuilder', () => {
describe("EventBuilder", () => {
afterEach(() => {
vi.restoreAllMocks();
});
it('should build a complete TaskCompletionEvent', () => {
it("should build a complete TaskCompletionEvent", () => {
const builder = new EventBuilder(makeConfig());
const event = builder.build({
task_duration_ms: 15000,
task_type: TaskType.IMPLEMENTATION,
complexity: Complexity.HIGH,
harness: Harness.CLAUDE_CODE,
model: 'claude-3-opus',
model: "claude-3-opus",
provider: Provider.ANTHROPIC,
estimated_input_tokens: 2000,
estimated_output_tokens: 1000,
@@ -49,37 +49,41 @@ describe('EventBuilder', () => {
estimated_cost_usd_micros: 100000,
actual_cost_usd_micros: 110000,
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: [],
context_compactions: 2,
context_rotations: 1,
context_utilization_final: 0.75,
outcome: Outcome.SUCCESS,
retry_count: 0,
language: 'typescript',
language: "typescript",
repo_size_category: RepoSizeCategory.MEDIUM,
});
expect(event.task_type).toBe(TaskType.IMPLEMENTATION);
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([
QualityGate.BUILD,
QualityGate.TEST,
QualityGate.LINT,
]);
expect(event.language).toBe('typescript');
expect(event.language).toBe("typescript");
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 event = builder.build({
task_duration_ms: 1000,
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,
@@ -108,7 +112,7 @@ describe('EventBuilder', () => {
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,
@@ -129,8 +133,8 @@ describe('EventBuilder', () => {
expect(event.event_id).not.toBe(event2.event_id);
});
it('should auto-set timestamp to ISO 8601', () => {
const now = new Date('2026-02-07T10:00:00.000Z');
it("should auto-set timestamp to ISO 8601", () => {
const now = new Date("2026-02-07T10:00:00.000Z");
vi.setSystemTime(now);
const builder = new EventBuilder(makeConfig());
@@ -139,7 +143,7 @@ describe('EventBuilder', () => {
task_type: TaskType.DEBUGGING,
complexity: Complexity.MEDIUM,
harness: Harness.OPENCODE,
model: 'claude-3-sonnet',
model: "claude-3-sonnet",
provider: Provider.ANTHROPIC,
estimated_input_tokens: 500,
estimated_output_tokens: 200,
@@ -157,10 +161,10 @@ describe('EventBuilder', () => {
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 builder = new EventBuilder(config);
const event = builder.build({
@@ -168,7 +172,7 @@ describe('EventBuilder', () => {
task_type: TaskType.PLANNING,
complexity: Complexity.LOW,
harness: Harness.UNKNOWN,
model: 'test-model',
model: "test-model",
provider: Provider.UNKNOWN,
estimated_input_tokens: 0,
estimated_output_tokens: 0,
@@ -186,17 +190,17 @@ describe('EventBuilder', () => {
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 event = builder.build({
task_duration_ms: 1000,
task_type: TaskType.REFACTORING,
complexity: Complexity.CRITICAL,
harness: Harness.KILO_CODE,
model: 'gemini-pro',
model: "gemini-pro",
provider: Provider.GOOGLE,
estimated_input_tokens: 3000,
estimated_output_tokens: 2000,
@@ -214,6 +218,6 @@ describe('EventBuilder', () => {
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 { PredictionCache } from '../src/prediction-cache.js';
import { PredictionQuery, PredictionResponse } from '../src/types/predictions.js';
import { TaskType, Complexity, Provider } from '../src/types/events.js';
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { PredictionCache } from "../src/prediction-cache.js";
import {
PredictionQuery,
PredictionResponse,
} from "../src/types/predictions.js";
import { TaskType, Complexity, Provider } from "../src/types/events.js";
function makeQuery(overrides: Partial<PredictionQuery> = {}): PredictionQuery {
return {
task_type: TaskType.IMPLEMENTATION,
model: 'claude-3-opus',
model: "claude-3-opus",
provider: Provider.ANTHROPIC,
complexity: Complexity.MEDIUM,
...overrides,
@@ -26,14 +29,14 @@ function makeResponse(sampleSize = 100): PredictionResponse {
metadata: {
sample_size: sampleSize,
fallback_level: 0,
confidence: 'high',
confidence: "high",
last_updated: new Date().toISOString(),
cache_hit: false,
},
};
}
describe('PredictionCache', () => {
describe("PredictionCache", () => {
beforeEach(() => {
vi.useFakeTimers();
});
@@ -42,13 +45,13 @@ describe('PredictionCache', () => {
vi.useRealTimers();
});
it('should return null for cache miss', () => {
it("should return null for cache miss", () => {
const cache = new PredictionCache(60_000);
const result = cache.get(makeQuery());
expect(result).toBeNull();
});
it('should return cached prediction on hit', () => {
it("should return cached prediction on hit", () => {
const cache = new PredictionCache(60_000);
const query = makeQuery();
const response = makeResponse();
@@ -59,7 +62,7 @@ describe('PredictionCache', () => {
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 query = makeQuery();
const response = makeResponse();
@@ -73,7 +76,7 @@ describe('PredictionCache', () => {
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 query1 = makeQuery({ task_type: TaskType.IMPLEMENTATION });
@@ -88,7 +91,7 @@ describe('PredictionCache', () => {
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);
cache.set(makeQuery(), makeResponse());
cache.set(makeQuery({ task_type: TaskType.TESTING }), makeResponse());
@@ -99,7 +102,7 @@ describe('PredictionCache', () => {
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 query = makeQuery();
@@ -110,7 +113,7 @@ describe('PredictionCache', () => {
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 query = makeQuery();

View File

@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest';
import { EventQueue } from '../src/queue.js';
import { describe, it, expect } from "vitest";
import { EventQueue } from "../src/queue.js";
import {
TaskType,
Complexity,
@@ -7,19 +7,19 @@ import {
Provider,
Outcome,
TaskCompletionEvent,
} from '../src/types/events.js';
} from "../src/types/events.js";
function makeEvent(id: string): 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: 1000,
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,
@@ -38,10 +38,10 @@ function makeEvent(id: string): TaskCompletionEvent {
};
}
describe('EventQueue', () => {
it('should enqueue and drain events', () => {
describe("EventQueue", () => {
it("should enqueue and drain events", () => {
const queue = new EventQueue(10);
const event = makeEvent('e1');
const event = makeEvent("e1");
queue.enqueue(event);
expect(queue.size).toBe(1);
@@ -49,102 +49,102 @@ describe('EventQueue', () => {
const drained = queue.drain(10);
expect(drained).toHaveLength(1);
expect(drained[0].event_id).toBe('e1');
expect(drained[0].event_id).toBe("e1");
expect(queue.isEmpty).toBe(true);
});
it('should respect maxSize with FIFO eviction', () => {
it("should respect maxSize with FIFO eviction", () => {
const queue = new EventQueue(3);
queue.enqueue(makeEvent('e1'));
queue.enqueue(makeEvent('e2'));
queue.enqueue(makeEvent('e3'));
queue.enqueue(makeEvent("e1"));
queue.enqueue(makeEvent("e2"));
queue.enqueue(makeEvent("e3"));
expect(queue.size).toBe(3);
// Adding a 4th should evict the oldest (e1)
queue.enqueue(makeEvent('e4'));
queue.enqueue(makeEvent("e4"));
expect(queue.size).toBe(3);
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);
queue.enqueue(makeEvent('e1'));
queue.enqueue(makeEvent('e2'));
queue.enqueue(makeEvent('e3'));
queue.enqueue(makeEvent("e1"));
queue.enqueue(makeEvent("e2"));
queue.enqueue(makeEvent("e3"));
const drained = queue.drain(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);
});
it('should remove drained items from the queue', () => {
it("should remove drained items from the queue", () => {
const queue = new EventQueue(10);
queue.enqueue(makeEvent('e1'));
queue.enqueue(makeEvent('e2'));
queue.enqueue(makeEvent("e1"));
queue.enqueue(makeEvent("e2"));
queue.drain(1);
expect(queue.size).toBe(1);
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);
expect(queue.isEmpty).toBe(true);
queue.enqueue(makeEvent('e1'));
queue.enqueue(makeEvent("e1"));
expect(queue.isEmpty).toBe(false);
queue.drain(1);
expect(queue.isEmpty).toBe(true);
});
it('should report size correctly', () => {
it("should report size correctly", () => {
const queue = new EventQueue(10);
expect(queue.size).toBe(0);
queue.enqueue(makeEvent('e1'));
queue.enqueue(makeEvent("e1"));
expect(queue.size).toBe(1);
queue.enqueue(makeEvent('e2'));
queue.enqueue(makeEvent("e2"));
expect(queue.size).toBe(2);
queue.drain(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 drained = queue.drain(10);
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);
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);
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);
queue.enqueue(makeEvent('e3'));
queue.enqueue(makeEvent('e4'));
queue.enqueue(makeEvent("e3"));
queue.enqueue(makeEvent("e4"));
// 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);
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 { BatchSubmitter } from '../src/submitter.js';
import { ResolvedConfig } from '../src/config.js';
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { BatchSubmitter } from "../src/submitter.js";
import { ResolvedConfig } from "../src/config.js";
import {
TaskCompletionEvent,
TaskType,
@@ -8,13 +8,13 @@ import {
Harness,
Provider,
Outcome,
} from '../src/types/events.js';
} from "../src/types/events.js";
function makeConfig(overrides: Partial<ResolvedConfig> = {}): ResolvedConfig {
return {
serverUrl: 'https://tel.example.com',
apiKey: 'a'.repeat(64),
instanceId: 'test-instance-id',
serverUrl: "https://tel.example.com",
apiKey: "a".repeat(64),
instanceId: "test-instance-id",
enabled: true,
submitIntervalMs: 300_000,
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 {
instance_id: 'test-instance-id',
instance_id: "test-instance-id",
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,13 +57,13 @@ function makeEvent(id = 'evt-1'): TaskCompletionEvent {
};
}
describe('BatchSubmitter', () => {
describe("BatchSubmitter", () => {
let fetchSpy: ReturnType<typeof vi.fn>;
beforeEach(() => {
vi.useFakeTimers();
fetchSpy = vi.fn();
vi.stubGlobal('fetch', fetchSpy);
vi.stubGlobal("fetch", fetchSpy);
});
afterEach(() => {
@@ -71,11 +71,11 @@ describe('BatchSubmitter', () => {
vi.unstubAllGlobals();
});
it('should submit a batch successfully', async () => {
it("should submit a batch successfully", async () => {
const responseBody = {
accepted: 1,
rejected: 0,
results: [{ event_id: 'evt-1', status: 'accepted' }],
results: [{ event_id: "evt-1", status: "accepted" }],
};
fetchSpy.mockResolvedValueOnce({
ok: true,
@@ -91,13 +91,13 @@ describe('BatchSubmitter', () => {
expect(fetchSpy).toHaveBeenCalledTimes(1);
const [url, options] = fetchSpy.mock.calls[0];
expect(url).toBe('https://tel.example.com/v1/events/batch');
expect(options.method).toBe('POST');
expect(options.headers['Authorization']).toBe(`Bearer ${'a'.repeat(64)}`);
expect(url).toBe("https://tel.example.com/v1/events/batch");
expect(options.method).toBe("POST");
expect(options.headers["Authorization"]).toBe(`Bearer ${"a".repeat(64)}`);
});
it('should handle 429 with Retry-After header', async () => {
const headers = new Map([['Retry-After', '1']]);
it("should handle 429 with Retry-After header", async () => {
const headers = new Map([["Retry-After", "1"]]);
fetchSpy.mockResolvedValueOnce({
ok: false,
status: 429,
@@ -108,7 +108,7 @@ describe('BatchSubmitter', () => {
const responseBody = {
accepted: 1,
rejected: 0,
results: [{ event_id: 'evt-1', status: 'accepted' }],
results: [{ event_id: "evt-1", status: "accepted" }],
};
fetchSpy.mockResolvedValueOnce({
ok: true,
@@ -129,23 +129,23 @@ describe('BatchSubmitter', () => {
expect(fetchSpy).toHaveBeenCalledTimes(2);
});
it('should handle 403 error', async () => {
it("should handle 403 error", async () => {
fetchSpy.mockResolvedValueOnce({
ok: false,
status: 403,
statusText: 'Forbidden',
statusText: "Forbidden",
});
const submitter = new BatchSubmitter(makeConfig({ maxRetries: 0 }));
const result = await submitter.submit([makeEvent()]);
expect(result.success).toBe(false);
expect(result.error?.message).toContain('Forbidden');
expect(result.error?.message).toContain('403');
expect(result.error?.message).toContain("Forbidden");
expect(result.error?.message).toContain("403");
});
it('should retry on network error with backoff', async () => {
fetchSpy.mockRejectedValueOnce(new Error('Network error'));
it("should retry on network error with backoff", async () => {
fetchSpy.mockRejectedValueOnce(new Error("Network error"));
fetchSpy.mockResolvedValueOnce({
ok: true,
status: 202,
@@ -153,7 +153,7 @@ describe('BatchSubmitter', () => {
Promise.resolve({
accepted: 1,
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);
});
it('should fail after max retries exhausted', async () => {
fetchSpy.mockRejectedValue(new Error('Network error'));
it("should fail after max retries exhausted", async () => {
fetchSpy.mockRejectedValue(new Error("Network error"));
const submitter = new BatchSubmitter(makeConfig({ maxRetries: 2 }));
const submitPromise = submitter.submit([makeEvent()]);
@@ -179,12 +179,15 @@ describe('BatchSubmitter', () => {
const result = await submitPromise;
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 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.response?.accepted).toBe(2);
@@ -192,12 +195,14 @@ describe('BatchSubmitter', () => {
expect(fetchSpy).not.toHaveBeenCalled();
});
it('should handle request timeout via AbortController', async () => {
it("should handle request timeout via AbortController", async () => {
fetchSpy.mockImplementation(
(_url: string, options: { signal: AbortSignal }) =>
new Promise((_resolve, reject) => {
options.signal.addEventListener('abort', () => {
reject(new DOMException('The operation was aborted.', 'AbortError'));
options.signal.addEventListener("abort", () => {
reject(
new DOMException("The operation was aborted.", "AbortError"),
);
});
}),
);
@@ -211,6 +216,6 @@ describe('BatchSubmitter', () => {
const result = await submitPromise;
expect(result.success).toBe(false);
expect(result.error?.message).toContain('aborted');
expect(result.error?.message).toContain("aborted");
});
});