58 lines
3.9 KiB
JavaScript
58 lines
3.9 KiB
JavaScript
import { test } from 'node:test'
|
||
import assert from 'node:assert/strict'
|
||
import { calculateTokenRetailPricing, groupDiscountLabel, modelStandardPriceLabel, platformPriceLabel, standardPriceLabel } from '../src/services/pricing.ts'
|
||
|
||
test('fixed-unit pricing shows platform task price without inventing a standard price or discount', () => {
|
||
const tripo = { price: 1.44, unit: 'CNY / task', upstreamCredits: 20, exchangeRate: 7.2, priceSource: 'Tripo 官方', standardPrice: { price: 0.2, unit: 'USD / task', credits: 20 } }
|
||
assert.equal(standardPriceLabel(tripo), '$0.2 / task')
|
||
assert.equal(modelStandardPriceLabel([tripo]), '$0.2 / task')
|
||
assert.equal(platformPriceLabel(tripo), '平台售价 ¥1.44 / 任务')
|
||
assert.equal(groupDiscountLabel(tripo), '折扣未提供')
|
||
})
|
||
|
||
test('image prices distinguish per-image and text/image token inputs', () => {
|
||
assert.equal(platformPriceLabel({price:0.4032,unit:'CNY / image'}),'平台售价 ¥0.4032 / 张(单张请求)')
|
||
assert.equal(platformPriceLabel({input:20.16,imageInput:32.256,output:120.96,unit:'CNY / 1M tokens'}),'¥20.16 文字输入 / ¥32.256 图像输入 / ¥120.96 输出 · 人民币 / 1M(一百万 Token)')
|
||
})
|
||
|
||
test('official standard price source is displayed, while a standard-price marker is not treated as a discount', () => {
|
||
const tripo = { price: 1.44, unit: 'CNY / task', discountLabel: '标准价', standardPrice: { price: 0.2, unit: 'USD / task', credits: 20, priceSource: 'Tripo 官方 API 定价' } }
|
||
assert.equal(standardPriceLabel(tripo), '$0.2 / task · Tripo 官方 API 定价')
|
||
assert.equal(groupDiscountLabel(tripo), '折扣未提供')
|
||
})
|
||
|
||
test('token pricing retains explicit source list price and discount', () => {
|
||
const pricing = { input: 1.2, output: 4.8, unit: 'CNY / 1M tokens', discount: 6.5, discountLabel: '6.5折', listPriceUsd: { input: 1, output: 4 } }
|
||
assert.equal(standardPriceLabel(pricing), '$1 输入 / $4 输出 · USD / 1M(一百万 Token)')
|
||
assert.equal(platformPriceLabel(pricing), '¥1.2 输入 / ¥4.8 输出 · 人民币 / 1M(一百万 Token)')
|
||
assert.equal(groupDiscountLabel(pricing), '6.5折')
|
||
})
|
||
|
||
test('CNY source keeps its currency and never multiplies the USD exchange rate', () => {
|
||
const result = calculateTokenRetailPricing('CNY', {input: 0.8, output: 2.8, cachedInput: 0.23}, 3, 7.2)
|
||
assert.deepEqual([result.input, result.output, result.cachedInput, result.exchangeRate], [0.24, 0.84, 0.069, 1])
|
||
assert.equal(standardPriceLabel({listPriceCny:{input:0.8,output:2.8}}), '¥0.8 输入 / ¥2.8 输出 · CNY / 1M(一百万 Token)')
|
||
assert.equal(modelStandardPriceLabel([{listPriceUsd:{input:1,output:4}},{listPriceCny:{input:1,output:4}}]), '各分组标准价见详情')
|
||
})
|
||
|
||
test('retail discount is in tenths; USD conversion and eight-place rounding happen once', () => {
|
||
const result = calculateTokenRetailPricing('USD', {input:1.25, output:10, cachedInput:0.125}, 3.5, 7.2)
|
||
assert.deepEqual([result.input,result.output,result.cachedInput],[3.15,25.2,0.315])
|
||
assert.equal(calculateTokenRetailPricing('CNY',{input:0.00000001,output:0},5).input,0.00000001)
|
||
assert.equal(calculateTokenRetailPricing('CNY',{input:0,output:0,cachedInput:0},10).cachedInput,0)
|
||
assert.throws(()=>calculateTokenRetailPricing('USD',{input:1,output:1},5),/汇率/)
|
||
assert.throws(()=>calculateTokenRetailPricing('CNY',{input:1,output:1},55),/折扣/)
|
||
assert.throws(()=>calculateTokenRetailPricing('CNY',{input:NaN,output:1},5),/原价/)
|
||
})
|
||
|
||
test('different route list prices are not presented as one universal model price', () => {
|
||
assert.equal(modelStandardPriceLabel([
|
||
{ unit: 'CNY / 1M tokens', listPriceUsd: { input: 1, output: 4 } },
|
||
{ unit: 'CNY / 1M tokens', listPriceUsd: { input: 2, output: 8 } },
|
||
]), '各分组标准价见详情')
|
||
assert.equal(modelStandardPriceLabel([
|
||
{ unit: 'CNY / 1M tokens', listPriceUsd: { input: 1, output: 4 } },
|
||
{ price: 1.44, unit: 'CNY / task' },
|
||
]), '部分分组标准价见详情')
|
||
})
|