# Examples and e2e scripts

> An example is a test: built, booted under the software renderer, photographed, and held to no mistake the framework reports; and the games, menus, scenes and input are played from a script, frame by frame, their pictures compared exactly with goldens recorded only in the build image.

Source: https://omardev29.github.io/rmp-docs/framework/contributing/examples-and-e2e.html

An example is what people copy, so it is held to more than compiling: CI builds every one, boots it with no window, takes its picture and fails it on any mistake the framework reports. And an example of a game, a menu, a scene or the input is played — from a script of what a player presses, frame by frame — and every picture the script takes has to come out exactly like the one recorded for it.

[Testing](https://omardev29.github.io/rmp-docs/framework/testing.html#examples) says what each step proves. This page is for adding an example, and for writing, playing and recording its script.

## An example is a test

An example is a small project laid out like a game: `examples/<area>/<name>/` with `src/main.cpp`, `include/` when it has headers, `src/scenes/` when it has more than one scene, and `resources/` when it brings art. Every one becomes a target of its own, `example_<area>_<name>`, built and booted headless with a screenshot. A new mechanic belongs here, in an example, and not in the framework's `src/main.cpp`, which stays the dozen lines of the demo game.

A game in `examples/games/` is playable from start to finish — win, lose, play again — or it does not belong there. A title screen that draws is not a game that plays, and the script [below](https://omardev29.github.io/rmp-docs/framework/contributing/examples-and-e2e.html#e2e) is what proves the difference.

### Its shape

Before anything is built, `tools/examples_check.sh` reads the shape of every file of every example. An example uses the framework's entry point, `RMP_GAME` or `RMP_ENTRY_POINT`, because that is the only loop that works on every target: the browser owns its frame loop and iOS never calls `main()`. So it refuses an example that `writes its own int main().`, one that `drives its own frame loop with
WindowShouldClose().`, and an `on_frame` that takes no `delta`. The one exception is `examples/plain_c/src/main.c`, which leaves the framework on purpose and has to keep saying which targets that rules out.

Every example also needs a row in `examples/README.md` that links its folder: an example nobody is pointed at is a file that rots unread. The check fails on an example with no link, and on a link to an example that is gone. The documentation site gives every example a page of its own, and its `examples.toml` says whether a browser plays it there; a new example is a line in that file the day the site moves to the commit that has it.

### The boot

```shell
rmp example games/01_pong
rmp test examples
bash tools/examples_build.sh boot
```

`rmp example` builds one example and starts it in a window, to play it yourself. `rmp test examples` is the whole check CI runs — every example built, booted and played — and it compiles all of them, which is why the plain `rmp test` leaves it out. `bash tools/examples_build.sh boot` is its first half, everything but the scripts.

The boot builds each example against raylib's software renderer — no GPU, no window, no display, the same pixels on every machine — and runs it for thirty frames. It passes when the example printed `RAY_TEST_BOOT_OK assets_failed=0`, `RAY_TEST_RENDER_OK` and `RAY_TEST_DONE_FRAMES`, wrote its picture, reported no mistake and exited with status 0, inside three minutes. The picture is the last frame, in `build/examples-check/screenshots/example_<area>_<name>.png`: look at it. Looking at the pixels is how most of what is wrong with an example is found.

```shell
RMP_EXAMPLE_FRAMES=120 bash tools/examples_build.sh boot
```

A picture of a later frame than the thirtieth is that variable away.

### No mistake reported: `RAY_TEST_REPORTS=0`

A mistake in the game that the framework can see — an action nobody defined, a widget outside `begin()` and `end()`, a number that is not one — is said once, as a warning in the log, and counted. On the last frame of a run with a frame budget the framework prints the count, `RAY_TEST_REPORTS=0` when there was none.

An example makes none. Any other count fails its boot, and the failure prints the warnings that were counted, so the line to fix is in the log. The plain C example is the one exception: it has no framework to report anything, and prints no count.

## A script: what a player presses

A script is `tests/e2e/<area>/<name>.e2e`, and it plays `examples/<area>/<name>`: what a player presses, frame by frame, what the screen has to show at the frames that matter, and which scene has to be on top. This is the whole of the menu example's:

[tests/e2e/ui/01_menu.e2e](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/tests/e2e/ui/01_menu.e2e#L1-L57)

```text
# examples/ui/01_menu, played: every screen, with the keyboard, the gamepad
# and the mouse; a disabled button that does nothing, and the quit dialog
# cancelled.
#
# One scene per screen, so each step says which is on top as well as what
# the screen shows; a scene changed to is on top from the next frame. A
# click is down for one frame and up the next, and a button fires on the
# way up; a screen that has just appeared can be clicked from its second
# frame, when its buttons have a place to be hit in. See tests/e2e/script.h
# for the grammar.

# --- the happy path: play, and back --------------------------------------------

# Play has the focus from the first frame: Enter alone presses it. The score
# is a point a frame.
1 tap KEY_ENTER
12 shot playing
12 expect scene PlayingScene

# Escape goes back to the menu. It read raylib's Escape key itself until this
# script, and so did nothing to anything a script or a gamepad could press;
# it is rmp::input's "ui_cancel" now.
13 tap KEY_ESCAPE

# The gamepad: the d-pad down to Options, A to open it. The new screen's
# first control has the focus, and the ring shows: a pad was used.
15 tap GAMEPAD_BUTTON_LEFT_FACE_DOWN
17 tap GAMEPAD_BUTTON_RIGHT_FACE_DOWN
28 shot options
28 expect scene OptionsScene
29 tap KEY_ENTER

# --- the failure path: a disabled button, and a dialog cancelled -------------

# Cloud saves is disabled: the click does nothing to it, and the ring goes,
# because the mouse is what is being used now. Music stays off.
31 click 400 269
42 shot music_off_cloud_disabled

# Back, then Quit, with the mouse: the dialog asks first.
43 click 400 325
47 click 400 297
58 shot really_quit
58 expect scene ConfirmScene

# Down to Cancel, Enter: the menu again, and nothing quit.
59 tap KEY_DOWN
61 tap KEY_ENTER

# Play once more, and back with the gamepad's B -- "ui_cancel" is bound to
# both.
63 tap KEY_ENTER
65 tap GAMEPAD_BUTTON_RIGHT_FACE_RIGHT
76 shot back_by_pad
76 expect scene MainMenuScene
76 end
```

Every line starts with the frame it happens on, counted from 0 at exactly a sixtieth of a second each, and never going back. `#` starts a comment, wherever it is.

| Line | What it does |
|---|---|
| `12 press KEY_SPACE` | holds the key down from this frame on |
| `40 release KEY_SPACE` | lets it go from this frame on |
| `1 tap KEY_ENTER` | down for this one frame, up the next |
| `15 tap GAMEPAD_BUTTON_LEFT_FACE_DOWN` | the same three, for the first gamepad's buttons, by raylib's names; its sticks stay at rest |
| `31 click 400 269` | the left mouse button, down for this one frame at a point of the window, in pixels; the pointer stays there |
| `43 type " Onf"` | characters typed in this frame into the text field that has the keyboard, between double quotes, so a space is one; `tap KEY_BACKSPACE` takes one off |
| `12 shot playing` | the picture this frame drew, compared with `tests/e2e/golden/<area>/<name>/playing.png` |
| `640 expect scene OverScene` | the scene on top once this frame is over: a class name, or its end, so `OverScene` matches one in an anonymous namespace |
| `76 end` | the last frame: the game quits after it |

A script that cannot mean what it says is refused before the game starts, with the line it is on:

- input on frame 0: the first frame has nothing to compare a key with, so a press there is held and never pressed — the first input goes on frame 1;
- a key pressed while it is down, released while it is up or in the frame it went down, or pressed again in the frame it comes up or the frame after its tap: it would never come up, so the second press would be no press at all; and a click in the frame of another click or the frame after it, for the same reason;
- a `type` with no quotes or nothing between them, a `"` or a `#` inside them, a control character, a byte that is not UTF-8, more than sixteen characters, or two in one frame;
- a shot name that is not lower case, digits and `_` — it is a file name — or one used twice;
- no `end`, anything after it, or a script that checks nothing.

The build makes a target of every script, `e2e_<area>_<name>`: the example as it ships, with `tests/e2e/driver.cpp` beside it. A script whose example is not there — renamed, or the script put in the wrong folder — stops the configure with `… plays no example: it has
to be …`, because a script nothing plays looks exactly like one that passes. A plain C example cannot have one: the driver plays a game through the framework's seams, and it has none.

### What the driver does to the game

The driver installs itself before `main()`, through the framework's seams, and the game does not know it is there. Every frame is exactly a sixtieth of a second and `rmp::random` starts from seed 1, so a run is the same run every time. What the script holds down reaches `rmp::input` and the interface's own navigation and pointer, and the input of frame N+1 is set at the end of frame N, which is when a real keyboard changes. Saves go to a fresh folder, so last run's best score cannot change this one, and there is no sound device.

A script can press only what the game reads through `rmp::input` or `rmp::ui`. Under the software renderer raylib's own keyboard and mouse never change, so a game that asks raylib about a key itself never sees it go down — and a key read that way is one no gamepad can press either.

Two things about menus decide a script's first lines: the first button of a screen has the focus already, so Enter alone presses it; and Space and Enter press the focused control of a menu on top, so a script that taps a game's Space under a pause menu resumes the game.

On every frame, whatever the script checks, the driver fails a control the example put where nobody can reach it: one that takes the focus, is not wholly inside the window, and is in no scroll area that could bring it in. And every mistake the framework reported during the run fails the script at its `end`, a line each, in the log's own words.

## Writing one

1. Play the example yourself, with `rmp example`, and choose two paths through it: one that gets somewhere and one that fails. Pong is lost and then won; Breakout loses its three balls and then clears its wall; the menu example walks every screen with the keyboard, the gamepad and the mouse, then clicks a disabled button and cancels a dialog.
2. Write the script: the input, a `shot` at each moment that proves something, an `expect scene` where the scene changes, and a comment above each step that says what it is for. The scripts in `tests/e2e/` are written that way, and they are the best guide to frame arithmetic: a click is down for one frame and a button fires on the way up; a screen that has just appeared can be clicked from its second frame.
3. Play it, on your machine. Outside the build image a shot that has no golden yet is not a failure: the run writes the picture it took and says so, and you look at it.
4. Move the frames until each picture shows what it should, and play it again.
5. Record the goldens inside the build image, look at every one, and commit them with the script.

```shell
bash tools/examples_build.sh e2e
```

That plays every script. A shot with no golden prints a line that starts `E2E NOTE` and leaves `<shot>.actual.png` in `build/examples-check/e2e/<area>/<name>/`. Once that run has configured the build, one script is its own program, built and played alone:

```shell
cmake --build build/examples-check --target e2e_ui_01_menu
./build/examples-check/e2e_ui_01_menu
```

It prints a line for every shot it took, with the picture's hash; a line for every check that failed, `E2E FAIL <line>: <reason>`; and `E2E PASS` at `end` when none did.

A script that cannot do what a player should is news about the example, and the fix goes in the example. Breakout's ball outran the paddle and Space Invaders' last alien landed before a ship could reach it: both were found by a script that could not win, and both were fixed in the game.

Every frame of a script is a frame of a sanitized debug build drawn in software, and a Pong point is 75 of them. A script proves its paths and stops; it does not play a whole match to show that scoring works. A script still playing after ten minutes is stopped, and fails.

## Goldens: exact, and only from the image

Every shot is compared with its golden, `tests/e2e/golden/<area>/<name>/<shot>.png`, pixel for pixel, with no tolerance. A fixed step, a fixed seed and the software renderer give the same bytes for the same script, so a tolerance would only be somewhere for a real change to hide. Exact also means one compiler, one maths library and one renderer: the comparison is enforced inside the pinned build image, where CI runs it, and anywhere else a difference is reported with a line that starts `E2E DIFF` and does not fail.

A shot that differs fails the script with its line and says how many pixels differ and where the first one is. It leaves three pictures in `build/examples-check/e2e/<area>/<name>/`: the one expected, the one drawn, and the expected one darkened with every pixel that differs in red. CI's `e2e` job uploads that folder, so a red run carries its pictures.

A tolerance is for a shot that proves it needs one, with the reason written beside it. None does.

### Recording them

The goldens are drawn by the compiler and renderer that compare them, so they are recorded inside the build image and nowhere else. `rmp test e2e-update` plays every script and writes every shot as its golden; outside the image it refuses before building anything, saying `RMP_E2E_UPDATE records the e2e goldens, and only inside the pinned build image:` and how to run it there. `tools/dev_shell.sh` starts a shell in the image, with the checkout mounted:

```shell
tools/dev_shell.sh
```

and in that shell:

```shell
python3 tools/configure.py
./rmp test e2e-update
```

The image sees the checkout at another path than your machine does, so the next run on your machine finds `build/examples-check` configured from somewhere else, says so, and starts that folder again: a full build of the examples.

Then look at every picture that changed before committing it. `git status tests/e2e/golden/` lists them; open each one. A golden is what the script will hold the game to from then on, and a picture recorded without being looked at holds the game to whatever it drew that day, bug included.

A test holds the two sides together: every shot a script takes has its golden, and every golden is a shot some script takes. A shot renamed leaves an orphan, and the orphan fails until it is deleted.

## Which examples are played

Every example in `examples/games/`, `examples/ui/`, `examples/scenes/` and `examples/input/` has a script. The test that says so reads `tests/e2e/pending.txt`, the examples of those areas still without one: a list that only gets shorter, and is empty. So a new example in those areas comes with its script, its goldens and nothing in that file.

Every other area of `examples/` is named in the same test with the reason it is not played: an ad shows only on Android; the loading example's keys only play a sound, in a run with no sound device; the platform examples are Android's own API, or never run; and the plain C example has none of the seams the driver plays a game through. A new area is one or the other, written down — a decision, not a gap.

The scripts are played side by side, one per processor, the longest first. Each is a sanitized program of a few hundred megabytes, so on a machine that is doing other things, give it fewer at once:

```shell
CMAKE_BUILD_PARALLEL_LEVEL=3 bash tools/examples_build.sh e2e
```

Each script has its own folder and a frame is a sixtieth of a second however busy the machine is, so how many run at once changes how long the step waits and nothing a script draws. In CI the boot is the `examples` job and the scripts are the `e2e` job, side by side.
