Skip to content

Commit 3507632

Browse files
committed
fix(longcat): surface invalid-session and drop unused today-token path
Addresses Codex review on #1697: - user-current now propagates envelope auth failures (HTTP 200 + code 401/403 -> .invalidSession) instead of swallowing them with try?, so expired cookies prompt re-auth rather than reporting an empty snapshot. - Remove the never-assigned todayTokens / freeQuota fields and the unreachable tertiary 'Today' window; LongCat's tokenUsage is a quota snapshot with no per-day figure. - Add envelope unit tests (invalid-session + success unwrap).
1 parent 8a6c0b4 commit 3507632

3 files changed

Lines changed: 29 additions & 35 deletions

File tree

Sources/CodexBarCore/Providers/LongCat/LongCatUsageFetcher.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ public struct LongCatUsageFetcher: Sendable {
1414

1515
public static func fetchUsage(cookieHeader: String, now: Date = Date()) async throws -> LongCatUsageSnapshot {
1616
// Account name. The user-current payload also carries a session token and
17-
// phone number, so its body is never logged. Failure here is non-fatal.
17+
// phone number, so its body is never logged. This is the required probe:
18+
// a Meituan envelope with HTTP 200 but code 401/403 surfaces as
19+
// `.invalidSession` here (via unwrap) so expired cookies are reported
20+
// rather than masked by an empty snapshot.
1821
var account: [String: Any]?
1922
if let data = try await self.get(self.userCurrentPath, cookieHeader: cookieHeader, required: true) {
20-
account = (try? LongCatEnvelope.unwrap(self.json(data))) as? [String: Any]
23+
account = try LongCatEnvelope.unwrap(self.json(data)) as? [String: Any]
2124
}
2225

2326
var usage: [String: Any]?
@@ -56,7 +59,6 @@ public struct LongCatUsageFetcher: Sendable {
5659
snapshot.totalQuota = LongCatJSON.double(usage["totalToken"])
5760
snapshot.usedQuota = LongCatJSON.double(usage["usedToken"])
5861
snapshot.remainingQuota = LongCatJSON.double(usage["availableToken"])
59-
snapshot.freeQuota = LongCatJSON.double(usage["freeAvailableToken"])
6062
}
6163

6264
if let pendingFuel {

Sources/CodexBarCore/Providers/LongCat/LongCatUsageSnapshot.swift

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,32 @@
11
import Foundation
22

33
/// Parsed, Sendable view of the LongCat console quota model:
4-
/// 总额度 (total) = 初始额度 (free, refreshed daily) + 加油包额度 (fuel packs, expiring),
5-
/// plus today's token usage from `tokenUsage`.
4+
/// 总额度 (total token quota) plus 加油包额度 (fuel packs, which expire).
65
public struct LongCatUsageSnapshot: Sendable {
76
public var totalQuota: Double?
8-
public var freeQuota: Double?
9-
public var fuelPackTotal: Double?
10-
public var fuelPackRemaining: Double?
117
public var usedQuota: Double?
128
public var remainingQuota: Double?
13-
public var todayTokens: Double?
9+
public var fuelPackTotal: Double?
10+
public var fuelPackRemaining: Double?
1411
public var nearestFuelExpiry: Date?
1512
public var accountName: String?
1613
public var updatedAt: Date
1714

1815
public init(
1916
totalQuota: Double? = nil,
20-
freeQuota: Double? = nil,
21-
fuelPackTotal: Double? = nil,
22-
fuelPackRemaining: Double? = nil,
2317
usedQuota: Double? = nil,
2418
remainingQuota: Double? = nil,
25-
todayTokens: Double? = nil,
19+
fuelPackTotal: Double? = nil,
20+
fuelPackRemaining: Double? = nil,
2621
nearestFuelExpiry: Date? = nil,
2722
accountName: String? = nil,
2823
updatedAt: Date = Date())
2924
{
3025
self.totalQuota = totalQuota
31-
self.freeQuota = freeQuota
32-
self.fuelPackTotal = fuelPackTotal
33-
self.fuelPackRemaining = fuelPackRemaining
3426
self.usedQuota = usedQuota
3527
self.remainingQuota = remainingQuota
36-
self.todayTokens = todayTokens
28+
self.fuelPackTotal = fuelPackTotal
29+
self.fuelPackRemaining = fuelPackRemaining
3730
self.nearestFuelExpiry = nearestFuelExpiry
3831
self.accountName = accountName
3932
self.updatedAt = updatedAt
@@ -48,7 +41,7 @@ extension LongCatUsageSnapshot {
4841
}
4942

5043
public func toUsageSnapshot() -> UsageSnapshot {
51-
// Primary: overall quota consumption (总额度).
44+
// Primary: overall token quota consumption (总额度).
5245
var primary = RateWindow(
5346
usedPercent: 0,
5447
windowMinutes: nil,
@@ -75,16 +68,6 @@ extension LongCatUsageSnapshot {
7568
resetDescription: "Fuel pack: \(Int(remaining))/\(Int(total))")
7669
}
7770

78-
// Tertiary: informational today-token count.
79-
var tertiary: RateWindow?
80-
if let today = todayTokens {
81-
tertiary = RateWindow(
82-
usedPercent: 0,
83-
windowMinutes: 1440,
84-
resetsAt: nil,
85-
resetDescription: "Today: \(Int(today)) tokens")
86-
}
87-
8871
let identity = ProviderIdentitySnapshot(
8972
providerID: .longcat,
9073
accountEmail: nil,
@@ -94,7 +77,7 @@ extension LongCatUsageSnapshot {
9477
return UsageSnapshot(
9578
primary: primary,
9679
secondary: secondary,
97-
tertiary: tertiary,
80+
tertiary: nil,
9881
providerCost: nil,
9982
updatedAt: self.updatedAt,
10083
identity: identity)

Tests/CodexBarTests/LongCatProviderTests.swift

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,6 @@ struct LongCatProviderTests {
6767
#expect(abs((usage.secondary?.usedPercent ?? 0) - 60) < 0.001)
6868
}
6969
70-
@Test
71-
func `today tokens populate tertiary window`() {
72-
let usage = LongCatUsageSnapshot(todayTokens: 12345).toUsageSnapshot()
73-
#expect(usage.tertiary != nil)
74-
}
75-
7670
// MARK: - buildSnapshot against captured live response shapes
7771
7872
private func object(_ json: String) throws -> [String: Any] {
@@ -113,4 +107,19 @@ struct LongCatProviderTests {
113107
#expect(snapshot.fuelPackRemaining == 750)
114108
#expect(snapshot.nearestFuelExpiry != nil)
115109
}
110+
111+
// MARK: - Envelope
112+
113+
@Test
114+
func `envelope surfaces invalid session on auth code`() {
115+
#expect(throws: LongCatAPIError.invalidSession) {
116+
try LongCatEnvelope.unwrap(["code": 401, "message": "unauthorized"])
117+
}
118+
}
119+
120+
@Test
121+
func `envelope unwraps data on success`() throws {
122+
let data = try LongCatEnvelope.unwrap(["code": 0, "data": ["x": 1]]) as? [String: Any]
123+
#expect(data?["x"] as? Int == 1)
124+
}
116125
}

0 commit comments

Comments
 (0)