# `Juggernaut.Peer.Switch`
[🔗](https://github.com/holsee/juggernaut/blob/v0.4.0/lib/juggernaut/peer/switch.ex#L1)

The runtime switch that compiled metamutants read (design notes §10.3).

One `:persistent_term` key, `:juggernaut_active`, holds a small integer:

  * `0` (or unset): every mutant is off and the code behaves as written;
  * a positive switch: exactly that mutant is on;
  * `-1` (`probe/0`): every mutant is off and each site that runs records a hit.

Every site of a metamutant calls `site/2` with its first switch and the number of
mutants it holds, and branches on the answer: `0` runs the original code, `i` runs the
site's `i`th mutant. Switches of one site are consecutive, so the call needs no map.

Hits are counted per site (the site's first switch) in a public ETS table created by
`arm_hits/0`, and handed to `Juggernaut.Peer.Recorder.hit/1`, which records which test
each hit is owed to. Without the tables a probe hit is dropped, so a metamutant never
fails because recording is off. Updating the key costs well under a millisecond at the
process counts of a large Phoenix app (`docs/dependencies/code-loading.md`): set it once
per job, never per test.

This module runs inside workers and is never mutated.

# `active`

```elixir
@spec active() :: integer()
```

The active switch (`0` when none was set).

# `arm_hits`

```elixir
@spec arm_hits() :: :ok
```

Creates the hit table (public, owned by the caller) if it does not exist, and empties
it if it does.

# `hits`

```elixir
@spec hits() :: %{required(pos_integer()) =&gt; pos_integer()}
```

Hit counts recorded so far, by site (its first switch). Empty without the table.

# `key`

```elixir
@spec key() :: atom()
```

The `:persistent_term` key holding the active switch.

# `probe`

```elixir
@spec probe() :: integer()
```

The switch value that turns probe recording on (and every mutant off).

# `set`

```elixir
@spec set(integer()) :: :ok
```

Sets the active switch: `0` for none, a mutant's switch, or `probe/0`.

# `site`

```elixir
@spec site(pos_integer(), pos_integer()) :: non_neg_integer()
```

Which branch the site whose switches are `first..first + count - 1` takes: `0` for the
original code, `i` (1-based) for its `i`th mutant. In probe mode the site's hit is
recorded and the original code runs.

---

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