Files
steipete--codexbar/TestsLinux/OpenCodeGoPercentUnitLinuxTests.swift
OfficialAbhinavSingh bda63b9b30 fix: don't rescale computed OpenCode/OpenCodeGo usage percent below 1%
OpenCode and OpenCodeGo parseWindow apply a fraction heuristic
(if percent <= 1 { percent *= 100 }) meant for a direct percent field
that may arrive as a 0...1 fraction. It also ran on the computed
used/limit percent, which is already 0...100 - so an account at or below
1% used (e.g. used=1, limit=100 -> 1.0) was rescaled to 100%, a false
quota-exhausted reading. Gate the heuristic behind percentIsDirect so it
only touches the direct field. Follow-up to #2255/#2265/#2293.
2026-07-19 14:44:13 +01:00

42 lines
1.8 KiB
Swift

#if os(Linux)
import Foundation
import Testing
@testable import CodexBarCore
struct OpenCodeGoPercentUnitLinuxTests {
private func rollingPercent(used: Int, limit: Int) throws -> Double {
let payload: [String: Any] = [
"usage": ["rollingUsage": ["used": used, "limit": limit, "resetInSec": 600]],
]
let text = try String(data: JSONSerialization.data(withJSONObject: payload), encoding: .utf8) ?? ""
return try OpenCodeGoUsageFetcher.parseSubscription(text: text, now: Date(timeIntervalSince1970: 0))
.rollingUsagePercent
}
@Test
func `sub-one-percent computed usage is not rescaled to 100`() throws {
// 1/100 = 1% used. The direct-fraction heuristic (<= 1 => * 100) must NOT touch a
// computed used/limit percent, which is already 0...100 — else 1.0 becomes 100.
#expect(try abs(self.rollingPercent(used: 1, limit: 100) - 1) < 0.0001)
// 1/200 = 0.5% used (was wrongly rescaled to 50).
#expect(try abs(self.rollingPercent(used: 1, limit: 200) - 0.5) < 0.0001)
}
@Test
func `normal computed usage is unchanged`() throws {
#expect(try abs(self.rollingPercent(used: 25, limit: 100) - 25) < 0.0001)
}
@Test
func `direct fractional percent is still scaled to percent`() throws {
// A direct percent field given as a 0...1 fraction keeps the existing behavior.
let payload: [String: Any] = [
"usage": ["rollingUsage": ["usagePercent": 0.25, "resetInSec": 600]],
]
let text = try String(data: JSONSerialization.data(withJSONObject: payload), encoding: .utf8) ?? ""
let snapshot = try OpenCodeGoUsageFetcher.parseSubscription(text: text, now: Date(timeIntervalSince1970: 0))
#expect(snapshot.rollingUsagePercent == 25)
}
}
#endif