Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
test(engine): make the BufferJSON round-trip test exercise the codec
Yudhi Armyndharis5686df2d98a9
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 4599b4ac2..679e54134 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Fixed+- The Baileys message-store round-trip test now exercises the serialization it names. Its fixture carried no binary field, so the assertions held under any replacer/reviver pair — including identity — and a regression in the BufferJSON round-trip could not fail CI, on rows that reply, forward, react and delete-by-id resolve against. The fixture now carries bytes, a new spec pins the encoded wire form, and both are checked against an identity codec so neither can go vacuous again.+- The Go SDK can now clear a webhook's `secret`, `headers` and `filters`, and a template's `header` and `footer`. Every field carried `omitempty` over a value type, so the values the server reads as "remove this" marshalled away to nothing: the request body came out `{}` and the stored values survived while the call reported success. Secret, headers, header and footer are now pointers, and a new `ClearFilters` flag emits the explicit `null` the server reads — which no struct tag can express for a nil pointer, and which dropping `omitempty` would have sent on every update instead.- The addressbook write guard now checks the id, not just its domain. `parseWaId('NOT A [email protected]').kind` is `'user'`, so free text, letters and an empty user-part cleared the guard, reached the engine and were reported to the caller as a saved contact — for an entry keyed by something that is not a phone number. It now applies `isIndividualWid`, the same predicate the group-participant, channel-admin and message-mention surfaces already use on these very shapes.diff --git a/src/engine/adapters/baileys-message-store.service.spec.ts b/src/engine/adapters/baileys-message-store.service.spec.tsindex 5c360446c..65e5fa167 100644--- a/src/engine/adapters/baileys-message-store.service.spec.ts+++ b/src/engine/adapters/baileys-message-store.service.spec.ts@@ -50,6 +50,10 @@ describe('BaileysMessageStoreService', () => {({key: { id, remoteJid: '[email protected]', fromMe: false },message: { conversation: id },+ // A real WAMessage carries binary fields, and they are the only reason the BufferJSON round-trip+ // exists. With a string-only fixture the assertions below held under any replacer/reviver pair,+ // including identity — the test named the codec without exercising it.+ mediaKey: Buffer.from([0x01, 0x02, 0x03, 0xff]),}) as unknown as Parameters<BaileysMessageStoreService['put']>[1];it('round-trips a WAMessage through BufferJSON', async () => {@@ -58,6 +62,10 @@ describe('BaileysMessageStoreService', () => {const got = await service.getMessage('s1', 'M1');expect(got?.key?.id).toBe('M1');expect(got?.message?.conversation).toBe('M1');+ // The assertion that makes this a round-trip test rather than a string-copy test.+ const mediaKey = (got as unknown as { mediaKey?: unknown }).mediaKey;+ expect(Buffer.isBuffer(mediaKey)).toBe(true);+ expect(mediaKey).toEqual(Buffer.from([0x01, 0x02, 0x03, 0xff]));});it('returns null for an unknown id and is session-scoped', async () => {diff --git a/src/engine/adapters/buffer-json-parity.spec.ts b/src/engine/adapters/buffer-json-parity.spec.tsnew file mode 100644index 000000000..06238c3e9--- /dev/null+++ b/src/engine/adapters/buffer-json-parity.spec.ts@@ -0,0 +1,54 @@+import { BufferJSON as stub } from '@whiskeysockets/baileys';++/**+ * `package.json`'s `jest.moduleNameMapper` redirects the whole `@whiskeysockets/baileys` package to a+ * hand-written stub, so the one test that named the message-store BufferJSON round-trip exercised the+ * stub — and its fixture carried no binary field, so the assertions held under ANY replacer/reviver+ * pair, including identity. A regression in that serialization could not fail CI.+ *+ * Real WAMessages carry binary fields (mediaKey, fileEncSha256, message secrets), and+ * baileys_stored_messages rows are what reply/forward/react/delete-by-id resolve against, so the+ * round-trip has to hold for BYTES, not just strings.+ *+ * The real library cannot be loaded here to compare against: it is ESM and jest cannot parse it,+ * which is why the stub exists at all. The wire form below is therefore pinned as a recorded value,+ * measured from the real `BufferJSON.replacer` outside jest. It cannot catch an upstream change on+ * its own — that is what `npm run check:versions` and a dependency bump review are for — but it does+ * stop the STUB drifting away from the shape the app actually persists.+ */+describe('the BufferJSON stub encodes bytes the way the library does', () => {+ const fixture = () => ({+ key: { id: 'M1', remoteJid: '[email protected]', fromMe: false },+ message: { conversation: 'hello' },+ mediaKey: Buffer.from([0x01, 0x02, 0x03, 0xff]),+ fileEncSha256: Buffer.from('deadbeef', 'hex'),+ });++ it('emits the recorded wire form for a Buffer', () => {+ // Measured from the real @whiskeysockets/baileys BufferJSON.replacer:+ // JSON.stringify({ b: Buffer.from([1,2,3]) }, BufferJSON.replacer)+ // === '{"b":{"type":"Buffer","data":"AQID"}}'+ expect(JSON.stringify({ b: Buffer.from([1, 2, 3]) }, stub.replacer)).toBe('{"b":{"type":"Buffer","data":"AQID"}}');+ });++ it('restores binary fields as Buffers, not as their JSON shape', () => {+ const encoded = JSON.stringify(fixture(), stub.replacer);+ const back = JSON.parse(encoded, stub.reviver) as { mediaKey: unknown; fileEncSha256: unknown };++ expect(Buffer.isBuffer(back.mediaKey)).toBe(true);+ expect(back.mediaKey).toEqual(Buffer.from([0x01, 0x02, 0x03, 0xff]));+ expect(Buffer.isBuffer(back.fileEncSha256)).toBe(true);+ expect(back.fileEncSha256).toEqual(Buffer.from('deadbeef', 'hex'));+ });++ // 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', () => {+ const identity = { replacer: (_k: string, v: unknown) => v, reviver: (_k: string, v: unknown) => v };+ const back = JSON.parse(JSON.stringify(fixture(), identity.replacer), identity.reviver) as {+ mediaKey: unknown;+ };++ expect(Buffer.isBuffer(back.mediaKey)).toBe(false);+ });+});