Skip to content

Utility Module Unit Tests [AARD-1932] #1178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 27, 2025
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
35 changes: 35 additions & 0 deletions fission/src/test/util/EasingFunctions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { test, expect, describe, vi } from "vitest"
import { easeOutQuad } from "@/util/EasingFunctions"

describe("Ease Out Quad Tests", () => {
test("Whole Numbers", () => {
expect(easeOutQuad(0)).toBeCloseTo(0)
expect(easeOutQuad(1)).toBeCloseTo(1)
})

test("Fractions", () => {
expect(easeOutQuad(0.5)).toBeCloseTo(0.75)
expect(easeOutQuad(0.25)).toBeCloseTo(0.4375)
expect(easeOutQuad(0.75)).toBeCloseTo(0.9375)
})

test("Within Range", () => {
const steps = 100
for (let i = 0; i <= steps; i++) {
const n = i / steps
const result = easeOutQuad(n)
expect(result).toBeGreaterThanOrEqual(0)
expect(result).toBeLessThanOrEqual(1)
}
})

test("Out of Range Inputs", () => {
const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {})
easeOutQuad(-0.1)
easeOutQuad(1.1)
easeOutQuad(2)
easeOutQuad(-3)
expect(consoleErrorSpy).toHaveBeenCalledTimes(4)
consoleErrorSpy.mockRestore()
})
})
33 changes: 33 additions & 0 deletions fission/src/test/util/Lazy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { describe, test, expect, vi } from "vitest"
import Lazy from "@/util/Lazy"

describe("Lazy<T> Tests", () => {
test("Should Call The Init Function Only Once", () => {
const initFn = vi.fn(() => "computed value")
const lazy = new Lazy(initFn)

const val1 = lazy.getValue()
const val2 = lazy.getValue()

expect(val1).toBe("computed value")
expect(val2).toBe("computed value")
expect(initFn).toHaveBeenCalledTimes(1)
})

test("Should Not Initialize Until GetValue Is Called", () => {
const initFn = vi.fn(() => "late init")
const lazy = new Lazy(initFn)

expect(initFn).not.toHaveBeenCalled()
lazy.getValue()
expect(initFn).toHaveBeenCalledTimes(1)
})

test("Should Work With Complex Types", () => {
const initFn = () => ({ x: 1, y: 2 })
const lazy = new Lazy(initFn)

const val = lazy.getValue()
expect(val).toEqual({ x: 1, y: 2 })
})
})
73 changes: 73 additions & 0 deletions fission/src/test/util/MeshCreation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { test, describe, expect } from "vitest"
import JOLT from "@/util/loading/JoltSyncLoader"
import { createMeshForShape, DeltaFieldTransforms_PhysicalProp } from "@/util/threejs/MeshCreation"
import { Vector3 } from "three"
import * as THREE from "three"

describe("Mesh Creation Tests", () => {
test("Sphere Mesh Creation", () => {
const sphereShape = new JOLT.SphereShape(4.0)
const shapeResult = createMeshForShape(sphereShape)

expect(shapeResult).toBeDefined()
shapeResult.computeBoundingSphere()
expect(shapeResult.boundingSphere?.radius).toBeCloseTo(4.0, 2)
})

test("Box Mesh Creation", () => {
const boxShape = new JOLT.BoxShape(new JOLT.Vec3(0.5, 2, 4.5))
const shapeResult = createMeshForShape(boxShape)

expect(shapeResult).toBeDefined()
shapeResult.computeBoundingBox()
const boxSize = new Vector3()
shapeResult.boundingBox?.getSize(boxSize)
expect(boxSize.x).toBeCloseTo(1.0, 2)
expect(boxSize.y).toBeCloseTo(4.0, 2)
expect(boxSize.z).toBeCloseTo(9.0, 2)
})
})

describe("Delta Field Transform Physical Properties Tests", () => {
test("Returns Identity Transform When Both Matrices Are Identity", () => {
const identity = new THREE.Matrix4()
const result = DeltaFieldTransforms_PhysicalProp(identity, identity)

expect(result.translation).toEqual(new THREE.Vector3(0, 0, 0))
expect(result.rotation).toEqual(new THREE.Quaternion(0, 0, 0, 1))
expect(result.scale).toEqual(new THREE.Vector3(1, 1, 1))
})

test("Applies Translation From Delta Only", () => {
const delta = new THREE.Matrix4().makeTranslation(5, 0, 0)
const field = new THREE.Matrix4()
const result = DeltaFieldTransforms_PhysicalProp(delta, field)

expect(result.translation).toEqual(new THREE.Vector3(5, 0, 0))
expect(result.rotation.equals(new THREE.Quaternion(0, 0, 0, 1))).toBe(true)
expect(result.scale.equals(new THREE.Vector3(1, 1, 1))).toBe(true)
})

test("Composes Rotation And Scale With Premultiply", () => {
const delta = new THREE.Matrix4().makeRotationZ(Math.PI / 2)
const field = new THREE.Matrix4().makeScale(2, 2, 2)
const result = DeltaFieldTransforms_PhysicalProp(delta, field)

expect(result.scale.x).toBeCloseTo(2)
expect(result.scale.y).toBeCloseTo(2)
expect(result.scale.z).toBeCloseTo(2)

const expectedQuat = new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(0, 0, 1), Math.PI / 2)
expect(result.rotation.angleTo(expectedQuat)).toBeLessThan(1e-6)
})

test("Handles Scaled Translation", () => {
const delta = new THREE.Matrix4().makeTranslation(1, 2, 3)
const field = new THREE.Matrix4().makeScale(2, 2, 2)

const result = DeltaFieldTransforms_PhysicalProp(delta, field)

expect(result.translation).toEqual(new THREE.Vector3(2, 4, 6))
expect(result.scale).toEqual(new THREE.Vector3(2, 2, 2))
})
})
100 changes: 100 additions & 0 deletions fission/src/test/util/Units.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { test, expect, describe, vi, beforeEach } from "vitest"
import { DOMUnit, DOMUnitExpression } from "@/util/Units"

