|
1
|
+ |
defmodule OpenAgentsWeb.ThreadVisibilityTest do
|
|
2
|
+ |
@moduledoc """
|
|
3
|
+ |
THREAD-002 at the two doors a thread is read through: the API and the web
|
|
4
|
+ |
transcript viewer.
|
|
5
|
+ |
|
|
6
|
+ |
Both doors answer the same three questions the same way — an owner reads
|
|
7
|
+ |
their own thread, a reader admitted by the thread's tier reads it without the
|
|
8
|
+ |
owner's budget, and a stranger at an owner-only thread gets the plain 404
|
|
9
|
+ |
that never confirms the thread exists.
|
|
10
|
+ |
"""
|
|
11
|
+ |
use OpenAgentsWeb.ConnCase, async: false
|
|
12
|
+ |
|
|
13
|
+ |
import Phoenix.LiveViewTest
|
|
14
|
+ |
|
|
15
|
+ |
alias OpenAgents.Threads
|
|
16
|
+ |
|
|
17
|
+ |
defp signed_in(conn, user), do: Plug.Test.init_test_session(conn, %{"user_id" => user.id})
|
|
18
|
+ |
|
|
19
|
+ |
describe "POST /api/v3/threads" do
|
|
20
|
+ |
test "a thread opens owner-only when the caller names no tier", %{conn: conn} do
|
|
21
|
+ |
thread =
|
|
22
|
+ |
conn
|
|
23
|
+ |
|> put_chat_api_token("visibility-default")
|
|
24
|
+ |
|> post(~p"/api/v3/threads", %{"objective" => "Keep it to myself."})
|
|
25
|
+ |
|> json_response(201)
|
|
26
|
+ |
|> Map.fetch!("thread")
|
|
27
|
+ |
|
|
28
|
+ |
assert thread["visibility"] == "dark"
|
|
29
|
+ |
end
|
|
30
|
+ |
|
|
31
|
+ |
test "an explicit tier is accepted and published", %{conn: conn} do
|
|
32
|
+ |
thread =
|
|
33
|
+ |
conn
|
|
34
|
+ |
|> put_chat_api_token("visibility-explicit")
|
|
35
|
+ |
|> post(~p"/api/v3/threads", %{
|
|
36
|
+ |
"objective" => "Share the transcript.",
|
|
37
|
+ |
"visibility" => "ledger"
|
|
38
|
+ |
})
|
|
39
|
+ |
|> json_response(201)
|
|
40
|
+ |
|> Map.fetch!("thread")
|
|
41
|
+ |
|
|
42
|
+ |
assert thread["visibility"] == "ledger"
|
|
43
|
+ |
assert thread["event_count"] == 2
|
|
44
|
+ |
end
|
|
45
|
+ |
|
|
46
|
+ |
test "an unknown tier is refused with its own code, not a bare 422", %{conn: conn} do
|
|
47
|
+ |
body =
|
|
48
|
+ |
conn
|
|
49
|
+ |
|> put_chat_api_token("visibility-unknown")
|
|
50
|
+ |
|> post(~p"/api/v3/threads", %{
|
|
51
|
+ |
"objective" => "Widen me.",
|
|
52
|
+ |
"visibility" => "public"
|
|
53
|
+ |
})
|
|
54
|
+ |
|> assert_api_error(422, nil)
|
|
55
|
+ |
|
|
56
|
+ |
assert body["code"] == "thread_visibility_unsupported"
|
|
57
|
+ |
assert body["message"] =~ "\"public\" is not an admitted thread visibility"
|
|
58
|
+ |
assert body["message"] =~ "Admitted: dark, ledger"
|
|
59
|
+ |
assert body["errors"]["visibility"] != nil
|
|
60
|
+ |
end
|
|
61
|
+ |
|
|
62
|
+ |
test "a tier this surface cannot enforce is refused the same way", %{conn: conn} do
|
|
63
|
+ |
for tier <- ~w(pulse glass) do
|
|
64
|
+ |
body =
|
|
65
|
+ |
conn
|
|
66
|
+ |
|> put_chat_api_token("visibility-unenforceable-" <> tier)
|
|
67
|
+ |
|> post(~p"/api/v3/threads", %{"objective" => "Widen me.", "visibility" => tier})
|
|
68
|
+ |
|> assert_api_error(422, nil)
|
|
69
|
+ |
|
|
70
|
+ |
assert body["code"] == "thread_visibility_unsupported"
|
|
71
|
+ |
end
|
|
72
|
+ |
end
|
|
73
|
+ |
|
|
74
|
+ |
test "a non-string tier is refused as a field error", %{conn: conn} do
|
|
75
|
+ |
body =
|
|
76
|
+ |
conn
|
|
77
|
+ |
|> put_chat_api_token("visibility-nonstring")
|
|
78
|
+ |
|> post(~p"/api/v3/threads", %{"objective" => "Widen me.", "visibility" => 3})
|
|
79
|
+ |
|> assert_api_error(422, nil)
|
|
80
|
+ |
|
|
81
|
+ |
assert body["code"] == "validation_failed"
|
|
82
|
+ |
assert body["errors"]["visibility"] != nil
|
|
83
|
+ |
end
|
|
84
|
+ |
end
|
|
85
|
+ |
|
|
86
|
+ |
describe "GET /api/v3/threads/{thread_id}" do
|
|
87
|
+ |
test "a reader admitted by the tier reads the thread without the owner's grant", %{conn: conn} do
|
|
88
|
+ |
{:ok, thread} =
|
|
89
|
+ |
Threads.open(github_user("api-wide-owner"), "Shared work", visibility: "ledger")
|
|
90
|
+ |
|
|
91
|
+ |
body =
|
|
92
|
+ |
conn
|
|
93
|
+ |
|> put_chat_api_token("api-wide-reader")
|
|
94
|
+ |
|> get(~p"/api/v3/threads/#{thread.id}")
|
|
95
|
+ |
|> json_response(200)
|
|
96
|
+ |
|
|
97
|
+ |
assert body["thread"]["id"] == thread.id
|
|
98
|
+ |
assert body["thread"]["visibility"] == "ledger"
|
|
99
|
+ |
# The tier discloses the transcript. It does not disclose what the
|
|
100
|
+ |
# owner's account is spending.
|
|
101
|
+ |
assert body["grant"] == nil
|
|
102
|
+ |
end
|
|
103
|
+ |
|
|
104
|
+ |
test "the owner still reads their own grant", %{conn: conn} do
|
|
105
|
+ |
conn = put_chat_api_token(conn, "api-owner-grant")
|
|
106
|
+ |
|
|
107
|
+ |
thread =
|
|
108
|
+ |
conn
|
|
109
|
+ |
|> post(~p"/api/v3/threads", %{
|
|
110
|
+ |
"objective" => "Shared work.",
|
|
111
|
+ |
"visibility" => "ledger"
|
|
112
|
+ |
})
|
|
113
|
+ |
|> json_response(201)
|
|
114
|
+ |
|> Map.fetch!("thread")
|
|
115
|
+ |
|
|
116
|
+ |
body = conn |> get(~p"/api/v3/threads/#{thread["id"]}") |> json_response(200)
|
|
117
|
+ |
|
|
118
|
+ |
assert body["grant"]["status"] == "active"
|
|
119
|
+ |
end
|
|
120
|
+ |
|
|
121
|
+ |
test "a stranger at an owner-only thread gets the plain 404", %{conn: conn} do
|
|
122
|
+ |
{:ok, thread} = Threads.open(github_user("api-dark-owner"), "Private work")
|
|
123
|
+ |
|
|
124
|
+ |
assert conn
|
|
125
|
+ |
|> put_chat_api_token("api-dark-stranger")
|
|
126
|
+ |
|> get(~p"/api/v3/threads/#{thread.id}")
|
|
127
|
+ |
|> api_error_code(404) == "not_found"
|
|
128
|
+ |
end
|
|
129
|
+ |
|
|
130
|
+ |
test "the transcript follows the same tier as the thread", %{conn: conn} do
|
|
131
|
+ |
owner = github_user("api-events-owner")
|
|
132
|
+ |
{:ok, wide} = Threads.open(owner, "Shared work", visibility: "ledger")
|
|
133
|
+ |
{:ok, _wide} = Threads.record_event(wide, "tool.ran", %{"tool" => "bash"})
|
|
134
|
+ |
{:ok, dark} = Threads.open(owner, "Private work")
|
|
135
|
+ |
|
|
136
|
+ |
reader = put_chat_api_token(conn, "api-events-reader")
|
|
137
|
+ |
|
|
138
|
+ |
body = reader |> get(~p"/api/v3/threads/#{wide.id}/events") |> json_response(200)
|
|
139
|
+ |
assert Enum.any?(body["events"], &(&1["event_type"] == "tool.ran"))
|
|
140
|
+ |
|
|
141
|
+ |
assert reader |> get(~p"/api/v3/threads/#{dark.id}/events") |> api_error_code(404) ==
|
|
142
|
+ |
"not_found"
|
|
143
|
+ |
end
|
|
144
|
+ |
|
|
145
|
+ |
test "a wider tier widens reads only: a reader cannot write, cancel, or re-mint", %{
|
|
146
|
+ |
conn: conn
|
|
147
|
+ |
} do
|
|
148
|
+ |
{:ok, thread} =
|
|
149
|
+ |
Threads.open(github_user("api-fence-owner"), "Shared work", visibility: "ledger")
|
|
150
|
+ |
|
|
151
|
+ |
reader = put_chat_api_token(conn, "api-fence-reader")
|
|
152
|
+ |
|
|
153
|
+ |
assert reader
|
|
154
|
+ |
|> post(~p"/api/v3/threads/#{thread.id}/events", %{
|
|
155
|
+ |
"event_type" => "turn.user",
|
|
156
|
+ |
"payload" => %{"text" => "not yours"}
|
|
157
|
+ |
})
|
|
158
|
+ |
|> api_error_code(404) == "not_found"
|
|
159
|
+ |
|
|
160
|
+ |
assert reader
|
|
161
|
+ |
|> post(~p"/api/v3/threads/#{thread.id}/grants", %{})
|
|
162
|
+ |
|> api_error_code(404) == "not_found"
|
|
163
|
+ |
|
|
164
|
+ |
assert reader |> delete(~p"/api/v3/threads/#{thread.id}") |> api_error_code(404) ==
|
|
165
|
+ |
"not_found"
|
|
166
|
+ |
|
|
167
|
+ |
assert Threads.get_for_user(github_user("api-fence-owner"), thread.id).status == "open"
|
|
168
|
+ |
end
|
|
169
|
+ |
end
|
|
170
|
+ |
|
|
171
|
+ |
describe "/threads/:id" do
|
|
172
|
+ |
test "the owner sees the tier and their own budget", %{conn: conn} do
|
|
173
|
+ |
owner = github_user("live-owner")
|
|
174
|
+ |
{:ok, thread} = Threads.open(owner, "Private work")
|
|
175
|
+ |
{:ok, _thread, _grant, _token} = Threads.mint_grant(thread)
|
|
176
|
+ |
|
|
177
|
+ |
{:ok, view, _html} = live(signed_in(conn, owner), ~p"/threads/#{thread.id}")
|
|
178
|
+ |
|
|
179
|
+ |
assert view |> element("#thread-visibility") |> render() =~ "dark"
|
|
180
|
+ |
assert has_element?(view, "#thread-budget")
|
|
181
|
+ |
end
|
|
182
|
+ |
|
|
183
|
+ |
test "a reader admitted by the tier reads the transcript without the budget", %{conn: conn} do
|
|
184
|
+ |
owner = github_user("live-wide-owner")
|
|
185
|
+ |
{:ok, thread} = Threads.open(owner, "Shared work", visibility: "ledger")
|
|
186
|
+ |
{:ok, thread} = Threads.record_event(thread, "turn.user", %{"text" => "Fix the parser"})
|
|
187
|
+ |
{:ok, _thread, _grant, _token} = Threads.mint_grant(thread)
|
|
188
|
+ |
|
|
189
|
+ |
reader = github_user("live-wide-reader")
|
|
190
|
+ |
{:ok, view, _html} = live(signed_in(conn, reader), ~p"/threads/#{thread.id}")
|
|
191
|
+ |
|
|
192
|
+ |
assert render(view) =~ "Fix the parser"
|
|
193
|
+ |
assert view |> element("#thread-visibility") |> render() =~ "ledger"
|
|
194
|
+ |
refute has_element?(view, "#thread-budget")
|
|
195
|
+ |
end
|
|
196
|
+ |
|
|
197
|
+ |
test "a stranger at an owner-only thread gets the same 404 as a missing one", %{conn: conn} do
|
|
198
|
+ |
{:ok, thread} = Threads.open(github_user("live-dark-owner"), "Private work")
|
|
199
|
+ |
stranger = signed_in(conn, github_user("live-dark-stranger"))
|
|
200
|
+ |
|
|
201
|
+ |
assert_raise OpenAgentsWeb.PublicNotFoundError, fn ->
|
|
202
|
+ |
live(stranger, ~p"/threads/#{thread.id}")
|
|
203
|
+ |
end
|
|
204
|
+ |
|
|
205
|
+ |
assert_raise OpenAgentsWeb.PublicNotFoundError, fn ->
|
|
206
|
+ |
live(stranger, ~p"/threads/#{Ecto.UUID.generate()}")
|
|
207
|
+ |
end
|
|
208
|
+ |
end
|
|
209
|
+ |
end
|
|
210
|
+ |
end
|