Something went wrong. Try again.
Minecraft text rendering inside the browser! www.npmjs.com/package/libmojangles
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267import { createServer, type ViteDevServer } from "vite";import { chromium, type Browser, type Page } from "playwright";import * as path from "path";import * as fs from "fs";import { testCases, DEFAULT_VIEWPORT, DEFAULT_POSITION, type TestCase } from "../cases";import { compareImages } from "./compare";import type { BboxCheck } from "../cases/types";
const ROOT_DIR = path.resolve(__dirname, "../..");const DEVSERVER_DIR = path.join(ROOT_DIR, "devserver");const TESTS_DIR = path.join(ROOT_DIR, "tests");const OUTPUT_DIR = path.join(TESTS_DIR, "visual", "output");
export interface RunnerOptions { headless?: boolean; timeout?: number; renderWaitTime?: number;}
const DEFAULT_OPTIONS: Required<RunnerOptions> = { headless: true, timeout: 30000, renderWaitTime: 2000,};
export class TestRunner { private server: ViteDevServer | null = null; private browser: Browser | null = null; private page: Page | null = null; private options: Required<RunnerOptions>; private port: number = 0;
constructor(options: RunnerOptions = {}) { this.options = { ...DEFAULT_OPTIONS, ...options }; }
async setup(): Promise<void> { this.server = await createServer({ root: DEVSERVER_DIR, server: { port: 0 }, logLevel: "silent", });
await this.server.listen(); const address = this.server.httpServer?.address(); if (address && typeof address === "object") { this.port = address.port; }
this.browser = await chromium.launch({ args: ["--use-gl=swiftshader", "--use-angle=swiftshader"], headless: this.options.headless, }); }
async teardown(): Promise<void> { if (this.page) { await this.page.close(); this.page = null; } if (this.browser) { await this.browser.close(); this.browser = null; } if (this.server) { await this.server.close(); this.server = null; } }
async runTest(testCase: TestCase): Promise<TestResult> { if (!this.browser) throw new Error("Runner not set up");
const viewport = { ...DEFAULT_VIEWPORT, ...testCase.viewport }; const position = { ...DEFAULT_POSITION, ...testCase.position };
this.page = await this.browser.newPage({ viewport: { width: viewport.width, height: viewport.height }, deviceScaleFactor: 1, });
this.page.on("console", (msg) => { if (msg.type() === "error") { console.error(`[${testCase.name}] Browser error:`, msg.text()); } });
this.page.on("pageerror", (err) => { console.error(`[${testCase.name}] Page error:`, err.message); });
await this.page.goto(`http://localhost:${this.port}/test-harness.html`);
await this.page.waitForFunction(() => window.__TEST_READY__ === true, { timeout: this.options.timeout, });
const resourcePackUrls = (testCase.resourcePacks || []).map( (pack) => `http://localhost:${this.port}/test-assets/${pack}` );
const shaderUrls = testCase.shader ? { vertex: testCase.shader.vertex ? `http://localhost:${this.port}/test-assets/${testCase.shader.vertex}` : undefined, fragment: testCase.shader.fragment ? `http://localhost:${this.port}/test-assets/${testCase.shader.fragment}` : undefined, } : undefined;
const config = { text: testCase.text, viewport, position, resourcePackUrls, shaderUrls, uniforms: testCase.uniforms, transformFeedback: testCase.transformFeedback, };
await this.page.evaluate(async (cfg) => { await window.__runTest__(cfg); }, config);
await this.page.waitForTimeout(this.options.renderWaitTime);
const result = await this.captureAndCompare(testCase);
await this.page.close(); this.page = null;
return result; }
private async captureAndCompare(testCase: TestCase): Promise<TestResult> { if (!this.page) throw new Error("No page");
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
const actualPath = path.join(OUTPUT_DIR, `${testCase.name}-actual.png`);
const canvas = await this.page.locator("canvas").first(); const box = await canvas.boundingBox(); if (!box) throw new Error("Canvas not visible");
await this.page.screenshot({ path: actualPath, clip: { x: box.x, y: box.y, width: box.width, height: box.height }, });
if (testCase.expect.type === "image") { const referencePath = path.join(TESTS_DIR, testCase.expect.reference); const diffPath = path.join(OUTPUT_DIR, `${testCase.name}-diff.png`);
const compareResult = await compareImages( actualPath, referencePath, diffPath, { threshold: testCase.expect.threshold ?? 0.1 } );
return { name: testCase.name, passed: compareResult.passed, actualPath, referencePath, diffPath: compareResult.diffImagePath, diffPercent: compareResult.diffPercent, diffPixels: compareResult.diffPixels, totalPixels: compareResult.totalPixels, }; } else if (testCase.expect.type === "pixels") { const pixelResults = await this.checkPixels(testCase.expect.checks); const allPassed = pixelResults.every((r) => r.passed);
return { name: testCase.name, passed: allPassed, actualPath, pixelChecks: pixelResults, }; } else { const bboxResult = await this.checkBboxIds(testCase.expect.ids); return { name: testCase.name, passed: bboxResult.passed, actualPath, bboxChecks: bboxResult.checks, }; } }
private async checkPixels( checks: Array<{ x: number; y: number; rgba: [number, number, number, number]; tolerance?: number }> ): Promise<Array<{ x: number; y: number; expected: number[]; actual: number[]; passed: boolean }>> { if (!this.page) throw new Error("No page");
const results = await this.page.evaluate((checks) => { const canvas = document.getElementById("canvas") as HTMLCanvasElement; const gl = canvas.getContext("webgl2"); if (!gl) throw new Error("No WebGL2 context");
return checks.map((check) => { const pixel = new Uint8Array(4); gl.readPixels(check.x, canvas.height - check.y - 1, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixel);
const tolerance = check.tolerance ?? 0; const passed = Math.abs(pixel[0] - check.rgba[0]) <= tolerance && Math.abs(pixel[1] - check.rgba[1]) <= tolerance && Math.abs(pixel[2] - check.rgba[2]) <= tolerance && Math.abs(pixel[3] - check.rgba[3]) <= tolerance;
return { x: check.x, y: check.y, expected: check.rgba, actual: Array.from(pixel), passed, }; }); }, checks);
return results; }
private async checkBboxIds( expectedIds: Array<string | undefined> ): Promise<{ passed: boolean; checks: BboxCheck[] }> { if (!this.page) throw new Error("No page");
const actualIds = await this.page.evaluate(() => window.__LAST_DRAW_RESULT__?.components.map((component) => component.id) );
const checks = expectedIds.map((expected, index) => { const actual = actualIds?.[index]; const passed = actual === expected; return { index, expected, actual, passed, }; });
return { passed: checks.every((check) => check.passed), checks }; }}
export interface TestResult { name: string; passed: boolean; actualPath?: string; referencePath?: string; diffPath?: string; diffPercent?: number; diffPixels?: number; totalPixels?: number; pixelChecks?: Array<{ x: number; y: number; expected: number[]; actual: number[]; passed: boolean }>; bboxChecks?: BboxCheck[];}
export { testCases };