From b39133941f3a68d37f48c95d0d1aed74320a1b27 Mon Sep 17 00:00:00 2001 From: Miguel Machado Date: Mon, 31 Aug 2026 13:51:46 -0300 Subject: [PATCH] fix(mac): map Z.ai body auth errors --- .../Data/ZaiSubscriptionService.swift | 4 ++++ .../CodeBurnMenubarTests/ZaiQuotaTests.swift | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/mac/Sources/CodeBurnMenubar/Data/ZaiSubscriptionService.swift b/mac/Sources/CodeBurnMenubar/Data/ZaiSubscriptionService.swift index 65e9b18e..c00a8a16 100644 --- a/mac/Sources/CodeBurnMenubar/Data/ZaiSubscriptionService.swift +++ b/mac/Sources/CodeBurnMenubar/Data/ZaiSubscriptionService.swift @@ -110,6 +110,10 @@ enum ZaiSubscriptionService { guard let root = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else { throw FetchError.parseFailure } + let bodyCode = jsonNumber(root["code"]).map(Int.init) + if bodyCode == 401 || bodyCode == 403 { throw FetchError.authenticationRejected } + if root["success"] as? Bool == false { throw FetchError.parseFailure } + let payload = (root["data"] as? [String: Any]) ?? root guard let limits = payload["limits"] as? [Any] else { throw FetchError.parseFailure } diff --git a/mac/Tests/CodeBurnMenubarTests/ZaiQuotaTests.swift b/mac/Tests/CodeBurnMenubarTests/ZaiQuotaTests.swift index 3417709b..8c9278ab 100644 --- a/mac/Tests/CodeBurnMenubarTests/ZaiQuotaTests.swift +++ b/mac/Tests/CodeBurnMenubarTests/ZaiQuotaTests.swift @@ -123,10 +123,28 @@ struct ZaiQuotaTests { } } + @Test("HTTP 200 body authentication failures are terminal") + func bodyAuthenticationFailure() async throws { + for code in [401, 403] { + let recorder = RequestRecorder() + let body = #"{"code":\#(code),"msg":"token expired or incorrect","success":false}"# + do { + _ = try await ZaiSubscriptionService.refresh( + apiKey: Self.syntheticKey, + deps: Self.deps(recorder: recorder, body: body) + ) + Issue.record("Expected body code \(code) to reject authentication") + } catch let error as ZaiSubscriptionService.FetchError { + #expect(error == .authenticationRejected) + } + } + } + @Test("malformed or empty quota fails") func malformedQuota() throws { for body in [ "not json", + #"{"code":500,"success":false}"#, #"{"data":{}}"#, #"{"data":{"limits":[]}}"#, #"{"data":{"limits":[{"type":"CREDIT_LIMIT","unit":3,"number":5}]}}"#,