fix: add error handling and tests for WA Web version fetch
The fetchLatestWaWebVersion call added in #443 could crash the connection flow if the HTTP fetch fails. Wrap with .catch() to log and fall back to the default Baileys version. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -84,6 +84,7 @@ vi.mock('@whiskeysockets/baileys', () => {
|
||||
timedOut: 408,
|
||||
restartRequired: 515,
|
||||
},
|
||||
fetchLatestWaWebVersion: vi.fn().mockResolvedValue({ version: [2, 3000, 0] }),
|
||||
makeCacheableSignalKeyStore: vi.fn((keys: unknown) => keys),
|
||||
useMultiFileAuthState: vi.fn().mockResolvedValue({
|
||||
state: {
|
||||
@@ -159,6 +160,31 @@ describe('WhatsAppChannel', () => {
|
||||
return p;
|
||||
}
|
||||
|
||||
// --- Version fetch ---
|
||||
|
||||
describe('version fetch', () => {
|
||||
it('connects with fetched version', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new WhatsAppChannel(opts);
|
||||
await connectChannel(channel);
|
||||
|
||||
const { fetchLatestWaWebVersion } = await import('@whiskeysockets/baileys');
|
||||
expect(fetchLatestWaWebVersion).toHaveBeenCalledWith({});
|
||||
});
|
||||
|
||||
it('falls back gracefully when version fetch fails', async () => {
|
||||
const { fetchLatestWaWebVersion } = await import('@whiskeysockets/baileys');
|
||||
vi.mocked(fetchLatestWaWebVersion).mockRejectedValueOnce(new Error('network error'));
|
||||
|
||||
const opts = createTestOpts();
|
||||
const channel = new WhatsAppChannel(opts);
|
||||
await connectChannel(channel);
|
||||
|
||||
// Should still connect successfully despite fetch failure
|
||||
expect(channel.isConnected()).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
// --- Connection lifecycle ---
|
||||
|
||||
describe('connection lifecycle', () => {
|
||||
|
||||
@@ -56,7 +56,10 @@ export class WhatsAppChannel implements Channel {
|
||||
|
||||
const { state, saveCreds } = await useMultiFileAuthState(authDir);
|
||||
|
||||
const { version } = await fetchLatestWaWebVersion({});
|
||||
const { version } = await fetchLatestWaWebVersion({}).catch((err) => {
|
||||
logger.warn({ err }, 'Failed to fetch latest WA Web version, using default');
|
||||
return { version: undefined };
|
||||
});
|
||||
this.sock = makeWASocket({
|
||||
version,
|
||||
auth: {
|
||||
|
||||
@@ -54,7 +54,10 @@ async function connectSocket(phoneNumber?: string, isReconnect = false): Promise
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
const { version } = await fetchLatestWaWebVersion({});
|
||||
const { version } = await fetchLatestWaWebVersion({}).catch((err) => {
|
||||
logger.warn({ err }, 'Failed to fetch latest WA Web version, using default');
|
||||
return { version: undefined };
|
||||
});
|
||||
const sock = makeWASocket({
|
||||
version,
|
||||
auth: {
|
||||
|
||||
Reference in New Issue
Block a user