|
1
|
+ |
defmodule OpenAgentsWeb.IssueLiveWorkTest do
|
|
2
|
+ |
@moduledoc """
|
|
3
|
+ |
Stage 3 of `#10`: show a running attempt live, and stop narrating it.
|
|
4
|
+ |
|
|
5
|
+ |
The narration went because it was a surface asserting things nothing could
|
|
6
|
+ |
contradict. Three Markdown comments restated `forge_assignments` in prose,
|
|
7
|
+ |
one of them — "claim released" — describing a credential revocation in a
|
|
8
|
+ |
different table, for two of the three terminal states, while `finish/1`
|
|
9
|
+ |
revokes for all three. Nothing compared the sentence to the row.
|
|
10
|
+ |
|
|
11
|
+ |
What replaces it is the row. These tests hold the three properties that makes
|
|
12
|
+ |
possible: the page moves when the attempt moves, the announcement that moves
|
|
13
|
+ |
it carries an id and nothing else, and the re-read it triggers goes through
|
|
14
|
+ |
the viewer's own authorization rather than through whatever the socket last
|
|
15
|
+ |
believed.
|
|
16
|
+ |
"""
|
|
17
|
+ |
use OpenAgentsWeb.ConnCase, async: false
|
|
18
|
+ |
|
|
19
|
+ |
import Ecto.Query
|
|
20
|
+ |
import Phoenix.LiveViewTest
|
|
21
|
+ |
|
|
22
|
+ |
alias OpenAgents.Forge.{Assignment, Assignments}
|
|
23
|
+ |
alias OpenAgents.Issues
|
|
24
|
+ |
alias OpenAgents.Repo
|
|
25
|
+ |
alias OpenAgents.Repositories
|
|
26
|
+ |
|
|
27
|
+ |
@sha String.duplicate("ab", 20)
|
|
28
|
+ |
|
|
29
|
+ |
setup %{conn: conn} do
|
|
30
|
+ |
repository = Repositories.get_by_path!("OpenAgentsInc", "openagents.com")
|
|
31
|
+ |
member = github_user("issue-live-work-member")
|
|
32
|
+ |
{:ok, _} = Repositories.add_member(repository, member, "maintainer")
|
|
33
|
+ |
|
|
34
|
+ |
{:ok, issue} = Issues.create_issue(repository, %{title: "Watch the work"})
|
|
35
|
+ |
|
|
36
|
+ |
%{
|
|
37
|
+ |
conn: Plug.Test.init_test_session(conn, %{"user_id" => member.id}),
|
|
38
|
+ |
member: member,
|
|
39
|
+ |
repository: repository,
|
|
40
|
+ |
issue: issue
|
|
41
|
+ |
}
|
|
42
|
+ |
end
|
|
43
|
+ |
|
|
44
|
+ |
defp path(issue), do: ~p"/OpenAgentsInc/openagents.com/issues/#{issue.number}"
|
|
45
|
+ |
|
|
46
|
+ |
describe "the announcement" do
|
|
47
|
+ |
test "carries the issue id and nothing else", context do
|
|
48
|
+ |
:ok = Assignments.subscribe_attempts(context.issue.id)
|
|
49
|
+ |
attempt = attempt(context)
|
|
50
|
+ |
|
|
51
|
+ |
Assignments.announce(attempt)
|
|
52
|
+ |
|
|
53
|
+ |
issue_id = context.issue.id
|
|
54
|
+ |
assert_receive {:attempts_changed, ^issue_id}
|
|
55
|
+ |
|
|
56
|
+ |
# Nothing else arrives, so nothing carried the branch, the state, the
|
|
57
|
+ |
# revision, or the row. A subscriber that wants any of those must re-read
|
|
58
|
+ |
# for them, which is the only place a gate can run.
|
|
59
|
+ |
refute_receive {:attempts_changed, _issue_id, _anything}
|
|
60
|
+ |
refute_receive %Assignment{}
|
|
61
|
+ |
end
|
|
62
|
+ |
|
|
63
|
+ |
test "reaches only the issue it names", context do
|
|
64
|
+ |
{:ok, other} = Issues.create_issue(context.repository, %{title: "Elsewhere"})
|
|
65
|
+ |
:ok = Assignments.subscribe_attempts(other.id)
|
|
66
|
+ |
|
|
67
|
+ |
Assignments.announce(attempt(context))
|
|
68
|
+ |
|
|
69
|
+ |
refute_receive {:attempts_changed, _issue_id}
|
|
70
|
+ |
end
|
|
71
|
+ |
end
|
|
72
|
+ |
|
|
73
|
+ |
describe "a running attempt on the page" do
|
|
74
|
+ |
test "renders its state and how long it has been running", context do
|
|
75
|
+ |
attempt(context, started_at: DateTime.add(DateTime.utc_now(), -90, :second))
|
|
76
|
+ |
|
|
77
|
+ |
{:ok, view, _html} = live(context.conn, path(context.issue))
|
|
78
|
+ |
|
|
79
|
+ |
assert has_element?(view, "#issue-work-live")
|
|
80
|
+ |
assert render(view) =~ "running"
|
|
81
|
+ |
assert has_element?(view, "#issue-work-elapsed")
|
|
82
|
+ |
assert render(view) =~ "1m 3"
|
|
83
|
+ |
end
|
|
84
|
+ |
|
|
85
|
+ |
test "lands its terminal event without a reload", context do
|
|
86
|
+ |
attempt = attempt(context)
|
|
87
|
+ |
|
|
88
|
+ |
{:ok, view, _html} = live(context.conn, path(context.issue))
|
|
89
|
+ |
|
|
90
|
+ |
assert has_element?(view, "#issue-work-live")
|
|
91
|
+ |
refute render(view) =~ "finished this work"
|
|
92
|
+ |
|
|
93
|
+ |
# The production terminal path, which announces on the topic the page
|
|
94
|
+ |
# subscribed to at mount. Nothing here touches the view.
|
|
95
|
+ |
{:ok, _finished} = Assignments.finish(attempt, "completed", @sha)
|
|
96
|
+ |
|
|
97
|
+ |
html = render(view)
|
|
98
|
+ |
assert html =~ "finished this work at #{String.slice(@sha, 0, 7)}"
|
|
99
|
+ |
refute has_element?(view, "#issue-work-live")
|
|
100
|
+ |
end
|
|
101
|
+ |
|
|
102
|
+ |
test "an attempt that started and never finished renders as started only", context do
|
|
103
|
+ |
attempt(context)
|
|
104
|
+ |
|
|
105
|
+ |
{:ok, view, _html} = live(context.conn, path(context.issue))
|
|
106
|
+ |
|
|
107
|
+ |
html = render(view)
|
|
108
|
+ |
assert html =~ "started work on a box"
|
|
109
|
+ |
refute html =~ "finished this work"
|
|
110
|
+ |
refute html =~ "stopped this work"
|
|
111
|
+ |
refute html =~ "cancelled this work"
|
|
112
|
+ |
end
|
|
113
|
+ |
end
|
|
114
|
+ |
|
|
115
|
+ |
describe "cancelling" do
|
|
116
|
+ |
test "a viewer with write authority ends the attempt through the terminal path",
|
|
117
|
+ |
context do
|
|
118
|
+ |
attempt = attempt(context)
|
|
119
|
+ |
|
|
120
|
+ |
{:ok, view, _html} = live(context.conn, path(context.issue))
|
|
121
|
+ |
|
|
122
|
+ |
assert has_element?(view, "#issue-work-cancel")
|
|
123
|
+ |
render_click(view, "cancel_work", %{})
|
|
124
|
+ |
|
|
125
|
+ |
cancelled = Repo.get!(Assignment, attempt.id)
|
|
126
|
+ |
assert cancelled.state == "cancelled"
|
|
127
|
+ |
assert cancelled.failure_reason == "cancelled_by_viewer"
|
|
128
|
+ |
assert cancelled.finished_at
|
|
129
|
+ |
|
|
130
|
+ |
refute has_element?(view, "#issue-work-live")
|
|
131
|
+ |
assert render(view) =~ "cancelled this work"
|
|
132
|
+ |
end
|
|
133
|
+ |
|
|
134
|
+ |
test "a reader without write authority is offered no control and refused the event",
|
|
135
|
+ |
context do
|
|
136
|
+ |
attempt = attempt(context)
|
|
137
|
+ |
|
|
138
|
+ |
{:ok, view, _html} = live(build_conn(), path(context.issue))
|
|
139
|
+ |
|
|
140
|
+ |
refute has_element?(view, "#issue-work-cancel")
|
|
141
|
+ |
render_click(view, "cancel_work", %{})
|
|
142
|
+ |
|
|
143
|
+ |
assert Repo.get!(Assignment, attempt.id).state == "running"
|
|
144
|
+ |
end
|
|
145
|
+ |
|
|
146
|
+ |
test "is refused for an attempt that already finished", context do
|
|
147
|
+ |
attempt = attempt(context)
|
|
148
|
+ |
{:ok, _} = Assignments.finish(attempt, "completed", @sha)
|
|
149
|
+ |
|
|
150
|
+ |
assert {:error, :assignment_not_live} = Assignments.cancel(attempt.id, context.member)
|
|
151
|
+ |
end
|
|
152
|
+ |
|
|
153
|
+ |
test "reads its authority from the attempt's own repository", context do
|
|
154
|
+ |
attempt = attempt(context)
|
|
155
|
+ |
stranger = github_user("issue-live-work-stranger")
|
|
156
|
+ |
|
|
157
|
+ |
assert {:error, :repository_not_writable} = Assignments.cancel(attempt.id, stranger)
|
|
158
|
+ |
assert {:error, :repository_not_writable} = Assignments.cancel(attempt.id, nil)
|
|
159
|
+ |
assert Repo.get!(Assignment, attempt.id).state == "running"
|
|
160
|
+ |
end
|
|
161
|
+ |
end
|
|
162
|
+ |
|
|
163
|
+ |
describe "the re-read the announcement triggers" do
|
|
164
|
+ |
test "runs through this viewer's own authority, not the one it mounted with",
|
|
165
|
+ |
context do
|
|
166
|
+ |
attempt(context)
|
|
167
|
+ |
|
|
168
|
+ |
{:ok, view, _html} = live(context.conn, path(context.issue))
|
|
169
|
+ |
|
|
170
|
+ |
# A member reads the attempt at `ledger`, so the branch is on the page.
|
|
171
|
+ |
assert render(view) =~ "agent/watched"
|
|
172
|
+ |
|
|
173
|
+ |
# Authority changes underneath a mounted socket. Nothing tells the view.
|
|
174
|
+ |
Repo.delete_all(
|
|
175
|
+ |
from membership in OpenAgents.Repositories.Membership,
|
|
176
|
+ |
where:
|
|
177
|
+ |
membership.repository_id == ^context.repository.id and
|
|
178
|
+ |
membership.user_id == ^context.member.id
|
|
179
|
+ |
)
|
|
180
|
+ |
|
|
181
|
+ |
Assignments.announce(Repo.get!(Assignment, attempt_id(context)))
|
|
182
|
+ |
|
|
183
|
+ |
# The re-read went through `Repositories.get_visible_repository/2` and
|
|
184
|
+ |
# `WorkDisclosure.viewer/2` again, so the reader dropped to `pulse` and
|
|
185
|
+ |
# the branch left the page. A message that carried the attempt would have
|
|
186
|
+ |
# put it back.
|
|
187
|
+ |
html = render(view)
|
|
188
|
+ |
refute html =~ "agent/watched"
|
|
189
|
+ |
refute has_element?(view, "#issue-work-cancel")
|
|
190
|
+ |
end
|
|
191
|
+ |
end
|
|
192
|
+ |
|
|
193
|
+ |
defp attempt_id(context) do
|
|
194
|
+ |
Repo.one!(from a in Assignment, where: a.issue_id == ^context.issue.id, select: a.id)
|
|
195
|
+ |
end
|
|
196
|
+ |
|
|
197
|
+ |
defp attempt(context, opts \\ []) do
|
|
198
|
+ |
now = DateTime.utc_now() |> DateTime.truncate(:microsecond)
|
|
199
|
+ |
started_at = Keyword.get(opts, :started_at, now) |> DateTime.truncate(:microsecond)
|
|
200
|
+ |
|
|
201
|
+ |
%Assignment{}
|
|
202
|
+ |
|> Assignment.changeset(%{
|
|
203
|
+ |
target_kind: "box",
|
|
204
|
+ |
conversation_box_id: box(context).id,
|
|
205
|
+ |
repository_id: context.repository.id,
|
|
206
|
+ |
issue_id: context.issue.id,
|
|
207
|
+ |
requesting_principal: %{
|
|
208
|
+ |
"type" => "user",
|
|
209
|
+ |
"id" => context.member.id,
|
|
210
|
+ |
"actor_type" => "user",
|
|
211
|
+ |
"actor_id" => context.member.id
|
|
212
|
+ |
},
|
|
213
|
+ |
branch: "agent/watched",
|
|
214
|
+ |
state: "running",
|
|
215
|
+ |
admitted_at: started_at,
|
|
216
|
+ |
started_at: started_at,
|
|
217
|
+ |
deadline_at: DateTime.add(now, 3_600, :second)
|
|
218
|
+ |
})
|
|
219
|
+ |
|> Repo.insert!()
|
|
220
|
+ |
end
|
|
221
|
+ |
|
|
222
|
+ |
defp box(context) do
|
|
223
|
+ |
{:ok, conversation} = OpenAgents.Conversations.ensure_conversation(context.member)
|
|
224
|
+ |
|
|
225
|
+ |
{:ok, box} =
|
|
226
|
+ |
%OpenAgents.Box.ConversationBox{}
|
|
227
|
+ |
|> OpenAgents.Box.ConversationBox.changeset(%{
|
|
228
|
+ |
conversation_id: conversation.id,
|
|
229
|
+ |
box_id: "bx_live_work_#{System.unique_integer([:positive])}",
|
|
230
|
+ |
state: "ready",
|
|
231
|
+ |
setup_status: "done"
|
|
232
|
+ |
})
|
|
233
|
+ |
|> Repo.insert()
|
|
234
|
+ |
|
|
235
|
+ |
box
|
|
236
|
+ |
end
|
|
237
|
+ |
end
|