Skip to content

Commit 8d877ab

Browse files
committed
fix: reset workspaces when re-enabling account
1 parent ba39fd6 commit 8d877ab

2 files changed

Lines changed: 57 additions & 0 deletions

File tree

lib/accounts.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,11 @@ export class AccountManager {
765765
if (index < 0 || index >= this.accounts.length) return null;
766766
const account = this.accounts[index];
767767
if (!account) return null;
768+
const wasEnabled = account.enabled !== false;
768769
account.enabled = enabled;
770+
if (enabled && !wasEnabled) {
771+
this.resetWorkspaces(account);
772+
}
769773
return account;
770774
}
771775

@@ -850,6 +854,22 @@ export class AccountManager {
850854
}
851855
}
852856

857+
private resetWorkspaces(account: ManagedAccount): void {
858+
if (!account.workspaces || account.workspaces.length === 0) {
859+
return;
860+
}
861+
862+
for (const workspace of account.workspaces) {
863+
workspace.enabled = true;
864+
delete workspace.disabledAt;
865+
}
866+
867+
const currentIdx = account.currentWorkspaceIndex ?? 0;
868+
if (currentIdx < 0 || currentIdx >= account.workspaces.length) {
869+
account.currentWorkspaceIndex = 0;
870+
}
871+
}
872+
853873
getCurrentWorkspace(account: ManagedAccount): Workspace | null {
854874
if (!account.workspaces || account.workspaces.length === 0) {
855875
return null;

test/accounts.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,43 @@ describe("AccountManager", () => {
256256
expect(account.workspaces?.map((workspace) => workspace.enabled)).toEqual([false, true]);
257257
});
258258

259+
it("re-enabling an exhausted account restores its workspaces", () => {
260+
const now = Date.now();
261+
const stored = {
262+
version: 3 as const,
263+
activeIndex: 0,
264+
accounts: [
265+
{
266+
refreshToken: "token-1",
267+
addedAt: now,
268+
lastUsed: now,
269+
enabled: false,
270+
workspaces: [
271+
{ id: "workspace-1", name: "Workspace 1", enabled: false, disabledAt: now - 2_000 },
272+
{ id: "workspace-2", name: "Workspace 2", enabled: false, disabledAt: now - 1_000 },
273+
],
274+
currentWorkspaceIndex: 1,
275+
},
276+
],
277+
};
278+
279+
const manager = new AccountManager(undefined, stored);
280+
const account = manager.setAccountEnabled(0, true);
281+
expect(account).not.toBeNull();
282+
if (!account) {
283+
throw new Error("account should exist");
284+
}
285+
286+
expect(account.enabled).toBe(true);
287+
expect(manager.hasEnabledWorkspaces(account)).toBe(true);
288+
expect(manager.getEnabledWorkspaceCount(account)).toBe(2);
289+
expect(manager.getCurrentWorkspace(account)?.id).toBe("workspace-2");
290+
expect(account.workspaces).toEqual([
291+
{ id: "workspace-1", name: "Workspace 1", enabled: true },
292+
{ id: "workspace-2", name: "Workspace 2", enabled: true },
293+
]);
294+
});
295+
259296
it("checks account availability by enabled/rate-limit/cooldown state", () => {
260297
const now = Date.now();
261298
const stored = {

0 commit comments

Comments
 (0)