github.com/wit-ai/wit-go/v2@v2.0.2/message_test.go (about)

     1  // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
     2  
     3  package witai
     4  
     5  import (
     6  	"bytes"
     7  	"net/http"
     8  	"net/http/httptest"
     9  	"net/url"
    10  	"reflect"
    11  	"testing"
    12  )
    13  
    14  func TestParse(t *testing.T) {
    15  	testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
    16  		res.Write([]byte(`{
    17  			"msg_id": "msg1",
    18  			"text": "text",
    19  			"intents": [
    20  				{"id": "intent1", "name": "intent1_name", "confidence": 0.9},
    21  				{"id": "intent2", "name": "intent2_name", "confidence": 0.7}
    22  			],
    23  			"entities": {
    24  				"entity1": [{
    25  					"id": "entity1-1",
    26  					"name": "entity1",
    27  					"role": "entity1",
    28  					"start": 1,
    29  					"end": 10,
    30  					"body": "value1",
    31  					"value": "value1",
    32  					"confidence": 0.8
    33  				}]
    34  			},
    35  			"traits": {
    36  				"trait1": [{
    37  					"id": "trait1-1",
    38  					"value": "value1",
    39  					"confidence": 0.8
    40  				}]
    41  			}
    42  		}`))
    43  	}))
    44  	defer func() { testServer.Close() }()
    45  
    46  	c := NewClient("token")
    47  	c.APIBase = testServer.URL
    48  	msg, _ := c.Parse(&MessageRequest{
    49  		Query: "hello",
    50  	})
    51  
    52  	wantMessage := &MessageResponse{
    53  		ID:   "msg1",
    54  		Text: "text",
    55  		Intents: []MessageIntent{
    56  			{ID: "intent1", Name: "intent1_name", Confidence: 0.9},
    57  			{ID: "intent2", Name: "intent2_name", Confidence: 0.7},
    58  		},
    59  		Entities: map[string][]MessageEntity{
    60  			"entity1": {{
    61  				ID:         "entity1-1",
    62  				Name:       "entity1",
    63  				Role:       "entity1",
    64  				Start:      1,
    65  				End:        10,
    66  				Body:       "value1",
    67  				Value:      "value1",
    68  				Confidence: 0.8,
    69  			}},
    70  		},
    71  		Traits: map[string][]MessageTrait{
    72  			"trait1": {{ID: "trait1-1", Value: "value1", Confidence: 0.8}},
    73  		},
    74  	}
    75  
    76  	if !reflect.DeepEqual(msg, wantMessage) {
    77  		t.Fatalf("expected \n\tmsg %v \n\tgot %v", wantMessage, msg)
    78  	}
    79  }
    80  
    81  func TestSpeech(t *testing.T) {
    82  	testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
    83  		res.Write([]byte(`{
    84  			"msg_id": "msg1",
    85  			"text": "text",
    86  			"intents": [
    87  				{"id": "intent1", "name": "intent1_name", "confidence": 0.9},
    88  				{"id": "intent2", "name": "intent2_name", "confidence": 0.7}
    89  			],
    90  			"entities": {
    91  				"entity1": [{
    92  					"id": "entity1-1",
    93  					"name": "entity1",
    94  					"role": "entity1",
    95  					"start": 1,
    96  					"end": 10,
    97  					"body": "value1",
    98  					"value": "value1",
    99  					"confidence": 0.8
   100  				}]
   101  			},
   102  			"traits": {
   103  				"trait1": [{
   104  					"id": "trait1-1",
   105  					"value": "value1",
   106  					"confidence": 0.8
   107  				}]
   108  			}
   109  		}`))
   110  	}))
   111  	defer func() { testServer.Close() }()
   112  
   113  	c := NewClient("token")
   114  	c.APIBase = testServer.URL
   115  	msg, _ := c.Speech(&MessageRequest{
   116  		Speech: &Speech{
   117  			File:        bytes.NewReader([]byte{}),
   118  			ContentType: "audio/raw;encoding=unsigned-integer;bits=16;rate=16k;endian=little",
   119  		},
   120  	})
   121  
   122  	wantMessage := &MessageResponse{
   123  		ID:   "msg1",
   124  		Text: "text",
   125  		Intents: []MessageIntent{
   126  			{ID: "intent1", Name: "intent1_name", Confidence: 0.9},
   127  			{ID: "intent2", Name: "intent2_name", Confidence: 0.7},
   128  		},
   129  		Entities: map[string][]MessageEntity{
   130  			"entity1": {{
   131  				ID:         "entity1-1",
   132  				Name:       "entity1",
   133  				Role:       "entity1",
   134  				Start:      1,
   135  				End:        10,
   136  				Body:       "value1",
   137  				Value:      "value1",
   138  				Confidence: 0.8,
   139  			}},
   140  		},
   141  		Traits: map[string][]MessageTrait{
   142  			"trait1": {{ID: "trait1-1", Value: "value1", Confidence: 0.8}},
   143  		},
   144  	}
   145  
   146  	if !reflect.DeepEqual(msg, wantMessage) {
   147  		t.Fatalf("expected \n\tmsg %v \n\tgot %v", wantMessage, msg)
   148  	}
   149  }
   150  
   151  func Test_buildParseQuery(t *testing.T) {
   152  	want := "?q=" + url.PathEscape("hello world") +
   153  		"&n=1&tag=tag" +
   154  		"&context=" +
   155  		url.PathEscape("{"+
   156  			"\"reference_time\":\"2014-10-30T12:18:45-07:00\","+
   157  			"\"timezone\":\"America/Los_Angeles\","+
   158  			"\"locale\":\"en_US\","+
   159  			"\"coords\":{\"lat\":32.47104,\"long\":-122.14703}"+
   160  			"}")
   161  
   162  	got := buildParseQuery(&MessageRequest{
   163  		Query: "hello world",
   164  		N:     1,
   165  		Tag:   "tag",
   166  		Context: &MessageContext{
   167  			Locale: "en_US",
   168  			Coords: MessageCoords{
   169  				Lat:  32.47104,
   170  				Long: -122.14703,
   171  			},
   172  			Timezone:      "America/Los_Angeles",
   173  			ReferenceTime: "2014-10-30T12:18:45-07:00",
   174  		},
   175  	})
   176  
   177  	if got != want {
   178  		t.Fatalf("expected \n\tquery = %v \n\tgot = %v", want, got)
   179  	}
   180  }