vi.mock("@/util/Utility", () => {
const original = vi.importActual<typeof import("@/util/Utility")>("@/util/Utility")
return { ...original, getFontSize: vi.fn(() => 16) }
})

describe("DOMUnit", () => {
let mockElement: HTMLElement

beforeEach(async () => {
mockElement = document.createElement("div")
Object.defineProperty(mockElement, "clientWidth", { value: 400 })
Object.defineProperty(mockElement, "clientHeight", { value: 200 })
})

test("Evaluates PX Units Correctly", () => {
const unit = new DOMUnit(42, "px")
expect(unit.evaluate(mockElement)).toBe(42)
})

test("Evaluates REM Units Correctly", () => {
const unit = new DOMUnit(2, "rem")
expect(unit.evaluate(mockElement)).toBe(32)
})

test("Evaluates Width (W) Units Correctly", () => {
const unit = new DOMUnit(0.5, "w")
expect(unit.evaluate(mockElement)).toBe(200)
})

test("Evaluates Height (H) Units Correctly", () => {
const unit = new DOMUnit(0.2, "h")
expect(unit.evaluate(mockElement)).toBe(40)
})
})

describe("DOMUnitExpression", () => {
let mockElement: HTMLElement

beforeEach(() => {
mockElement = document.createElement("div")
Object.defineProperty(mockElement, "clientWidth", { value: 400 })
Object.defineProperty(mockElement, "clientHeight", { value: 200 })
})

test("Evaluates Single Unit Expression Correctly", () => {
const result = DOMUnitExpression.fromUnit(42, "px").evaluate(mockElement)
expect(result).toBe(42)
})

test("Evaluates Addition of Two PX Units Correctly", () => {
const result = DOMUnitExpression.fromUnit(20, "px").add(DOMUnitExpression.fromUnit(22, "px"))
expect(result.evaluate(mockElement)).toBe(42)
})

test("Evaluates Addition of PX and REM Units Correctly", () => {
const expr = DOMUnitExpression.fromUnit(16, "px").add(DOMUnitExpression.fromUnit(1, "rem"))
expect(expr.evaluate(mockElement)).toBe(32)
})

test("Evaluates Subtraction of PX Units Correctly", () => {
const expr = DOMUnitExpression.fromUnit(50, "px").sub(DOMUnitExpression.fromUnit(8, "px"))
expect(expr.evaluate(mockElement)).toBe(42)
})

test("Evaluates Multiplication of PX Unit by Scalar", () => {
const expr = DOMUnitExpression.fromUnit(7, "px").mul(DOMUnitExpression.fromUnit(6, "px"))
expect(expr.evaluate(mockElement)).toBe(42)
})

test("Evaluate Multiplication of PX Unit by Decimal", () => {
const expr = DOMUnitExpression.fromUnit(84, "px").mul(DOMUnitExpression.fromUnit(0.5, "px"))
expect(expr.evaluate(mockElement)).toBe(42)
})

test("Evaluates Division of PX Unit by Scalar", () => {
const expr = DOMUnitExpression.fromUnit(84, "px").div(DOMUnitExpression.fromUnit(2, "px"))
expect(expr.evaluate(mockElement)).toBe(42)
})

test("Evaluates Addition of Width (W) and Height (H) Units Correctly", () => {
const expr = DOMUnitExpression.fromUnit(0.1, "w").add(DOMUnitExpression.fromUnit(0.11, "h"))
// 0.1 * 400 + 0.11 * 200 = 40 + 22 = 62
expect(expr.evaluate(mockElement)).toBe(62)
})

test("Evaluates Subtraction of Width (W) and Height (H) Units Correctly", () => {
const expr = DOMUnitExpression.fromUnit(0.5, "w").sub(DOMUnitExpression.fromUnit(0.2, "h"))
// 0.5 * 400 - 0.2 * 200 = 200 - 40 = 160
Copy link
Contributor

Choose a reason for hiding this comment

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

Very helpful comments!

expect(expr.evaluate(mockElement)).toBe(160)
})

test("Evaluates Subtraction of REM and PX Units Correctly", () => {
const expr = DOMUnitExpression.fromUnit(3, "rem").sub(DOMUnitExpression.fromUnit(6, "px"))
// 3 * 16 - 6 = 48 - 6 = 42
expect(expr.evaluate(mockElement)).toBe(42)
})
})
1 change: 1 addition & 0 deletions fission/src/util/EasingFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
* @returns -(n^2) + 2n
*/
export function easeOutQuad(n: number): number {
if (n < 0 || n > 1) console.error(`easeOutQuad: input ${n} is outside the expected range [0, 1]`)
return 1 - (1 - n) * (1 - n)
}
Loading