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): 残缺 200 不得用 null 盖掉 SuperGrok 套餐或周百分比

Dash2f74c4cc673e
diff --git a/apps/desktop/src/main/__tests__/usageBroadcasterXaiSubscription.test.ts b/apps/desktop/src/main/__tests__/usageBroadcasterXaiSubscription.test.ts
index 1e6682fa36..56330582fe 100644
--- a/apps/desktop/src/main/__tests__/usageBroadcasterXaiSubscription.test.ts
+++ b/apps/desktop/src/main/__tests__/usageBroadcasterXaiSubscription.test.ts
@@ -90,4 +90,26 @@ describe('xai subscription snapshot hydration', () => {
updatedAt: 2,
});
});
+
+ it('does not let a later partial record null out cached plan or weekly percent', async () => {
+ const broadcaster = await import('../usageBroadcaster');
+ mocks.queryOne.mockResolvedValue(null);
+ await broadcaster.recordXaiSubscriptionUsageSnapshot({
+ planLabel: 'SuperGrok Heavy',
+ creditUsagePercent: 2,
+ accountFingerprint: 'bbbb',
+ updatedAt: 2,
+ });
+ await broadcaster.recordXaiSubscriptionUsageSnapshot({
+ planLabel: null,
+ creditUsagePercent: 5,
+ accountFingerprint: 'bbbb',
+ updatedAt: 3,
+ });
+ await expect(broadcaster.readXaiSubscriptionUsageSnapshot()).resolves.toMatchObject({
+ planLabel: 'SuperGrok Heavy',
+ creditUsagePercent: 5,
+ accountFingerprint: 'bbbb',
+ });
+ });
});
diff --git a/apps/desktop/src/main/usage/__tests__/xaiSubscriptionUsage.test.ts b/apps/desktop/src/main/usage/__tests__/xaiSubscriptionUsage.test.ts
index ea45baa68b..6389cd7af0 100644
--- a/apps/desktop/src/main/usage/__tests__/xaiSubscriptionUsage.test.ts
+++ b/apps/desktop/src/main/usage/__tests__/xaiSubscriptionUsage.test.ts
@@ -104,6 +104,23 @@ describe('fetchXaiSubscriptionUsageSnapshot', () => {
})).resolves.toBeNull();
});
+ it('does not replace cache when billing 200 only has a reset time', async () => {
+ const fetchFn = vi.fn(async (url: string) => {
+ if (url.includes('/settings')) {
+ return jsonResponse(200, { subscription_tier_display: 'SuperGrok Heavy' });
+ }
+ if (url.includes('format=credits')) {
+ return jsonResponse(200, { config: { currentPeriod: { end: 1_800_000_000 } } });
+ }
+ 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 when settings fails but billing has weekly data', async () => {
const fetchFn = vi.fn(async (url: string) => {
if (url.includes('/settings')) return jsonResponse(503, {});
diff --git a/apps/desktop/src/main/usage/xaiSubscriptionUsage.ts b/apps/desktop/src/main/usage/xaiSubscriptionUsage.ts
index 3f350463df..2a46621a28 100644
--- a/apps/desktop/src/main/usage/xaiSubscriptionUsage.ts
+++ b/apps/desktop/src/main/usage/xaiSubscriptionUsage.ts
@@ -166,8 +166,13 @@ export async function fetchXaiSubscriptionUsageSnapshot(opts: {
const planLabel = parseXaiSettingsPlanLabel(settings.data);
const parsedCredits = parseXaiBillingCreditsConfig(credits.data);
const subject = subjectFromUserinfo(userinfo);
- if (!settings.ok || !credits.ok || !parsedCredits) {
- // 任一权威端点失败或 billing 没有周窗口:不完整快照不能盖掉缓存里的套餐/周用量。
+ if (
+ !settings.ok
+ || !credits.ok
+ || !parsedCredits
+ || typeof parsedCredits.creditUsagePercent !== 'number'
+ ) {
+ // 权威端点失败,或 200 但缺周用量百分比:残缺快照不能盖掉缓存。
return null;
}
const snapshot = buildXaiSubscriptionUsageSnapshot({
diff --git a/apps/desktop/src/main/usageBroadcaster.ts b/apps/desktop/src/main/usageBroadcaster.ts
index eb2c967e80..b702de4dc1 100644
--- a/apps/desktop/src/main/usageBroadcaster.ts
+++ b/apps/desktop/src/main/usageBroadcaster.ts
@@ -983,9 +983,15 @@ export async function recordXaiSubscriptionUsageSnapshot(snapshot: unknown): Pro
) {
xaiSubscriptionUsageSnapshot = incoming;
} else {
+ // 已核验的同账号:只覆盖解析到的字段。incoming 里的 null 不能把套餐/周百分比抹掉。
xaiSubscriptionUsageSnapshot = {
- ...current,
- ...incoming,
+ planLabel: incoming.planLabel ?? current.planLabel ?? null,
+ creditUsagePercent: incoming.creditUsagePercent ?? current.creditUsagePercent ?? null,
+ resetsAt: incoming.resetsAt ?? current.resetsAt ?? null,
+ productUsage: incoming.productUsage ?? current.productUsage ?? [],
+ prepaidBalance: incoming.prepaidBalance ?? current.prepaidBalance ?? null,
+ source: incoming.source ?? current.source ?? null,
+ updatedAt: incoming.updatedAt ?? current.updatedAt ?? null,
accountFingerprint: incoming.accountFingerprint ?? current.accountFingerprint ?? null,
};
}