-
Notifications
You must be signed in to change notification settings - Fork 438
/
openai_spec.rb
136 lines (110 loc) · 3.69 KB
/
openai_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
require "rails_helper"
include ActiveJob::TestHelper
RSpec.describe Api::V1::Hooks::ProviderController, type: :controller do
let!(:app) do
FactoryBot.create(:app)
end
let!(:user) do
app.add_user(email: "test@test.cl")
end
let!(:agent_role) do
app.add_agent({ email: "test2@test.cl" })
end
let(:app_package) do
AppPackage.find_by(name: "OpenAi")
end
let(:conversation) do
app.start_conversation(
message: { html_content: "This is a test" },
from: user
)
end
describe "triggers" do
before do
AppPackagesCatalog.update_all
end
before :each do
ActiveJob::Base.queue_adapter = :test
ActiveJob::Base.queue_adapter.perform_enqueued_at_jobs = true
@pkg = app.app_package_integrations.create(
api_secret: "sk-xxx",
app_package: app_package
)
end
it "triggers on conversation first user message" do
AppUser.any_instance
.stub(:last_visited_at)
.and_return(Time.zone.now)
AppUser.any_instance
.stub(:last_visited_at)
.and_return(Time.zone.now)
AppUser.any_instance
.stub(:last_visited_at)
.and_return(Time.zone.now)
ConversationPartContent.any_instance.stub(:serialized_content)
.and_return(
'{"blocks": [{"key":"bl82q","text":"foobar","type":"unstyled","depth":0,"inlineStyleRanges":[],"entityRanges":[],"data":{}}],"entityMap":{}}'
)
perform_enqueued_jobs do
conversation
end
end
end
describe "hooks" do
before do
AppPackagesCatalog.update_all
end
before :each do
ActiveJob::Base.queue_adapter = :test
ActiveJob::Base.queue_adapter.perform_enqueued_at_jobs = false
conversation
AppPackageIntegration.any_instance
.stub(:handle_registration)
.and_return({})
# @pkg = app.app_package_integrations.create(
# api_secret: "aaa"
# )
@pkg = app.app_package_integrations.create(
app_package: app_package,
settings: {
main_prompt: "system prompt",
api_secret: "sk-xxx"
}
)
end
describe "single hook" do
it "receive message" do
conversation.conversation_channels.create({
provider: "open_ai",
provider_channel_id: conversation.id,
data: { prompt: "foofof" }
})
channel = conversation.conversation_channels.find_by(provider: "open_ai")
# get(:process_event, params: message_blocks(
# channel: channel.provider_channel_id)
# )
serialized = "{\"blocks\":
[{\"key\":\"bl82q\",\"text\":\"bar\",\"type\":\"unstyled\",\"depth\":0,\"inlineStyleRanges\":[],\"entityRanges\":[],\"data\":{}}],
\"entityMap\":{}}"
allow_any_instance_of(
MessageApis::OpenAi::Api
).to receive(
:get_gpt_response
).and_return(
{ "id" => "xx", "choices" => [{ "message" => { "content" => "bla bla bla" } }] }
)
perform_enqueued_jobs do
message = conversation.add_message(
from: user,
message: {
html_content: "foo",
serialized_content: serialized
}
)
end
expect(conversation.messages.last.authorable).to be_a(Agent)
expect(conversation.messages.last.messageable.html_content).to be == "bla bla bla"
end
end
end
end