Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/account-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ export class AccountManager {
if (next) return next;
}
if (this._isAvailable(current)) {
return current;
// A strictly higher-priority (lower value) available account preempts a
// healthy current one. Within the same priority tier we stay put, so the
// common case (all accounts at the default priority 0) is unchanged and
// never thrashes — preemption only triggers when priorities differ.
const betterExists = this.accounts.some(a =>
this._isAvailable(a) && (a.priority || 0) < (current.priority || 0));
return betterExists ? this._selectNext() : current;
}
return this._selectNext();
}
Expand Down
23 changes: 23 additions & 0 deletions test/rotation-priority.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,26 @@ test('exhausted highest-priority account is skipped, next priority wins', () =>
exhaust(am, 0);
assert.equal(am._selectNext().name, 'b');
});

test('getActiveAccount preempts a healthy current account for a higher-priority one', () => {
const am = new AccountManager([oauth('a', { priority: 0 }), oauth('b', { priority: 1 })], 0.98);
am.currentIndex = 1; // currently on the lower-priority account
assert.equal(am.getActiveAccount().name, 'a');
assert.equal(am.currentIndex, 0);
});

test('getActiveAccount stays sticky within the same priority tier (no thrash)', () => {
const am = new AccountManager([oauth('a', { priority: 0 }), oauth('b', { priority: 0 })], 0.98);
am.currentIndex = 0;
// b has a sooner weekly reset, but same priority → must NOT switch away from a.
am.accounts[0].quota.unified7dReset = 5000;
am.accounts[1].quota.unified7dReset = 1000;
assert.equal(am.getActiveAccount().name, 'a');
assert.equal(am.currentIndex, 0);
});

test('common case: all-equal priority leaves getActiveAccount on the healthy current account', () => {
const am = new AccountManager([oauth('a'), oauth('b'), oauth('c')], 0.98);
am.currentIndex = 2;
assert.equal(am.getActiveAccount().name, 'c'); // unchanged — no preemption when priorities tie
});
Loading