3.5 KiB · text History 1619312
defmodule Vigil.SourcemapFixtures do
  @moduledoc """
  Artifact bundles straight into the database and `Vigil.Storage`, for
  symbolication tests, and the fixtures under `test/support/fixtures/sourcemaps/`,
  generated once and committed (tests never run the tools):

    * `dist/`: `esbuild src/app.ts --bundle --minify --sourcemap
      --sources-content=true --format=iife --outfile=dist/app.min.js`
      (esbuild 0.28);
    * `injected/`: `dist/` after `sentry-cli sourcemaps inject` (2.58.2),
      debug id `ce611417-f4a3-5bf0-b5e9-1b5924613d97`;
    * `artifact_bundle.zip`: what `sentry-cli sourcemaps upload --org o
      --project p --release web@1.0.0 --dist d1 --url-prefix '~/static/js'
      injected` sent, captured from its chunk upload.

  `startApp("{}")` throws; node's frames, innermost first, are
  `app.min.js:1:77` (t), `1:182` (n), `1:217` (o) in `dist/`, and line 3
  in `injected/` (the debug id snippet comes first).
  """

  alias Vigil.Artifacts.{ArtifactBundle, ArtifactFile}
  alias Vigil.Repo

  @dir Path.expand("sourcemaps", __DIR__)

  def sourcemap_fixture(path), do: File.read!(Path.join(@dir, path))

  @doc """
  A file of `artifact_bundle.zip` by its url's path (`static/js/app.min.js`).
  sentry-cli rewrites maps as it bundles them, so they differ from `injected/`.
  """
  def bundled_fixture(path) do
    {:ok, %{files: files}} = Vigil.Sourcemap.read_bundle(sourcemap_fixture("artifact_bundle.zip"))
    Map.fetch!(files, "files/_/_/" <> path)
  end

  @doc """
  A bundle of `files` (`%{kind:, url:, contents:}`, optionally `debug_id:`,
  `sourcemap:`, `headers:`) for the project, with `attrs` (`release:`,
  `dist:`, `inserted_at:`).
  """
  def artifact_bundle_fixture(org_id, project_id, attrs, files) do
    bundle =
      Repo.insert!(%ArtifactBundle{
        organization_id: org_id,
        checksum: :crypto.strong_rand_bytes(20) |> Base.encode16(case: :lower),
        bundle_id: attrs[:bundle_id],
        release: attrs[:release],
        dist: attrs[:dist],
        artifact_count: length(files),
        inserted_at: attrs[:inserted_at]
      })

    Repo.insert_all("artifact_bundle_projects", [
      %{
        artifact_bundle_id: Ecto.UUID.dump!(bundle.id),
        project_id: Ecto.UUID.dump!(project_id)
      }
    ])

    for file <- files do
      key = "artifacts/#{org_id}/#{bundle.id}/#{Vigil.Schema.uuid7()}"
      :ok = Vigil.Storage.put(key, file.contents)

      Repo.insert!(%ArtifactFile{
        artifact_bundle_id: bundle.id,
        organization_id: org_id,
        kind: file.kind,
        url: file.url,
        debug_id: file[:debug_id],
        sourcemap: file[:sourcemap],
        headers: file[:headers] || %{},
        size: byte_size(file.contents),
        storage_key: key
      })
    end

    bundle
  end

  @doc "The sentry-cli bundle zip at `path`, unpacked as assemble would."
  def artifact_bundle_from_zip(org_id, project_id, path \\ "artifact_bundle.zip") do
    {:ok, %{manifest: manifest, files: contents}} =
      Vigil.Sourcemap.read_bundle(sourcemap_fixture(path))

    files =
      for {zip_path, info} <- manifest["files"] do
        headers = info["headers"] || %{}

        %{
          kind: info["type"],
          url: info["url"],
          debug_id: headers["debug-id"],
          sourcemap: headers["sourcemap"],
          headers: headers,
          contents: Map.fetch!(contents, zip_path)
        }
      end

    artifact_bundle_fixture(
      org_id,
      project_id,
      %{release: manifest["release"], dist: manifest["dist"], bundle_id: manifest["debug_id"]},
      files
    )
  end
end