From 23a1d381877c4c509172c665a724ea04b2636b19 Mon Sep 17 00:00:00 2001 From: "chihao.ou" Date: Sun, 20 Sep 2026 18:05:23 +0800 Subject: [PATCH] fix(pricing): parse scientific notation in token bounds --- .../components/admin/channel/IntervalRow.vue | 4 +-- .../channel/__tests__/IntervalRow.spec.ts | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 frontend/src/components/admin/channel/__tests__/IntervalRow.spec.ts diff --git a/frontend/src/components/admin/channel/IntervalRow.vue b/frontend/src/components/admin/channel/IntervalRow.vue index a8930993da..08adbea600 100644 --- a/frontend/src/components/admin/channel/IntervalRow.vue +++ b/frontend/src/components/admin/channel/IntervalRow.vue @@ -136,13 +136,13 @@ function emitField(field: keyof IntervalFormEntry, value: string | number | null } function toInt(val: string): number { - const n = parseInt(val, 10) + const n = Math.trunc(Number(val)) return isNaN(n) ? 0 : n } function toIntOrNull(val: string): number | null { if (val === '') return null - const n = parseInt(val, 10) + const n = Math.trunc(Number(val)) return isNaN(n) ? null : n } diff --git a/frontend/src/components/admin/channel/__tests__/IntervalRow.spec.ts b/frontend/src/components/admin/channel/__tests__/IntervalRow.spec.ts new file mode 100644 index 0000000000..079a8a2ea7 --- /dev/null +++ b/frontend/src/components/admin/channel/__tests__/IntervalRow.spec.ts @@ -0,0 +1,29 @@ +import { enableAutoUnmount, mount } from '@vue/test-utils' +import { afterEach, describe, expect, it, vi } from 'vitest' +import IntervalRow from '../IntervalRow.vue' +import type { IntervalFormEntry } from '../types' + +vi.mock('vue-i18n', () => ({ useI18n: () => ({ t: (key: string) => key }) })) +enableAutoUnmount(afterEach) +const interval: IntervalFormEntry = { + min_tokens: 0, max_tokens: null, tier_label: '', sort_order: 0, + input_price: null, output_price: null, cache_write_price: null, cache_read_price: null, + input_multiplier: null, output_multiplier: null, cache_write_multiplier: null, + cache_read_multiplier: null, per_request_price: null, +} + +describe('pricing interval token bounds', () => { + it.each([ + ['1e6', 1000000, 1000000], + ['2.72e5', 272000, 272000], + ['128000', 128000, 128000], + ['', 0, null], + ])('parses %s without losing the exponent', async (value, min, max) => { + const wrapper = mount(IntervalRow, { props: { interval, mode: 'token' } }) + const inputs = wrapper.findAll('input[type="number"]') + await inputs[0].setValue(value) + await inputs[1].setValue(value) + expect(wrapper.emitted('update')?.[0]?.[0]).toMatchObject({ min_tokens: min }) + expect(wrapper.emitted('update')?.[1]?.[0]).toMatchObject({ max_tokens: max }) + }) +})