Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/autopsy/__tests__/metrics.test.ts

Raw
import { describe, expect, it } from "vitest";
import { metricsOf, usageOf } from "../metrics.ts";

describe("usage and size measurements", () => {
	it("keeps missing and invalid quantities distinct from zero and estimates independently", () => {
		const result = usageOf(
			{
				input: 0,
				output: 1_000_000,
				cacheRead: NaN,
				cacheWrite: 2,
				cost: { total: 7, totalFromProvider: true },
			},
			{ input: 1, output: 3, cacheRead: 4, cacheWrite: 5 },
		);
		expect(result).toEqual({
			input: 0,
			output: 1_000_000,
			cacheRead: null,
			cacheWrite: 2,
			totalTokens: null,
			cost: 7,
			costSource: "provider",
			costBreakdown: {
				input: null,
				output: null,
				cacheRead: null,
				cacheWrite: null,
				total: 7,
			},
			catalogCost: {
				input: 0,
				output: 3,
				cacheRead: null,
				cacheWrite: expect.closeTo(0.00001),
				total: null,
			},
		});
		expect(usageOf({ input: Infinity, output: -1, cost: 0 })).toMatchObject({
			input: null,
			output: null,
			cost: 0,
			costSource: "recorded",
		});
		expect(
			usageOf({ cost: { totalFromProvider: false, total: 1 } })?.costSource,
		).toBe("catalog");
	});

	it("matches one-hour cache-write pricing without mutating recorded usage", () => {
		const usage = {
			input: 0,
			output: 0,
			cacheRead: 0,
			cacheWrite: 1_000_000,
			cacheWrite1h: 1_000_000,
			cost: { total: 7, totalFromProvider: true },
		};
		const prices = { input: 3, output: 15, cacheRead: 0.3, cacheWrite: 3.75 };
		expect(usageOf(usage, prices)?.catalogCost).toMatchObject({
			cacheWrite: 6,
			total: 6,
		});
		expect(usage.cost.total).toBe(7);
		expect(
			usageOf({ ...usage, cacheWrite1h: undefined }, prices)?.catalogCost
				?.total,
		).toBe(3.75);
		expect(
			usageOf({ ...usage, cacheWrite1h: 2_000_000 }, prices)?.catalogCost
				?.total,
		).toBeNull();
	});

	it("selects request-wide price tiers strictly above thresholds using all input categories", () => {
		const prices = {
			input: 1,
			output: 2,
			cacheRead: 3,
			cacheWrite: 4,
			tiers: [
				{
					inputTokensAbove: 100,
					input: 10,
					output: 20,
					cacheRead: 30,
					cacheWrite: 40,
				},
			],
		};
		const usage = { input: 40, output: 10, cacheRead: 40, cacheWrite: 20 };
		expect(usageOf(usage, prices)?.catalogCost?.total).toBeCloseTo(0.00026);
		expect(usageOf({ ...usage, input: 41 }, prices)?.catalogCost).toMatchObject(
			{
				input: expect.closeTo(0.00041),
				output: expect.closeTo(0.0002),
				total: expect.closeTo(0.00261),
			},
		);
		expect(
			usageOf({ ...usage, cacheRead: undefined }, prices)?.catalogCost,
		).toEqual({
			input: null,
			output: null,
			cacheRead: null,
			cacheWrite: null,
			total: null,
		});
	});

	it("uses recorded durations and assistant starts without inferring tool starts", () => {
		expect(
			metricsOf({
				message: {
					role: "assistant",
					timestamp: 1000,
					durationMs: 0,
					content: "é",
				},
			}).metrics,
		).toEqual({
			durationMs: 0,
			startedAt: "1970-01-01T00:00:01.000Z",
			textBytes: 2,
			imageBytes: 0,
			imageCount: 0,
			totalBytes: 2,
		});
		expect(
			metricsOf({
				timestamp: "2025-01-01",
				message: {
					role: "toolResult",
					timestamp: 2000,
					durationMs: 10,
					content: [],
				},
			}).metrics,
		).toMatchObject({ durationMs: 10, startedAt: null });
		expect(
			metricsOf({
				message: { role: "assistant", timestamp: 1000, content: [] },
			}).metrics.durationMs,
		).toBeNull();
	});

	it("counts canonical image bytes and bounded headers without returning payload", () => {
		const data =
			"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+aMioAAAAASUVORK5CYII=";
		const result = metricsOf({
			message: {
				content: [
					{ type: "text", text: "🦊" },
					{ type: "image", mimeType: "image/png", data },
				],
			},
		});
		expect(result.images).toEqual([
			{
				index: 1,
				mimeType: "image/png",
				bytes: Buffer.from(data, "base64").length,
				width: 1,
				height: 1,
			},
		]);
		expect(result.metrics.totalBytes).toBe(
			4 + Buffer.from(data, "base64").length,
		);
		expect(JSON.stringify(result)).not.toContain(data);
		expect(
			metricsOf({ content: [{ type: "image", data: "invalid!" }] }).metrics
				.imageBytes,
		).toBeNull();
	});

	it("reports unknown size when content exceeds work limits", () => {
		expect(
			metricsOf({ message: { content: "x".repeat(1_048_577) } }).metrics
				.textBytes,
		).toBeNull();
		expect(
			metricsOf({
				message: {
					content: Array.from({ length: 2001 }, () => ({
						type: "text",
						text: "x",
					})),
				},
			}).metrics,
		).toMatchObject({
			textBytes: null,
			imageBytes: null,
			imageCount: null,
			totalBytes: null,
		});
		expect(
			metricsOf({
				type: "unknown",
				data: { text: "not a known content schema" },
			}).metrics.totalBytes,
		).toBeNull();
	});
});