Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ on:
branches:
- main
workflow_dispatch:
inputs:
update-snapshots:
description: Regenerate the graphics e2e reference screenshots (uploaded as the graphics-e2e artifact)
type: boolean
default: false

jobs:
tests:
Expand All @@ -24,3 +29,29 @@ jobs:

- name: "Run linter"
run: pnpm lint

graphics:
# Pinned: the reference screenshots are rendered by this OS's Chromium and SwiftShader.
runs-on: ubuntu-24.04
steps:
- name: Checkout repository
uses: actions/checkout@v7

- name: Prepare
uses: ./.github/actions/prepare

- name: Install Chromium
run: pnpm exec playwright install --with-deps chromium

- name: Run graphics e2e tests
run: pnpm test:e2e:graphics ${{ inputs.update-snapshots && '--update-snapshots' || '' }}

- name: Upload report and screenshots
if: always()
uses: actions/upload-artifact@v7
with:
name: graphics-e2e
path: |
playwright-report/
test-results/
e2e/graphics/*-snapshots/
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,6 @@ emsdk-cache/
references/
.direnv

packages/old-core/
packages/old-core/
playwright-report/
test-results/
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ CHANGELOG.md

emsdk-cache/
docs/api

playwright-report/
test-results/
1 change: 1 addition & 0 deletions e2e/game-2d/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.nanoforge/
5 changes: 5 additions & 0 deletions e2e/game-2d/apps/client/nanoforge.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { defineConfig } from "@nanoforge-dev/config";

export default defineConfig({
type: "client",
});
14 changes: 14 additions & 0 deletions e2e/game-2d/apps/client/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "e2e-game-2d-client",
"version": "0.0.0",
"type": "module",
"devDependencies": {
"@nanoforge-dev/common": "workspace:*",
"@nanoforge-dev/core": "workspace:*",
"@nanoforge-dev/ecs": "workspace:*",
"@nanoforge-dev/graphics-2d": "workspace:*",
"@nanoforge-dev/input": "workspace:*",
"typescript": "catalog:core"
},
"private": true
}
60 changes: 60 additions & 0 deletions e2e/game-2d/apps/client/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import type { ClientRunOptions, Context } from "@nanoforge-dev/common";
import { NanoforgeFactory } from "@nanoforge-dev/core";
import { EcsLibrary, type Registry } from "@nanoforge-dev/ecs/client";
import { Circle, Graphics2DLibrary, Rect } from "@nanoforge-dev/graphics-2d";
import { InputEnum, InputLibrary } from "@nanoforge-dev/input";

/** Read by the Playwright tests in `e2e/graphics`. */
export interface E2EState {
tick: number;
player: { x: number; y: number };
}

class Player {
name = "Player";

constructor(public shape: Rect) {}
}

// Fixed step per tick, not per second: screenshots and positions must not depend on frame timing.
const SPEED = 5;

export const main = async (options: ClientRunOptions): Promise<void> => {
const app = NanoforgeFactory.createClient({
tickRate: 60,
viewport: { width: 800, height: 600, fit: "contain" },
});
const graphics = new Graphics2DLibrary();
const ecs = new EcsLibrary();
const input = new InputLibrary();
app.use(graphics);
app.use(ecs);
app.use(input);

await app.init(options);

const layer = graphics.baseLayer;
layer.add(new Rect({ x: 0, y: 0, width: 800, height: 600, fill: "#1e1e2e" }));
layer.add(new Circle({ x: 600, y: 300, radius: 80, fill: "#00ff00" }));
const square = new Rect({ x: 100, y: 250, width: 100, height: 100, fill: "#ff0000" });
layer.add(square);

const registry = ecs.registry;
registry.addComponent(registry.spawnEntity(), new Player(square));

const state: E2EState = { tick: 0, player: { x: square.x(), y: square.y() } };
(window as unknown as { __e2e: E2EState }).__e2e = state;

registry.addSystem((registry: Registry, ctx: Context) => {
registry.getZipper([Player]).forEach(({ Player }) => {
const dx =
(ctx.input.isKeyPressed(InputEnum.ArrowRight) ? SPEED : 0) -
(ctx.input.isKeyPressed(InputEnum.ArrowLeft) ? SPEED : 0);
Player.shape.x(Player.shape.x() + dx);
state.player = { x: Player.shape.x(), y: Player.shape.y() };
});
state.tick++;
});

await app.run();
};
6 changes: 6 additions & 0 deletions e2e/game-2d/nanoforge.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from "@nanoforge-dev/config";

