# Matrix: local end-to-end check

The unit tests run against a fake homeserver (`tests/fakes/matrix.py`). To see real messages in a
real room without an account anywhere, run a throwaway [Conduit](https://conduit.rs/) homeserver
in Docker. This is a manual check; it is not part of CI. `scripts/e2e-local.sh matrix` runs
the same steps automatically and checks the room (see [release-checklist.md](release-checklist.md)).

## 1. Start Conduit

```bash
docker network create herald-matrix
docker run -d --rm --name herald-conduit --network herald-matrix -p 127.0.0.1:6167:6167 \
  -e CONDUIT_CONFIG="" -e CONDUIT_SERVER_NAME=localhost \
  -e CONDUIT_DATABASE_BACKEND=rocksdb -e CONDUIT_DATABASE_PATH=/var/lib/matrix-conduit/ \
  -e CONDUIT_ADDRESS=0.0.0.0 -e CONDUIT_PORT=6167 \
  -e CONDUIT_ALLOW_REGISTRATION=true -e CONDUIT_ALLOW_FEDERATION=false \
  matrixconduit/matrix-conduit:latest
```

Open registration is fine here: the server listens on 127.0.0.1 only and is gone when the
container stops (`--rm`, no volume).

## 2. Create the bot user and a room

```bash
curl -s -X POST http://127.0.0.1:6167/_matrix/client/v3/register \
  -H "Content-Type: application/json" \
  -d '{"username":"herald","password":"local-only","auth":{"type":"m.login.dummy"}}'
# {"access_token":"...","user_id":"@herald:localhost","device_id":"..."}

curl -s -X POST http://127.0.0.1:6167/_matrix/client/v3/createRoom \
  -H "Authorization: Bearer <access_token>" -H "Content-Type: application/json" \
  -d '{"name":"Herald"}'
# {"room_id":"!w6cZJoMxsbWl3wcx5YR5gAb6Q1NPr6Q5PvcbbskQKQk"}
```

Recent room versions (12 and later) have room IDs without a `:server` suffix, as above; Herald
accepts both forms.

## 3. Run Herald with only Matrix enabled

Use a copy of your config with every other notifier disabled and this block:

```json
{
  "type": "matrix", "enabled": true, "languages": ["pt-BR"],
  "homeserver": "http://herald-conduit:6167", "access_token": "${MATRIX_TOKEN}",
  "room_id": "!w6cZJoMxsbWl3wcx5YR5gAb6Q1NPr6Q5PvcbbskQKQk"
}
```

Then run the runtime image on the same network, with a temporary data volume (never the host's
`data/`):

```bash
docker build --target runtime -t herald:local .
docker run --rm --network herald-matrix --env-file .env -e MATRIX_TOKEN=<access_token> \
  -v "$PWD/config.matrix.json:/app/config.json:ro" -v herald-matrix-data:/app/data \
  herald:local run
```

## 4. Read the room

```bash
curl -s "http://127.0.0.1:6167/_matrix/client/v3/rooms/<room_id, ! as %21>/messages?dir=b&limit=50" \
  -H "Authorization: Bearer <access_token>"
```

Each `m.room.message` event has `msgtype: m.text`, a plain `body` and an HTML `formatted_body`.
Run Herald again on the same day: the homeserver recognizes the transaction ids and the room
gets no duplicates. To see it in a client, point Element (or any client) at
`http://127.0.0.1:6167` and log in as `herald`.

## 5. Clean up

```bash
docker stop herald-conduit
docker volume rm herald-matrix-data
docker network rm herald-matrix
```
