# `Juggernaut.Coverage.Index`
[🔗](https://github.com/holsee/juggernaut/blob/v0.4.0/lib/juggernaut/coverage/index.ex#L1)

The coverage index (design notes §10.5, `_build/juggernaut/coverage/index.etf`): what
each probed test file hit, kept between runs so that a run probes only the test files
whose coverage may have changed.

One record per test file: the file's hash, the hash of each mutated source file it was
probed against (`sources`), the fingerprint of each source file's mutant keys it was
probed for (`keys`), the files of the sites it hit (`hit_sources`), its hits by mutant
key (stable across builds, unlike switches), and its clock. Labels name tests by
strings (`Juggernaut.Coverage.Selection.string_id/1`), so reading the index creates no
atoms. The index also holds the hash of the inputs every test depends on (`globals`:
`mix.lock`, `config/`, and every file under the test paths that is not a test file,
such as `test_helper.exs` and test support), and the keys behind each fingerprint
(`key_sets`).

Hits are stored only under the keys of the mutants a probe ran for, so the keys matter
as much as the hashes: a run over unchanged sources with mutants the index has never
seen (another operator set, a Juggernaut upgrade, an earlier run over a subset of a
file's mutants) probes again, or those mutants would find no labels and be reported
as `no_coverage`.

`stale/2` says what to probe again (`:all`, or each stale file with its reason):

  * everything when there is no index, or `globals` changed;
  * a test file that is new or whose content changed;
  * a test file that hit a source file whose content changed since it was probed
    (when the run knows its functions: in a function it ran sites of, or in any
    function of that file that had no mutants, added and removed ones included);
  * a test file probed before a source file was mutated (its sites were not there);
  * a test file probed for fewer of a source file's mutants than the run has (the
    run's keys of that file are not all among the keys it was probed for).

Limit (the same as any site-level coverage): an edit can send a test file to sites it
never hit through code the index cannot see it run. A test file is kept when the
edited code is in a file it never hit (a file without mutants, or one it reached only
through functions without sites), or in a function with mutants whose sites it never
ran (it entered through a clause with no sites). Its coverage of the sites it reaches
now is then missing, so a mutant only it would kill can read as survived or
`no_coverage` until it is probed again. A core survivor's reconfirmation (gate and
release lanes) runs every test file that hit its source file, so it recovers the
second case but not the first.

# `current`

```elixir
@type current() :: %{
  :globals =&gt; String.t(),
  :sources =&gt; %{required(String.t()) =&gt; String.t()},
  :tests =&gt; %{required(String.t()) =&gt; String.t()},
  optional(:keys) =&gt; %{required(String.t()) =&gt; [String.t()]},
  optional(:functions) =&gt; %{
    required(String.t()) =&gt; Juggernaut.Cache.Footprint.hashes()
  },
  optional(:key_functions) =&gt; %{required(String.t()) =&gt; String.t()}
}
```

`keys`: each mutated source file's mutant keys, sorted (a missing file has none).
`functions`: each mutated source file's function hashes now; `key_functions`: each
mutant key's function. Without the last two, staleness is judged by file.

# `file_record`

```elixir
@type file_record() :: %{
  :hash =&gt; String.t(),
  :sources =&gt; %{required(String.t()) =&gt; String.t()},
  :keys =&gt; %{required(String.t()) =&gt; String.t()},
  :hit_sources =&gt; [String.t()],
  :hits =&gt; %{required(String.t()) =&gt; [Juggernaut.Coverage.Selection.label()]},
  :us =&gt; non_neg_integer(),
  optional(:hit_functions) =&gt; %{required(String.t()) =&gt; [String.t()]},
  optional(:site_functions) =&gt; %{required(String.t()) =&gt; [String.t()]},
  optional(:functions) =&gt; %{
    required(String.t()) =&gt; %{required(String.t()) =&gt; String.t()}
  }
}
```

A probed test file. `hit_functions` and `site_functions` hold, by source file, the
functions (`Juggernaut.Cache.Footprint.key/3`) it ran sites of and the functions that
had mutants when it was probed; `functions` the hashes of those and of every other
function the file wrote then.

# `t`

```elixir
@type t() :: %Juggernaut.Coverage.Index{
  files: %{required(String.t()) =&gt; file_record()},
  globals: String.t() | nil,
  key_sets: %{required(String.t()) =&gt; [String.t()]}
}
```

# `current`

```elixir
@spec current(Path.t(), [String.t()], [String.t()], [String.t()]) :: current()
```

The current hashes of the project at `root`: its `tests` (test file → hash), the
mutated `sources` (each of `source_files`), and `globals` (`mix.lock`, `config/` and
every other file under `test_paths`). `test_files` are the test files, relative to
`root`, as the workers load them.

# `fingerprint`

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

The fingerprint of a sorted list of mutant keys.

# `function_key`

```elixir
@spec function_key(%{module: module(), function: {atom(), arity()}}) :: String.t()
```

The function of a mutant as the index names it (`Juggernaut.Cache.Footprint.key/3`).

# `key_functions`

```elixir
@spec key_functions([map()]) :: %{required(String.t()) =&gt; String.t()}
```

Each mutant key's function (`function_key/1`), the run's `key_functions`.

# `keys_by_source`

```elixir
@spec keys_by_source([%{key: String.t(), file: String.t()}]) :: %{
  required(String.t()) =&gt; [String.t()]
}
```

Each source file of `mutants` with its mutants' keys, sorted (`current`'s `keys`).

# `labels`

```elixir
@spec labels(t()) :: %{
  required(String.t()) =&gt; [Juggernaut.Coverage.Selection.label()]
}
```

Every label of each mutant key, over the index's test files.

# `read`

```elixir
@spec read(Path.t()) :: t() | nil
```

The index at `path`, or `nil` when it is missing, unreadable or of another format.

# `record`

```elixir
@spec record(map(), String.t(), current(), %{required(pos_integer()) =&gt; [map()]}) ::
  file_record()
```

The record of one probed test file (`Juggernaut.Coverage.Probe`), its site hits put
under the keys of the site's mutants: `site_mutants` maps each site to its mutants
(`%{key, file}`). `keys` holds the fingerprint of each source file's current keys.

# `stale`

```elixir
@spec stale(t() | nil, current()) ::
  {:all, String.t()} | {:files, %{required(String.t()) =&gt; String.t()}}
```

What to probe again (see the module doc), given the `current` hashes.

# `update`

```elixir
@spec update(t() | nil, current(), %{required(String.t()) =&gt; file_record()}) :: t()
```

The index with `probed` (`%{test_file => record}`) put in, records of test files no
longer in `current` dropped, `current`'s globals, and the keys behind every fingerprint
a record refers to (`key_sets`).

# `write`

```elixir
@spec write(Path.t(), t()) :: :ok | {:error, Juggernaut.Error.t()}
```

Writes `index` to `path` atomically.

---

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