# `Juggernaut.View`
[🔗](https://github.com/holsee/juggernaut/blob/v0.4.0/lib/juggernaut/view.ex#L1)

The output formats of a run (design notes §5, §11) and the pieces they share.

Every view is a pure function of a `Juggernaut.Run` (and, for the live view, of the run's
events) plus options; none reads engine internals, the clock or the filesystem. The
caller supplies what a view cannot know from the run: the configured thresholds, how
timeouts are scored, the source text of the mutated files and the test files that exist.

`render/3` picks a format by its `--format` name:

  * `:human` - `Juggernaut.View.Human`
  * `:agent` - `Juggernaut.View.Agent`
  * `:json` - `Juggernaut.View.MTE` (mutation-testing-elements)
  * `:markdown` - `Juggernaut.View.Markdown`
  * `:github` - `Juggernaut.View.Github`

## Options

* `:thresholds` (`t:keyword/0`) - The configured thresholds (`.juggernaut.exs` `thresholds`). The default value is `[]`.

  * `:core` - Minimum score of core mutants. The default value is `100`.

  * `:changed` - Minimum score elsewhere. The default value is `80`.

  * `:per` - Per file or per run. The default value is `:file`.

* `:timeouts` - How a `timeout` kill is scored; `:survived` lists it with the survivors. The default value is `:killed`.

* `:page` (`t:pos_integer/0`) - Page of survivors to show. The default value is `1`.

* `:per_page` (`t:pos_integer/0`) - Survivors per page. The default value is `20`.

* `:sources` (map of `t:String.t/0` keys and `t:String.t/0` values) - Original source text by file path, for excerpts and the MTE export. The default value is `%{}`.

* `:test_files` (list of `t:String.t/0`) - Test files that exist, for the suggested test file. The default value is `[]`.

* `:ansi` (`t:boolean/0`) - Colour the human output. The default value is `false`.

* `:report` (`t:boolean/0`) - Stream: end with a `report` event holding the agent document. The default value is `false`.

* `:ci` (`t:boolean/0`) - MTE `system.ci`. The default value is `false`.

# `options`

```elixir
@type options() :: %{
  thresholds: keyword(),
  timeouts: :killed | :survived,
  page: pos_integer(),
  per_page: pos_integer(),
  sources: %{required(String.t()) =&gt; String.t()},
  test_files: [String.t()],
  ansi: boolean(),
  report: boolean(),
  ci: boolean()
}
```

# `page`

```elixir
@type page() :: %{
  page: pos_integer(),
  per_page: pos_integer(),
  total: non_neg_integer(),
  more: non_neg_integer()
}
```

Where a page of survivors sits in the whole list.

# `survivor`

```elixir
@type survivor() :: {Juggernaut.Mutant.t(), Juggernaut.Result.t(), survivor_status()}
```

A listed mutant with its result.

# `survivor_status`

```elixir
@type survivor_status() :: :survived | :no_coverage | :timeout
```

How a listed mutant is shown: survived, not covered, or a timeout scored as survived.

# `columns`

```elixir
@spec columns([[String.t()]]) :: [String.t()]
```

Rows of cells as left-aligned text columns, each padded to its widest cell.

# `conventional_test_file`

```elixir
@spec conventional_test_file(String.t()) :: String.t()
```

The conventional test file of a source file: `lib/a/b.ex` → `test/a/b_test.exs`, and in
an umbrella `apps/x/lib/a.ex` → `apps/x/test/a_test.exs`. The first `lib` segment becomes
`test`; a path without one has its first segment replaced (`gates/x.ex` → `test/x_test.exs`).

# `core_files`

```elixir
@spec core_files(Juggernaut.Run.t()) :: MapSet.t(String.t())
```

The files of `run` that hold core mutants.

# `count`

```elixir
@spec count(non_neg_integer(), String.t(), String.t()) :: String.t()
```

`n` followed by the singular or plural noun.

# `duration`

```elixir
@spec duration(non_neg_integer()) :: String.t()
```

A duration for people: `850 ms`, `3.1 s`, `2 min 05 s`.

# `excerpt`

```elixir
@spec excerpt(Juggernaut.Mutant.t(), %{required(String.t()) =&gt; String.t()}) ::
  %{start_line: pos_integer(), lines: [String.t()]} | nil
```

The source lines around a mutant: from the nearest `def`/`defp`/`defmacro(p)` head at
most 20 lines above it (else 3 lines above) to
3 lines below. `nil` when the file's source was not given.

# `family`

```elixir
@spec family(Juggernaut.Mutant.t()) :: String.t()
```

A mutant's family for people: `relational:>=`, or the operator alone.

# `find_mutant`

```elixir
@spec find_mutant(Juggernaut.Run.t(), String.t()) ::
  {:ok, Juggernaut.Mutant.t()} | {:error, Juggernaut.Error.t()}
```

The mutant of `run` named by `ref` (design notes §3): its full short id, its hash part, or
a unique prefix of 4 or more hex digits of the hash. An unknown or ambiguous `ref` is a
usage error (exit 2); an ambiguous one lists the candidates.

# `hash`

```elixir
@spec hash(String.t()) :: String.t()
```

The hash part of a short id (`Grant.check/2:relational:4be1c09` → `4be1c09`).

# `integration_only`

```elixir
@spec integration_only(Juggernaut.Run.t(), options()) :: [
  {Juggernaut.Mutant.t(), Juggernaut.Result.t()}
]
```

The mutants killed only by integration tests (`Juggernaut.Result.integration_only?/1`:
every unit test of their selection passed first), core first, then by file, line and
id. They count as killed; a timeout kill scored as survived is a survivor instead
(`survivors/2`), so it is not listed here.

# `last_page`

```elixir
@spec last_page(page()) :: non_neg_integer()
```

The number of the last page of `page`'s list; `0` when the list is empty.

# `next_page_command`

```elixir
@spec next_page_command(String.t(), page(), atom() | nil) :: String.t()
```

The command that shows the page after `page` of `run_id`'s survivors, in `format` (`nil`
for human output). It always names `--per-page`, so following it shows the next items
whatever page size the current output used.

