118 lines
4.6 KiB
Go
118 lines
4.6 KiB
Go
package tools
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
)
|
|
|
|
func TestUiaSendMessageConnectorRequiresEnabledModeAndWindowConfig(t *testing.T) {
|
|
connector := NewUiaSendMessageConnector(UiaSendMessageAdapterConfig{Mode: "disabled"}, &fakeUiaHelperCaller{})
|
|
result, err := connector.ExecuteSendMessage(context.Background(), SendMessageConnectorRequest{
|
|
TargetRef: "contact:alice@imopenfire1-lanzhou",
|
|
ContentText: "hello",
|
|
ContentSHA256: sha256HexForSendMessageTest("hello"),
|
|
IdempotencyKeySHA256: sha256HexForSendMessageTest("idem"),
|
|
ExecutionMode: sendMessageProductionMode,
|
|
})
|
|
if err == nil {
|
|
t.Fatalf("expected disabled connector error")
|
|
}
|
|
if result.ErrorCode != "uia_rpa_mode_blocked" || result.ConnectorMode != "uia-rpa-disabled" {
|
|
t.Fatalf("unexpected result: %+v", result)
|
|
}
|
|
|
|
connector = NewUiaSendMessageConnector(UiaSendMessageAdapterConfig{Mode: "enabled"}, &fakeUiaHelperCaller{})
|
|
result, err = connector.ExecuteSendMessage(context.Background(), SendMessageConnectorRequest{
|
|
TargetRef: "contact:alice@imopenfire1-lanzhou",
|
|
ContentText: "hello",
|
|
ContentSHA256: sha256HexForSendMessageTest("hello"),
|
|
IdempotencyKeySHA256: sha256HexForSendMessageTest("idem"),
|
|
ExecutionMode: sendMessageProductionMode,
|
|
})
|
|
if err == nil {
|
|
t.Fatalf("expected missing config error")
|
|
}
|
|
if result.ErrorCode != "uia_rpa_config_missing" {
|
|
t.Fatalf("unexpected missing config result: %+v", result)
|
|
}
|
|
}
|
|
|
|
func TestUiaSendMessageConnectorCallsHelperAndMapsAck(t *testing.T) {
|
|
caller := &fakeUiaHelperCaller{response: uiaHelperResponse{
|
|
OK: true,
|
|
Data: map[string]any{
|
|
"action_mode": "uia_send_message",
|
|
"target_ref": "contact:alice@imopenfire1-lanzhou",
|
|
"content_sha256": sha256HexForSendMessageTest("hello uia"),
|
|
"sent_message": true,
|
|
"typed_text": true,
|
|
"clicked_ui": true,
|
|
"ack_ref": "uia:0x1234:btnSend",
|
|
},
|
|
}}
|
|
connector := NewUiaSendMessageConnector(UiaSendMessageAdapterConfig{
|
|
Mode: "enabled",
|
|
Hwnd: "0x1234",
|
|
SendEditorAutomationID: "rtbSendMessage",
|
|
SendButtonAutomationID: "btnSend",
|
|
}, caller)
|
|
result, err := connector.ExecuteSendMessage(context.Background(), SendMessageConnectorRequest{
|
|
TargetRef: "contact:alice@imopenfire1-lanzhou",
|
|
ContentText: "hello uia",
|
|
ContentSHA256: sha256HexForSendMessageTest("hello uia"),
|
|
IdempotencyKeySHA256: sha256HexForSendMessageTest("idem-uia"),
|
|
ExecutionMode: sendMessageProductionMode,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("ExecuteSendMessage: %v", err)
|
|
}
|
|
if !result.Accepted || result.Status != "accepted" || result.AckRef != "uia:0x1234:btnSend" || result.ConnectorMode != "uia-rpa" || !result.ProductionEnabled {
|
|
t.Fatalf("unexpected result: %+v", result)
|
|
}
|
|
if caller.op != "uia_send_message" {
|
|
t.Fatalf("helper op = %q", caller.op)
|
|
}
|
|
if caller.args["hwnd"] != "0x1234" || caller.args["send_editor_automation_id"] != "rtbSendMessage" || caller.args["send_button_automation_id"] != "btnSend" {
|
|
t.Fatalf("unexpected helper args: %#v", caller.args)
|
|
}
|
|
if caller.args["content_text"] != "hello uia" || caller.args["content_sha256"] != sha256HexForSendMessageTest("hello uia") {
|
|
t.Fatalf("helper did not receive send content/hash: %#v", caller.args)
|
|
}
|
|
}
|
|
|
|
func TestSendMessageConnectorFromEnvBuildsUiaRPAConnector(t *testing.T) {
|
|
t.Setenv(EnvSendConnectorMode, "uia_rpa")
|
|
t.Setenv(EnvSendUIAHwnd, "0x1234")
|
|
t.Setenv(EnvSendUIAEditorAutomationID, "editorA")
|
|
t.Setenv(EnvSendUIButtonAutomationID, "buttonA")
|
|
t.Setenv(EnvSendUIAHelperPath, t.TempDir()+"\\missing-helper.exe")
|
|
|
|
connector := NewSendMessageConnectorFromEnv()
|
|
if connector == nil {
|
|
t.Fatalf("NewSendMessageConnectorFromEnv returned nil for uia_rpa config")
|
|
}
|
|
result, err := connector.ExecuteSendMessage(context.Background(), SendMessageConnectorRequest{
|
|
TargetRef: "contact:alice@imopenfire1-lanzhou",
|
|
ContentText: "hello",
|
|
ContentSHA256: sha256HexForSendMessageTest("hello"),
|
|
IdempotencyKeySHA256: sha256HexForSendMessageTest("idem"),
|
|
ExecutionMode: sendMessageProductionMode,
|
|
})
|
|
if err == nil || result.ErrorCode != "uia_rpa_helper_error" {
|
|
t.Fatalf("expected configured connector to reach helper layer and fail on missing helper, got result=%+v err=%v", result, err)
|
|
}
|
|
}
|
|
|
|
type fakeUiaHelperCaller struct {
|
|
response uiaHelperResponse
|
|
err error
|
|
op string
|
|
args map[string]any
|
|
}
|
|
|
|
func (f *fakeUiaHelperCaller) CallUiaHelper(ctx context.Context, op string, args map[string]any) (uiaHelperResponse, error) {
|
|
f.op = op
|
|
f.args = args
|
|
return f.response, f.err
|
|
}
|