Testing the Engine
v2’s tests are Cypress specs: each one opens the starter game, dist/index.html, in a real browser, replaces its script with a few statements, plays them, and checks the page and the engine’s state. There is no unit-test suite and no test harness; a spec tests the built engine in dist/engine/core/, so rebuild before you run one.
Run the specs
From the repository’s root, after bun install (Setting Up the Repository):
bun run test # every spec
bun run test --spec cypress/e2e/actions/dialog.spec.js # one spec
bun run test --spec 'cypress/e2e/actions/show_scene.spec.js,cypress/e2e/components/load-screen.spec.js'
bun run test --browser firefox --spec cypress/e2e/actions/dialog.spec.js # in another browser
bun run test:open # the interactive runner, on a machine with a displaybun run test runs scripts/cypress.ts run, and bun run test:open runs scripts/cypress.ts open. The script passes every argument to Cypress and adds the browser: Google Chrome, which CI uses, or Chromium when Chrome isn’t installed (it says so), never the Electron that Cypress ships with, which can’t take screenshots on machines without a GPU. A --browser argument (chromium, chrome, firefox…; bunx cypress info lists what Cypress finds) or the MONOGATARI_CYPRESS_BROWSER environment variable chooses another. bunx cypress run --browser chromium … works too. On a machine without a display, Cypress starts its own virtual one; when several runs share the machine, give each its own with xvfb-run -a bun run test ….
A spec takes from 2 seconds (next.spec.js) to a minute (dialog.spec.js); the whole suite took about seven minutes here. When a test fails, Cypress saves a screenshot of the page in cypress/screenshots/ (Git ignores the folder) and prints the failing assertion with the spec’s line number.
Specs load dist/index.html, which loads dist/engine/core/monogatari.js and .css. After you change src/, rebuild what you changed (bun build.ts --browser, bun build.ts --css) or the specs test the old engine.
How a spec runs
cypress.config.js has no baseUrl, so Cypress serves the repository’s folder from its own server and cy.visit('./dist/index.html') opens the starter game from it, over HTTP. The game boots as it would for a player: its js/options.js, js/storage.js, js/script.js and js/main.js run, and so does the debug module. Then the spec replaces what it needs:
context('Show Image', () => {
beforeEach(() => {
cy.open();
cy.loadTestAssets();
});
it('Displays the image correctly', function() {
this.monogatari.setting('TypeAnimation', false);
this.monogatari.script({
'Start': [
'show image polaroid with fadeIn',
'One',
],
});
cy.start();
cy.get('[data-image="polaroid"]').should('be.visible');
cy.wrap(this.monogatari).invoke('state', 'images').should('deep.equal', ['show image polaroid with fadeIn']);
cy.wrap(this.monogatari).invoke('history', 'image').should('deep.equal', ['show image polaroid with fadeIn']);
cy.get('text-box').contains('One');
});
});(The first test of cypress/e2e/actions/show_image.spec.js.)
Four things about this environment are worth knowing before you write a spec:
- The engine knows it’s under Cypress.
init()checkswindow.Cypressand turnsExperimentalFeatureson, so the experimentalshow character layerandhide character layeractions work in specs but not in a game that doesn’t opt in. - Uncaught errors don’t fail a test.
cypress/support/e2e.jsreturnsfalsefromCypress.on('uncaught:exception', …). An engine exception only shows up as an assertion that times out afterwards, or in the browser’s console inbunx cypress open. - The debug module is loaded and the level is 5, so the engine’s error dialogs appear. A dialog is a
.modal, andshouldProceed()refuses to move the game while a modal is visible: a spec that triggers an error dialog stalls at its nextcy.proceed(). - The art and sound come from the network.
cy.loadTestAssets()pointsAssetsPath.rootathttps://datadyne.hyuchia.com/monogatari/assets. Without that host, specs that wait for images or audio time out.
The custom commands
cypress/support/commands.js defines the commands every spec uses. They’re written with function, not arrow functions, because they read the alias this.monogatari that cy.open() sets:
| Command | What it does |
|---|---|
cy.open() |
visits ./dist/index.html, aliases window.Monogatari.default as monogatari, and sets the debug level to 5 |
cy.loadTestAssets({ nvl }) |
sets AssetsPath to the test server and ExperimentalFeatures to true, declares the test videos, images, scenes, music, sounds and voices, two particle presets, three canvas objects, the characters y (Yui, with layers), yd1, yd2 and m (Mio), and a preload block named default. nvl: true makes the characters NVL ones |
cy.start() |
clicks [data-action="start"] and waits 150 ms |
cy.proceed() |
calls monogatari.proceed({ userInitiated: true, skip: false, autoPlay: false }) and waits 150 ms |
cy.rollback() |
calls monogatari.rollback() and waits 150 ms |
cy.clearStorage() |
calls monogatari.Storage.clear() and waits 150 ms |
cy.save(slot) |
returns monogatari.saveTo('SaveLabel', slot) |
cy.load(slot) |
loads Save_<slot>, then runs the statement at the loaded step, as the load screen does |
cy.proceed() calls the engine and skips the click; to test the click itself, click the element (cy.get('game-screen').click()).
Where the tests live
| Spec | Tests | What it covers |
|---|---|---|
accessibility.spec.js |
9 | names for buttons and sliders, sprite descriptions, the live region that reads new lines |
autosave.spec.js |
4 | autosaves: which slot, when nothing is saved, the slot names |
error_dialogs.spec.js |
9 | the error dialog: one per mistake, readable details, a typo in a game file, errors after the page loaded |
file_system_storage.spec.js |
3 | the FileSystem storage through a stand-in desktop bridge: saving, loading, an answer that never comes |
main_menu_after_load.spec.js |
1 | the main menu shows once the starter game has loaded (it visits the page without cy.open()) |
orientation.spec.js |
3 | the rotate message and its OK button |
storage.spec.js |
14 | opening the storage: upgrade(), a lowered Version, a changed Store, names without Latin letters, another tab |
style.spec.js |
7 | modal widths, quick menu icon size, the colour variables, Kayros’ margin |
actions/action.spec.js |
2 | registering actions: loadingOrder sorted as numbers |
actions/choices.spec.js |
23 | Choice: Clickable, Do, onChosen, the choice history, timers, nesting, NVL, every option hidden, a missing Do, going back from the Do’s line, loading a save made there |
actions/clear.spec.js |
4 | clear |
actions/conditionals.spec.js |
18 | Conditional: branches by boolean, name and number, missing branches, its error dialogs, the conditional history, rollback, nesting |
actions/dialog.spec.js |
67 | Dialog: characters, narrator, NVL, centered text, typing markers and every text effect |
actions/end.spec.js |
1 | end: the storage goes back to what js/storage.js declares |
actions/functions.spec.js |
8 | plain function statements: advancing, stopping on false, no rollback, error dialogs |
actions/gallery.spec.js |
6 | gallery unlock, gallery lock and going back over them |
actions/hide_canvas.spec.js |
8 | hide canvas |
actions/hide_character.spec.js |
10 | hide character |
actions/hide_image.spec.js |
10 | hide image |
actions/hide_particles.spec.js |
3 | hide particles |
actions/hide_textbox.spec.js |
12 | hide textbox |
actions/hide_video.spec.js |
12 | hide video |
actions/input.spec.js |
15 | Input |
actions/jump.spec.js |
4 | jump: a missing or empty label, an endless loop of jumps |
actions/message.spec.js |
4 | show message: plain text and translated labels, a language change |
actions/next.spec.js |
2 | next |
actions/notification.spec.js |
10 | show notification, with the browser’s notifications replaced by the spec |
actions/pause.spec.js |
6 | pause |
actions/play.spec.js |
30 | play music, play sound, play voice: loop, volume, fade, resume, loading |
actions/preload.spec.js |
19 | preload |
actions/reversible_functions.spec.js |
10 | { 'Function': { Apply, Revert } }, and Reverse after a load |
actions/show_background.spec.js |
6 | show background and show scene with a color and an animation, their classes, going back past the first one, duration |
actions/show_canvas.spec.js |
16 | show canvas |
actions/show_character.spec.js |
21 | show character: sprites, positions, the move transition, rollback, loading |
actions/show_character_layer.spec.js |
9 | show character layer (experimental) |
actions/show_image.spec.js |
9 | show image |
actions/show_particles.spec.js |
6 | show particles |
actions/show_scene.spec.js |
3 | show scene, its rollback and loading |
actions/show_textbox.spec.js |
11 | show textbox |
actions/show_video.spec.js |
28 | show video in each mode |
actions/stop.spec.js |
10 | stop music, stop sound, stop voice |
actions/unload.spec.js |
11 | unload |
actions/vibrate.spec.js |
5 | vibrate, with and without the browser’s vibration |
actions/wait.spec.js |
6 | wait |
components/alert-modal.spec.js |
3 | alerts: plain text labels, alerts before the game starts, which alert a button closes |
components/centered-dialog.spec.js |
1 | centered lines and their colors |
components/component.spec.js |
4 | Component.get(), instance(), registering a tag twice, setup() receiving the selector |
components/credits-screen.spec.js |
3 | the credits screen: its stylesheet, text sections, variables shown as text |
components/dialog-log-scroll.spec.js |
4 | the dialog log’s scrolling |
components/dialog-log.spec.js |
1 | the dialog log: the narrator’s lines dimmed |
components/gallery-screen.spec.js |
1 | gallery cards with a size |
components/game-screen.spec.js |
5 | clicks on the game screen and on data-action buttons inside it |
components/help-screen.spec.js |
3 | the help screen’s keys: no A, no Skip or Back when they’re off |
components/load-screen.spec.js |
9 | the load screen and its slots |
components/loading-screen.spec.js |
1 | the loading bar fills |
components/main-menu.spec.js |
7 | menu helpers before init(), a menu’s template, configuration() redrawing, plain-text labels |
components/save-screen-screenshots.spec.js |
6 | save slot screenshots |
components/save-screen.spec.js |
6 | the save screen |
components/screens.spec.js |
3 | opening a screen that isn’t in the page |
components/settings-screen-desktop.spec.js |
3 | the window sizes the settings list in a desktop app (with a stand-in bridge) |
components/settings-screen.spec.js |
4 | the settings screen: volume sliders and Text Speed from the player’s preferences, the slider’s direction |
components/text-box-scroll.spec.js |
5 | the text box’s scrolling |
components/text-box.spec.js |
5 | the text box: rtl, the side-image classes, right to left pages |
components/type-writer.spec.js |
10 | the type writer: effects on lines shown at once, untyped letters, icons, didStartTyping, TextSpeed, the cursor, markers |
engine/api.spec.js |
4 | configuration(), onSave(), fn() |
engine/failed-statements.spec.js |
17 | a statement that fails: its dialog, the next click, going back over it |
engine/proceed-and-rollback.spec.js |
3 | two proceeds or rollbacks at the same moment |
engine/skip-and-auto-play.spec.js |
6 | the Skip button and key, stopping auto-play |
engine/translations.spec.js |
21 | translations: missing variables, values shown as text or, with AllowHTMLInVariables, as HTML, _(Key) markers, translations(), the page’s lang and dir, falling back to English |
69 specs, 610 tests, in about eleven minutes. All paths are under cypress/e2e/. No spec covers the Placeholder action. The desktop hosts themselves are checked by running them (The Starter Game and Desktop Hosts); the specs reach them only through a stand-in bridge.
Known failures
None on develop: the whole suite passes. A few specs load their pictures, sounds and videos from datadyne.hyuchia.com, and a test that waits for them can time out when that host is slow (“Plays multiple sounds simultaneously” in actions/play.spec.js is the usual one). Run that spec again on its own; if it passes, the failure was the network.
On the v2.8.1 tag, two tests fail: “Show Scene: Shows the correct background on when both scene and background are used” and “Load Screen: Gives priority to the last known background”. Both expected the test assets on datadyne.perfectdark.space, the host cypress/support/commands.js used before commit 4b5fff30, and the load screen test also broke when the starter game turned save screenshots on in commit f6bf8beb. 2.9.0 fixed both. Stage 1 of the learning path has you find out why on the tag.
Write a spec for a change
Put the test in the spec of the action or component it’s about, in a context named after it, or in a new cypress/e2e/actions/<action>.spec.js for a new action. Each test:
- sets
TypeAnimationtofalse, unless typing is what it tests, so lines appear at once; - replaces the script with the fewest statements that show the behavior;
- plays them with
cy.start()andcy.proceed(), and checks what the player sees (cy.get(…)) and what the engine recorded (cy.wrap(this.monogatari).invoke('state', 'images'),invoke('history', 'image')); - for anything shown, goes back with
cy.rollback()and checks it’s undone, and saves and loads withcy.save(1)andcy.load(1)and checks it’s rebuilt.
Before you trust a test, see it fail: run it before your fix, or break the code on purpose. Stage 4 of the learning path writes a complete spec this way.
To look at a failing test, run it with bunx cypress open and watch the page; put .only on the test (it.only (…)) to run it alone, and cy.pause () before the step you want to inspect.
What CI runs
Two GitHub workflows run on pushes and pull requests to develop:
| Workflow | Steps | Runs when the change touches |
|---|---|---|
.github/workflows/lint.yml |
bun install, bun run lint, bun run check |
.ts, .js, .json or .css files outside dist/ and cypress/ |
.github/workflows/test.yml |
bun install --frozen-lockfile, bun run build, the bundle check, bunx cypress run --browser chrome (with --record when the record key is there) |
.ts, .js, .json, .css or .html files anywhere, the starter game in dist/ included |
The lint workflow installs the latest Bun. The test workflow pins Bun 1.4.4, the version the committed bundles are built with, because another version can write different bundles. When you move to a newer Bun, rebuild, commit the bundles and change the version in test.yml together.
The bundle check. After building, the test workflow runs git status --porcelain on dist/engine/core/, dist/engine/debug/, dist/types/ and lib/, and fails with The committed bundles don’t match src/ when it lists anything: a branch that changed src/ without committing the rebuilt files. Run bun run build and commit what it changes. Until 2.9.0 no workflow checked this, and such a branch passed CI and shipped stale files.
The test workflow records to Cypress Cloud (the project id in cypress.config.js, with the CYPRESS_RECORD_KEY secret); the badge at the top of README.md shows those runs. A pull request from a fork has no access to the secret, and its specs run unrecorded. No server is started: with no baseUrl, Cypress serves the files itself (the specs visit ./dist/index.html).