# Testing

> Read when writing tests. Make a test realm per test, record what a select changes to, check what a collection still holds, hold every stop in using, and test programs on TestClock.

## Example

A test makes its own realm with `testRealm` from `@frondruntime/sorus/testing`.
`ChatUnderTest` is the chat's model with a connection, `$canSend`, and the
messages; the first test records `$canSend` across updates.

```ts
import { expect, test } from "bun:test";
import { Realm } from "@frondruntime/sorus";
import { record, testRealm } from "@frondruntime/sorus/testing";

test("typing text lets a message be sent", () => {
  // A realm per test: nothing one test made is seen by the next. Its dispose,
  // when the test ends, throws if anything the test started is still running.
  using realm = testRealm();
  const chat = new ChatUnderTest(realm);

  chat.type("hi");

  expect(Realm.untracked(realm, (read) => read(chat.$canSend))).toBe(true);
});

test("typing twice in one update records one change", () => {
  using realm = testRealm();
  const chat = new ChatUnderTest(realm);
  // Declared after the realm, so it stops before the realm checks.
  using canSend = record(realm, (read) => read(chat.$canSend));

  Realm.update(realm, () => {
    chat.type("h");
    chat.type("hi");
  });
  chat.type("hi again");

  // "hi again" leaves $canSend true: no change, nothing recorded.
  expect(canSend.values).toEqual([true]);
});
```

A collection that dropped a row keeps nothing for its key, and `retainedKeys`
checks it where a count of rows cannot.

```ts
import { expect, test } from "bun:test";
import { Realm, Source } from "@frondruntime/sorus";
import { held, retainedKeys, testRealm } from "@frondruntime/sorus/testing";

test("a refetch lets go of the messages it drops", () => {
  using realm = testRealm();
  const chat = new ChatUnderTest(realm);
  Realm.update(realm, (_read, write) => {
    write.replaceAll(chat.$messages, [hello, { ...hello, id: "m2" }, { ...hello, id: "m3" }]);
  });
  // A reader of a row makes a box and a computed value for its key.
  using _ = held(Source.observe(chat.$messages.row("m3")));

  Realm.update(realm, (_read, write) => {
    write.replaceAll(chat.$messages, [hello]);
  });

  // "m3" is gone with its box and computed value, though still observed.
  expect(retainedKeys(chat.$messages)).toEqual(new Set(["m1"]));
});
```

## Rules

- One realm per test, made with `using realm = testRealm()`. A realm made at
  module level carries state from one test into the next.
- Read with `Realm.untracked`, write with `Realm.update` or a model method that
  calls it. The testing entry adds no reader or writer, so a test runs the
  same calls as application code.
- Hold every stop in `using`, the realm first: `record` for a watch, `held` for
  the rest. `using` disposes in reverse order, and a stop function called at
  the end is skipped when an assertion fails.
- Stop every watch, autorun, `Source.observe`, `Collection.onRowChange`
  listener, view, and `Realm.onUpdate` listener, and apply or drop every stage.
  The realm's dispose throws one `sorus/test-realm-not-at-rest` naming each one left.
- Check a collection with `retainedKeys`, never its private fields: they change
  without notice.

## Answers

| Call | Answers |
| --- | --- |
| `testRealm(options?)` | A realm that is disposed with its `using` scope. Used after its dispose, it throws `sorus/realm-disposed`. |
| `record(realm, select, options?)` | `values`, each value `select` changed to, oldest first, rerun once per update; and `stop`, which `using` also runs. |
| `held(stop)` | A `Disposable` that runs `stop` when its `using` scope ends. |
| `retainedKeys($items)` | A copy of the set of keys `$items` keeps a row, a box, or a computed value for. Throws for a value Sorus did not make. |

## With Effect

`Model.decode(realm, Model)(json)` decodes into the test's realm, and a
program takes it as `Program.watchScoped(realm, ...)` does; close every
program's scope before the dispose. A program that sleeps runs on `TestClock`
from `effect/testing`, and the test waits on a `Deferred` or on
`Program.changes`, never on `TestClock.adjust`.

## See also

- [Stages](../state/stages.md): a stage left open in a test realm.
- [Reacting](../state/reacting.md): when a watch reruns.
- [Programs](../effect/programs.md): `Program.*` and `Source.observeScoped`.
- [Error messages](../reference/errors.md): `sorus/test-realm-not-at-rest`.
