# Checking in CI

Check the scenarios on every push and PR in your project's repository. The template is [templates/ci/liminalia-check.yml](../templates/ci/liminalia-check.yml).
A template that exports on version tags and distributes to GitHub Pages and itch.io is [templates/ci/liminalia-publish.yml](../templates/ci/liminalia-publish.yml) ("GitHub Pages" in [distribution.md](distribution.md)).
Copy it into `.github/workflows/` in your project's repository. The engine is taken from the engine repository (`skyt-a/liminalia`; if you use a copy of the engine, change `repository:` to its name).
It takes the version tag written in `LIMINALIA_ENGINE_REF` in the template, not the engine's `main` (the template has the engine version of the time, such as `v0.2.0`; [versioning.md](versioning.md)),
so changes to the engine don't suddenly break your project's CI. To use a newer engine, change it there and check that CI passes (a commit works as well as a tag).
`lim new <folder> --ci` creates the project with the check template at `.github/workflows/liminalia-check.yml` (`LIMINALIA_ENGINE_REF` is the tag of that lim's version). A script test (`tests/start.limtest`) is included with the sample story even without `--ci`,
and for a blank project (`--blank`) or one made from a TyranoScript game (`--from-tyrano`), a test that checks the game runs from start to end is added when `--ci` is given.
A project in a language other than Japanese (`--lang en` and so on) gets the English template ([templates/ci/en/liminalia-check.yml](../templates/ci/en/liminalia-check.yml)). Its comments and step names are in English, and `LIMINALIA_LANG: en` makes lim's output English too.

## lim check output for CI

```bash
lim check [project] --github           # also prints diagnostics as GitHub Actions annotations (::error, ::warning, ::notice)
lim check [project] --strict           # treats warnings as failures too (exit code 1)
lim check [project] --github --strict  # the combination recommended for CI
```

- The `file` of an annotation is a path from the current folder (in Actions, the root of the repository). Scenarios are `scenario/…/<name>.lim`,
  and other files are `cast.json`, `lang/en.json` and so on. On the diff screen of a PR, the annotations appear on the lines with problems
- Errors become `::error`, warnings `::warning`, and info (such as an unneeded `@cg-unlock`) `::notice`. The suggested fix (hint) goes on the second line of the annotation's body
- With `--strict` and only warnings, it prints `::error title=lim check::警告が N 件あります（--strict は警告も失敗にします）` at the end and fails
  (with `--lang en`, `::error title=lim check::N warnings (--strict treats warnings as failures)`)
- With `--json`, the annotations follow the JSON (if you only read the JSON, don't add `--github`)
- The check before export (`lim build`) still stops only on errors, as before (`--strict` is for CI)

## What the template does

1. Checks out the project, and the engine repository at the version in `LIMINALIA_ENGINE_REF` (into `.liminalia-engine/`). Until the engine is published on npm, it runs from the repository this way.
   If the engine repository is private, put a token that can read it in secrets and uncomment the `token:` line.
   The project's Git LFS assets are fetched too (`lfs: true`, so that assets in LFS aren't passed to the check and the export as pointer files)
2. `npm ci` (with `ELECTRON_SKIP_BINARY_DOWNLOAD=1`, the Electron binary isn't downloaded)
3. `lim check . --github --strict`, `lim fmt . --check`, `lim coverage . --runs 4`, `lim test . --github` (the script tests; does nothing if there's no `tests/`)
4. Installs Chromium, then exports the browser version with `lim playtest . --rebuild --runs 2` and plays it to the end. If it fails, the screenshot of where it stopped (`dist/playtest/`) is kept as an artifact

## Covering every branch (lim coverage)

`lim coverage` runs the game in two ways.

- Per file: runs every file from the top up to `--runs` times (default 8) with different choices, and reports text coverage and runtime diagnostics
- From the beginning: runs from `entry` in `liminalia.json` to the end, across files, up to `--route-runs` times (default 8). Variables decided in earlier files carry over, so
  branches that depend on choices in earlier chapters are covered too. Commands that end the play (`@title-screen ending=bad title="Silence"` and so on) are treated as endings, and it reports the endings reached (with the kind, name and line, as in `bad「Silence」（start:40）`), the number reached for each kind (`bad 1/2`),
  the ones not reached, and other ways the play ended (the end of a file and so on). If there are diagnostics that only appeared across files, it fails
- With packs (`packs/<id>/scenario`), pack scenarios are run per file too (with all packs laid over the main scripts and that pack last; counted in `--min-lines`),
  and it also runs from the beginning with all packs laid over the main scripts (as in an export), showing whether the endings in pack scenarios are reached (counted in `--require-endings` too; `route.packs` in `--json`)

Both widen out to choices not tried yet. It remembers the choices it ran, and the next run picks again, first a choice not picked yet, then from the shallow (earlier) choices,
a number not tried yet. When it comes to a choice in the same state as in an earlier run (the place, the variables and so on) with a different way of choosing, what follows is the same, so it stops that run.
When every way of choosing has been tried, it stops before the limit, and if it hits the limit before trying them all, it says so ("上限の 8 回で打ち切りました", stopped at the limit of 8 runs, for the runs from the beginning,
and a list of the files it stopped for in the per-file runs; `complete` in `files` and `route.complete` in `--json`). A game with no more ways of choosing than the limit goes through all of them.

For each ending that wasn't reached, it adds the conditions for getting there (the `@if` around that line, and the conditions on the arrows into that scene) and the lines that decide the variables in those conditions,
and points out that `lim reach file:line` can find choices that get there.

Criteria and output for CI:

| Option | What it does |
|---|---|
| `--min-lines 80` | Fails if the share (%) of text reached in the per-file runs is lower than 80 |
| `--require-endings` | Fails if any ending wasn't reached in the runs from the beginning (without it, the endings not reached are only reported) |
| `--json` | Prints the results as JSON (`ok`, `lines`, `files`, `diagnostics`, `missedEndings` in `route` with the clues for them in `missedHints`, and so on) |
| `--github` | Also prints runtime diagnostics and unmet criteria (too little text reached for `--min-lines`, endings not reached) as GitHub Actions annotations |

## Script tests (lim test)

```bash
lim test [project]                       # runs every .limtest under tests/
lim test [project] tests/route.limtest    # only that file (or folder)
lim test [project] --github --json        # failures as annotations on the lines of the test files, results as JSON
```

- Runs tests that write down choices (`choose`) and expectations (`expect`) without a screen, and checks them. How to write them is in "Script tests" in [format.md](design/format.md)
- A file or folder you pass is looked for in the current folder first, then in the project folder (so `lim test my-game tests/route.limtest` works from outside the project). If it's in neither, it stops (exit code 2)
- Fails (exit code 1) if something doesn't match what's expected, there's a runtime error, or a line can't be read. If you pass no paths and there are no tests at all, it says so and passes. If you pass paths and there's no `.limtest` in them, it fails (exit code 1)
- `--github` turns failures into `::error file=tests/….limtest,line=N,title=lim test::…` annotations (on the diff screen of a PR, they appear on the lines of the test)
- Find choices that reach the scene you want with `lim reach`, and put the test it prints into `tests/` as is

## Playing the exported game to the end (lim playtest)

```bash
lim playtest [project]                        # opens and runs the newest browser version (<out>/browser/)
lim playtest [project] dist/browser/my_game --runs 3 --choose 1,0
lim playtest [project] --rebuild --json      # exports the browser version first (skipping the check), with the results as JSON
```

- Opens the exported browser version on a local server (127.0.0.1), starts a new game in Chromium (Playwright, the same one `lim shot` uses), and keeps advancing without stopping.
  An encrypted browser version (`build.browser.encrypt`) also runs, through its Service Worker
- Choices are picked in the order of the numbers in `--choose`. Once those are used up, run k picks option k + n, where n is the number of times it has come back to the same choice (`--runs` sets the number of runs)
- A run ends when the play finishes or returns to the title screen (an ending). If neither the page nor the position advances for `--stuck` seconds (default 20), the run is treated as "stuck", and
  it reports that position (file:line) and a screenshot (`<out>/playtest/run-N.png`). Runs are cut off at `--max-pages` (default 5000) and `--timeout` seconds (default 600)
- It reports page errors, console errors and files that couldn't be loaded (400s). If any run fails to reach the end, or there is an error, the exit code is 1
- It runs as an automated check, so screens that wait for the player and movies go on through the command's `autoplay` ("How automated checks go on" in [plugins.md](plugins.md)).
  `@text-input` keeps its initial value, `@dialog` answers OK, `@open-screen` and `@codex` don't open, and `@movie` doesn't play (even with `skip=false`).
  `@investigate` and project commands that choose a destination with `ctx.pick` turn their list of destinations into choices, picked like any other choice with `--choose` and `--runs`
- Screens of project commands without `autoplay` (minigames and so on) don't advance just by clicking, so they're reported as places where the run got stuck. Add `autoplay` to that command
- `--lang en` prints the results in English (the content of `--json` doesn't change)

```bash
lim playtest [project] --fast --log --shots   # shortens waits, and keeps a page log and a screenshot each time the look changes
lim playtest [project] --avoid-ending        # doesn't pick choices that can only reach an ending of the kind bad, or one named DEAD END or GAME OVER
lim playtest [project] --avoid-ending bad,lost --avoid-choice "Fall asleep,Run away"
```

- `--fast` cuts waits (`setTimeout`) to 1/20 and plays animations with a set number of repeats (CSS ones too) 20 times as fast. Animations that loop forever are left as they are.
  Runs get shorter, but waits for loading get shorter too, so if a run gets stuck, check it again without `--fast`
- `--log` writes one line of JSON per page that waits for a click to `<out>/playtest/run-N.jsonl`: the position (`at`, file:line), the page number (`n`), the name box (`name`), the text (`text`),
  the voice (`voice`), and the state of the stage (`stage`): the background (`bg`, with `cg: true` when it's a background registered as a CG), the `@plane` images (`planes`), the images on image layers (`images`),
  the sprites (`sprites`, character ID → `sprite set/variant@position`), the BGM (`bgm`), looping sound effects (`se`) and the mask (`mask`).
  Choices are `kind: "choice"` (`options`, the picked number `pick`, the avoided ones `avoided`), and the last line is `kind: "end"` (the result and seconds). You can trace later, with `jq` and so on, what was on screen at which line
- `--shots` does what `--log` does and also takes a JPEG screenshot into `<out>/playtest/run-N/` on each page where the background, a `@plane` image, an image or a sprite (including its variant and position) changed,
  and at each choice, after finishing animations that have an end (named `0001_file_line.jpg`, with `_choice` for choices; the log's `shot` has the name).
  The previous run's `run-N/` and `run-N.jsonl` are removed first. Taking screenshots takes time, so raise `--timeout` for long games
- `--avoid-ending [kinds,…]` doesn't pick a choice that can only reach endings of those kinds when there is another choice to pick. The kind is `ending=` of a command that ends the play
  (`@title-screen ending=bad`, including kinds the project adds), without regard to case. A kind's wording (`BAD END`) is read as the kind too. With text that isn't a kind, endings whose name (`title=`)
  contains that text are avoided. Without a value, it avoids the kind `bad` and endings whose name contains `DEAD END`, `GAME OVER`, `デッドエンド` or `ゲームオーバー`
  (these four aren't the wording of a kind, so `lim migrate` moves them to the kind `other` with that name. A kind the project adds is avoided too when its wording is one of these names).
  It goes by the original script, so a translated project avoids the same endings whatever language it's played in. At the start, it lists the endings it avoids and their scenes (`file#label`), and says so when nothing matches.
  It follows the flowchart made from the scripts, so a scene with another ending after a condition isn't avoided (avoid endings decided by variables with `--avoid-choice`)
- `--avoid-choice text,…` doesn't pick a choice containing that text when there is another choice to pick
- Neither changes choices decided with `--choose`. They can also be written in the game's settings (`playtest` in `liminalia.json`, used together with `--avoid-…`):

```json
{ "playtest": { "avoidEndings": ["bad"], "avoidChoices": ["Fall asleep"] } }
```

## Recording at real speed (lim shot --video)

```bash
lim shot [project] chapter1 --skip 10 --video clip.mp4              # records 20 pages from page 10, advancing every 1.8 seconds
lim shot [project] chapter1#night --video --pages 40 --read 2500 --choose 1,North road
lim shot [project] start --video recording --split file --avoid-ending   # records from the start to the end, one mp4 per file
lim shot [project] start --video recording --split file --resume         # goes on from where it stopped
lim shot [project] start --video recording --split file --until chapter3  # stops at chapter3; go on later with --resume (and a later --until)
lim shot [project] start --video recording --split file --redo 2         # records only the second part (002_….mp4) again
lim shot [project] start --video recording --split file --auto           # advances with the same timing as a player using auto
```

- Like `lim shot`, it starts the development server itself and records from the click wait at the starting position (`--skip N` is the `[N]` in `lim trace`). Effects and text play at the game's own speed,
  and it advances `--read` milliseconds (default 1800) after the text has finished showing. If a voice is playing, it waits until the voice ends.
  It stops after reading `--pages` pages (default 20; `all` for the whole game), or when the play ends. With `--split` or `--until`, `--pages` defaults to `all`
- `--auto` leaves advancing the text to the engine's auto (the same as `@auto start` and “Auto” in the menu). After the text and the voice finish, it waits the project's auto wait
  (`ui.defaults.autoWait` in `liminalia.json`, 1500 milliseconds if not written, plus `ui.settings.autoPerChar` per character, 25 milliseconds by default) before advancing, so the timing and the look (the auto mark) are the same as when a player turns on auto.
  With both `--auto` and `--read`, `--auto` decides when the text advances, and `--read` is only used for how long choices and choice screens are shown before picking
  (auto turns off at choices, so it's turned on again at the next click wait). If a system screen replaced by the project doesn't take auto, it advances with `--read`
- Up to the starting position it goes on without screens like automated checks (the commands' `autoplay`); from there it keeps the player's screens. `@movie` plays to the end, even one that can be skipped (`skip=true`, or one already watched),
  and screens that wait for the player (choices, `@investigate`, `@map-choice`, `@text-input`, `@dialog`, `@codex`, `@open-screen`, `@item-select`
  and the project's plugin screens) stay on screen for `--read`, then it picks (a plugin screen only when its renderer tells what can be chosen;
  see “How recordings go on” in [plugins.md](plugins.md)). `@text-input` keeps its initial value, `@dialog` answers OK, and `@codex`, `@open-screen` and `@item-select` are closed
- It picks, in order, the numbers in `--choose` (from 0) or the one containing that text; once those are used up, like the first run of `lim playtest`,
  it picks the next one each time it comes back to the same choice (the first one that can be pressed the first time, the second one the second time and so on), so it doesn't keep picking the same one in a loop.
  `--avoid-ending [kind,…]` and `--avoid-choice text,…` (and `playtest.avoidEndings` and `avoidChoices` in `liminalia.json`) work as in `lim playtest`:
  if there's something else to pick, it doesn't pick a choice that can only lead to an avoided ending or one containing that text (choices set with `--choose` aren't changed)
- The sound (music, sound effects, voices and the sound of movies) is recorded too. It copies the engine's sound output (after the overall volume) inside the page and lays it over the video at the time it played (off by about a frame at most).
  The recording browser doesn't play the sound through your speakers. `--no-audio` leaves the sound out
- Frames and sound go straight into ffmpeg as they're recorded (no frames are stored in a temporary folder). While writing, the mp4 is fragmented, so what was recorded can be watched even if it stops halfway.
  When a file is finished, it's rewritten as a single mp4 (30fps, H.264 or MPEG-4 if that isn't available, AAC sound) if there's room. Without ffmpeg, it stops before starting and says how to install it (exit code 2)
- Before starting, it shows how long and how large the recording is likely to be (from the pages and text of the scenes reachable from the starting position in the scenario map;
  every branch is counted, so it's on the high side) and the free space. If the free space is less than that plus 512MB, it doesn't start (shorten it with `--pages` or `--until`, or make room).
  If the free space falls below 512MB while recording, it writes what it has and stops
- `--split file` writes one file per scenario file and `--split chapter` one per chapter (`chapters` in `liminalia.json`; files in no chapter are split per file)
  as `folder/<3-digit number>_<file or chapter id>.mp4` (`--video` without a value writes to the `video` folder). Splits are decided by the position in a file's body, not inside calls or macros
- `--until file[#label]` stops when it gets there (for a label, past that line), without recording it
- With `--split`, it writes how far it got, and saves at the first page of each part and at the last wait (click wait or choice screen) before the part (with `temp` variables, which aren't in saves),
  to `folder/liminalia-video.json`. Even when it stops halfway (Ctrl+C, stuck, low on space), what was recorded stays, and `--resume` records the unfinished part again and goes on.
  When resuming, the parts to record again and the original record are backed up to a `resume-backup-…` folder just before recording starts, and then deleted. If the backup can't be made, it stops without changing the original files. When the rest is recorded, the backup is removed; if it stops before that, the backup is kept and its location is shown.
  It goes on without recording from the last wait before the part and starts recording when it enters the part, so the scene change and other effects at the start of the part are recorded
  as in an uninterrupted recording (the progress of the choices, the page count and `temp` variables carry over). The first part of a recording, and records made by an earlier version,
  are recorded again from their first page (or choice screen), without the effects at the start of the part. Recording into a folder that has a record without `--resume` stops (delete the folder to record again)
- When it stops at `--until` or `--pages`, it goes on without recording to the next click wait (or choice screen), takes a save (and `temp` variables) there and writes it to the record as the next part. `--resume` records from there
  and doesn't record finished parts again (if it stopped partway through a part, the rest becomes an mp4 with the same name and the next number). To go on, move `--until` further or remove it, and raise `--pages` or remove it
  (if they stay the same, it stops, saying there's nothing to record). A recording only counts as finished when the play ends
- `--redo number` records one recorded part again from its first page (if there's a wait before the part, it goes on from there without recording to the start of the part) and stops when it enters the next part.
  The other parts' mp4 files, and whether the record is finished and where it goes on, stay as they are. Use it to record again only the scenes whose script you fixed (not used with `--resume`)
- If for `--stuck` seconds (default 60) neither the page, the position nor the choice screen changes, and no movie, voice or effect with an end is playing, it counts as stuck and stops
  (while an `@movie` plays, even one that can be skipped, or a long effect goes on, doesn't count). It shows where it stopped (file:line). If it stops at a plugin screen, give that screen “How recordings go on”
- The exit code is 0 when it stops at `--pages`, at the end of the play or at `--until`, and 1 when it was stuck, stopped halfway, ran low on space or hit an error while recording
- With `--line`, the top left of the screen shows the script line being run (`file.lim:line` and how that line is written). It's only shown on the recorded page, never on a player's screen (the position shown at the top right during development is hidden in recordings)
- As a rough guide, 1280×720 takes about 0.5 to 1GB an hour (more when scenes with a lot of motion go on). For games of dozens of hours, split with `--split` and go on with `--resume` while watching the free space
- The estimate, progress and closing output are in English when the language being recorded (`--lang`, or the game's original language if it's left out) isn't Japanese
