Files
opencode-greenpt-usage/test/format.test.ts
Stéphan Sainléger 5129f295da new: [opencode-greenpt-usage] port plugin to OpenCode v2
Rewrite the TUI plugin against the OpenCode v2 plugin API
(``@opencode/plugin`` + ``@opencode/plugin/tui``):

- ``src/index.ts``: minimal server-side ``Plugin.define`` entry.
- ``src/tui.tsx``: CLI plugin registered as ``./tui``, appending the
  widget to the ``sidebar.content`` slot through ``context.ui.slot``
  and exposing the refresh/reset commands via ``context.keymap.layer``.
- ``src/store.ts``: durable plugin storage (``context.storage.store``)
  replaces the KV store; ``session.idle``/``session.status`` events and
  the ``client.session`` API drive reconciliation and backfill.
- ``src/widget.tsx``: v2 semantic theme tokens
  (``theme.text.base``/``text.muted``/``text.feedback``).
- Tests updated to a structural fake of the v2 context.

The free ``HEAD /v1/chat/completions`` balance probe and the credential
resolution order are unchanged. The ``order`` option is gone: v2 slot
placement is additive.
2026-10-19 17:00:48 +02:00

53 lines
1.5 KiB
TypeScript

import { describe, expect, it } from "vitest"
import { formatBalanceError, formatCredits, formatTokens } from "../src/format"
describe("formatCredits", () => {
it("formats with two decimals", () => {
expect(formatCredits(5.309411174)).toBe("5.31")
})
it("formats zero", () => {
expect(formatCredits(0)).toBe("0.00")
})
it("returns a dash for non-finite input", () => {
expect(formatCredits(Number.NaN)).toBe("-")
})
})
describe("formatTokens", () => {
it("formats small counts as integers", () => {
expect(formatTokens(842)).toBe("842")
expect(formatTokens(0)).toBe("0")
})
it("formats thousands with one decimal below 100k", () => {
expect(formatTokens(12_400)).toBe("12.4k")
expect(formatTokens(999)).toBe("999")
})
it("rounds thousands above 100k", () => {
expect(formatTokens(234_500)).toBe("235k")
})
it("formats millions", () => {
expect(formatTokens(3_200_000)).toBe("3.2M")
expect(formatTokens(45_000_000)).toBe("45.0M")
})
it("handles non-finite and negative input", () => {
expect(formatTokens(Number.NaN)).toBe("0")
expect(formatTokens(-5)).toBe("0")
})
})
describe("formatBalanceError", () => {
it("renders each error kind", () => {
expect(formatBalanceError({ kind: "unauthorized" })).toContain("invalid")
expect(formatBalanceError({ kind: "network" })).toContain("network")
expect(formatBalanceError({ kind: "missing-header", status: 400 })).toContain("400")
expect(formatBalanceError({ kind: "no-credential", reason: "missing" })).toContain("GreenPT")
})
})