usage.test.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // kilocode_change - new file
  2. import { describe, expect, test } from "bun:test"
  3. import type { AssistantMessage, Message, UserMessage } from "@kilocode/sdk/v2"
  4. import { formatCount, getUsage } from "../../../src/cli/cmd/tui/routes/session/usage"
  5. function assistant(id: string, input: number, output: number, read: number): AssistantMessage {
  6. return {
  7. id,
  8. sessionID: "ses_1",
  9. role: "assistant",
  10. time: { created: 1 },
  11. parentID: "msg_parent",
  12. modelID: "claude-sonnet",
  13. providerID: "anthropic",
  14. mode: "code",
  15. agent: "code",
  16. path: { cwd: "/tmp", root: "/tmp" },
  17. cost: 0,
  18. tokens: {
  19. input,
  20. output,
  21. reasoning: 99,
  22. cache: {
  23. read,
  24. write: 7,
  25. },
  26. },
  27. }
  28. }
  29. function user(): UserMessage {
  30. return {
  31. id: "msg_user",
  32. sessionID: "ses_1",
  33. role: "user",
  34. time: { created: 0 },
  35. agent: "code",
  36. model: {
  37. providerID: "anthropic",
  38. modelID: "claude-sonnet",
  39. },
  40. }
  41. }
  42. describe("session usage", () => {
  43. test("sums input, output, and cache read across assistant messages only", () => {
  44. const msg: Message[] = [user(), assistant("a", 1200, 45, 300), assistant("b", 800, 55, 700)]
  45. expect(getUsage(msg)).toEqual({
  46. input: 2000,
  47. output: 100,
  48. cached: 1000,
  49. })
  50. })
  51. test("formats full counts with thousands separators", () => {
  52. expect(formatCount(0)).toBe("0")
  53. expect(formatCount(12345)).toBe("12,345")
  54. expect(formatCount(9876543)).toBe("9,876,543")
  55. })
  56. })