diff --git a/devserver/src/test-runner.ts b/devserver/src/test-runner.ts index 61ebf2a..abca38e 100644 --- a/devserver/src/test-runner.ts +++ b/devserver/src/test-runner.ts @@ -23,6 +23,7 @@ declare global { __TEST_CONFIG__?: TestConfig; __TEST_READY__?: boolean; __TEST_ERROR__?: string; + __LAST_DRAW_RESULT__?: { components: Array<{ id?: string; bbox: { min: [number, number]; max: [number, number] } }> }; __runTest__: (config: TestConfig) => Promise; } } @@ -127,8 +128,9 @@ async function runTest(config: TestConfig): Promise { screenX = position.x * viewport.guiScale; } - lib.drawText(text, screenX, screenY, { + window.__LAST_DRAW_RESULT__ = lib.drawText(text, screenX, screenY, { scale: viewport.guiScale, + cachePicking: true, ...(programId ? { programId } : {}), }); diff --git a/package.json b/package.json index 6e641c7..0815952 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "libmojangles", "type": "module", - "version": "1.3.1", + "version": "1.4.0", "module": "dist/index.js", "types": "dist/index.d.ts", "exports": { diff --git a/src/text/parser.ts b/src/text/parser.ts index 1fe05df..513aee6 100644 --- a/src/text/parser.ts +++ b/src/text/parser.ts @@ -208,20 +208,27 @@ export class TextParserImpl implements TextParser { private collectComponentRanges( component: TextComponent, - startIndex: number + startIndex: number, + inheritedId?: string ): ComponentRange[] { const ranges: ComponentRange[] = []; if (typeof component === "string") { - return [{ sourceRange: [startIndex, startIndex + component.length] }]; + return [ + { + id: inheritedId, + sourceRange: [startIndex, startIndex + component.length], + }, + ]; } let currentIndex = startIndex; + const componentId = component.id ?? inheritedId; if (component.text && component.text.length > 0) { const endIndex = currentIndex + component.text.length; if (endIndex > currentIndex) { ranges.push({ - id: component.id, + id: componentId, sourceRange: [currentIndex, endIndex], }); } @@ -229,8 +236,10 @@ export class TextParserImpl implements TextParser { } if (component.extra) { - for (const extra of component.extra) { - const childRanges = this.collectComponentRanges(extra, currentIndex); + for (let index = 0; index < component.extra.length; index++) { + const extra = component.extra[index]!; + const childId = componentId ? `${componentId}/extra[${index}]` : undefined; + const childRanges = this.collectComponentRanges(extra, currentIndex, childId); ranges.push(...childRanges); if (childRanges.length > 0) { currentIndex = childRanges[childRanges.length - 1]!.sourceRange[1]; diff --git a/tests/cases/bbox-id-cascade.ts b/tests/cases/bbox-id-cascade.ts new file mode 100644 index 0000000..a328529 --- /dev/null +++ b/tests/cases/bbox-id-cascade.ts @@ -0,0 +1,21 @@ +import type { TestCase } from "./types"; + +const testCase: TestCase = { + name: "bbox-id-cascade", + text: { + id: "/hud/layout/element[0]/text[0]", + text: "T", + extra: [{ text: "e" }, { text: "x" }, { text: "t" }], + }, + expect: { + type: "bbox", + ids: [ + "/hud/layout/element[0]/text[0]", + "/hud/layout/element[0]/text[0]/extra[0]", + "/hud/layout/element[0]/text[0]/extra[1]", + "/hud/layout/element[0]/text[0]/extra[2]", + ], + }, +}; + +export default testCase; diff --git a/tests/cases/index.ts b/tests/cases/index.ts index aa2adc1..cb0fe4b 100644 --- a/tests/cases/index.ts +++ b/tests/cases/index.ts @@ -1,11 +1,13 @@ import stylesNoHud from "./styles-no-hud"; import simpleShader from "./simple-shader"; import hudkitPrebonk from "./hudkit-prebonk"; +import bboxIdCascade from "./bbox-id-cascade"; export const testCases = [ stylesNoHud, simpleShader, hudkitPrebonk, + bboxIdCascade, ]; export * from "./types"; diff --git a/tests/cases/types.ts b/tests/cases/types.ts index d487599..fb7f3c8 100644 --- a/tests/cases/types.ts +++ b/tests/cases/types.ts @@ -18,7 +18,7 @@ export interface TestCase { vertex?: string; // path relative to tests/ fragment?: string; }; - expect: ImageExpectation | PixelExpectation; + expect: ImageExpectation | PixelExpectation | BboxExpectation; } export interface ImageExpectation { @@ -32,6 +32,18 @@ export interface PixelExpectation { checks: PixelCheck[]; } +export interface BboxExpectation { + type: "bbox"; + ids: Array; +} + +export interface BboxCheck { + index: number; + expected: string | undefined; + actual: string | undefined; + passed: boolean; +} + export interface PixelCheck { x: number; y: number; diff --git a/tests/text/parser.test.ts b/tests/text/parser.test.ts index a278d9f..5ad1acb 100644 --- a/tests/text/parser.test.ts +++ b/tests/text/parser.test.ts @@ -165,6 +165,22 @@ describe("TextParser", () => { expect(result[0]!.style.color).toEqual({ r: 85 / 255, g: 1, b: 85 / 255, a: 1 }); }); }); + + describe("parseComponentRanges", () => { + it("should cascade ids through nested extras", () => { + const result = parser.parseComponentRanges({ + id: "/hud/layout/element[0]/text[0]", + text: "T", + extra: [{ text: "e" }, { text: "x" }, { text: "t" }], + }); + + expect(result).toHaveLength(4); + expect(result[0]!.id).toBe("/hud/layout/element[0]/text[0]"); + expect(result[1]!.id).toBe("/hud/layout/element[0]/text[0]/extra[0]"); + expect(result[2]!.id).toBe("/hud/layout/element[0]/text[0]/extra[1]"); + expect(result[3]!.id).toBe("/hud/layout/element[0]/text[0]/extra[2]"); + }); + }); }); describe("createTextParser", () => { diff --git a/tests/visual/runner.ts b/tests/visual/runner.ts index 09d64ea..b378f0f 100644 --- a/tests/visual/runner.ts +++ b/tests/visual/runner.ts @@ -4,6 +4,7 @@ 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"); @@ -168,7 +169,7 @@ export class TestRunner { diffPixels: compareResult.diffPixels, totalPixels: compareResult.totalPixels, }; - } else { + } else if (testCase.expect.type === "pixels") { const pixelResults = await this.checkPixels(testCase.expect.checks); const allPassed = pixelResults.every((r) => r.passed); @@ -178,6 +179,14 @@ export class TestRunner { actualPath, pixelChecks: pixelResults, }; + } else { + const bboxResult = await this.checkBboxIds(testCase.expect.ids); + return { + name: testCase.name, + passed: bboxResult.passed, + actualPath, + bboxChecks: bboxResult.checks, + }; } } @@ -214,6 +223,29 @@ export class TestRunner { return results; } + + private async checkBboxIds( + expectedIds: Array + ): 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 { @@ -226,6 +258,7 @@ export interface TestResult { diffPixels?: number; totalPixels?: number; pixelChecks?: Array<{ x: number; y: number; expected: number[]; actual: number[]; passed: boolean }>; + bboxChecks?: BboxCheck[]; } export { testCases }; diff --git a/tests/visual/visual.test.ts b/tests/visual/visual.test.ts index ce00be8..d4bf6aa 100644 --- a/tests/visual/visual.test.ts +++ b/tests/visual/visual.test.ts @@ -27,6 +27,15 @@ function formatResult(result: TestResult): string { } } + if (result.bboxChecks) { + for (const check of result.bboxChecks) { + const checkStatus = check.passed ? "OK" : "FAIL"; + lines.push( + ` [${checkStatus}] [${check.index}]: expected ${JSON.stringify(check.expected)}, got ${JSON.stringify(check.actual)}` + ); + } + } + return lines.join("\n"); }