contextUsage.test.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { describe, expect, it } from 'vitest'
  2. import { normalizeChatUsage } from '@/utils/contextUsage'
  3. describe('context usage shared by web and desktop', () => {
  4. it('never treats cumulative billing or an output limit as context occupancy', () => {
  5. const usage = normalizeChatUsage({ input_tokens: 1_800_000, output_tokens: 100_000,
  6. total_tokens: 1_900_000, max_tokens: 262_144, used: 1_900_000, limit: 262_144 })
  7. expect(usage).toMatchObject({ inputTokens: 1_800_000, outputTokens: 100_000, totalTokens: 1_900_000 })
  8. expect(usage?.contextTokens).toBeUndefined()
  9. expect(usage?.contextLimit).toBeUndefined()
  10. })
  11. it('uses the same last-prompt source order and server percentage as mobile', () => {
  12. const usage = normalizeChatUsage({ usage: { context_used: 2000, context_max: 8000, context_percent: 45.5 } },
  13. { context_used: 3000, context_max: 16000 })
  14. expect(usage).toMatchObject({ contextTokens: 2000, contextLimit: 8000, percentUsed: 45.5 })
  15. })
  16. it('fills missing gauges from context breakdown without using cumulative totals', () => {
  17. expect(normalizeChatUsage({ total: 1_900_000 }, { context_used: 12_500, context_max: 114_688 }))
  18. .toMatchObject({ totalTokens: 1_900_000, contextTokens: 12_500, contextLimit: 114_688 })
  19. })
  20. it('recognizes nested session metadata and camel-case context fields', () => {
  21. expect(normalizeChatUsage({ info: { usage: { contextUsed: 2000, contextMax: 8000, contextPercent: 0 } } }))
  22. .toMatchObject({ contextTokens: 2000, contextLimit: 8000, percentUsed: 0 })
  23. expect(normalizeChatUsage({ contextTokens: 2000, contextLimit: 8000 }))
  24. .toMatchObject({ contextTokens: 2000, contextLimit: 8000 })
  25. })
  26. it('preserves an explicit reset to zero and ignores missing or invalid measurements', () => {
  27. expect(normalizeChatUsage({ context_used: 0, context_percent: 0, total: 1_900_000 }))
  28. .toMatchObject({ contextTokens: 0, percentUsed: 0 })
  29. expect(normalizeChatUsage({ text: '完成', context_used: -1, context_max: 0, context_percent: Number.NaN }))
  30. .toBeUndefined()
  31. expect(normalizeChatUsage(undefined, {})).toBeUndefined()
  32. })
  33. })