defmodule VigilWeb.AndroidFixturesTest do
@moduledoc """
Writes real `/api/0` responses to `android/app/src/test/resources/api/`,
where the Android app's ContractTest decodes them, so a change of shape
here fails there. Not part of the usual run; when the API changes:
mix test --only android_fixtures
cd android && ./gradlew :app:testDebugUnitTest
"""
use VigilWeb.ConnCase, async: false
import Vigil.AccountsFixtures
import Vigil.OrganizationsFixtures
alias Vigil.{ApiTokens, Monitors, Repo, Uptime}
alias Vigil.Ingest
alias Vigil.Ingest.{Drain, Envelope}
@moduletag :android_fixtures
@dir Path.expand("../../../android/app/src/test/resources/api", __DIR__)
test "writes the responses the app reads" do
owner = user_fixture()
org = organization_fixture(owner, %{name: "Acme", slug: "acme"})
scope = org_scope(owner, org)
project = project_fixture(scope, %{"name" => "Shop", "slug" => "shop"})
{:ok, t} = ApiTokens.create_token(owner, %{label: "Phone", scopes: ApiTokens.mobile_scopes()})
ingest(project, org)
[issue] = Repo.all(Vigil.Issues.Issue)
Monitors.check_in(project, %{
"monitor_slug" => "nightly-backup",
"status" => "error",
"monitor_config" => %{"schedule" => %{"type" => "crontab", "value" => "0 3 * * *"}}
})
{:ok, _} =
Uptime.create_monitor(scope, project, %{
"name" => "Storefront",
"type" => "get",
"url" => "https://shop.example/health"
})
File.mkdir_p!(@dir)
# Triage first, so the reads below show an assigned, ignored issue.
update =
build_conn()
|> put_req_header("authorization", "Bearer " <> t.token)
|> put(~p"/api/0/projects/acme/shop/issues/?id=#{issue.short_id}", %{
status: "ignored",
assignedTo: "user:#{owner.id}"
})
|> json_response(200)
File.write!(Path.join(@dir, "issue_update.json"), Jason.encode!(update, pretty: true) <> "\n")
for {name, path} <- [
root: ~p"/api/0/",
organizations: ~p"/api/0/organizations/",
projects: ~p"/api/0/organizations/acme/projects/",
members: ~p"/api/0/organizations/acme/members/",
issues: ~p"/api/0/organizations/acme/issues/?per_page=1&query=is:ignored",
issue: ~p"/api/0/projects/acme/shop/issues/#{issue.short_id}/",
event: ~p"/api/0/projects/acme/shop/issues/#{issue.short_id}/events/latest/",
monitors: ~p"/api/0/organizations/acme/monitors/",
uptime: ~p"/api/0/organizations/acme/uptime/"
] do
conn = build_conn() |> put_req_header("authorization", "Bearer " <> t.token) |> get(path)
body = json_response(conn, 200)
File.write!(Path.join(@dir, "#{name}.json"), Jason.encode!(body, pretty: true) <> "\n")
if name == :issues do
[link] = get_resp_header(conn, "link")
File.write!(Path.join(@dir, "issues_link.txt"), link <> "\n")
end
end
end
defp ingest(project, org) do
target = %Ingest.Target{
project_id: project.id,
dsn_id: project.dsn_id,
organization_id: org.id,
key_id: Ecto.UUID.generate()
}
items =
for user <- ["u-1", "u-2"] do
%Envelope.Item{
type: "event",
headers: %{"type" => "event"},
payload: Jason.encode!(event(user))
}
end
Ingest.accept(target, %{headers: %{}, items: items}, %{client_ip: "203.0.113.7"})
Drain.drain(project.id)
end
defp event(user) do
%{
"event_id" => Vigil.Schema.uuid7() |> String.replace("-", ""),
"platform" => "python",
"level" => "error",
"environment" => "production",
"release" => "shop@1.4.2",
"tags" => %{"browser" => "Firefox", "region" => "eu-west"},
"user" => %{"id" => user, "email" => "#{user}@example.test"},
"exception" => %{
"values" => [
%{
"type" => "ConnectionError",
"value" => "db went away",
"stacktrace" => %{"frames" => [frame("db/pool.py", "checkout", 88, false)]}
},
%{
"type" => "CheckoutFailed",
"value" => "could not take payment for order 42",
"mechanism" => %{"type" => "excepthook", "handled" => false},
"stacktrace" => %{
"frames" => [
frame("flask/app.py", "dispatch", 1820, false),
frame("shop/views.py", "checkout", 41, true)
]
}
}
]
},
"breadcrumbs" => %{
"values" => [
%{
"timestamp" => "2026-11-30T10:00:00Z",
"category" => "http",
"level" => "info",
"data" => %{"url" => "/cart", "status_code" => 200}
},
%{
"timestamp" => 1_764_496_801.5,
"category" => "query",
"level" => "warning",
"message" => "SELECT * FROM orders WHERE id = 42"
}
]
},
"request" => %{
"url" => "https://shop.example/checkout",
"method" => "POST",
"query_string" => "step=pay",
"headers" => [["User-Agent", "Mozilla/5.0"]],
"data" => %{"order" => 42}
},
"contexts" => %{"runtime" => %{"name" => "CPython", "version" => "3.12.1"}}
}
end
defp frame(file, function, line, in_app) do
%{
"filename" => file,
"function" => function,
"lineno" => line,
"in_app" => in_app,
"pre_context" => ["def #{function}():"],
"context_line" => " raise Boom()",
"post_context" => [""]
}
end
end
neiam
/vigil
public · Issues · Pulls · Labels · Forks · Compare · Actions queued · Badges · Packages
⭐
Log in to mark this repository.
5.5 KiB · text
History
7ad8440