mattrguerra/cortexmainPrivate on GitHub, published here in full

format.test.ts

Language TypeScript2.3 KB62 lines

src/lib/format.test.ts

  1. import { describe, expect, it } from 'vitest'
  2. import { costNano, estimateTokens } from '../ipc/mock/mockClient'
  3. import { perMillion, relativeDay, shortModel, snippetParts, tokens, usd } from './format'
  4. describe('money and tokens', () => {
  5. it('formats nano-dollars like cortex-core does', () => {
  6. expect(usd(0)).toBe('$0')
  7. expect(usd(12)).toBe('<$0.0001')
  8. expect(usd(3_500_000)).toBe('$0.0035')
  9. expect(usd(1_240_000_000)).toBe('$1.24')
  10. })
  11. it('prices per million', () => {
  12. expect(perMillion(0)).toBe('free')
  13. expect(perMillion(0.5)).toBe('$0.50')
  14. expect(perMillion(0.075)).toBe('$0.075')
  15. expect(perMillion(15)).toBe('$15.00')
  16. })
  17. it('token counts', () => {
  18. expect(tokens(412)).toBe('412')
  19. expect(tokens(1234)).toBe('1.2k')
  20. expect(tokens(262144)).toBe('262k')
  21. expect(tokens(1_048_576)).toBe('1.0M')
  22. })
  23. it('the mock bills exactly as cost.rs', () => {
  24. // Same cases as cost.rs's tests.
  25. const p = { input_per_m: 0.5, output_per_m: 1.5, cache_read_per_m: 0.1 }
  26. expect(costNano({ prompt_tokens: 1000, completion_tokens: 2000, cached_tokens: 0 }, p)).toBe(3_500_000)
  27. expect(costNano({ prompt_tokens: 1000, completion_tokens: 0, cached_tokens: 400 }, p)).toBe(340_000)
  28. expect(costNano({ prompt_tokens: 10, completion_tokens: -5, cached_tokens: 99 }, p)).toBe(1_000)
  29. expect(estimateTokens('abcde')).toBe(2)
  30. expect(estimateTokens('你好')).toBe(2)
  31. })
  32. })
  33. describe('text helpers', () => {
  34. it('short model names', () => {
  35. expect(shortModel('Qwen/Qwen3-235B-A22B-Instruct-2507')).toBe('Qwen3-235B-A22B-Instruct-2507')
  36. expect(shortModel('llama3.2')).toBe('llama3.2')
  37. })
  38. it('search snippet marks', () => {
  39. expect(snippetParts('use \u0001rsync\u0002 with \u0001delete\u0002.')).toEqual([
  40. { text: 'use ', hit: false },
  41. { text: 'rsync', hit: true },
  42. { text: ' with ', hit: false },
  43. { text: 'delete', hit: true },
  44. { text: '.', hit: false },
  45. ])
  46. })
  47. it('relative days', () => {
  48. const now = new Date(2026, 8, 28, 15).getTime()
  49. expect(relativeDay(new Date(2026, 8, 28, 1).getTime(), now)).toBe('Today')
  50. expect(relativeDay(new Date(2026, 8, 27, 23).getTime(), now)).toBe('Yesterday')
  51. expect(relativeDay(new Date(2026, 8, 24).getTime(), now)).toBe('This week')
  52. expect(relativeDay(new Date(2026, 5, 1).getTime(), now)).toBe('Earlier')
  53. })
  54. })