2026-10-01 00:12:03 +08:00
|
|
|
import { describe, expect, test } from 'bun:test';
|
2026-11-23 19:00:04 +08:00
|
|
|
import type { TFunction } from 'i18next';
|
|
|
|
|
import {
|
|
|
|
|
createAuthFileRefreshPending,
|
|
|
|
|
refreshAllAuthFiles,
|
|
|
|
|
refreshAuthFile,
|
|
|
|
|
type AuthFileRefreshDeps,
|
|
|
|
|
} from '@/features/authFiles/refreshActions';
|
|
|
|
|
import type { AuthFileItem } from '@/types';
|
2026-10-01 00:12:03 +08:00
|
|
|
|
|
|
|
|
function harness() {
|
|
|
|
|
let revision = 1;
|
|
|
|
|
let confirmation: (() => Promise<void>) | undefined;
|
|
|
|
|
let results: unknown = null;
|
|
|
|
|
let loads = 0;
|
|
|
|
|
let notifications = 0;
|
|
|
|
|
let invalidations = 0;
|
2026-11-23 19:00:04 +08:00
|
|
|
let mutations = 0;
|
|
|
|
|
let refreshing: Record<string, boolean> = {};
|
2026-10-01 00:12:03 +08:00
|
|
|
const calls: unknown[][] = [];
|
|
|
|
|
let resolve!: (value: unknown) => void;
|
|
|
|
|
let reject!: (error: Error) => void;
|
2026-11-23 19:00:04 +08:00
|
|
|
const request = <T>(...args: unknown[]) => {
|
2026-10-01 00:12:03 +08:00
|
|
|
calls.push(args);
|
2026-11-23 19:00:04 +08:00
|
|
|
return new Promise<T>((res, rej) => {
|
|
|
|
|
resolve = res as (value: unknown) => void;
|
2026-10-01 00:12:03 +08:00
|
|
|
reject = rej;
|
|
|
|
|
});
|
|
|
|
|
};
|
2026-11-23 19:00:04 +08:00
|
|
|
const pending = createAuthFileRefreshPending();
|
|
|
|
|
const deps: AuthFileRefreshDeps = {
|
|
|
|
|
requestManualRefresh: (...args) => request(...args),
|
|
|
|
|
requestAllManualRefresh: () => request(),
|
|
|
|
|
getConnectionRevision: () => revision,
|
|
|
|
|
setManualRefreshing: (update) => {
|
|
|
|
|
refreshing = typeof update === 'function' ? update(refreshing) : update;
|
|
|
|
|
},
|
2026-10-01 00:12:03 +08:00
|
|
|
setRefreshingAllCredentials: () => {},
|
2026-11-23 19:00:04 +08:00
|
|
|
setRefreshResults: (value) => {
|
2026-10-01 00:12:03 +08:00
|
|
|
results = value;
|
|
|
|
|
},
|
2026-11-23 19:00:04 +08:00
|
|
|
showConfirmation: (options) => {
|
2026-10-01 00:12:03 +08:00
|
|
|
confirmation = options.onConfirm;
|
|
|
|
|
},
|
|
|
|
|
showNotification: () => {
|
|
|
|
|
notifications++;
|
|
|
|
|
},
|
|
|
|
|
invalidateInFlightLoads: () => {
|
|
|
|
|
invalidations++;
|
|
|
|
|
},
|
|
|
|
|
notifyAuthFilesChanged: () => {},
|
2026-11-23 19:00:04 +08:00
|
|
|
onFilesMutated: () => {
|
|
|
|
|
mutations++;
|
|
|
|
|
},
|
|
|
|
|
reloadFiles: async () => {
|
2026-10-01 00:12:03 +08:00
|
|
|
loads++;
|
|
|
|
|
},
|
2026-11-23 19:00:04 +08:00
|
|
|
t: ((key: string) => key) as TFunction,
|
2026-10-01 00:12:03 +08:00
|
|
|
};
|
|
|
|
|
return {
|
2026-11-23 19:00:04 +08:00
|
|
|
single: (item: AuthFileItem) => refreshAuthFile(deps, pending, item),
|
|
|
|
|
all: () => refreshAllAuthFiles(deps, pending),
|
2026-10-01 00:12:03 +08:00
|
|
|
confirm: () => confirmation?.(),
|
|
|
|
|
switchConnection: () => {
|
|
|
|
|
revision++;
|
|
|
|
|
},
|
|
|
|
|
resolve: (value: unknown) => resolve(value),
|
|
|
|
|
reject: (error: Error) => reject(error),
|
|
|
|
|
calls,
|
|
|
|
|
get results() {
|
|
|
|
|
return results;
|
|
|
|
|
},
|
|
|
|
|
get loads() {
|
|
|
|
|
return loads;
|
|
|
|
|
},
|
|
|
|
|
get notifications() {
|
|
|
|
|
return notifications;
|
|
|
|
|
},
|
|
|
|
|
get invalidations() {
|
|
|
|
|
return invalidations;
|
|
|
|
|
},
|
2026-11-23 19:00:04 +08:00
|
|
|
get mutations() {
|
|
|
|
|
return mutations;
|
|
|
|
|
},
|
|
|
|
|
get refreshing() {
|
|
|
|
|
return refreshing;
|
|
|
|
|
},
|
2026-10-01 00:12:03 +08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-23 19:00:04 +08:00
|
|
|
const credential = (authIndex: string): AuthFileItem => ({
|
|
|
|
|
name: 'one.json',
|
|
|
|
|
type: 'codex',
|
|
|
|
|
authIndex,
|
|
|
|
|
});
|
|
|
|
|
|
2026-10-01 00:12:03 +08:00
|
|
|
describe('credential refresh action lifecycle', () => {
|
|
|
|
|
test('requires confirmation, prevents overlap, and exposes individual results', async () => {
|
|
|
|
|
const h = harness();
|
|
|
|
|
h.all();
|
|
|
|
|
expect(h.calls).toHaveLength(0);
|
|
|
|
|
const pending = h.confirm();
|
|
|
|
|
await h.confirm();
|
2026-11-23 19:00:04 +08:00
|
|
|
await h.single(credential('one'));
|
2026-10-01 00:12:03 +08:00
|
|
|
expect(h.calls).toHaveLength(1);
|
|
|
|
|
const results = [{ id: 'one.json', success: false, error: 'fixture' }];
|
|
|
|
|
h.resolve(results);
|
|
|
|
|
await pending;
|
|
|
|
|
expect(h.results).toEqual(results);
|
|
|
|
|
expect(h.loads).toBe(1);
|
|
|
|
|
expect(h.invalidations).toBe(1);
|
2026-11-23 19:00:04 +08:00
|
|
|
expect(h.mutations).toBe(1);
|
2026-10-01 00:12:03 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('a failed all-refresh still reloads potentially changed backend state', async () => {
|
|
|
|
|
const h = harness();
|
|
|
|
|
h.all();
|
|
|
|
|
const pending = h.confirm();
|
|
|
|
|
h.reject(new Error('timeout'));
|
|
|
|
|
await pending;
|
|
|
|
|
expect(h.results).toBeNull();
|
|
|
|
|
expect(h.notifications).toBe(1);
|
|
|
|
|
expect(h.loads).toBe(1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('a confirmation from an old connection cannot send a request', async () => {
|
|
|
|
|
const h = harness();
|
|
|
|
|
h.all();
|
|
|
|
|
h.switchConnection();
|
|
|
|
|
await h.confirm();
|
|
|
|
|
expect(h.calls).toHaveLength(0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
for (const all of [true, false]) {
|
|
|
|
|
test(`stale ${all ? 'all' : 'single'} refresh cannot publish results or invalidate new caches`, async () => {
|
|
|
|
|
const h = harness();
|
|
|
|
|
if (all) h.all();
|
2026-11-23 19:00:04 +08:00
|
|
|
const pending = all ? h.confirm() : h.single(credential('one'));
|
2026-10-01 00:12:03 +08:00
|
|
|
h.switchConnection();
|
|
|
|
|
h.resolve([{ id: 'one.json', success: true }]);
|
|
|
|
|
await pending;
|
|
|
|
|
expect(h.results).toBeNull();
|
|
|
|
|
expect(h.notifications).toBe(0);
|
|
|
|
|
expect(h.loads).toBe(0);
|
|
|
|
|
expect(h.invalidations).toBe(0);
|
2026-11-23 19:00:04 +08:00
|
|
|
expect(h.mutations).toBe(0);
|
2026-10-01 00:12:03 +08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
test('single refresh forwards the selected identity and blocks a duplicate', async () => {
|
|
|
|
|
const h = harness();
|
2026-11-23 19:00:04 +08:00
|
|
|
const item = credential('two');
|
2026-10-01 00:12:03 +08:00
|
|
|
const pending = h.single(item);
|
|
|
|
|
await h.single(item);
|
|
|
|
|
h.all();
|
|
|
|
|
expect(h.confirm()).toBeUndefined();
|
|
|
|
|
expect(h.calls).toEqual([['one.json', 'two']]);
|
|
|
|
|
h.resolve(undefined);
|
|
|
|
|
await pending;
|
|
|
|
|
expect(h.loads).toBe(1);
|
2026-11-23 19:00:04 +08:00
|
|
|
expect(h.mutations).toBe(1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('a failed single refresh reports the error without reloading', async () => {
|
|
|
|
|
const h = harness();
|
|
|
|
|
const pending = h.single(credential('one'));
|
|
|
|
|
expect(Object.values(h.refreshing)).toEqual([true]);
|
|
|
|
|
h.reject(new Error('expired'));
|
|
|
|
|
await pending;
|
|
|
|
|
expect(h.notifications).toBe(1);
|
|
|
|
|
expect(h.loads).toBe(0);
|
|
|
|
|
expect(h.refreshing).toEqual({});
|
|
|
|
|
// The identity is released, so a retry is sent.
|
|
|
|
|
void h.single(credential('one'));
|
|
|
|
|
expect(h.calls).toHaveLength(2);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test.each([
|
|
|
|
|
['disabled', { ...credential('x'), disabled: true }],
|
|
|
|
|
['unsupported provider', { ...credential('x'), type: 'gemini-cli' }],
|
|
|
|
|
['blank name', { ...credential('x'), name: ' ' }],
|
|
|
|
|
])('ignores a %s credential', async (_, item) => {
|
|
|
|
|
const h = harness();
|
|
|
|
|
await h.single(item);
|
|
|
|
|
expect(h.calls).toEqual([]);
|
2026-10-01 00:12:03 +08:00
|
|
|
});
|
|
|
|
|
});
|