Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
test(baileys): revive BufferJSON the way Baileys 7 does
Yudhi Armyndharis6522dc827114
diff --git a/src/engine/adapters/buffer-json-parity.spec.ts b/src/engine/adapters/buffer-json-parity.spec.tsindex 06238c3e9..104c48ee2 100644--- a/src/engine/adapters/buffer-json-parity.spec.ts+++ b/src/engine/adapters/buffer-json-parity.spec.ts@@ -41,6 +41,17 @@ describe('the BufferJSON stub encodes bytes the way the library does', () => {expect(back.fileEncSha256).toEqual(Buffer.from('deadbeef', 'hex'));});+ it('revives only the shapes the library revives', () => {+ // Measured from the real @whiskeysockets/baileys 7.0.0-rc14 BufferJSON.reviver: a numeric-key+ // object of numbers becomes a Buffer, while array-form data and the old `{ buffer, value }`+ // shape come back untouched.+ const revive = (json: string): unknown => (JSON.parse(json, stub.reviver) as { a: unknown }).a;++ expect(revive('{"a":{"0":1,"1":2}}')).toEqual(Buffer.from([1, 2]));+ expect(revive('{"a":{"type":"Buffer","data":[1,2,3]}}')).toEqual({ type: 'Buffer', data: [1, 2, 3] });+ expect(revive('{"a":{"buffer":true,"value":"AQID"}}')).toEqual({ buffer: true, value: 'AQID' });+ });+// The discriminating check: an identity codec — the thing the old fixture could not tell apart// from a working one — must fail this.it('fails for an identity codec, so the assertion is not vacuous', () => {diff --git a/test/__mocks__/@whiskeysockets/baileys.ts b/test/__mocks__/@whiskeysockets/baileys.tsindex e9edf8537..1996440de 100644--- a/test/__mocks__/@whiskeysockets/baileys.ts+++ b/test/__mocks__/@whiskeysockets/baileys.ts@@ -32,7 +32,7 @@ export const proto = {// (the package is pure ESM; ts-jest runs CJS, so the mock owns the serialisation helpers)type BufferLike = { type: 'Buffer'; data: string | number[] };-type BufferJsonObject = { buffer?: boolean; type?: string; data?: string | number[]; value?: string | number[] };+type BufferJsonObject = { type?: string; data?: unknown };export const BufferJSON = {replacer: (_k: string, value: unknown): unknown => {@@ -47,9 +47,17 @@ export const BufferJSON = {reviver: (_: string, value: unknown): unknown => {if (typeof value === 'object' && value !== null) {const obj = value as BufferJsonObject;- if (obj.buffer === true || obj.type === 'Buffer') {- const val = obj.data ?? obj.value;- return typeof val === 'string' ? Buffer.from(val, 'base64') : Buffer.from(val ?? []);+ if (obj.type === 'Buffer' && typeof obj.data === 'string') {+ return Buffer.from(obj.data, 'base64');+ }+ if (!Array.isArray(value)) {+ const keys = Object.keys(value);+ if (keys.length > 0 && keys.every(k => !isNaN(parseInt(k, 10)))) {+ const values = Object.values(value);+ if (values.every(v => typeof v === 'number')) {+ return Buffer.from(values);+ }+ }}}return value;