Sam Hofius
5 years ago
4 changed files with 143 additions and 3 deletions
@ -0,0 +1,70 @@
@@ -0,0 +1,70 @@
|
||||
package keybase |
||||
|
||||
import ( |
||||
"os/exec" |
||||
"testing" |
||||
|
||||
"samhofi.us/x/keybase/v2/types/chat1" |
||||
) |
||||
|
||||
func TestListConvsOnName(t *testing.T) { |
||||
execCommand = createFakeExecCommand("listconvsonname") |
||||
defer func() { execCommand = exec.Command }() |
||||
|
||||
channel := chat1.ChatChannel{ |
||||
Name: "mkbot", |
||||
MembersType: TEAM, |
||||
} |
||||
k := New() |
||||
res, err := k.ListConvsOnName(channel) |
||||
if err != nil { |
||||
t.Errorf("Expected nil error, got %#v", err) |
||||
} |
||||
|
||||
channelcount := 10 |
||||
if len(*res) != channelcount { |
||||
t.Errorf("Expected %d channels, got %d channels", channelcount, len(*res)) |
||||
} |
||||
} |
||||
|
||||
func TestSendMessageByChannel(t *testing.T) { |
||||
execCommand = createFakeExecCommand("send") |
||||
defer func() { execCommand = exec.Command }() |
||||
|
||||
channel := chat1.ChatChannel{ |
||||
Name: "user1,user2", |
||||
MembersType: USER, |
||||
} |
||||
k := New() |
||||
res, err := k.SendMessageByChannel(channel, "Hello!") |
||||
if err != nil { |
||||
t.Errorf("Expected nil error, got %#v", err) |
||||
} |
||||
|
||||
if expected := "message sent"; res.Message != expected { |
||||
t.Errorf(`res.Message: expected "%s", got "%v"`, expected, res.Message) |
||||
} |
||||
|
||||
if expected := uint(894); uint(*res.MessageID) != expected { |
||||
t.Errorf(`res.MessageID: expected %d, got %d`, expected, uint(*res.MessageID)) |
||||
} |
||||
} |
||||
|
||||
func TestSendMessageByConvID(t *testing.T) { |
||||
execCommand = createFakeExecCommand("send") |
||||
defer func() { execCommand = exec.Command }() |
||||
|
||||
k := New() |
||||
res, err := k.SendMessageByConvID(chat1.ConvIDStr("000049d2395435dff0c865c18832d9645eb69fd74a2814ef55310b294092ba6d"), "Hello!") |
||||
if err != nil { |
||||
t.Errorf("Expected nil error, got %#v", err) |
||||
} |
||||
|
||||
if expected := "message sent"; res.Message != expected { |
||||
t.Errorf(`res.Message: expected "%s", got "%v"`, expected, res.Message) |
||||
} |
||||
|
||||
if expected := uint(894); uint(*res.MessageID) != expected { |
||||
t.Errorf(`res.MessageID: expected %d, got %d`, expected, uint(*res.MessageID)) |
||||
} |
||||
} |
@ -0,0 +1,68 @@
@@ -0,0 +1,68 @@
|
||||
package keybase |
||||
|
||||
import ( |
||||
"fmt" |
||||
"os" |
||||
"os/exec" |
||||
"testing" |
||||
) |
||||
|
||||
func createFakeExecCommand(caller string) func(command string, args ...string) *exec.Cmd { |
||||
return func(command string, args ...string) *exec.Cmd { |
||||
cs := []string{"-test.run=ExecHelper", "--"} |
||||
cs = append(cs, args...) |
||||
cmd := exec.Command(os.Args[0], cs...) |
||||
cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1", "GO_WANT_HELPER_CALLER=" + caller} |
||||
return cmd |
||||
} |
||||
} |
||||
|
||||
func TestNew(t *testing.T) { |
||||
execCommand = createFakeExecCommand("new") |
||||
defer func() { execCommand = exec.Command }() |
||||
|
||||
k := New() |
||||
|
||||
if expected := "keybase"; k.ExePath != expected { |
||||
t.Errorf(`k.ExePath: expected "%s", got "%s"`, expected, k.ExePath) |
||||
} |
||||
if expected := ""; k.HomePath != expected { |
||||
t.Errorf(`k.HomePath: expected "%s", got "%s"`, expected, k.HomePath) |
||||
} |
||||
|
||||
k = New( |
||||
SetExePath("/path/to/exepath"), |
||||
SetHomePath("/path/to/homepath"), |
||||
) |
||||
|
||||
if expected := "/path/to/exepath"; k.ExePath != expected { |
||||
t.Errorf(`k.ExePath: expected "%s", got "%s"`, expected, k.ExePath) |
||||
} |
||||
if expected := "/path/to/homepath"; k.HomePath != expected { |
||||
t.Errorf(`k.HomePath: expected "%s", got "%s"`, expected, k.HomePath) |
||||
} |
||||
} |
||||
|
||||
func TestExecHelper(t *testing.T) { |
||||
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" { |
||||
return |
||||
} |
||||
|
||||
var ( |
||||
listconvsonname = `{"result":{"conversations":[{"id":"0000b9d9be8586029d4876af2192b94d6705603cf6859fc27c746de94befd45c","channel":{"name":"mkbot","members_type":"team","topic_type":"chat"},"is_default_conv":false,"unread":true,"active_at":1589468132,"active_at_ms":1589468132734,"member_status":"active","creator_info":{"ctime":1551848940888,"username":"dxb"}},{"id":"0000d2c4d915aa04c093a25b9496cd885ff510f4eeeacac5a7249f65d82ed0ad","channel":{"name":"mkbot","members_type":"team","topic_type":"chat"},"is_default_conv":false,"unread":false,"active_at":1589633805,"active_at_ms":1589633805970,"member_status":"active","creator_info":{"ctime":1551848919282,"username":"dxb"}},{"id":"0000e967261971be5aae47d1cfd7d77e695d4a2f90e2ee35236ef3472b2884d4","channel":{"name":"mkbot","members_type":"team","topic_type":"chat","topic_name":"gameroom"},"is_default_conv":false,"unread":true,"active_at":1589468113,"active_at_ms":1589468113173,"member_status":"active","creator_info":{"ctime":1566244683161,"username":"dxb"}},{"id":"0000d5ae3da566307f6c9906881e5bd08dc9a0bf8c341b5769240026e367c478","channel":{"name":"mkbot","members_type":"team","topic_type":"chat","topic_name":"general"},"is_default_conv":true,"unread":false,"active_at":1589348381,"active_at_ms":1589348381358,"member_status":"active","creator_info":{"ctime":1551840458201,"username":"dxb"}},{"id":"0000d7cf1e6f51d75f9a354c2cb7c3bd30415f184bbb9eba0c57aa50827b7663","channel":{"name":"mkbot","members_type":"team","topic_type":"chat","topic_name":"test1"},"is_default_conv":false,"unread":true,"active_at":1589468203,"active_at_ms":1589468203686,"member_status":"active","creator_info":{"ctime":1551849049656,"username":"dxb"}},{"id":"0000d0cf70804671490e7f8f21c207a1ac6a8bc2ee05db804fb4531ab6c06f05","channel":{"name":"mkbot","members_type":"team","topic_type":"chat","topic_name":"test2"},"is_default_conv":false,"unread":true,"active_at":1589468118,"active_at_ms":1589468118254,"member_status":"active","creator_info":{"ctime":1551849050007,"username":"dxb"}},{"id":"000044e620fef1e84b623350faff06ebef7a0cd7e403ba81a1b35d311976b9f6","channel":{"name":"mkbot","members_type":"team","topic_type":"chat","topic_name":"test3"},"is_default_conv":false,"unread":true,"active_at":1589468117,"active_at_ms":1589468117094,"member_status":"active","creator_info":{"ctime":1551849050351,"username":"dxb"}},{"id":"0000a8dd5969f6bb414562278a5abf8f3bd80b39d7cdcf0d3df5045f05fbac77","channel":{"name":"mkbot","members_type":"team","topic_type":"chat","topic_name":"test4"},"is_default_conv":false,"unread":true,"active_at":1589468197,"active_at_ms":1589468197735,"member_status":"active","creator_info":{"ctime":1551849050729,"username":"dxb"}},{"id":"00004380e20bf4d56cf5e80a7435d594e07ebe043da93468c93c9bf0080f9ef5","channel":{"name":"mkbot","members_type":"team","topic_type":"chat","topic_name":"test5"},"is_default_conv":false,"unread":true,"active_at":1589468203,"active_at_ms":1589468203980,"member_status":"active","creator_info":{"ctime":1551849051084,"username":"dxb"}},{"id":"00003bd0aa429c33eee546f20efb76c9e0a9854b0ca18278300bcf6fc4c4fb93","channel":{"name":"mkbot","members_type":"team","topic_type":"chat","topic_name":"trivia"},"is_default_conv":false,"unread":false,"active_at":1589468113,"active_at_ms":1589468113074,"member_status":"active","creator_info":{"ctime":1580428008401,"username":"dxb"}}],"offline":false}}` |
||||
send = `{"result":{"message":"message sent","id":894,"ratelimits":[{"tank":"chat","capacity":9000,"reset":155,"gas":8991}]}}` |
||||
) |
||||
|
||||
var jsonOut string |
||||
switch os.Getenv("GO_WANT_HELPER_CALLER") { |
||||
case "listconvsonname": |
||||
jsonOut = listconvsonname |
||||
case "send": |
||||
jsonOut = send |
||||
default: |
||||
jsonOut = "" |
||||
} |
||||
|
||||
fmt.Fprintf(os.Stdout, jsonOut) |
||||
os.Exit(0) |
||||
} |
Loading…
Reference in new issue