/**
 * Tests for PI_TOKEN_TELEMETRY=1 per-call token data written to stderr.
 *
 * Uses extracted logic mirrors to avoid importing modules with native
 * dependencies (same pattern as headless-events.test.ts).
 */

import assert from "node:assert/strict";
import { test } from "vitest";

// Extracted cost_update handler logic mirrors headless.ts.

interface CostUpdateEvent {
	type: "cost_update";
	cumulativeCost?: {
		costUsd?: number;
	};
	tokens?: {
		input?: number;
		output?: number;
		cacheRead?: number;
		cacheWrite?: number;
	};
}

function handleCostUpdateForTelemetry(
	event: CostUpdateEvent,
	env: { PI_TOKEN_TELEMETRY?: string },
	stderrWrites: string[],
): void {
	const cumCost = event.cumulativeCost;
	if (!cumCost) return;
	const tokens = event.tokens;
	if (env.PI_TOKEN_TELEMETRY === "1") {
		stderrWrites.push(
			`[PI_TOKEN] input=${tokens?.input ?? 0} output=${tokens?.output ?? 0} cache_read=${tokens?.cacheRead ?? 0} cache_write=${tokens?.cacheWrite ?? 0} cost=$${Number(cumCost.costUsd ?? 0).toFixed(4)}\n`,
		);
	}
}

// PI_TOKEN_TELEMETRY behavior contracts.

test("PI_TOKEN_TELEMETRY=1 writes formatted token line to stderr", () => {
	const writes: string[] = [];
	const event: CostUpdateEvent = {
		type: "cost_update",
		cumulativeCost: { costUsd: 0.042 },
		tokens: {
			input: 5000,
			output: 1200,
			cacheRead: 3000,
			cacheWrite: 150,
		},
	};

	handleCostUpdateForTelemetry(event, { PI_TOKEN_TELEMETRY: "1" }, writes);

	assert.equal(writes.length, 1);
	assert.ok(writes[0].startsWith("[PI_TOKEN] "));
	assert.ok(writes[0].includes("input=5000"));
	assert.ok(writes[0].includes("output=1200"));
	assert.ok(writes[0].includes("cache_read=3000"));
	assert.ok(writes[0].includes("cache_write=150"));
	assert.ok(writes[0].includes("cost=$0.0420"));
});

test("PI_TOKEN_TELEMETRY unset does not write to stderr", () => {
	const writes: string[] = [];
	const event: CostUpdateEvent = {
		type: "cost_update",
		cumulativeCost: { costUsd: 0.1 },
		tokens: { input: 100, output: 50 },
	};

	handleCostUpdateForTelemetry(event, {}, writes);

	assert.equal(writes.length, 0);
});

test("PI_TOKEN_TELEMETRY=0 does not write to stderr", () => {
	const writes: string[] = [];
	const event: CostUpdateEvent = {
		type: "cost_update",
		cumulativeCost: { costUsd: 0.1 },
		tokens: { input: 100, output: 50 },
	};

	handleCostUpdateForTelemetry(event, { PI_TOKEN_TELEMETRY: "0" }, writes);

	assert.equal(writes.length, 0);
});

test("cost_update without cumulativeCost does not write to stderr", () => {
	const writes: string[] = [];
	const event: CostUpdateEvent = {
		type: "cost_update",
	};

	handleCostUpdateForTelemetry(event, { PI_TOKEN_TELEMETRY: "1" }, writes);

	assert.equal(writes.length, 0);
});

test("cost_update with missing tokens defaults to zeros", () => {
	const writes: string[] = [];
	const event: CostUpdateEvent = {
		type: "cost_update",
		cumulativeCost: { costUsd: 0.005 },
	};

	handleCostUpdateForTelemetry(event, { PI_TOKEN_TELEMETRY: "1" }, writes);

	assert.equal(writes.length, 1);
	assert.ok(writes[0].includes("input=0"));
	assert.ok(writes[0].includes("output=0"));
	assert.ok(writes[0].includes("cache_read=0"));
	assert.ok(writes[0].includes("cache_write=0"));
	assert.ok(writes[0].includes("cost=$0.0050"));
});

test("cost_update rounds cost to 4 decimal places", () => {
	const writes: string[] = [];
	const event: CostUpdateEvent = {
		type: "cost_update",
		cumulativeCost: { costUsd: 0.1234567 },
		tokens: { input: 1, output: 1 },
	};

	handleCostUpdateForTelemetry(event, { PI_TOKEN_TELEMETRY: "1" }, writes);

	assert.ok(writes[0].includes("cost=$0.1235"));
});
