# Testing

Each kind of plugin has a checker in `lk.testing` for the laws the core relies on. We call them from the plugin’s own tests:

``` python
import laplacekit as lk


def test_last_layers_is_a_subset():
    for n in (1, 2, 3):
        lk.testing.check_subset(last_layers(n), model, caps)
```

A checker that finds a broken law raises an `AssertionError` that names the law and the object, e.g., `monoid law: fold(a ++ b) != fold(a) + fold(b) for MyCurvature (max rel err 3e-02)`.

| Checker | For | What it checks |
|----|----|----|
| `check_subset(subset, model, caps)` | `Subset` | A non-empty mapping of the module’s own parameters, by their names; the same selection on a second call. |
| `check_prior(prior, params)` | `PriorWithCovariance` | A finite `log_prob`; `add_precision_to` keeps the operator type; `cov_mv` is the inverse of the precision; for a `TunablePrior`, `tunable()` returns the prior’s own positive leaves. |
| `check_likelihood(likelihood, f, y)` | `Likelihood` | `log_prob` is shaped `[N]` and agrees with `distribution`; `unit()` returns self unless it drops a hyperparameter; the output Hessian’s `mv` matches `dense()`, which is symmetric PSD. |
| `check_objective(objective, posterior)` | `Objective` | A finite 0-dim tensor, differentiable in every hyperparameter of the posterior. |
| `check_link(link, likelihood, outputs)` | `LinkApprox` | `supports` agrees with the call; a valid `Distribution`. |
| `check_predictive(predictive, posterior, inputs)` | `Predictive` | `requires` is a `Requirements`; a `Distribution` with one batch entry per input. |
| `check_curvature(curv, model, likelihood, params, batches)` | `Curvature`, `Staged` | `init` is the identity; the monoid law; `finalize` returns averages; `diagonal`, `mv` and `logdet` agree with the dense matrix. |

The curvature and prior checks run in float64, so they need float64 params and a float64 model (`model.double()`). Checks that build a dense matrix run only up to `lk.testing.DENSE_MAX_PARAMS` (512) parameters, so the test model should be small. The output Hessian laws of `check_likelihood` likewise run only for events up to `lk.core.MAX_DENSE_EVENT` (4096) entries. The [reference](../reference/index.llms.md#testing-plugins) has each checker’s full list of laws.

> **NOTE:**
>
> The checkers test the contract only. A curvature that returns zeros keeps every law, so test the numbers against an independent reference as well: a dense matrix, `torch.func`, a published result. The test in [Your first plugin](../plugins/first.llms.md) compares against names written by hand.

## Gradients of an objective

`check_objective` detaches the posterior, makes the leaves that `where` picks require grad, and calls the objective. When a test needs the gradient itself, the same three helpers from `lk.core` do it:

``` python
where = lambda post: post.prior.prior_precision

posterior = lk.core.detach_leaves(post)
names, old = lk.core.where_leaves(posterior, where)
new = tuple(t.clone().requires_grad_() for t in old)

loss = my_objective(lk.core.replace_leaves(posterior, old, new))
(grad,) = torch.autograd.grad(loss, new)
```

## Imports

A plugin imports `laplacekit` and `laplacekit.core`, never a submodule such as `laplacekit.core.layout`. The submodules change without notice, while the public API is what the plugin can rely on. `import-linter` checks it in CI:

``` ini
[importlinter]
root_packages =
    laplacekit_example
    laplacekit

[importlinter:contract:public-api]
name = laplacekit_example imports only the public laplacekit API
type = forbidden
source_modules =
    laplacekit_example
forbidden_modules =
    laplacekit.core.*
allow_indirect_imports = true
```

`laplacekit` has to be among the `root_packages`. Without it, `import-linter` sees only the top-level package and keeps the contract even when the plugin imports a submodule.
