Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

test(engine): pin the inbound poll limits to their numbers

Yudhi Armyndharis07c3f499d795
diff --git a/src/engine/adapters/baileys-message-mapper.spec.ts b/src/engine/adapters/baileys-message-mapper.spec.ts
index 3c18fb28a..493247922 100644
--- a/src/engine/adapters/baileys-message-mapper.spec.ts
+++ b/src/engine/adapters/baileys-message-mapper.spec.ts
@@ -1216,6 +1216,27 @@ describe('native poll choice metadata', () => {
expect(extractBaileysBody(content(question, options) as BaileysBodyContent)).toBe(question);
});
+ // The numbers are written out here, not read from the constants, so moving either by one shows up.
+ it('keeps a poll of 32 options with 1024 characters in each text whole', () => {
+ const text = (i: number) => `${i}`.padEnd(1024, 'x');
+ const options = Array.from({ length: 32 }, (_, i) => text(i));
+ expect(extractBaileysPoll(content(text(99), options))).toEqual({
+ name: text(99),
+ options,
+ allowMultipleAnswers: false,
+ });
+ expect(extractBaileysBody(content(text(99), options) as BaileysBodyContent)).toBe(text(99));
+ });
+
+ it('cuts a poll at its 33rd option and at the 1025th character of a text', () => {
+ const text = (i: number) => `${i}`.padEnd(1025, 'x');
+ const options = Array.from({ length: 33 }, (_, i) => text(i));
+ const poll = extractBaileysPoll(content(text(99), options))!;
+ expect(poll.options).toEqual(options.slice(0, 32).map(option => option.slice(0, 1024)));
+ expect(poll.name).toBe(text(99).slice(0, 1024));
+ expect(extractBaileysBody(content(text(99), options) as BaileysBodyContent)).toBe(text(99).slice(0, 1024));
+ });
+
it('truncates an oversized poll, in the poll and in the body, and keeps it a poll', () => {
const long = 'x'.repeat(POLL_TEXT_MAX_LENGTH * 3);
const optionNames = Array.from({ length: POLL_MAX_OPTIONS * 50 }, (_, i) => `${i}${long}`);
diff --git a/src/engine/adapters/message-mapper.spec.ts b/src/engine/adapters/message-mapper.spec.ts
index d54b0c652..52fac8374 100644
--- a/src/engine/adapters/message-mapper.spec.ts
+++ b/src/engine/adapters/message-mapper.spec.ts
@@ -326,6 +326,24 @@ describe('poll choice metadata', () => {
expect(mapped.body).toBe(question);
});
+ // The numbers are written out here, not read from the constants, so moving either by one shows up.
+ it('keeps a poll of 32 options with 1024 characters in each text whole', () => {
+ const text = (i: number) => `${i}`.padEnd(1024, 'x');
+ const pollOptions = Array.from({ length: 32 }, (_, i) => text(i));
+ const mapped = buildIncomingMessageBase(poll({ body: text(99), pollName: text(99), pollOptions }));
+ expect(mapped.poll).toEqual({ name: text(99), options: pollOptions, allowMultipleAnswers: false });
+ expect(mapped.body).toBe(text(99));
+ });
+
+ it('cuts a poll at its 33rd option and at the 1025th character of a text', () => {
+ const text = (i: number) => `${i}`.padEnd(1025, 'x');
+ const pollOptions = Array.from({ length: 33 }, (_, i) => text(i));
+ const mapped = buildIncomingMessageBase(poll({ body: text(99), pollName: text(99), pollOptions }));
+ expect(mapped.poll?.options).toEqual(pollOptions.slice(0, 32).map(option => option.slice(0, 1024)));
+ expect(mapped.poll?.name).toBe(text(99).slice(0, 1024));
+ expect(mapped.body).toBe(text(99).slice(0, 1024));
+ });
+
it('truncates an oversized poll, in the poll and in the body, and keeps it a poll', () => {
const long = 'x'.repeat(POLL_TEXT_MAX_LENGTH * 3);
const pollOptions = Array.from({ length: POLL_MAX_OPTIONS * 50 }, (_, i) => ({ name: `${i}${long}` }));