Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(sdk): correct the gate's stated reach and cover the decode
Yudhi Armyndhariscd234befa999
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex fa7533a5d..e4039b803 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -13,7 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Fixed-- The JavaScript, Python, Go and Java SDKs omitted `contact`, `call` and `ephemeralDuration` from their chat-history message type, so a typed client had to cast to read three fields the endpoint returns; all four now mirror the engine payload, and a type-level contract assertion in the JavaScript SDK fails the build if that type and the engine interface drift apart again.+- The JavaScript, Python, Go and Java SDKs omitted `contact`, `call` and `ephemeralDuration` from their chat-history message type, so a typed client had to cast to read three fields the endpoint returns; all four now mirror the engine payload.### Documentationdiff --git a/sdk/go/chat_history_decode_test.go b/sdk/go/chat_history_decode_test.gonew file mode 100644index 000000000..2007abdc8--- /dev/null+++ b/sdk/go/chat_history_decode_test.go@@ -0,0 +1,56 @@+package openwa++import (+ "encoding/json"+ "testing"+)++// The chat-history route returns the engine payload verbatim, so the only thing standing between a+// caller and a silently-empty field is this struct's tags. Nothing else exercises them: the routing+// test asserts the URL and never decodes a body.+func TestChatHistoryMessageDecodesEngineFields(t *testing.T) {+ raw := []byte(`{+ "id":"[email protected]_ABC","from":"[email protected]","to":"[email protected]","chatId":"[email protected]",+ "body":"","type":"call","timestamp":1719312000,"fromMe":false,"isGroup":false,+ "kind":"individual","ephemeralDuration":604800,+ "call":{"video":true,"missed":false},+ "contact":{"pushName":"Budi","number":"628","isMyContact":true,"verifiedLevel":0,"labels":["vip"]}+ }`)++ var m ChatHistoryMessage+ if err := json.Unmarshal(raw, &m); err != nil {+ t.Fatalf("unmarshal: %v", err)+ }+ if m.EphemeralDuration != 604800 {+ t.Errorf("ephemeralDuration = %d, want 604800", m.EphemeralDuration)+ }+ if m.Call == nil || !m.Call.Video || m.Call.Missed {+ t.Errorf("call = %+v, want {Video:true Missed:false}", m.Call)+ }+ if m.Contact == nil || m.Contact.PushName != "Budi" || m.Contact.Number != "628" || !m.Contact.IsMyContact {+ t.Errorf("contact = %+v", m.Contact)+ }+ if len(m.Contact.Labels) != 1 || m.Contact.Labels[0] != "vip" {+ t.Errorf("contact.labels = %v, want [vip]", m.Contact.Labels)+ }+ // The pointer is the point: verifiedLevel 0 is "unverified", not "the server said nothing".+ if m.Contact.VerifiedLevel == nil || *m.Contact.VerifiedLevel != 0 {+ t.Errorf("contact.verifiedLevel = %v, want a pointer to 0", m.Contact.VerifiedLevel)+ }+}++func TestChatHistoryMessageLeavesAbsentFieldsEmpty(t *testing.T) {+ raw := []byte(`{"id":"x","from":"a","to":"b","chatId":"c","body":"","type":"text",+ "timestamp":1,"fromMe":false,"isGroup":false,"kind":"individual"}`)++ var m ChatHistoryMessage+ if err := json.Unmarshal(raw, &m); err != nil {+ t.Fatalf("unmarshal: %v", err)+ }+ if m.Call != nil || m.Contact != nil || m.Font != nil {+ t.Errorf("absent optionals should stay nil: call=%v contact=%v font=%v", m.Call, m.Contact, m.Font)+ }+ if m.EphemeralDuration != 0 || m.BackgroundColor != "" {+ t.Errorf("absent scalars should stay zero: ephemeral=%d bg=%q", m.EphemeralDuration, m.BackgroundColor)+ }+}diff --git a/sdk/go/types_message.go b/sdk/go/types_message.goindex 7d962ce62..159db809a 100644--- a/sdk/go/types_message.go+++ b/sdk/go/types_message.go@@ -206,29 +206,31 @@ type MessageLocation struct {// ChatHistoryMessage is a message read live from WhatsApp by Messages.History —// the richer engine payload, differently shaped from MessageRecord.type ChatHistoryMessage struct {- ID string `json:"id"`- From string `json:"from"`- To string `json:"to"`- ChatID string `json:"chatId"`- Body string `json:"body,omitempty"`- Type string `json:"type"`- Timestamp int64 `json:"timestamp"`- FromMe bool `json:"fromMe"`- IsGroup bool `json:"isGroup"`- IsStatusBroadcast bool `json:"isStatusBroadcast"`- Kind string `json:"kind"`- EphemeralDuration int `json:"ephemeralDuration,omitempty"`- Author string `json:"author,omitempty"`- MentionedIDs []string `json:"mentionedIds,omitempty"`- Call *MessageCall `json:"call,omitempty"`- IsLidSender bool `json:"isLidSender,omitempty"`- SenderPhone *string `json:"senderPhone,omitempty"`- Contact *MessageContact `json:"contact,omitempty"`- BackgroundColor string `json:"backgroundColor,omitempty"`- Font int `json:"font,omitempty"`- Media *ChatHistoryMedia `json:"media,omitempty"`- QuotedMessage *QuotedMessage `json:"quotedMessage,omitempty"`- Location *MessageLocation `json:"location,omitempty"`+ ID string `json:"id"`+ From string `json:"from"`+ To string `json:"to"`+ ChatID string `json:"chatId"`+ Body string `json:"body,omitempty"`+ Type string `json:"type"`+ Timestamp int64 `json:"timestamp"`+ FromMe bool `json:"fromMe"`+ IsGroup bool `json:"isGroup"`+ IsStatusBroadcast bool `json:"isStatusBroadcast"`+ Kind string `json:"kind"`+ EphemeralDuration int `json:"ephemeralDuration,omitempty"`+ Author string `json:"author,omitempty"`+ MentionedIDs []string `json:"mentionedIds,omitempty"`+ Call *MessageCall `json:"call,omitempty"`+ IsLidSender bool `json:"isLidSender,omitempty"`+ SenderPhone *string `json:"senderPhone,omitempty"`+ Contact *MessageContact `json:"contact,omitempty"`+ BackgroundColor string `json:"backgroundColor,omitempty"`+ // Pointer because font index 0 is a real style, so a value type could not tell it from absent.+ // Matches StatusRecord.Font, which carries the same wire field.+ Font *int `json:"font,omitempty"`+ Media *ChatHistoryMedia `json:"media,omitempty"`+ QuotedMessage *QuotedMessage `json:"quotedMessage,omitempty"`+ Location *MessageLocation `json:"location,omitempty"`}// MessageCall is the call block on a live history message, present on call messages only.@@ -240,18 +242,19 @@ type MessageCall struct {// MessageContact is the sender contact block on a live history message. History carries PushName// only; the richer fields arrive on message.received when WEBHOOK_CONTACT_DETAILS is enabled.type MessageContact struct {- ID string `json:"id,omitempty"`- Number string `json:"number,omitempty"`- Name string `json:"name,omitempty"`- PushName string `json:"pushName,omitempty"`- ShortName string `json:"shortName,omitempty"`- Type string `json:"type,omitempty"`- IsMyContact bool `json:"isMyContact,omitempty"`- IsWAContact bool `json:"isWAContact,omitempty"`- IsBusiness bool `json:"isBusiness,omitempty"`- IsEnterprise bool `json:"isEnterprise,omitempty"`- VerifiedName string `json:"verifiedName,omitempty"`- VerifiedLevel int `json:"verifiedLevel,omitempty"`+ ID string `json:"id,omitempty"`+ Number string `json:"number,omitempty"`+ Name string `json:"name,omitempty"`+ PushName string `json:"pushName,omitempty"`+ ShortName string `json:"shortName,omitempty"`+ Type string `json:"type,omitempty"`+ IsMyContact bool `json:"isMyContact,omitempty"`+ IsWAContact bool `json:"isWAContact,omitempty"`+ IsBusiness bool `json:"isBusiness,omitempty"`+ IsEnterprise bool `json:"isEnterprise,omitempty"`+ VerifiedName string `json:"verifiedName,omitempty"`+ // Pointer for the same reason as Font: level 0 (unverified) is a real value, not "absent".+ VerifiedLevel *int `json:"verifiedLevel,omitempty"`IsBlocked bool `json:"isBlocked,omitempty"`Labels []string `json:"labels,omitempty"`}diff --git a/sdk/java/src/test/java/com/rmyndharis/openwa/model/ChatHistoryMessageDecodeTest.java b/sdk/java/src/test/java/com/rmyndharis/openwa/model/ChatHistoryMessageDecodeTest.javanew file mode 100644index 000000000..9b5b7e6bd--- /dev/null+++ b/sdk/java/src/test/java/com/rmyndharis/openwa/model/ChatHistoryMessageDecodeTest.java@@ -0,0 +1,59 @@+package com.rmyndharis.openwa.model;++import static org.junit.jupiter.api.Assertions.assertEquals;+import static org.junit.jupiter.api.Assertions.assertFalse;+import static org.junit.jupiter.api.Assertions.assertNotNull;+import static org.junit.jupiter.api.Assertions.assertNull;+import static org.junit.jupiter.api.Assertions.assertTrue;++import com.google.gson.Gson;+import java.util.List;+import org.junit.jupiter.api.Test;++/**+ * The chat-history route returns the engine payload verbatim, so nothing between the wire and a+ * caller checks these record components. MessagesResourceTest responds with {@code []} and asserts+ * the URL only, so without this the deserialization is unexercised.+ */+class ChatHistoryMessageDecodeTest {++ private static final Gson GSON = new Gson();++ @Test+ void decodesTheEnginePayloadFields() {+ String json = "{\"id\":\"[email protected]_ABC\",\"from\":\"[email protected]\",\"to\":\"[email protected]\","+ + "\"chatId\":\"[email protected]\",\"body\":\"\",\"type\":\"call\",\"timestamp\":1719312000,"+ + "\"fromMe\":false,\"isGroup\":false,\"kind\":\"individual\",\"ephemeralDuration\":604800,"+ + "\"call\":{\"video\":true,\"missed\":false},"+ + "\"contact\":{\"pushName\":\"Budi\",\"number\":\"628\",\"isMyContact\":true,"+ + "\"verifiedLevel\":0,\"labels\":[\"vip\"]}}";++ ChatHistoryMessage m = GSON.fromJson(json, ChatHistoryMessage.class);++ assertEquals(604800, m.ephemeralDuration());+ assertNotNull(m.call());+ assertTrue(m.call().video());+ assertFalse(m.call().missed());+ assertNotNull(m.contact());+ assertEquals("Budi", m.contact().pushName());+ assertEquals("628", m.contact().number());+ assertTrue(m.contact().isMyContact());+ assertEquals(List.of("vip"), m.contact().labels());+ // Boxed, so level 0 (unverified) stays distinguishable from "the server said nothing".+ assertEquals(0, m.contact().verifiedLevel());+ }++ @Test+ void leavesAbsentOptionalsNull() {+ String json = "{\"id\":\"x\",\"from\":\"a\",\"to\":\"b\",\"chatId\":\"c\",\"body\":\"\","+ + "\"type\":\"text\",\"timestamp\":1,\"fromMe\":false,\"isGroup\":false,\"kind\":\"individual\"}";++ ChatHistoryMessage m = GSON.fromJson(json, ChatHistoryMessage.class);++ assertNull(m.call());+ assertNull(m.contact());+ assertNull(m.ephemeralDuration());+ assertNull(m.font());+ assertNull(m.backgroundColor());+ }+}diff --git a/sdk/javascript/test/wire-contract.test-d.ts b/sdk/javascript/test/wire-contract.test-d.tsindex 80ba62c9e..07a3290ae 100644--- a/sdk/javascript/test/wire-contract.test-d.ts+++ b/sdk/javascript/test/wire-contract.test-d.ts@@ -76,6 +76,13 @@ interface WireChannelMessage {* that interface serialized. `backgroundColor`/`font` are set by the Baileys extended-text mapper but* are not reachable through this route (Baileys answers `getChatHistory` with `unsupported`); they are* declared for the same forward-compatibility reason as on `WireStatus`.+ *+ * Scope, so nobody reads more into a green build than it carries: `Mirrors` compares TOP-LEVEL keys,+ * so drift inside `contact`, `call`, `media`, `quotedMessage` or `location` compiles clean. And like+ * every `Wire*` type here this one is a hand transcription, so a field added to the server's+ * `IncomingMessage` does not fail this build either — `sdk-ci.yml` re-runs the job when that file+ * changes, which prompts a human to update the transcription. What this pair does lock is the SDK+ * type against the transcription: neither side can lose or gain a top-level key unnoticed.*/interface WireChatHistoryMessage {id: string;