# `options!`

```elixir
@spec options!(keyword() | options()) :: options()
```

Validates view options, a keyword list or a map (such as one this returned), and fills
the defaults. Raises `ArgumentError` when invalid.

# `page`

```elixir
@spec page([term()], pos_integer(), pos_integer()) :: {[term()], page()}
```

One page of `items` and where it sits. A page past the end is empty.

# `page_command`

```elixir
@spec page_command(String.t(), pos_integer(), pos_integer(), atom() | nil) ::
  String.t()
```

The command that shows page `number` of `run_id`'s survivors at `per_page` a page, in
`format` (`nil` for human output).

# `percent`

```elixir
@spec percent(number() | nil) :: String.t()
```

A percentage for people: `97.4%`, or `-` when nothing was scored.

# `phases`

```elixir
@spec phases(Juggernaut.Run.t()) :: [{String.t(), non_neg_integer()}]
```

The run's phases as `{name, duration_ms}`, in run order.

# `render`

```elixir
@spec render(
  :human | :agent | :json | :markdown | :github,
  Juggernaut.Run.t(),
  keyword() | options()
) ::
  iodata()
```

Renders a finished `run` in `format` (a `--format` name) as iodata.

# `result`

```elixir
@spec result(Juggernaut.Run.t(), Juggernaut.Mutant.t()) :: Juggernaut.Result.t()
```

The result of `mutant` in `run`; a mutant without one never ran (`:pending`).

# `reused`

```elixir
@spec reused(%{results: non_neg_integer(), recorded_ms: non_neg_integer()}) ::
  String.t() | nil
```

The reuse note: how many results were reused and the run time recorded for them. Time
saved is only ever reported from these recorded durations, never measured or estimated.

# `scope`

```elixir
@spec scope(Juggernaut.Run.t()) :: String.t()
```

The scope of a run for people: `changed vs main (2 files)`, `full`, ...

# `selection_text`

```elixir
@spec selection_text(Juggernaut.Result.selection()) :: String.t()
```

What a selection (`t:Juggernaut.Result.selection/0`) runs, for people. The run's own
counted reason, when it has one, is the result's `reason`.

# `suggested_test_file`

```elixir
@spec suggested_test_file(Juggernaut.Mutant.t(), Juggernaut.Result.t(), [String.t()]) ::
  String.t() | nil
```

Where a test that kills the mutant belongs: the nearest miss's test file, else the
conventional test file (`lib/a/b.ex` → `test/a/b_test.exs`) when it exists, else `nil`.

# `survivor_status`

```elixir
@spec survivor_status(Juggernaut.Result.t() | nil, :killed | :survived) ::
  survivor_status() | nil
```

How a result is listed among the survivors, or `nil` when it is not a survivor.

# `survivors`

```elixir
@spec survivors(Juggernaut.Run.t(), options()) :: [survivor()]
```

The mutants listed as survivors, core first, then by file, line and id: `survived` and
`no_coverage` results, and `timeout` kills when timeouts are scored as survived.

# `test_file`

```elixir
@spec test_file(String.t()) :: String.t()
```

The file part of a `file:line` test id.

# `verify_command`

```elixir
@spec verify_command(String.t(), atom() | nil) :: String.t()
```

The command that re-checks one mutant, in `format` (`nil` for human output).

---

*Consult [api-reference.md](api-reference.md) for complete listing*
