项目文件夹

文件
2026-07-13 13:39:12 +08:00

33 行
1.4 KiB
TypeScript

此文件含有模棱两可的 Unicode 字符
此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。
import { test } from "node:test";
import assert from "node:assert/strict";
import {
computeImageCost,
computeAudioCost,
computeRerankCost,
computeVideoCost,
calculateModalCost,
} from "../../src/lib/usage/costCalculator.ts";
test("computeImageCost: per-image flat × n", () => {
assert.equal(computeImageCost({ output_cost_per_image: 0.04 }, { n: 3 }), 0.12);
assert.equal(computeImageCost({}, { n: 3 }), 0);
assert.equal(computeImageCost({ output_cost_per_image: 0.04 }, { n: 0 }), 0);
});
test("computeAudioCost: per-second OR per-character, else 0", () => {
assert.equal(computeAudioCost({ input_cost_per_second: 0.0001 }, { seconds: 30 }), 0.003);
assert.equal(computeAudioCost({ input_cost_per_character: 0.000015 }, { characters: 1000 }), 0.015);
assert.equal(computeAudioCost({ input_cost_per_second: 0.0001 }, {}), 0);
});
test("computeRerankCost: per search unit", () => {
assert.equal(computeRerankCost({ search_unit_cost: 0.002 }, { searchUnits: 5 }), 0.01);
assert.equal(computeRerankCost({}, { searchUnits: 5 }), 0);
});
test("computeVideoCost: per video-second", () => {
assert.equal(computeVideoCost({ output_cost_per_video_per_second: 0.5 }, { seconds: 8 }), 4);
assert.equal(computeVideoCost({}, { seconds: 8 }), 0);
});
test("calculateModalCost returns 0 for unknown pricing (fail-open)", async () => {
const cost = await calculateModalCost("image", "no-such-provider", "no-such-model", { n: 2 });
assert.equal(cost, 0);
});