Skip to content
Open
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
2 changes: 1 addition & 1 deletion packages/core/src/2d/atlas/SpriteAtlas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class SpriteAtlas extends ReferResource {
sprite.name === name && outSprites.push(sprite);
}
} else {
console.warn("The name of the sprite you want to find is not exit in SpriteAtlas.");
console.warn("There is no sprite named " + name + " in the atlas.");
}
return outSprites;
}
Expand Down
63 changes: 63 additions & 0 deletions tests/src/core/SpriteAtlas.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Sprite, SpriteAtlas, Texture2D } from "@galacean/engine-core";
import { WebGLEngine } from "@galacean/engine";
import { afterEach, describe, expect, it, vi } from "vitest";

describe("SpriteAtlas", async () => {
const canvas = document.createElement("canvas");
const engine = await WebGLEngine.create({ canvas: canvas });

Comment on lines +5 to +8

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Add engine teardown to prevent test resource leaks.

WebGLEngine is created once but never destroyed. This can leak WebGL resources and cause cross-test instability in browser runs. Add explicit teardown.

Suggested fix
-import { describe, expect, it, vi } from "vitest";
+import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";

-describe("SpriteAtlas", async () => {
-  const canvas = document.createElement("canvas");
-  const engine = await WebGLEngine.create({ canvas: canvas });
+describe("SpriteAtlas", () => {
+  let engine: WebGLEngine;
+
+  beforeAll(async () => {
+    const canvas = document.createElement("canvas");
+    engine = await WebGLEngine.create({ canvas });
+  });
+
+  afterAll(() => {
+    engine.destroy();
+  });
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
describe("SpriteAtlas", async () => {
const canvas = document.createElement("canvas");
const engine = await WebGLEngine.create({ canvas: canvas });
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
describe("SpriteAtlas", () => {
let engine: WebGLEngine;
beforeAll(async () => {
const canvas = document.createElement("canvas");
engine = await WebGLEngine.create({ canvas });
});
afterAll(() => {
engine.destroy();
});
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/src/core/SpriteAtlas.test.ts` around lines 5 - 8, The WebGLEngine
instance created via WebGLEngine.create is never destroyed, causing WebGL
resource leaks. Add an afterEach hook in the SpriteAtlas describe block that
calls the appropriate destroy or dispose method on the engine variable to ensure
proper cleanup after each test runs and prevent cross-test instability.

afterEach(() => {
vi.restoreAllMocks();
});

it("getSprite returns the sprite when it exists", () => {
const atlas = new SpriteAtlas(engine);
const sprite = new Sprite(engine, new Texture2D(engine, 100, 100));
sprite.name = "test";
// @ts-ignore
atlas._addSprite(sprite);

const result = atlas.getSprite("test");
expect(result).to.eq(sprite);
});

it("getSprite warns when sprite not found", () => {
const atlas = new SpriteAtlas(engine);
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});

const result = atlas.getSprite("nonexistent");
expect(result).to.be.undefined;
expect(warnSpy).toHaveBeenCalledWith("There is no sprite named nonexistent in the atlas.");
});
Comment thread
coderabbitai[bot] marked this conversation as resolved.

it("getSprites returns matching sprites", () => {
const atlas = new SpriteAtlas(engine);
const sprite1 = new Sprite(engine, new Texture2D(engine, 100, 100));
const sprite2 = new Sprite(engine, new Texture2D(engine, 200, 200));
sprite1.name = "shared";
sprite2.name = "shared";
// @ts-ignore
atlas._addSprite(sprite1);
// @ts-ignore
atlas._addSprite(sprite2);

const out: Sprite[] = [];
const result = atlas.getSprites("shared", out);
expect(result).to.eq(out);
expect(out.length).to.be.greaterThan(0);
for (const s of out) {
expect(s.name).to.eq("shared");
}
});

it("getSprites warns when sprite not found", () => {
const atlas = new SpriteAtlas(engine);
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});

const out: Sprite[] = [];
const result = atlas.getSprites("nonexistent", out);
expect(result).to.eq(out);
expect(out.length).to.eq(0);
expect(warnSpy).toHaveBeenCalledWith("There is no sprite named nonexistent in the atlas.");
});
});
Loading