new: [opencode-greenpt-usage] add GreenPT credit balance plugin
Show the remaining GreenPT API credit balance in the OpenCode TUI sidebar. The key is resolved from the ``apiKey`` option, the ``GREENPT_API_KEY`` environment variable, or the active OpenCode ``auth.json`` (provider ``greenpt``); the section is hidden when no key is present, so the plugin stays portable across machines and profiles. The balance is read from the ``x-credits-remaining`` header returned by a free ``HEAD /v1/chat/completions`` probe (HTTP 400 before any credit is consumed), which avoids a billable call per refresh. Tests cover the credential resolution, the header parsing and the live API. Distribute via git clone; there is no npm publish.
This commit is contained in:
24
test/integration.greenpt.test.ts
Normal file
24
test/integration.greenpt.test.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { describe, expect, it } from "vitest"
|
||||
import { fetchBalance } from "../src/balance"
|
||||
import { DEFAULT_BASE_URL } from "../src/store"
|
||||
|
||||
const apiKey = process.env.GREENPT_API_KEY
|
||||
const enabled = process.env.GREENPT_INTEGRATION === "1" && typeof apiKey === "string" && apiKey.length > 0
|
||||
|
||||
const suite = enabled ? describe : describe.skip
|
||||
|
||||
suite("GreenPT live API", () => {
|
||||
it("returns a finite balance from the free HEAD probe", async () => {
|
||||
const result = await fetchBalance(DEFAULT_BASE_URL, apiKey as string)
|
||||
if ("kind" in result) {
|
||||
throw new Error(`expected a balance, got error: ${result.kind}`)
|
||||
}
|
||||
expect(Number.isFinite(result.credits)).toBe(true)
|
||||
expect(result.credits).toBeGreaterThanOrEqual(0)
|
||||
})
|
||||
|
||||
it("reports unauthorized for an invalid key", async () => {
|
||||
const result = await fetchBalance(DEFAULT_BASE_URL, "sk-definitely-invalid")
|
||||
expect(result).toEqual({ kind: "unauthorized" })
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user