Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/__tests__/tokscale.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
versionAtLeast,
reportToStats,
resetDetectionCache,
TokscaleNotFoundError,
} from "../tokscale"

const mockExecFile = vi.mocked(execFile)
Expand Down Expand Up @@ -282,6 +283,26 @@ describe("fetchPeriodStats", () => {
mockExecFileError(error)
await expect(fetchPeriodStats("today")).rejects.toThrow()
})

it("throws TokscaleNotFoundError and resets detection cache on ENOENT", async () => {
// Prime cache as installed
mockDetectSequence("tokscale 4.0.5")
await detectTokscale()
vi.clearAllMocks()

// Now simulate binary gone (ENOENT)
const error = Object.assign(new Error("spawn tokscale ENOENT"), {
code: "ENOENT" as unknown as number,
})
mockExecFileError(error)

await expect(fetchPeriodStats("today")).rejects.toThrow(TokscaleNotFoundError)

// Detection cache should be reset — next detectTokscale() must re-probe
mockExecFileError(Object.assign(new Error("not found"), { code: 1 }))
const result = await detectTokscale()
expect(result).toBe(false)
})
})

describe("parseModelReport", () => {
Expand Down
12 changes: 12 additions & 0 deletions src/tokscale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import { PERIOD_FLAGS } from "./types"

type SemVer = readonly [number, number, number]

export class TokscaleNotFoundError extends Error {
constructor() {
super("tokscale binary not found")
this.name = "TokscaleNotFoundError"
}
}

let cachedDetection: boolean | null = null
let cachedVersion: SemVer | null = null

Expand Down Expand Up @@ -70,6 +77,11 @@ export function fetchPeriodStats(
{ timeout: 15000, maxBuffer: 1024 * 1024 },
(error, stdout) => {
if (error) {
if ((error as NodeJS.ErrnoException).code === "ENOENT") {
resetDetectionCache()
reject(new TokscaleNotFoundError())
return
}
reject(new Error(`tokscale CLI failed: ${error.message}`))
return
}
Expand Down
9 changes: 8 additions & 1 deletion src/tui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createSignal } from "solid-js"
import type { TimePeriod, PeriodState, TokscalePluginOptions } from "./types"
import { TIME_PERIODS, PERIOD_LABELS } from "./types"
import { formatTokens, formatCost } from "./format"
import { detectTokscale, fetchPeriodStats } from "./tokscale"
import { detectTokscale, fetchPeriodStats, TokscaleNotFoundError } from "./tokscale"

const TOKSCALE_BLUE = "#0073FF"

Expand Down Expand Up @@ -45,6 +45,13 @@ const tui: TuiPlugin = async (api, options, _meta) => {
})
setState({ status: "success", stats, error: null })
} catch (e) {
if (e instanceof TokscaleNotFoundError) {
setInstalled(false)
for (const p of TIME_PERIODS) {
signals[p][1]({ status: "not-installed", stats: null, error: null })
}
return
}
setState({ status: "error", stats: signals[period][0]().stats, error: String(e) })
}
})
Expand Down
Loading