export default defineConfig({
type: "workspace",
packages: ["apps/*"],
});
19 changes: 19 additions & 0 deletions e2e/game-2d/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"$schema": "https://json.schemastore.org/package.json",
"name": "@nanoforge-dev/e2e-game-2d",
"version": "0.0.0",
"description": "NanoForge Engine - End-to-End test game for graphics-2d",
"type": "module",
"scripts": {
"start": "nf start"
},
"devDependencies": {
"@nanoforge-dev/cli": "^2.3.0",
"@nanoforge-dev/config": "catalog:config",
"typescript": "catalog:core"
},
"private": true,
"engines": {
"node": "26"
}
}
6 changes: 6 additions & 0 deletions e2e/game-2d/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "https://json.schemastore.org/tsconfig.json",
"extends": "../../tsconfig.json",
"include": ["apps/*/src/**/*.ts"],
"exclude": ["node_modules", "**/dist", ".nanoforge"]
}
1 change: 1 addition & 0 deletions e2e/game-3d/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.nanoforge/
5 changes: 5 additions & 0 deletions e2e/game-3d/apps/client/nanoforge.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { defineConfig } from "@nanoforge-dev/config";

export default defineConfig({
type: "client",
});
16 changes: 16 additions & 0 deletions e2e/game-3d/apps/client/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "e2e-game-3d-client",
"version": "0.0.0",
"type": "module",
"devDependencies": {
"@nanoforge-dev/common": "workspace:*",
"@nanoforge-dev/core": "workspace:*",
"@nanoforge-dev/ecs": "workspace:*",
"@nanoforge-dev/graphics-3d": "workspace:*",
"@nanoforge-dev/input": "workspace:*",
"@types/three": "catalog:graphics",
"three": "catalog:graphics",
"typescript": "catalog:core"
},
"private": true
}
82 changes: 82 additions & 0 deletions e2e/game-3d/apps/client/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import type { ClientRunOptions, Context } from "@nanoforge-dev/common";
import { NanoforgeFactory } from "@nanoforge-dev/core";
import { EcsLibrary, type Registry } from "@nanoforge-dev/ecs/client";
import { type Graphics3DBackend, Graphics3DLibrary } from "@nanoforge-dev/graphics-3d";
import {
BoxGeometry,
Color,
DirectionalLight,
HemisphereLight,
Mesh,
MeshStandardMaterial,
type Object3D,
} from "@nanoforge-dev/graphics-3d/three";
import { InputEnum, InputLibrary } from "@nanoforge-dev/input";

/** Read by the Playwright tests in `e2e/graphics`. */
export interface E2EState {
tick: number;
backend: Graphics3DBackend;
cube: { rotationY: number };
}

class Model {
name = "Model";

constructor(public object: Object3D) {}
}

// Fixed step per tick, not per second: screenshots and rotations must not depend on frame timing.
const TURN = 0.05;

