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 after #447: 取消订阅绑定到确认时的账号

Lizi186302a6ab59
diff --git a/apps/desktop/src/renderer/features/billing/BillingPage.tsx b/apps/desktop/src/renderer/features/billing/BillingPage.tsx
index d6e0448f30..ec8e04629f 100644
--- a/apps/desktop/src/renderer/features/billing/BillingPage.tsx
+++ b/apps/desktop/src/renderer/features/billing/BillingPage.tsx
@@ -253,6 +253,22 @@ export function BillingSettingsSection({ accountId }: { accountId: string | null
const checkout = useBillingCheckout(accountId);
const previousCheckoutPhaseRef = useRef(checkout.state.phase);
const cancelSubscriptionLockRef = useRef(false);
+ // 取消订阅的 DELETE 不带 subscriptionId,服务端按「请求时已认证的账号」执行。
+ // ConfirmDialogProvider 挂在 AuthProvider 之外(见 App.tsx),弹窗会活过本 section
+ // 因 dataOwnerId 变化而发生的卸载,所以必须记住确认时的账号与挂载态。
+ const accountIdRef = useRef(accountId);
+ const sectionMountedRef = useRef(true);
+
+ useEffect(() => {
+ accountIdRef.current = accountId;
+ }, [accountId]);
+
+ useEffect(() => {
+ sectionMountedRef.current = true;
+ return () => {
+ sectionMountedRef.current = false;
+ };
+ }, []);
const resetSelection = useCallback(() => {
setSelectedOfferCode(null);
@@ -506,6 +522,7 @@ export function BillingSettingsSection({ accountId }: { accountId: string | null
}
cancelSubscriptionLockRef.current = true;
try {
+ const confirmingAccountId = accountIdRef.current;
const periodEndAt = currentPlanFacts?.periodEndAt ?? null;
const confirmed = await confirm({
title: t('billing.settings.subscriptionCard.cancelConfirmTitle'),
@@ -518,6 +535,9 @@ export function BillingSettingsSection({ accountId }: { accountId: string | null
cancelText: t('commonUi.confirmDialog.cancel'),
});
if (!confirmed) return;
+ // 确认期间账号被换掉(或本 section 已卸载)就放弃:再发请求会取消到另一个账号
+ // 的订阅,而取消不可撤销。
+ if (!sectionMountedRef.current || accountIdRef.current !== confirmingAccountId) return;
setCancelingSubscription(true);
try {
diff --git a/apps/desktop/src/renderer/features/billing/__tests__/BillingPage.test.tsx b/apps/desktop/src/renderer/features/billing/__tests__/BillingPage.test.tsx
index aaba4e8251..1efc08ec3f 100644
--- a/apps/desktop/src/renderer/features/billing/__tests__/BillingPage.test.tsx
+++ b/apps/desktop/src/renderer/features/billing/__tests__/BillingPage.test.tsx
@@ -15,6 +15,8 @@ const uiMocks = vi.hoisted(() => ({
toastSuccess: vi.fn(),
}));
+const authState = vi.hoisted(() => ({ dataOwnerId: 'account-fixture' as string | null }));
+
const checkout = {
state: {
open: false,
@@ -59,7 +61,7 @@ vi.mock('@/features/feature-context', () => ({
useRegisterContentHeader: vi.fn(),
}));
vi.mock('@/contexts/AuthContext', () => ({
- useAuth: () => ({ dataOwnerId: 'account-fixture' }),
+ useAuth: () => ({ dataOwnerId: authState.dataOwnerId }),
}));
vi.mock('@/components/ui/confirm-dialog-provider', () => ({
useConfirmDialog: () => ({ confirm: uiMocks.confirm }),
@@ -83,6 +85,7 @@ beforeEach(() => {
uiMocks.confirm.mockReset().mockResolvedValue(false);
uiMocks.toastError.mockReset();
uiMocks.toastSuccess.mockReset();
+ authState.dataOwnerId = 'account-fixture';
});
describe('BillingPage remote catalog rendering', () => {
@@ -1403,6 +1406,30 @@ describe('BillingPage plan change', () => {
await act(async () => resolveConfirm(false));
});
+ it('drops a confirmed cancellation when the account changed while confirming', async () => {
+ const billing = install(billingMocks());
+ let resolveConfirm!: (confirmed: boolean) => void;
+ uiMocks.confirm.mockReturnValueOnce(
+ new Promise<boolean>((resolve) => {
+ resolveConfirm = resolve;
+ }),
+ );
+
+ const view = render(<BillingPage />);
+ fireEvent.click(await screen.findByText('billing.settings.subscriptionCard.cancelAction'));
+ expect(uiMocks.confirm).toHaveBeenCalledTimes(1);
+
+ // 弹窗还开着时账号被换掉:section 按 dataOwnerId 重挂,但弹窗挂在 AuthProvider
+ // 之外仍然存活。此时确认不能落到新账号的订阅上。
+ authState.dataOwnerId = 'account-switched';
+ view.rerender(<BillingPage />);
+ await screen.findByText('billing.settings.subscriptionCard.cancelAction');
+
+ await act(async () => resolveConfirm(true));
+
+ expect(billing.cancelCurrentSubscription).not.toHaveBeenCalled();
+ });
+
it('keeps the loading cancellation accessible and disables competing actions', async () => {
const billing = billingMocks();
billing.getCurrentSubscription = vi.fn(async () => ({