Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(desktop): settings 失败时也不用残缺快照盖掉套餐名

Dash88d0cd3bcff1
diff --git a/apps/desktop/src/main/usage/__tests__/xaiSubscriptionUsage.test.ts b/apps/desktop/src/main/usage/__tests__/xaiSubscriptionUsage.test.ts
index dd06a668a9..ea45baa68b 100644
--- a/apps/desktop/src/main/usage/__tests__/xaiSubscriptionUsage.test.ts
+++ b/apps/desktop/src/main/usage/__tests__/xaiSubscriptionUsage.test.ts
@@ -104,6 +104,21 @@ describe('fetchXaiSubscriptionUsageSnapshot', () => {
})).resolves.toBeNull();
});
+ it('does not replace cache when settings fails but billing has weekly data', async () => {
+ const fetchFn = vi.fn(async (url: string) => {
+ if (url.includes('/settings')) return jsonResponse(503, {});
+ if (url.includes('format=credits')) {
+ return jsonResponse(200, { config: { creditUsagePercent: 2 } });
+ }
+ if (url.includes('/user?') || url.includes('/userinfo')) return jsonResponse(200, {});
+ return jsonResponse(404, {});
+ });
+ await expect(fetchXaiSubscriptionUsageSnapshot({
+ accessToken: 'tok',
+ fetchFn: fetchFn as unknown as typeof fetch,
+ })).resolves.toBeNull();
+ });
+
it('does not replace cache with plan-only data when billing fails', async () => {
const fetchFn = vi.fn(async (url: string) => {
if (url.includes('/settings')) {
diff --git a/apps/desktop/src/main/usage/xaiSubscriptionUsage.ts b/apps/desktop/src/main/usage/xaiSubscriptionUsage.ts
index df68454740..3f350463df 100644
--- a/apps/desktop/src/main/usage/xaiSubscriptionUsage.ts
+++ b/apps/desktop/src/main/usage/xaiSubscriptionUsage.ts
@@ -166,8 +166,8 @@ export async function fetchXaiSubscriptionUsageSnapshot(opts: {
const planLabel = parseXaiSettingsPlanLabel(settings.data);
const parsedCredits = parseXaiBillingCreditsConfig(credits.data);
const subject = subjectFromUserinfo(userinfo);
- if (!credits.ok || !parsedCredits) {
- // billing 非 ok,或 200 但没有周窗口字段:只剩套餐名,不能当全量快照盖掉缓存。
+ if (!settings.ok || !credits.ok || !parsedCredits) {
+ // 任一权威端点失败或 billing 没有周窗口:不完整快照不能盖掉缓存里的套餐/周用量。
return null;
}
const snapshot = buildXaiSubscriptionUsageSnapshot({