Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
refactor(whatsapp-bridge): enhance version resolution logic
- Moved the `resolveWaVersion` function to `bridge_helpers.js` for better modularity.
- Updated `startSocket` to utilize the new `resolveWaVersion` function, passing the appropriate fetch functions.
- Added tests for `resolveWaVersion` to ensure correct behavior for live version retrieval and fallback scenarios.
  • Loading branch information
lucaslokchan committed Jul 31, 2026
commit d993dbe412841f49e36942604729cb2e6cf6e52b
14 changes: 5 additions & 9 deletions scripts/whatsapp-bridge/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
mediaPayloadForFile,
pollCreationMessageFromPayload,
pollUpdateForAggregation,
resolveWaVersion,
} from './bridge_helpers.js';

// Parse CLI args
Expand Down Expand Up @@ -393,17 +394,12 @@ function emitPairEvent(event) {
} catch {}
}

async function resolveWaVersion() {
const waWeb = await fetchLatestWaWebVersion();
if (waWeb.isLatest && !waWeb.error) {
return waWeb.version;
}
return (await fetchLatestBaileysVersion()).version;
}

async function startSocket() {
const { state, saveCreds } = await useMultiFileAuthState(SESSION_DIR);
const version = await resolveWaVersion();
const version = await resolveWaVersion({
fetchLiveVersion: fetchLatestWaWebVersion,
fetchBaileysVersion: fetchLatestBaileysVersion,
});

sock = makeWASocket({
version,
Expand Down
32 changes: 32 additions & 0 deletions scripts/whatsapp-bridge/bridge.native.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
mediaPayloadForFile,
pollCreationMessageFromPayload,
pollUpdateForAggregation,
resolveWaVersion,
} from './bridge_helpers.js';

// -- inbound read receipts ------------------------------------------------
Expand Down Expand Up @@ -411,4 +412,35 @@ import {
console.log(' ✓ captioned failed download keeps caption and appends note');
}

// -- resolveWaVersion (live-success and Baileys-fallback) ------------------
{
const liveVersion = [2, 3000, 1015901307];
const version = await resolveWaVersion({
fetchLiveVersion: async () => ({ version: liveVersion, isLatest: true, error: null }),
fetchBaileysVersion: async () => { throw new Error('should not be called'); },
});
assert.deepEqual(version, liveVersion);
console.log(' ✓ resolveWaVersion returns live WhatsApp Web version when isLatest');
}

{
const baileysVersion = [2, 2413, 1];
const version = await resolveWaVersion({
fetchLiveVersion: async () => ({ version: [2, 0, 0], isLatest: false, error: null }),
fetchBaileysVersion: async () => ({ version: baileysVersion }),
});
assert.deepEqual(version, baileysVersion);
console.log(' ✓ resolveWaVersion falls back to Baileys version when isLatest is false');
}

{
const baileysVersion = [2, 2413, 2];
const version = await resolveWaVersion({
fetchLiveVersion: async () => ({ version: null, isLatest: true, error: 'network timeout' }),
fetchBaileysVersion: async () => ({ version: baileysVersion }),
});
assert.deepEqual(version, baileysVersion);
console.log(' ✓ resolveWaVersion falls back to Baileys version when live fetch has error');
}

console.log('\n✅ All WhatsApp native bridge helper tests passed.');
8 changes: 8 additions & 0 deletions scripts/whatsapp-bridge/bridge_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,14 @@ export function mediaPayloadForFile({ buffer, filePath, mediaType, caption, file
}
}

export async function resolveWaVersion({ fetchLiveVersion, fetchBaileysVersion }) {
const waWeb = await fetchLiveVersion();
if (waWeb.isLatest && !waWeb.error) {
return waWeb.version;
}
return (await fetchBaileysVersion()).version;
}

export function buildPollPayload({ question, options, selectableCount = 1 }) {
const cleanQuestion = String(question || '').trim();
const cleanOptions = (options || []).map(option => String(option || '').trim()).filter(Boolean);
Expand Down