# `Juggernaut.Engine.Reuse`
[🔗](https://github.com/holsee/juggernaut/blob/v0.4.0/lib/juggernaut/engine/reuse.ex#L1)

What a run takes from earlier work and leaves for later runs (design notes §7.3, §7.4):
triage claims and the per-mutant result cache, as `Juggernaut.Engine` uses them.

  * `start/2`, when the run starts: the cache mode of the lane and request
    (`Juggernaut.Cache.mode/3`), the triage file, and whether this run may write the
    cache: the gate lane with the cache on, a commit, and a tree clean under `paths`,
    the test paths, `config/`, `priv/`, `mix.lock` and the data files of `cache_inputs`
    (the clean-HEAD rule). Only such a run measures footprints. The digest of the data
    files (`data_files/2`) joins the cache's globals;
  * `triage/3`, before compile: the mutants whose claim applies become `equivalent`
    results and are not compiled; every claim on a mutant of the run is surfaced;
  * `lookup/5`, after the probe: the cache is loaded under the run's globals, the tests
    are checked for weakening since each entry's commit, and the jobs whose entry
    stands become reused results instead of jobs (`[:juggernaut, :cache, :lookup]`);
  * `record/4`, once the run is recorded: the new results of the jobs that ran are
    written to the cache, only if the tree is still clean at the same commit, so the
    hashes recorded are those of the code that was tested.

# `t`

```elixir
@type t() :: %{
  mode: Juggernaut.Cache.mode(),
  writable?: boolean(),
  commit: String.t() | nil,
  triage: Juggernaut.Triage.t(),
  claims: [map()],
  weakening: [map()],
  cache: Juggernaut.Cache.t() | nil,
  tests: %{required(String.t()) =&gt; String.t()},
  data: String.t() | nil
}
```

# `checkout_inputs`

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

The pathspecs whose uncommitted changes make a run dirty (`Juggernaut.Engine.Checkout`):
`inputs/2`, `mix.exs`, the configuration `.juggernaut.exs` and the triage file, since
the options and the claims decide what runs and how it is scored.

# `clean_commit`

```elixir
@spec clean_commit(map(), [String.t()]) :: String.t() | nil
```

The full commit when the project at `request.root` is clean under what cached results
rest on, else `nil`. Clean means `git status` lists nothing there nor among the data
files of `cache_inputs` (with `:tracked`, no tracked file is modified) and the project's
`mix.exs` is tracked: a project inside a directory its repository ignores (whose files
git status never lists) is never clean. Without git, or before the first commit, `nil`.

# `data_files`

```elixir
@spec data_files(map(), [String.t()]) :: [String.t()]
```

The files outside the code and the tests that cached results rest on (`cache_inputs`),
relative to `request.root` and sorted: with `:tracked`, every file git tracks except
those under `paths` and `test_paths` (their functions and test files are hashed
one by one) and the triage file (claims apply on their own); none without git. With
a list of globs, every regular file matching one.

# `inputs`

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

The pathspecs a run's results rest on: `paths`, the test paths, `config`, `priv`, `mix.lock`.

# `lookup`

```elixir
@spec lookup(t(), [map()], map()) :: {[Juggernaut.Result.t()], [map()], t()}
```

The jobs of `jobs` the cache answers: `{reused results, the jobs still to run, the
state}`. `ctx` holds the run's `:root`, `:run_id`, `:store`, `:fingerprint`,
`:test_paths`, `:git`, the index's `:current` hashes (`globals`, `tests`) and the boot
`:inventory`. With the cache off, every job runs.

# `record`

```elixir
@spec record(t(), Juggernaut.Run.t(), [map()], map()) ::
  :ok | {:error, Juggernaut.Error.t()}
```

Writes the cache entries of the `jobs` that ran in `run` (their results, with the
footprints by mutant id), when `recordable?/2` holds. `ctx` is as for `lookup/3` plus
`:config`.

# `recordable?`

```elixir
@spec recordable?(t(), map()) :: boolean()
```

Whether this run writes the cache when it ends: it may write (`writable?`) and the tree
is still clean at the commit it started on. `ctx` is as for `record/4`.

# `start`

```elixir
@spec start(map(), [String.t()]) :: {:ok, t()} | {:error, Juggernaut.Error.t()}
```

The reuse state of a run of `request` (`:root`, `:config`, `:lane`, `:cache`, `:git`)
with test paths `test_paths`, or the config error of a malformed triage file.

# `triage`

```elixir
@spec triage(t(), [Juggernaut.Mutant.t()], Path.t()) ::
  {[Juggernaut.Result.t()], [Juggernaut.Mutant.t()], t()}
```

Splits `mutants` by their triage claims: `{equivalent results, the mutants still to
run}`, with the claims of the run kept in the state.

# `writable?`

```elixir
@spec writable?(t()) :: boolean()
```

Whether this run may write the cache when it ends: the lane writes, the tree was clean
when it started and the cache was loaded. `recordable?/2` adds that the tree is still
clean.

# `write`

```elixir
@spec write(t(), Juggernaut.Run.t(), [map()], map()) ::
  :ok | {:error, Juggernaut.Error.t()}
```

Writes the cache entries as `record/4` does, once the caller has checked
`recordable?/2` (the engine checks it first, so a run that writes nothing has no
`cache` phase).

---

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