export const main = async (options: ClientRunOptions): Promise<void> => {
const app = NanoforgeFactory.createClient({
tickRate: 60,
viewport: { width: 800, height: 600, fit: "contain" },
});
// CI browsers have no GPU: WebGL2 runs on SwiftShader there, WebGPU does not run reliably.
const graphics = new Graphics3DLibrary({ forceWebGL: true });
const ecs = new EcsLibrary();
const input = new InputLibrary();
app.use(graphics);
app.use(ecs);
app.use(input);

await app.init(options);

const { scene, camera } = graphics;
scene.background = new Color("#20232a");
camera.position.set(0, 1.5, 4);
camera.lookAt(0, 0, 0);

const sun = new DirectionalLight("white", 3);
sun.position.set(3, 5, 4);
scene.add(new HemisphereLight("white", "#445566", 1), sun);

const mesh = new Mesh(new BoxGeometry(1, 1, 1), new MeshStandardMaterial({ color: "#44aa88" }));
mesh.rotation.set(0.4, 0.6, 0);
scene.add(mesh);

const registry = ecs.registry;
registry.addComponent(registry.spawnEntity(), new Model(mesh));

const state: E2EState = {
tick: 0,
backend: graphics.backend,
cube: { rotationY: mesh.rotation.y },
};
(window as unknown as { __e2e: E2EState }).__e2e = state;

registry.addSystem((registry: Registry, ctx: Context) => {
registry.getZipper([Model]).forEach(({ Model }) => {
const turn =
(ctx.input.isKeyPressed(InputEnum.ArrowRight) ? TURN : 0) -
(ctx.input.isKeyPressed(InputEnum.ArrowLeft) ? TURN : 0);
Model.object.rotation.y += turn;
state.cube = { rotationY: Model.object.rotation.y };
});
state.tick++;
});

await app.run();
};
6 changes: 6 additions & 0 deletions e2e/game-3d/nanoforge.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from "@nanoforge-dev/config";

export default defineConfig({
type: "workspace",
packages: ["apps/*"],
});
19 changes: 19 additions & 0 deletions e2e/game-3d/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"$schema": "https://json.schemastore.org/package.json",
"name": "@nanoforge-dev/e2e-game-3d",
"version": "0.0.0",
"description": "NanoForge Engine - End-to-End test game for graphics-3d",
"type": "module",
"scripts": {
"start": "nf start"
},
"devDependencies": {
"@nanoforge-dev/cli": "^2.3.0",
"@nanoforge-dev/config": "catalog:config",
"typescript": "catalog:core"
},
"private": true,
"engines": {
"node": "26"
}
}
6 changes: 6 additions & 0 deletions e2e/game-3d/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "https://json.schemastore.org/tsconfig.json",
"extends": "../../tsconfig.json",
"include": ["apps/*/src/**/*.ts"],
"exclude": ["node_modules", "**/dist", ".nanoforge"]
}
43 changes: 43 additions & 0 deletions e2e/graphics/game-2d.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { expect, test } from "@playwright/test";

import type { E2EState } from "../game-2d/apps/client/src/main";
import { expectColor, gameArea, openGame, pixelAt, readState, waitTicks } from "./helpers";

test("boots without errors and keeps ticking", async ({ page }) => {
const errors = await openGame(page);
await waitTicks(page, 30);
await expect(gameArea(page).locator("canvas").first()).toBeVisible();
expect(errors).toEqual([]);
});

test("moves the player while an arrow key is held", async ({ page }) => {
await openGame(page);
await waitTicks(page, 1);
const before = (await readState<E2EState>(page)).player;

await page.keyboard.down("ArrowRight");
await waitTicks(page, 10);
await page.keyboard.up("ArrowRight");
await waitTicks(page, 2);
const after = (await readState<E2EState>(page)).player;
expect(after.x).toBeGreaterThan(before.x);
expect(after.y).toBe(before.y);

await waitTicks(page, 10);
expect((await readState<E2EState>(page)).player).toEqual(after);
});

test("draws the shapes at their positions", async ({ page }) => {
await openGame(page);
await waitTicks(page, 5);
const png = await gameArea(page).screenshot();
expectColor(await pixelAt(page, png, 20, 20), [0x1e, 0x1e, 0x2e]); // background
expectColor(await pixelAt(page, png, 150, 300), [0xff, 0x00, 0x00]); // player square
expectColor(await pixelAt(page, png, 600, 300), [0x00, 0xff, 0x00]); // circle
});

test("matches the reference screenshot", async ({ page }) => {
await openGame(page);
await waitTicks(page, 5);
await expect(gameArea(page)).toHaveScreenshot("scene.png");
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading