Skip to content

Alexey/1934/input system testing [AARD-1934] #1171

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

Draft
wants to merge 4 commits into
base: dev
Choose a base branch
from
Draft
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
77 changes: 71 additions & 6 deletions fission/src/test/InputSystem.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,47 @@
import { test, describe, assert, expect } from "vitest"
import InputSystem, { EmptyModifierState, ModifierState } from "@/systems/input/InputSystem"
import InputSchemeManager from "@/systems/input/InputSchemeManager"
import InputSystem, { AxisInput, ButtonInput, EmptyModifierState, ModifierState } from "@/systems/input/InputSystem"
import InputSchemeManager, { InputScheme } from "@/systems/input/InputSchemeManager"
import DefaultInputs from "@/systems/input/DefaultInputs"
import PreferencesSystem from "@/systems/preferences/PreferencesSystem"

describe("Input Scheme Manager Checks", () => {
test("Available Schemes", () => {
assert(InputSchemeManager.availableInputSchemes[0].schemeName == DefaultInputs.ernie().schemeName)
assert(InputSchemeManager.defaultInputSchemes.length >= 1)
})

test("Add a Custom Scheme", () => {
const startingLength = InputSchemeManager.availableInputSchemes.length
InputSchemeManager.addCustomScheme(DefaultInputs.newBlankScheme)

expect(InputSchemeManager.availableInputSchemes.length).toBe(startingLength + 1)
})
test("Add a Custom Scheme", () => {
const startingLength = InputSchemeManager.availableInputSchemes.length
InputSchemeManager.addCustomScheme(DefaultInputs.newBlankScheme)

assert((InputSchemeManager.availableInputSchemes.length = startingLength + 1))
test("Change Custom Scheme Values", () => {
const scheme = DefaultInputs.newBlankScheme
scheme.schemeName = "Test Scheme"
expect(scheme.schemeName).toBe("Test Scheme")
InputSchemeManager.addCustomScheme(scheme)
scheme.inputs[0].inputName = "Test Input"
scheme.inputs.forEach(input => {
if (input instanceof ButtonInput) {
input.keyCode = "KeyA"
expect(input.keyCode).toBe("KeyA")
} else if (input instanceof AxisInput) {
input.posGamepadButton = 0
expect(input.posGamepadButton).toBe(0)
}
})
})

test("Saving Schemes", () => {
const startingLength = PreferencesSystem.getGlobalPreference<InputScheme[]>("InputSchemes").length
InputSchemeManager.addCustomScheme(DefaultInputs.newBlankScheme)
InputSchemeManager.saveSchemes()
const newLength = PreferencesSystem.getGlobalPreference<InputScheme[]>("InputSchemes").length
expect(newLength).toBe(startingLength + 1)
})

test("Get Random Names", () => {
const names: string[] = []
for (let i = 0; i < 20; i++) {
Expand Down Expand Up @@ -53,6 +76,48 @@ describe("Input System Checks", () => {
expect(InputSystem.isGamepadButtonPressed(1)).toBe(false)
})

test("Keyboard Input", () => {
function testKeyPress(key: string) {
// Simulate key press
document.dispatchEvent(new KeyboardEvent("keydown", { code: key }))

// Check if the key is registered as pressed
expect(InputSystem.isKeyPressed(key)).toBe(true)

// Simulate key release
document.dispatchEvent(new KeyboardEvent("keyup", { code: key }))

// Check if the key is no longer registered as pressed
expect(InputSystem.isKeyPressed(key)).toBe(false)
}

testKeyPress("keyA")
testKeyPress("KeyK")
testKeyPress("KeyR")
testKeyPress("RightShift")
testKeyPress("LeftControl")
testKeyPress("Enter")
testKeyPress("Escape")
testKeyPress("Space")
})

test("Arcade Drive", () => {
InputSystem.brainIndexSchemeMap.set(0, DefaultInputs.ernie())
inputSystem.Update(-1) // Initialize the input system

function testArcadeInput(inputMap: string, key: string, expectedValue: number) {
document.dispatchEvent(new KeyboardEvent("keydown", { code: key }))
expect(InputSystem.getInput(inputMap, 0)).toBe(expectedValue)
document.dispatchEvent(new KeyboardEvent("keyup", { code: key }))
expect(InputSystem.getInput(inputMap, 0)).toBe(0)
}

testArcadeInput("arcadeDrive", "KeyW", 1) // Forward
testArcadeInput("arcadeDrive", "KeyS", -1) // Backward
testArcadeInput("arcadeTurn", "KeyD", 1) // Right
testArcadeInput("arcadeTurn", "KeyA", -1) // Left
})

test("Modifier State Comparison", () => {
const allFalse: ModifierState = {
alt: false,
Expand Down
Loading