From 35e733e87a9ab3ac09b58c6bc41d3811774ea6d8 Mon Sep 17 00:00:00 2001 From: Sirz Benjie <142067137+SirzBenjie@users.noreply.github.com> Date: Fri, 28 Mar 2025 16:30:38 -0500 Subject: [PATCH] [Test] [Refactor] [GitHub] Enable no isolate for vitest (#5566) * Reuse global scene between tests Co-authored-by: PigeonBar <56974298+PigeonBar@users.noreply.github.com> * Add missing each method to mockContainer * Fix select-modifier-phase test * Sanitize overrides before tests Co-authored-by: PigeonBar <56974298+PigeonBar@users.noreply.github.com> * Sanitize overrides before tests Co-authored-by: PigeonBar <56974298+PigeonBar@users.noreply.github.com> * [WIP] fix things * Fix tests not working with --no-isolate * Update npm tests to use no isolate * Update test-shard-template * Update package.json Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com> --------- Co-authored-by: PigeonBar <56974298+PigeonBar@users.noreply.github.com> Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com> --- .github/workflows/test-shard-template.yml | 2 +- .github/workflows/tests.yml | 21 --- lefthook.yml | 2 +- package-lock.json | 174 +++++++++--------- package.json | 12 +- src/@types/DexData.ts | 16 ++ src/battle-scene.ts | 17 +- src/overrides.ts | 5 + src/system/game-data.ts | 78 ++++---- src/ui/starter-select-ui-handler.ts | 9 + test/abilities/steely_spirit.test.ts | 4 +- test/abilities/supreme_overlord.test.ts | 7 +- test/abilities/unseen_fist.test.ts | 16 +- test/abilities/wonder_skin.test.ts | 13 +- test/achievements/achievement.test.ts | 22 ++- test/battlerTags/substitute.test.ts | 28 ++- test/data/status_effect.test.ts | 6 - test/moves/copycat.test.ts | 3 +- test/moves/dynamax_cannon.test.ts | 4 +- test/moves/fusion_flare_bolt.test.ts | 19 +- test/moves/hard_press.test.ts | 4 +- test/moves/last_respects.test.ts | 7 +- test/moves/metronome.test.ts | 3 +- test/moves/rage_fist.test.ts | 4 +- test/moves/retaliate.test.ts | 4 +- test/moves/shell_side_arm.test.ts | 7 +- test/moves/spit_up.test.ts | 4 +- test/moves/tera_blast.test.ts | 8 +- test/moves/triple_arrows.test.ts | 10 +- .../mystery-encounter/encounter-test-utils.ts | 10 - test/phases/phases.test.ts | 6 +- test/phases/select-modifier-phase.test.ts | 4 +- .../plugins/api/pokerogue-account-api.test.ts | 10 +- test/plugins/api/pokerogue-admin-api.test.ts | 10 +- test/plugins/api/pokerogue-api.test.ts | 10 +- test/plugins/api/pokerogue-daily-api.test.ts | 10 +- .../api/pokerogue-savedata-api.test.ts | 10 +- .../pokerogue-session-savedata-api.test.ts | 10 +- .../api/pokerogue-system-savedata-api.test.ts | 11 +- test/pre.test.ts | 6 - test/testUtils/gameManager.ts | 34 +++- test/testUtils/gameWrapper.ts | 37 +--- test/testUtils/helpers/overridesHelper.ts | 29 +-- test/testUtils/listenersManager.ts | 41 +++++ test/testUtils/mocks/mockConsoleLog.ts | 156 ++++++++-------- test/testUtils/mocks/mockContextCanvas.ts | 26 +++ test/testUtils/mocks/mockLocalStorage.ts | 4 +- test/testUtils/mocks/mockTextureManager.ts | 2 +- .../mocks/mocksContainer/mockContainer.ts | 6 + .../mocks/mocksContainer/mockImage.ts | 2 +- .../mocks/mocksContainer/mockRectangle.ts | 3 + test/testUtils/testFileInitialization.ts | 117 ++++++++++++ test/vitest.setup.ts | 23 +-- vitest.config.ts | 30 ++- 54 files changed, 714 insertions(+), 402 deletions(-) create mode 100644 src/@types/DexData.ts delete mode 100644 test/pre.test.ts create mode 100644 test/testUtils/listenersManager.ts create mode 100644 test/testUtils/mocks/mockContextCanvas.ts create mode 100644 test/testUtils/testFileInitialization.ts diff --git a/.github/workflows/test-shard-template.yml b/.github/workflows/test-shard-template.yml index 185764c86a8..9fc41d1b965 100644 --- a/.github/workflows/test-shard-template.yml +++ b/.github/workflows/test-shard-template.yml @@ -29,4 +29,4 @@ jobs: - name: Install Node.js dependencies run: npm ci - name: Run tests - run: npx vitest --project ${{ inputs.project }} --shard=${{ inputs.shard }}/${{ inputs.totalShards }} ${{ !runner.debug && '--silent' || '' }} + run: npx vitest --project ${{ inputs.project }} --no-isolate --shard=${{ inputs.shard }}/${{ inputs.totalShards }} ${{ !runner.debug && '--silent' || '' }} diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index d30d8adba38..167a108e58c 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -15,29 +15,8 @@ on: types: [checks_requested] jobs: - pre-test: - name: Run Pre-test - runs-on: ubuntu-latest - steps: - - name: Check out Git repository - uses: actions/checkout@v4 - with: - submodules: 'recursive' - path: tests-action - - name: Set up Node.js - uses: actions/setup-node@v4 - with: - node-version: 20 - - name: Install Node.js dependencies - working-directory: tests-action - run: npm ci - - name: Run Pre-test - working-directory: tests-action - run: npx vitest run --project pre ${{ !runner.debug && '--silent' || '' }} - run-tests: name: Run Tests - needs: [pre-test] strategy: matrix: shard: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] diff --git a/lefthook.yml b/lefthook.yml index 4eff2ad1f8e..aa64a176191 100644 --- a/lefthook.yml +++ b/lefthook.yml @@ -13,7 +13,7 @@ pre-push: commands: biome-lint: glob: "*.{js,ts,jsx,tsx}" - run: npx @biomejs/biome check --write --reporter=summary {push_files} --no-errors-on-unmatched + run: npx @biomejs/biome check --reporter=summary {push_files} --no-errors-on-unmatched post-merge: commands: diff --git a/package-lock.json b/package-lock.json index 87e2e150c65..40ef47965ed 100644 --- a/package-lock.json +++ b/package-lock.json @@ -29,7 +29,7 @@ "@types/node": "^20.12.13", "@typescript-eslint/eslint-plugin": "^8.0.0-alpha.54", "@typescript-eslint/parser": "^8.0.0-alpha.54", - "@vitest/coverage-istanbul": "^2.1.9", + "@vitest/coverage-istanbul": "^3.0.9", "dependency-cruiser": "^16.3.10", "eslint": "^9.7.0", "eslint-plugin-import-x": "^4.2.1", @@ -43,7 +43,7 @@ "typescript-eslint": "^8.0.0-alpha.54", "vite": "^5.4.14", "vite-tsconfig-paths": "^4.3.2", - "vitest": "^2.1.9", + "vitest": "^3.0.9", "vitest-canvas-mock": "^0.3.3" }, "engines": { @@ -2312,14 +2312,14 @@ } }, "node_modules/@vitest/coverage-istanbul": { - "version": "2.1.9", - "resolved": "https://registry.npmjs.org/@vitest/coverage-istanbul/-/coverage-istanbul-2.1.9.tgz", - "integrity": "sha512-vdYE4FkC/y2lxcN3Dcj54Bw+ericmDwiex0B8LV5F/YNYEYP1mgVwhPnHwWGAXu38qizkjOuyczKbFTALfzFKw==", + "version": "3.0.9", + "resolved": "https://registry.npmjs.org/@vitest/coverage-istanbul/-/coverage-istanbul-3.0.9.tgz", + "integrity": "sha512-/TXh2qmOhclmVPjOnPTpIO4Xr6l2P5EwyXQygenwq4/ZQ/vPsrz+GCRZF9kBeQi6xrGcHv368Si9PGImWQawVg==", "dev": true, "license": "MIT", "dependencies": { "@istanbuljs/schema": "^0.1.3", - "debug": "^4.3.7", + "debug": "^4.4.0", "istanbul-lib-coverage": "^3.2.2", "istanbul-lib-instrument": "^6.0.3", "istanbul-lib-report": "^3.0.1", @@ -2327,48 +2327,48 @@ "istanbul-reports": "^3.1.7", "magicast": "^0.3.5", "test-exclude": "^7.0.1", - "tinyrainbow": "^1.2.0" + "tinyrainbow": "^2.0.0" }, "funding": { "url": "https://opencollective.com/vitest" }, "peerDependencies": { - "vitest": "2.1.9" + "vitest": "3.0.9" } }, "node_modules/@vitest/expect": { - "version": "2.1.9", - "resolved": "https://registry.npmjs.org/@vitest/expect/-/expect-2.1.9.tgz", - "integrity": "sha512-UJCIkTBenHeKT1TTlKMJWy1laZewsRIzYighyYiJKZreqtdxSos/S1t+ktRMQWu2CKqaarrkeszJx1cgC5tGZw==", + "version": "3.0.9", + "resolved": "https://registry.npmjs.org/@vitest/expect/-/expect-3.0.9.tgz", + "integrity": "sha512-5eCqRItYgIML7NNVgJj6TVCmdzE7ZVgJhruW0ziSQV4V7PvLkDL1bBkBdcTs/VuIz0IxPb5da1IDSqc1TR9eig==", "dev": true, "license": "MIT", "dependencies": { - "@vitest/spy": "2.1.9", - "@vitest/utils": "2.1.9", - "chai": "^5.1.2", - "tinyrainbow": "^1.2.0" + "@vitest/spy": "3.0.9", + "@vitest/utils": "3.0.9", + "chai": "^5.2.0", + "tinyrainbow": "^2.0.0" }, "funding": { "url": "https://opencollective.com/vitest" } }, "node_modules/@vitest/mocker": { - "version": "2.1.9", - "resolved": "https://registry.npmjs.org/@vitest/mocker/-/mocker-2.1.9.tgz", - "integrity": "sha512-tVL6uJgoUdi6icpxmdrn5YNo3g3Dxv+IHJBr0GXHaEdTcw3F+cPKnsXFhli6nO+f/6SDKPHEK1UN+k+TQv0Ehg==", + "version": "3.0.9", + "resolved": "https://registry.npmjs.org/@vitest/mocker/-/mocker-3.0.9.tgz", + "integrity": "sha512-ryERPIBOnvevAkTq+L1lD+DTFBRcjueL9lOUfXsLfwP92h4e+Heb+PjiqS3/OURWPtywfafK0kj++yDFjWUmrA==", "dev": true, "license": "MIT", "dependencies": { - "@vitest/spy": "2.1.9", + "@vitest/spy": "3.0.9", "estree-walker": "^3.0.3", - "magic-string": "^0.30.12" + "magic-string": "^0.30.17" }, "funding": { "url": "https://opencollective.com/vitest" }, "peerDependencies": { "msw": "^2.4.9", - "vite": "^5.0.0" + "vite": "^5.0.0 || ^6.0.0" }, "peerDependenciesMeta": { "msw": { @@ -2380,51 +2380,51 @@ } }, "node_modules/@vitest/pretty-format": { - "version": "2.1.9", - "resolved": "https://registry.npmjs.org/@vitest/pretty-format/-/pretty-format-2.1.9.tgz", - "integrity": "sha512-KhRIdGV2U9HOUzxfiHmY8IFHTdqtOhIzCpd8WRdJiE7D/HUcZVD0EgQCVjm+Q9gkUXWgBvMmTtZgIG48wq7sOQ==", + "version": "3.0.9", + "resolved": "https://registry.npmjs.org/@vitest/pretty-format/-/pretty-format-3.0.9.tgz", + "integrity": "sha512-OW9F8t2J3AwFEwENg3yMyKWweF7oRJlMyHOMIhO5F3n0+cgQAJZBjNgrF8dLwFTEXl5jUqBLXd9QyyKv8zEcmA==", "dev": true, "license": "MIT", "dependencies": { - "tinyrainbow": "^1.2.0" + "tinyrainbow": "^2.0.0" }, "funding": { "url": "https://opencollective.com/vitest" } }, "node_modules/@vitest/runner": { - "version": "2.1.9", - "resolved": "https://registry.npmjs.org/@vitest/runner/-/runner-2.1.9.tgz", - "integrity": "sha512-ZXSSqTFIrzduD63btIfEyOmNcBmQvgOVsPNPe0jYtESiXkhd8u2erDLnMxmGrDCwHCCHE7hxwRDCT3pt0esT4g==", + "version": "3.0.9", + "resolved": "https://registry.npmjs.org/@vitest/runner/-/runner-3.0.9.tgz", + "integrity": "sha512-NX9oUXgF9HPfJSwl8tUZCMP1oGx2+Sf+ru6d05QjzQz4OwWg0psEzwY6VexP2tTHWdOkhKHUIZH+fS6nA7jfOw==", "dev": true, "license": "MIT", "dependencies": { - "@vitest/utils": "2.1.9", - "pathe": "^1.1.2" + "@vitest/utils": "3.0.9", + "pathe": "^2.0.3" }, "funding": { "url": "https://opencollective.com/vitest" } }, "node_modules/@vitest/snapshot": { - "version": "2.1.9", - "resolved": "https://registry.npmjs.org/@vitest/snapshot/-/snapshot-2.1.9.tgz", - "integrity": "sha512-oBO82rEjsxLNJincVhLhaxxZdEtV0EFHMK5Kmx5sJ6H9L183dHECjiefOAdnqpIgT5eZwT04PoggUnW88vOBNQ==", + "version": "3.0.9", + "resolved": "https://registry.npmjs.org/@vitest/snapshot/-/snapshot-3.0.9.tgz", + "integrity": "sha512-AiLUiuZ0FuA+/8i19mTYd+re5jqjEc2jZbgJ2up0VY0Ddyyxg/uUtBDpIFAy4uzKaQxOW8gMgBdAJJ2ydhu39A==", "dev": true, "license": "MIT", "dependencies": { - "@vitest/pretty-format": "2.1.9", - "magic-string": "^0.30.12", - "pathe": "^1.1.2" + "@vitest/pretty-format": "3.0.9", + "magic-string": "^0.30.17", + "pathe": "^2.0.3" }, "funding": { "url": "https://opencollective.com/vitest" } }, "node_modules/@vitest/spy": { - "version": "2.1.9", - "resolved": "https://registry.npmjs.org/@vitest/spy/-/spy-2.1.9.tgz", - "integrity": "sha512-E1B35FwzXXTs9FHNK6bDszs7mtydNi5MIfUWpceJ8Xbfb1gBMscAnwLbEu+B44ed6W3XjL9/ehLPHR1fkf1KLQ==", + "version": "3.0.9", + "resolved": "https://registry.npmjs.org/@vitest/spy/-/spy-3.0.9.tgz", + "integrity": "sha512-/CcK2UDl0aQ2wtkp3YVWldrpLRNCfVcIOFGlVGKO4R5eajsH393Z1yiXLVQ7vWsj26JOEjeZI0x5sm5P4OGUNQ==", "dev": true, "license": "MIT", "dependencies": { @@ -2435,15 +2435,15 @@ } }, "node_modules/@vitest/utils": { - "version": "2.1.9", - "resolved": "https://registry.npmjs.org/@vitest/utils/-/utils-2.1.9.tgz", - "integrity": "sha512-v0psaMSkNJ3A2NMrUEHFRzJtDPFn+/VWZ5WxImB21T9fjucJRmS7xCS3ppEnARb9y11OAzaD+P2Ps+b+BGX5iQ==", + "version": "3.0.9", + "resolved": "https://registry.npmjs.org/@vitest/utils/-/utils-3.0.9.tgz", + "integrity": "sha512-ilHM5fHhZ89MCp5aAaM9uhfl1c2JdxVxl3McqsdVyVNN6JffnEen8UMCdRTzOhGXNQGo5GNL9QugHrz727Wnng==", "dev": true, "license": "MIT", "dependencies": { - "@vitest/pretty-format": "2.1.9", - "loupe": "^3.1.2", - "tinyrainbow": "^1.2.0" + "@vitest/pretty-format": "3.0.9", + "loupe": "^3.1.3", + "tinyrainbow": "^2.0.0" }, "funding": { "url": "https://opencollective.com/vitest" @@ -2738,9 +2738,9 @@ "license": "CC-BY-4.0" }, "node_modules/chai": { - "version": "5.1.2", - "resolved": "https://registry.npmjs.org/chai/-/chai-5.1.2.tgz", - "integrity": "sha512-aGtmf24DW6MLHHG5gCx4zaI3uBq3KRtxeVs0DjFH6Z0rDNbsvTxFASFvdj79pxjxZ8/5u3PIiN3IwEIQkiiuPw==", + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/chai/-/chai-5.2.0.tgz", + "integrity": "sha512-mCuXncKXk5iCLhfhwTc0izo0gtEmpz5CtG2y8GiOINBlMVS6v8TMRc5TaLWKS6692m9+dVVfzgeVxR5UxWHTYw==", "dev": true, "license": "MIT", "dependencies": { @@ -5450,9 +5450,9 @@ } }, "node_modules/pathe": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/pathe/-/pathe-1.1.2.tgz", - "integrity": "sha512-whLdWMYL2TwI08hn8/ZqAbrVemu0LNaNNJZX73O6qaIdCTfXutsLhMkjdENX0qhsQ9uIimo4/aQOmXkoon2nDQ==", + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/pathe/-/pathe-2.0.3.tgz", + "integrity": "sha512-WUjGcAqP1gQacoQe+OBJsFA7Ld4DyXuUIjZ5cc75cLHvJ7dtNsTugphxIADwspS+AraAUePCKrSVtPLFj/F88w==", "dev": true, "license": "MIT" }, @@ -6308,9 +6308,9 @@ } }, "node_modules/tinyrainbow": { - "version": "1.2.0", - "resolved": "https://registry.npmjs.org/tinyrainbow/-/tinyrainbow-1.2.0.tgz", - "integrity": "sha512-weEDEq7Z5eTHPDh4xjX789+fHfF+P8boiFB+0vbWzpbnbsEr/GRaohi/uMKxg8RZMXnl1ItAi/IUHWMsjDV7kQ==", + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/tinyrainbow/-/tinyrainbow-2.0.0.tgz", + "integrity": "sha512-op4nsTR47R6p0vMUUoYl/a+ljLFVtlfaXkLQmqfLR1qHma1h/ysYk4hEXZ880bf2CYgTskvTa/e196Vd5dDQXw==", "dev": true, "license": "MIT", "engines": { @@ -6677,23 +6677,23 @@ } }, "node_modules/vite-node": { - "version": "2.1.9", - "resolved": "https://registry.npmjs.org/vite-node/-/vite-node-2.1.9.tgz", - "integrity": "sha512-AM9aQ/IPrW/6ENLQg3AGY4K1N2TGZdR5e4gu/MmmR2xR3Ll1+dib+nook92g4TV3PXVyeyxdWwtaCAiUL0hMxA==", + "version": "3.0.9", + "resolved": "https://registry.npmjs.org/vite-node/-/vite-node-3.0.9.tgz", + "integrity": "sha512-w3Gdx7jDcuT9cNn9jExXgOyKmf5UOTb6WMHz8LGAm54eS1Elf5OuBhCxl6zJxGhEeIkgsE1WbHuoL0mj/UXqXg==", "dev": true, "license": "MIT", "dependencies": { "cac": "^6.7.14", - "debug": "^4.3.7", - "es-module-lexer": "^1.5.4", - "pathe": "^1.1.2", - "vite": "^5.0.0" + "debug": "^4.4.0", + "es-module-lexer": "^1.6.0", + "pathe": "^2.0.3", + "vite": "^5.0.0 || ^6.0.0" }, "bin": { "vite-node": "vite-node.mjs" }, "engines": { - "node": "^18.0.0 || >=20.0.0" + "node": "^18.0.0 || ^20.0.0 || >=22.0.0" }, "funding": { "url": "https://opencollective.com/vitest" @@ -6720,47 +6720,48 @@ } }, "node_modules/vitest": { - "version": "2.1.9", - "resolved": "https://registry.npmjs.org/vitest/-/vitest-2.1.9.tgz", - "integrity": "sha512-MSmPM9REYqDGBI8439mA4mWhV5sKmDlBKWIYbA3lRb2PTHACE0mgKwA8yQ2xq9vxDTuk4iPrECBAEW2aoFXY0Q==", + "version": "3.0.9", + "resolved": "https://registry.npmjs.org/vitest/-/vitest-3.0.9.tgz", + "integrity": "sha512-BbcFDqNyBlfSpATmTtXOAOj71RNKDDvjBM/uPfnxxVGrG+FSH2RQIwgeEngTaTkuU/h0ScFvf+tRcKfYXzBybQ==", "dev": true, "license": "MIT", "dependencies": { - "@vitest/expect": "2.1.9", - "@vitest/mocker": "2.1.9", - "@vitest/pretty-format": "^2.1.9", - "@vitest/runner": "2.1.9", - "@vitest/snapshot": "2.1.9", - "@vitest/spy": "2.1.9", - "@vitest/utils": "2.1.9", - "chai": "^5.1.2", - "debug": "^4.3.7", + "@vitest/expect": "3.0.9", + "@vitest/mocker": "3.0.9", + "@vitest/pretty-format": "^3.0.9", + "@vitest/runner": "3.0.9", + "@vitest/snapshot": "3.0.9", + "@vitest/spy": "3.0.9", + "@vitest/utils": "3.0.9", + "chai": "^5.2.0", + "debug": "^4.4.0", "expect-type": "^1.1.0", - "magic-string": "^0.30.12", - "pathe": "^1.1.2", + "magic-string": "^0.30.17", + "pathe": "^2.0.3", "std-env": "^3.8.0", "tinybench": "^2.9.0", - "tinyexec": "^0.3.1", - "tinypool": "^1.0.1", - "tinyrainbow": "^1.2.0", - "vite": "^5.0.0", - "vite-node": "2.1.9", + "tinyexec": "^0.3.2", + "tinypool": "^1.0.2", + "tinyrainbow": "^2.0.0", + "vite": "^5.0.0 || ^6.0.0", + "vite-node": "3.0.9", "why-is-node-running": "^2.3.0" }, "bin": { "vitest": "vitest.mjs" }, "engines": { - "node": "^18.0.0 || >=20.0.0" + "node": "^18.0.0 || ^20.0.0 || >=22.0.0" }, "funding": { "url": "https://opencollective.com/vitest" }, "peerDependencies": { "@edge-runtime/vm": "*", - "@types/node": "^18.0.0 || >=20.0.0", - "@vitest/browser": "2.1.9", - "@vitest/ui": "2.1.9", + "@types/debug": "^4.1.12", + "@types/node": "^18.0.0 || ^20.0.0 || >=22.0.0", + "@vitest/browser": "3.0.9", + "@vitest/ui": "3.0.9", "happy-dom": "*", "jsdom": "*" }, @@ -6768,6 +6769,9 @@ "@edge-runtime/vm": { "optional": true }, + "@types/debug": { + "optional": true + }, "@types/node": { "optional": true }, diff --git a/package.json b/package.json index ce9e8b4a07b..6048b0bae36 100644 --- a/package.json +++ b/package.json @@ -9,10 +9,10 @@ "build": "vite build", "build:beta": "vite build --mode beta", "preview": "vite preview", - "test": "vitest run --project pre && vitest run --project main", - "test:cov": "vitest run --project pre && vitest run --project main --coverage", - "test:watch": "vitest run --project pre && vitest watch --project main --coverage", - "test:silent": "vitest run --project pre && vitest run --project main --silent", + "test": "vitest run", + "test:cov": "vitest run --coverage --no-isolate", + "test:watch": "vitest watch --coverage --no-isolate", + "test:silent": "vitest run --silent --no-isolate", "typecheck": "tsc --noEmit", "eslint": "eslint --fix .", "eslint-ci": "eslint .", @@ -36,7 +36,7 @@ "@types/node": "^20.12.13", "@typescript-eslint/eslint-plugin": "^8.0.0-alpha.54", "@typescript-eslint/parser": "^8.0.0-alpha.54", - "@vitest/coverage-istanbul": "^2.1.9", + "@vitest/coverage-istanbul": "^3.0.9", "dependency-cruiser": "^16.3.10", "eslint": "^9.7.0", "eslint-plugin-import-x": "^4.2.1", @@ -50,7 +50,7 @@ "typescript-eslint": "^8.0.0-alpha.54", "vite": "^5.4.14", "vite-tsconfig-paths": "^4.3.2", - "vitest": "^2.1.9", + "vitest": "^3.0.9", "vitest-canvas-mock": "^0.3.3" }, "dependencies": { diff --git a/src/@types/DexData.ts b/src/@types/DexData.ts new file mode 100644 index 00000000000..19bb0357471 --- /dev/null +++ b/src/@types/DexData.ts @@ -0,0 +1,16 @@ +/** + * Dex entry for a single Pokemon Species + */ +export interface DexEntry { + seenAttr: bigint; + caughtAttr: bigint; + natureAttr: number; + seenCount: number; + caughtCount: number; + hatchedCount: number; + ivs: number[]; +} + +export interface DexData { + [key: number]: DexEntry; +} diff --git a/src/battle-scene.ts b/src/battle-scene.ts index 544dbc40350..443113daef6 100644 --- a/src/battle-scene.ts +++ b/src/battle-scene.ts @@ -1403,7 +1403,10 @@ export default class BattleScene extends SceneBase { this.field.add(newTrainer); } } else { - if (!this.gameMode.hasTrainers) { + if ( + !this.gameMode.hasTrainers || + (Overrides.DISABLE_STANDARD_TRAINERS_OVERRIDE && isNullOrUndefined(trainerData)) + ) { newBattleType = BattleType.WILD; } else if (battleType === undefined) { newBattleType = this.gameMode.isWaveTrainer(newWaveIndex, this.arena) ? BattleType.TRAINER : BattleType.WILD; @@ -2721,6 +2724,18 @@ export default class BattleScene extends SceneBase { this.phaseQueue.splice(0, this.phaseQueue.length); } + /** + * Clears all phase-related stuff, including all phase queues, the current and standby phases, and a splice index + */ + clearAllPhases(): void { + for (const queue of [this.phaseQueue, this.phaseQueuePrepend, this.conditionalQueue, this.nextCommandPhaseQueue]) { + queue.splice(0, queue.length); + } + this.currentPhase = null; + this.standbyPhase = null; + this.clearPhaseQueueSplice(); + } + /** * Used by function unshiftPhase(), sets index to start inserting at current length instead of the end of the array, useful if phaseQueuePrepend gets longer with Phases */ diff --git a/src/overrides.ts b/src/overrides.ts index 882a634ff90..3a9a54e740b 100644 --- a/src/overrides.ts +++ b/src/overrides.ts @@ -254,6 +254,11 @@ class DefaultOverrides { * Note that, for all items in the array, `count` is not used. */ readonly ITEM_REWARD_OVERRIDE: ModifierOverride[] = []; + + /** + * If `true`, disable all non-scripted opponent trainer encounters. + */ + readonly DISABLE_STANDARD_TRAINERS_OVERRIDE: boolean = false; } export const defaultOverrides = new DefaultOverrides(); diff --git a/src/system/game-data.ts b/src/system/game-data.ts index 82ad2276fef..7845d50b2ad 100644 --- a/src/system/game-data.ts +++ b/src/system/game-data.ts @@ -98,12 +98,13 @@ export function getDataTypeKey(dataType: GameDataType, slotId = 0): string { switch (dataType) { case GameDataType.SYSTEM: return "data"; - case GameDataType.SESSION: + case GameDataType.SESSION: { let ret = "sessionData"; if (slotId) { ret += slotId; } return ret; + } case GameDataType.SETTINGS: return "settings"; case GameDataType.TUTORIALS: @@ -201,39 +202,6 @@ export interface DexEntry { ivs: number[]; } -export const DexAttr = { - NON_SHINY: 1n, - SHINY: 2n, - MALE: 4n, - FEMALE: 8n, - DEFAULT_VARIANT: 16n, - VARIANT_2: 32n, - VARIANT_3: 64n, - DEFAULT_FORM: 128n, -}; - -export interface DexAttrProps { - shiny: boolean; - female: boolean; - variant: Variant; - formIndex: number; -} - -export const AbilityAttr = { - ABILITY_1: 1, - ABILITY_2: 2, - ABILITY_HIDDEN: 4, -}; - -export type RunHistoryData = Record; - -export interface RunEntry { - entry: SessionSaveData; - isVictory: boolean; - /*Automatically set to false at the moment - implementation TBD*/ - isFavorite: boolean; -} - export type StarterMoveset = [Moves] | [Moves, Moves] | [Moves, Moves, Moves] | [Moves, Moves, Moves, Moves]; export interface StarterFormMoveData { @@ -260,6 +228,39 @@ export interface StarterPreferences { [key: number]: StarterAttributes; } +export interface DexAttrProps { + shiny: boolean; + female: boolean; + variant: Variant; + formIndex: number; +} + +export type RunHistoryData = Record; + +export interface RunEntry { + entry: SessionSaveData; + isVictory: boolean; + /*Automatically set to false at the moment - implementation TBD*/ + isFavorite: boolean; +} + +export const DexAttr = { + NON_SHINY: 1n, + SHINY: 2n, + MALE: 4n, + FEMALE: 8n, + DEFAULT_VARIANT: 16n, + VARIANT_2: 32n, + VARIANT_3: 64n, + DEFAULT_FORM: 128n, +}; + +export const AbilityAttr = { + ABILITY_1: 1, + ABILITY_2: 2, + ABILITY_HIDDEN: 4, +}; + // the latest data saved/loaded for the Starter Preferences. Required to reduce read/writes. Initialize as "{}", since this is the default value and no data needs to be stored if present. // if they ever add private static variables, move this into StarterPrefs const StarterPrefers_DEFAULT: string = "{}"; @@ -1553,16 +1554,18 @@ export class GameData { try { dataName = GameDataType[dataType].toLowerCase(); switch (dataType) { - case GameDataType.SYSTEM: + case GameDataType.SYSTEM: { dataStr = this.convertSystemDataStr(dataStr); const systemData = this.parseSystemData(dataStr); valid = !!systemData.dexData && !!systemData.timestamp; break; - case GameDataType.SESSION: + } + case GameDataType.SESSION: { const sessionData = this.parseSessionData(dataStr); valid = !!sessionData.party && !!sessionData.enemyParty && !!sessionData.timestamp; break; - case GameDataType.RUN_HISTORY: + } + case GameDataType.RUN_HISTORY: { const data = JSON.parse(dataStr); const keys = Object.keys(data); dataName = i18next.t("menuUiHandler:RUN_HISTORY").toLowerCase(); @@ -1572,6 +1575,7 @@ export class GameData { ["isFavorite", "isVictory", "entry"].every(v => entryKeys.includes(v)) && entryKeys.length === 3; }); break; + } case GameDataType.SETTINGS: case GameDataType.TUTORIALS: valid = true; diff --git a/src/ui/starter-select-ui-handler.ts b/src/ui/starter-select-ui-handler.ts index 91940d3af76..1599c86aa87 100644 --- a/src/ui/starter-select-ui-handler.ts +++ b/src/ui/starter-select-ui-handler.ts @@ -4553,4 +4553,13 @@ export default class StarterSelectUiHandler extends MessageUiHandler { icon.setFrame(species.getIconId(female, formIndex, false, variant)); } } + + /** + * Clears this UI's starter preferences. + * + * Designed to be used for unit tests that utilize this UI. + */ + clearStarterPreferences() { + this.starterPreferences = {}; + } } diff --git a/test/abilities/steely_spirit.test.ts b/test/abilities/steely_spirit.test.ts index 9b4d32efcae..b180ff8919e 100644 --- a/test/abilities/steely_spirit.test.ts +++ b/test/abilities/steely_spirit.test.ts @@ -12,7 +12,8 @@ describe("Abilities - Steely Spirit", () => { let game: GameManager; const steelySpiritMultiplier = 1.5; const moveToCheck = Moves.IRON_HEAD; - const ironHeadPower = allMoves[moveToCheck].power; + + let ironHeadPower: number; beforeAll(() => { phaserGame = new Phaser.Game({ @@ -25,6 +26,7 @@ describe("Abilities - Steely Spirit", () => { }); beforeEach(() => { + ironHeadPower = allMoves[moveToCheck].power; game = new GameManager(phaserGame); game.override.battleType("double"); game.override.enemySpecies(Species.SHUCKLE); diff --git a/test/abilities/supreme_overlord.test.ts b/test/abilities/supreme_overlord.test.ts index b70acbd8d17..a71bf0a9354 100644 --- a/test/abilities/supreme_overlord.test.ts +++ b/test/abilities/supreme_overlord.test.ts @@ -1,4 +1,5 @@ import { Moves } from "#app/enums/moves"; +import type Move from "#app/data/moves/move"; import { Abilities } from "#enums/abilities"; import { Species } from "#enums/species"; import { BattlerIndex } from "#app/battle"; @@ -12,8 +13,8 @@ describe("Abilities - Supreme Overlord", () => { let phaserGame: Phaser.Game; let game: GameManager; - const move = allMoves[Moves.TACKLE]; - const basePower = move.power; + let move: Move; + let basePower: number; beforeAll(() => { phaserGame = new Phaser.Game({ @@ -26,6 +27,8 @@ describe("Abilities - Supreme Overlord", () => { }); beforeEach(() => { + move = allMoves[Moves.TACKLE]; + basePower = move.power; game = new GameManager(phaserGame); game.override .battleType("single") diff --git a/test/abilities/unseen_fist.test.ts b/test/abilities/unseen_fist.test.ts index 73ae25ff3b0..459bb00628c 100644 --- a/test/abilities/unseen_fist.test.ts +++ b/test/abilities/unseen_fist.test.ts @@ -32,22 +32,22 @@ describe("Abilities - Unseen Fist", () => { game.override.enemyLevel(100); }); - it("should cause a contact move to ignore Protect", () => - testUnseenFistHitResult(game, Moves.QUICK_ATTACK, Moves.PROTECT, true)); + it("should cause a contact move to ignore Protect", async () => + await testUnseenFistHitResult(game, Moves.QUICK_ATTACK, Moves.PROTECT, true)); - it("should not cause a non-contact move to ignore Protect", () => - testUnseenFistHitResult(game, Moves.ABSORB, Moves.PROTECT, false)); + it("should not cause a non-contact move to ignore Protect", async () => + await testUnseenFistHitResult(game, Moves.ABSORB, Moves.PROTECT, false)); it("should not apply if the source has Long Reach", async () => { game.override.passiveAbility(Abilities.LONG_REACH); await testUnseenFistHitResult(game, Moves.QUICK_ATTACK, Moves.PROTECT, false); }); - it("should cause a contact move to ignore Wide Guard", () => - testUnseenFistHitResult(game, Moves.BREAKING_SWIPE, Moves.WIDE_GUARD, true)); + it("should cause a contact move to ignore Wide Guard", async () => + await testUnseenFistHitResult(game, Moves.BREAKING_SWIPE, Moves.WIDE_GUARD, true)); - it("should not cause a non-contact move to ignore Wide Guard", () => - testUnseenFistHitResult(game, Moves.BULLDOZE, Moves.WIDE_GUARD, false)); + it("should not cause a non-contact move to ignore Wide Guard", async () => + await testUnseenFistHitResult(game, Moves.BULLDOZE, Moves.WIDE_GUARD, false)); it("should cause a contact move to ignore Protect, but not Substitute", async () => { game.override.enemyLevel(1); diff --git a/test/abilities/wonder_skin.test.ts b/test/abilities/wonder_skin.test.ts index f2cb0faed72..db746831753 100644 --- a/test/abilities/wonder_skin.test.ts +++ b/test/abilities/wonder_skin.test.ts @@ -56,16 +56,21 @@ describe("Abilities - Wonder Skin", () => { expect(moveToCheck.calculateBattleAccuracy).toHaveReturnedWith(100); }); - const bypassAbilities = [Abilities.MOLD_BREAKER, Abilities.TERAVOLT, Abilities.TURBOBLAZE]; + const bypassAbilities = [ + [Abilities.MOLD_BREAKER, "Mold Breaker"], + [Abilities.TERAVOLT, "Teravolt"], + [Abilities.TURBOBLAZE, "Turboblaze"], + ]; bypassAbilities.forEach(ability => { - it(`does not affect pokemon with ${allAbilities[ability].name}`, async () => { + it(`does not affect pokemon with ${ability[1]}`, async () => { const moveToCheck = allMoves[Moves.CHARM]; - game.override.ability(ability); + // @ts-ignore ts doesn't know that ability[0] is an ability and not a string... + game.override.ability(ability[0]); vi.spyOn(moveToCheck, "calculateBattleAccuracy"); - await game.startBattle([Species.PIKACHU]); + await game.classicMode.startBattle([Species.PIKACHU]); game.move.select(Moves.CHARM); await game.phaseInterceptor.to(MoveEffectPhase); diff --git a/test/achievements/achievement.test.ts b/test/achievements/achievement.test.ts index 26d33adb00a..5c53e38e208 100644 --- a/test/achievements/achievement.test.ts +++ b/test/achievements/achievement.test.ts @@ -14,7 +14,7 @@ import { NumberHolder } from "#app/utils"; import GameManager from "#test/testUtils/gameManager"; import Phaser from "phaser"; import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; -import BattleScene from "#app/battle-scene"; +import type BattleScene from "#app/battle-scene"; describe("check some Achievement related stuff", () => { it("should check Achievement creation", () => { @@ -77,6 +77,25 @@ describe("Achv", () => { }); describe("MoneyAchv", () => { + let phaserGame: Phaser.Game; + let game: GameManager; + let scene: BattleScene; + + beforeAll(() => { + phaserGame = new Phaser.Game({ + type: Phaser.HEADLESS, + }); + }); + + afterEach(() => { + game.phaseInterceptor.restoreOg(); + }); + + beforeEach(() => { + game = new GameManager(phaserGame); + scene = game.scene; + }); + it("should create an instance of MoneyAchv", () => { const moneyAchv = new MoneyAchv("", "Test Money Achievement", 10000, "money_icon", 10); expect(moneyAchv).toBeInstanceOf(MoneyAchv); @@ -85,7 +104,6 @@ describe("MoneyAchv", () => { it("should validate the achievement based on the money amount", () => { const moneyAchv = new MoneyAchv("", "Test Money Achievement", 10000, "money_icon", 10); - const scene = new BattleScene(); scene.money = 5000; expect(moneyAchv.validate([])).toBe(false); diff --git a/test/battlerTags/substitute.test.ts b/test/battlerTags/substitute.test.ts index e80453d2933..fca3dc5ef7e 100644 --- a/test/battlerTags/substitute.test.ts +++ b/test/battlerTags/substitute.test.ts @@ -1,22 +1,40 @@ -import { beforeEach, describe, expect, it, vi } from "vitest"; +import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; import type { PokemonTurnData, TurnMove, PokemonMove } from "#app/field/pokemon"; import type Pokemon from "#app/field/pokemon"; import { MoveResult } from "#app/field/pokemon"; -import BattleScene from "#app/battle-scene"; +import type BattleScene from "#app/battle-scene"; import { BattlerTagLapseType, BindTag, SubstituteTag } from "#app/data/battler-tags"; import { Moves } from "#app/enums/moves"; import { PokemonAnimType } from "#app/enums/pokemon-anim-type"; import * as messages from "#app/messages"; import { allMoves } from "#app/data/moves/move"; import type { MoveEffectPhase } from "#app/phases/move-effect-phase"; +import GameManager from "#test/testUtils/gameManager"; describe("BattlerTag - SubstituteTag", () => { + let phaserGame: Phaser.Game; + let game: GameManager; + + beforeAll(() => { + phaserGame = new Phaser.Game({ + type: Phaser.HEADLESS, + }); + }); + + afterEach(() => { + game.phaseInterceptor.restoreOg(); + }); + + beforeEach(() => { + game = new GameManager(phaserGame); + }); + let mockPokemon: Pokemon; describe("onAdd behavior", () => { beforeEach(() => { mockPokemon = { - scene: new BattleScene(), + scene: game.scene, hp: 101, id: 0, getMaxHp: vi.fn().mockReturnValue(101) as Pokemon["getMaxHp"], @@ -77,7 +95,7 @@ describe("BattlerTag - SubstituteTag", () => { describe("onRemove behavior", () => { beforeEach(() => { mockPokemon = { - scene: new BattleScene(), + scene: game.scene, hp: 101, id: 0, isFainted: vi.fn().mockReturnValue(false) as Pokemon["isFainted"], @@ -109,7 +127,7 @@ describe("BattlerTag - SubstituteTag", () => { describe("lapse behavior", () => { beforeEach(() => { mockPokemon = { - scene: new BattleScene(), + scene: game.scene, hp: 101, id: 0, turnData: { acted: true } as PokemonTurnData, diff --git a/test/data/status_effect.test.ts b/test/data/status_effect.test.ts index 61dafc1c9b8..0fd2daa308b 100644 --- a/test/data/status_effect.test.ts +++ b/test/data/status_effect.test.ts @@ -13,17 +13,12 @@ import { Species } from "#enums/species"; import { StatusEffect } from "#enums/status-effect"; import GameManager from "#test/testUtils/gameManager"; import { mockI18next } from "#test/testUtils/testUtils"; -import i18next from "i18next"; import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; const pokemonName = "PKM"; const sourceText = "SOURCE"; describe("Status Effect Messages", () => { - beforeAll(async () => { - await i18next.init(); - }); - describe("NONE", () => { const statusEffect = StatusEffect.NONE; @@ -31,7 +26,6 @@ describe("Status Effect Messages", () => { mockI18next(); const text = getStatusEffectObtainText(statusEffect, pokemonName); - console.log("text:", text); expect(text).toBe(""); const emptySourceText = getStatusEffectObtainText(statusEffect, pokemonName, ""); diff --git a/test/moves/copycat.test.ts b/test/moves/copycat.test.ts index c7242f0940e..0d9b0951f89 100644 --- a/test/moves/copycat.test.ts +++ b/test/moves/copycat.test.ts @@ -13,7 +13,7 @@ describe("Moves - Copycat", () => { let phaserGame: Phaser.Game; let game: GameManager; - const randomMoveAttr = allMoves[Moves.METRONOME].getAttrs(RandomMoveAttr)[0]; + let randomMoveAttr: RandomMoveAttr; beforeAll(() => { phaserGame = new Phaser.Game({ @@ -26,6 +26,7 @@ describe("Moves - Copycat", () => { }); beforeEach(() => { + randomMoveAttr = allMoves[Moves.METRONOME].getAttrs(RandomMoveAttr)[0]; game = new GameManager(phaserGame); game.override .moveset([Moves.COPYCAT, Moves.SPIKY_SHIELD, Moves.SWORDS_DANCE, Moves.SPLASH]) diff --git a/test/moves/dynamax_cannon.test.ts b/test/moves/dynamax_cannon.test.ts index 0ff0712710d..9cf3106b9c1 100644 --- a/test/moves/dynamax_cannon.test.ts +++ b/test/moves/dynamax_cannon.test.ts @@ -3,6 +3,7 @@ import { allMoves } from "#app/data/moves/move"; import { DamageAnimPhase } from "#app/phases/damage-anim-phase"; import { MoveEffectPhase } from "#app/phases/move-effect-phase"; import { Moves } from "#enums/moves"; +import type Move from "#app/data/moves/move"; import { Species } from "#enums/species"; import GameManager from "#test/testUtils/gameManager"; import Phaser from "phaser"; @@ -12,7 +13,7 @@ describe("Moves - Dynamax Cannon", () => { let phaserGame: Phaser.Game; let game: GameManager; - const dynamaxCannon = allMoves[Moves.DYNAMAX_CANNON]; + let dynamaxCannon: Move; beforeAll(() => { phaserGame = new Phaser.Game({ @@ -25,6 +26,7 @@ describe("Moves - Dynamax Cannon", () => { }); beforeEach(() => { + dynamaxCannon = allMoves[Moves.DYNAMAX_CANNON]; game = new GameManager(phaserGame); game.override.moveset([dynamaxCannon.id]); diff --git a/test/moves/fusion_flare_bolt.test.ts b/test/moves/fusion_flare_bolt.test.ts index 9a379cb4588..c340aeea63f 100644 --- a/test/moves/fusion_flare_bolt.test.ts +++ b/test/moves/fusion_flare_bolt.test.ts @@ -1,6 +1,7 @@ import { Stat } from "#enums/stat"; import { BattlerIndex } from "#app/battle"; import { allMoves } from "#app/data/moves/move"; +import type Move from "#app/data/moves/move"; import { DamageAnimPhase } from "#app/phases/damage-anim-phase"; import { MoveEffectPhase } from "#app/phases/move-effect-phase"; import { MoveEndPhase } from "#app/phases/move-end-phase"; @@ -15,8 +16,8 @@ describe("Moves - Fusion Flare and Fusion Bolt", () => { let phaserGame: Phaser.Game; let game: GameManager; - const fusionFlare = allMoves[Moves.FUSION_FLARE]; - const fusionBolt = allMoves[Moves.FUSION_BOLT]; + let fusionFlare: Move; + let fusionBolt: Move; beforeAll(() => { phaserGame = new Phaser.Game({ @@ -29,6 +30,8 @@ describe("Moves - Fusion Flare and Fusion Bolt", () => { }); beforeEach(() => { + fusionFlare = allMoves[Moves.FUSION_FLARE]; + fusionBolt = allMoves[Moves.FUSION_BOLT]; game = new GameManager(phaserGame); game.override.moveset([fusionFlare.id, fusionBolt.id]); game.override.startingLevel(1); @@ -45,7 +48,7 @@ describe("Moves - Fusion Flare and Fusion Bolt", () => { }); it("FUSION_FLARE should double power of subsequent FUSION_BOLT", async () => { - await game.startBattle([Species.ZEKROM, Species.ZEKROM]); + await game.classicMode.startBattle([Species.ZEKROM, Species.ZEKROM]); game.move.select(fusionFlare.id, 0, BattlerIndex.ENEMY); game.move.select(fusionBolt.id, 1, BattlerIndex.ENEMY); @@ -65,7 +68,7 @@ describe("Moves - Fusion Flare and Fusion Bolt", () => { }, 20000); it("FUSION_BOLT should double power of subsequent FUSION_FLARE", async () => { - await game.startBattle([Species.ZEKROM, Species.ZEKROM]); + await game.classicMode.startBattle([Species.ZEKROM, Species.ZEKROM]); game.move.select(fusionBolt.id, 0, BattlerIndex.ENEMY); game.move.select(fusionFlare.id, 1, BattlerIndex.ENEMY); @@ -85,7 +88,7 @@ describe("Moves - Fusion Flare and Fusion Bolt", () => { }, 20000); it("FUSION_FLARE should double power of subsequent FUSION_BOLT if a move failed in between", async () => { - await game.startBattle([Species.ZEKROM, Species.ZEKROM]); + await game.classicMode.startBattle([Species.ZEKROM, Species.ZEKROM]); game.move.select(fusionFlare.id, 0, BattlerIndex.PLAYER); game.move.select(fusionBolt.id, 1, BattlerIndex.PLAYER); @@ -111,7 +114,7 @@ describe("Moves - Fusion Flare and Fusion Bolt", () => { it("FUSION_FLARE should not double power of subsequent FUSION_BOLT if a move succeeded in between", async () => { game.override.enemyMoveset([Moves.SPLASH, Moves.SPLASH, Moves.SPLASH, Moves.SPLASH]); - await game.startBattle([Species.ZEKROM, Species.ZEKROM]); + await game.classicMode.startBattle([Species.ZEKROM, Species.ZEKROM]); game.move.select(fusionFlare.id, 0, BattlerIndex.ENEMY); game.move.select(fusionBolt.id, 1, BattlerIndex.ENEMY); @@ -156,7 +159,7 @@ describe("Moves - Fusion Flare and Fusion Bolt", () => { it("FUSION_FLARE and FUSION_BOLT alternating throughout turn should double power of subsequent moves", async () => { game.override.enemyMoveset([fusionFlare.id, fusionFlare.id, fusionFlare.id, fusionFlare.id]); - await game.startBattle([Species.ZEKROM, Species.ZEKROM]); + await game.classicMode.startBattle([Species.ZEKROM, Species.ZEKROM]); const party = game.scene.getPlayerParty(); const enemyParty = game.scene.getEnemyParty(); @@ -210,7 +213,7 @@ describe("Moves - Fusion Flare and Fusion Bolt", () => { it("FUSION_FLARE and FUSION_BOLT alternating throughout turn should double power of subsequent moves if moves are aimed at allies", async () => { game.override.enemyMoveset([fusionFlare.id, fusionFlare.id, fusionFlare.id, fusionFlare.id]); - await game.startBattle([Species.ZEKROM, Species.ZEKROM]); + await game.classicMode.startBattle([Species.ZEKROM, Species.ZEKROM]); const party = game.scene.getPlayerParty(); const enemyParty = game.scene.getEnemyParty(); diff --git a/test/moves/hard_press.test.ts b/test/moves/hard_press.test.ts index 1bb6adc8e90..8891f0bf0e2 100644 --- a/test/moves/hard_press.test.ts +++ b/test/moves/hard_press.test.ts @@ -6,12 +6,13 @@ import { Species } from "#enums/species"; import GameManager from "#test/testUtils/gameManager"; import Phaser from "phaser"; import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; +import type Move from "#app/data/moves/move"; describe("Moves - Hard Press", () => { let phaserGame: Phaser.Game; let game: GameManager; - const moveToCheck = allMoves[Moves.HARD_PRESS]; + let moveToCheck: Move; beforeAll(() => { phaserGame = new Phaser.Game({ @@ -24,6 +25,7 @@ describe("Moves - Hard Press", () => { }); beforeEach(() => { + moveToCheck = allMoves[Moves.HARD_PRESS]; game = new GameManager(phaserGame); game.override.battleType("single"); game.override.ability(Abilities.BALL_FETCH); diff --git a/test/moves/last_respects.test.ts b/test/moves/last_respects.test.ts index 57752cea1af..ccab8a43415 100644 --- a/test/moves/last_respects.test.ts +++ b/test/moves/last_respects.test.ts @@ -4,6 +4,7 @@ import { Species } from "#enums/species"; import { Abilities } from "#enums/abilities"; import GameManager from "#test/testUtils/gameManager"; import { allMoves } from "#app/data/moves/move"; +import type Move from "#app/data/moves/move"; import { MoveEffectPhase } from "#app/phases/move-effect-phase"; import Phaser from "phaser"; import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; @@ -12,8 +13,8 @@ describe("Moves - Last Respects", () => { let phaserGame: Phaser.Game; let game: GameManager; - const move = allMoves[Moves.LAST_RESPECTS]; - const basePower = move.power; + let move: Move; + let basePower: number; beforeAll(() => { phaserGame = new Phaser.Game({ @@ -27,6 +28,8 @@ describe("Moves - Last Respects", () => { beforeEach(() => { game = new GameManager(phaserGame); + move = allMoves[Moves.LAST_RESPECTS]; + basePower = move.power; game.override .battleType("single") .disableCrits() diff --git a/test/moves/metronome.test.ts b/test/moves/metronome.test.ts index 15790777ed3..80f32a3a6fb 100644 --- a/test/moves/metronome.test.ts +++ b/test/moves/metronome.test.ts @@ -13,7 +13,7 @@ describe("Moves - Metronome", () => { let phaserGame: Phaser.Game; let game: GameManager; - const randomMoveAttr = allMoves[Moves.METRONOME].getAttrs(RandomMoveAttr)[0]; + let randomMoveAttr: RandomMoveAttr; beforeAll(() => { phaserGame = new Phaser.Game({ @@ -26,6 +26,7 @@ describe("Moves - Metronome", () => { }); beforeEach(() => { + randomMoveAttr = allMoves[Moves.METRONOME].getAttrs(RandomMoveAttr)[0]; game = new GameManager(phaserGame); game.override .moveset([Moves.METRONOME, Moves.SPLASH]) diff --git a/test/moves/rage_fist.test.ts b/test/moves/rage_fist.test.ts index 8bcb212d60e..f44901c5aba 100644 --- a/test/moves/rage_fist.test.ts +++ b/test/moves/rage_fist.test.ts @@ -3,6 +3,7 @@ import { Abilities } from "#enums/abilities"; import { Moves } from "#enums/moves"; import { Species } from "#enums/species"; import { allMoves } from "#app/data/moves/move"; +import type Move from "#app/data/moves/move"; import GameManager from "#test/testUtils/gameManager"; import Phaser from "phaser"; import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; @@ -10,7 +11,7 @@ import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vite describe("Moves - Rage Fist", () => { let phaserGame: Phaser.Game; let game: GameManager; - const move = allMoves[Moves.RAGE_FIST]; + let move: Move; beforeAll(() => { phaserGame = new Phaser.Game({ @@ -23,6 +24,7 @@ describe("Moves - Rage Fist", () => { }); beforeEach(() => { + move = allMoves[Moves.RAGE_FIST]; game = new GameManager(phaserGame); game.override .battleType("single") diff --git a/test/moves/retaliate.test.ts b/test/moves/retaliate.test.ts index 5cc0b08ccc6..e916c9ffeaa 100644 --- a/test/moves/retaliate.test.ts +++ b/test/moves/retaliate.test.ts @@ -4,12 +4,13 @@ import GameManager from "#test/testUtils/gameManager"; import { Species } from "#enums/species"; import { Moves } from "#enums/moves"; import { allMoves } from "#app/data/moves/move"; +import type Move from "#app/data/moves/move"; describe("Moves - Retaliate", () => { let phaserGame: Phaser.Game; let game: GameManager; - const retaliate = allMoves[Moves.RETALIATE]; + let retaliate: Move; beforeAll(() => { phaserGame = new Phaser.Game({ @@ -22,6 +23,7 @@ describe("Moves - Retaliate", () => { }); beforeEach(() => { + retaliate = allMoves[Moves.RETALIATE]; game = new GameManager(phaserGame); game.override .battleType("single") diff --git a/test/moves/shell_side_arm.test.ts b/test/moves/shell_side_arm.test.ts index 47da5e1c2f7..a5b065b76cb 100644 --- a/test/moves/shell_side_arm.test.ts +++ b/test/moves/shell_side_arm.test.ts @@ -1,5 +1,6 @@ import { BattlerIndex } from "#app/battle"; import { allMoves, ShellSideArmCategoryAttr } from "#app/data/moves/move"; +import type Move from "#app/data/moves/move"; import { Abilities } from "#enums/abilities"; import { Moves } from "#enums/moves"; import { Species } from "#enums/species"; @@ -10,8 +11,8 @@ import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vite describe("Moves - Shell Side Arm", () => { let phaserGame: Phaser.Game; let game: GameManager; - const shellSideArm = allMoves[Moves.SHELL_SIDE_ARM]; - const shellSideArmAttr = shellSideArm.getAttrs(ShellSideArmCategoryAttr)[0]; + let shellSideArm: Move; + let shellSideArmAttr: ShellSideArmCategoryAttr; beforeAll(() => { phaserGame = new Phaser.Game({ @@ -24,6 +25,8 @@ describe("Moves - Shell Side Arm", () => { }); beforeEach(() => { + shellSideArm = allMoves[Moves.SHELL_SIDE_ARM]; + shellSideArmAttr = shellSideArm.getAttrs(ShellSideArmCategoryAttr)[0]; game = new GameManager(phaserGame); game.override .moveset([Moves.SHELL_SIDE_ARM, Moves.SPLASH]) diff --git a/test/moves/spit_up.test.ts b/test/moves/spit_up.test.ts index d986ae4d141..d71647bda52 100644 --- a/test/moves/spit_up.test.ts +++ b/test/moves/spit_up.test.ts @@ -7,6 +7,7 @@ import { MoveResult } from "#app/field/pokemon"; import GameManager from "#test/testUtils/gameManager"; import { Abilities } from "#enums/abilities"; import { Moves } from "#enums/moves"; +import type Move from "#app/data/moves/move"; import { Species } from "#enums/species"; import Phaser from "phaser"; import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; @@ -17,7 +18,7 @@ describe("Moves - Spit Up", () => { let phaserGame: Phaser.Game; let game: GameManager; - const spitUp = allMoves[Moves.SPIT_UP]; + let spitUp: Move; beforeAll(() => { phaserGame = new Phaser.Game({ type: Phaser.HEADLESS }); @@ -28,6 +29,7 @@ describe("Moves - Spit Up", () => { }); beforeEach(() => { + spitUp = allMoves[Moves.SPIT_UP]; game = new GameManager(phaserGame); game.override.battleType("single"); diff --git a/test/moves/tera_blast.test.ts b/test/moves/tera_blast.test.ts index dffe39f4d87..c1a2b999fa0 100644 --- a/test/moves/tera_blast.test.ts +++ b/test/moves/tera_blast.test.ts @@ -1,6 +1,7 @@ import { BattlerIndex } from "#app/battle"; import { Stat } from "#enums/stat"; import { allMoves, TeraMoveCategoryAttr } from "#app/data/moves/move"; +import type Move from "#app/data/moves/move"; import { PokemonType } from "#enums/pokemon-type"; import { Abilities } from "#app/enums/abilities"; import { HitResult } from "#app/field/pokemon"; @@ -13,13 +14,16 @@ import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vite describe("Moves - Tera Blast", () => { let phaserGame: Phaser.Game; let game: GameManager; - const moveToCheck = allMoves[Moves.TERA_BLAST]; - const teraBlastAttr = moveToCheck.getAttrs(TeraMoveCategoryAttr)[0]; + + let moveToCheck: Move; + let teraBlastAttr: TeraMoveCategoryAttr; beforeAll(() => { phaserGame = new Phaser.Game({ type: Phaser.HEADLESS, }); + moveToCheck = allMoves[Moves.TERA_BLAST]; + teraBlastAttr = moveToCheck.getAttrs(TeraMoveCategoryAttr)[0]; }); afterEach(() => { diff --git a/test/moves/triple_arrows.test.ts b/test/moves/triple_arrows.test.ts index c1114b69b99..eb434b25815 100644 --- a/test/moves/triple_arrows.test.ts +++ b/test/moves/triple_arrows.test.ts @@ -1,6 +1,7 @@ import { allMoves, FlinchAttr, StatStageChangeAttr } from "#app/data/moves/move"; import { Abilities } from "#enums/abilities"; import { Moves } from "#enums/moves"; +import type Move from "#app/data/moves/move"; import { Species } from "#enums/species"; import GameManager from "#test/testUtils/gameManager"; import Phaser from "phaser"; @@ -9,14 +10,17 @@ import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vite describe("Moves - Triple Arrows", () => { let phaserGame: Phaser.Game; let game: GameManager; - const tripleArrows = allMoves[Moves.TRIPLE_ARROWS]; - const flinchAttr = tripleArrows.getAttrs(FlinchAttr)[0]; - const defDropAttr = tripleArrows.getAttrs(StatStageChangeAttr)[0]; + let tripleArrows: Move; + let flinchAttr: FlinchAttr; + let defDropAttr: StatStageChangeAttr; beforeAll(() => { phaserGame = new Phaser.Game({ type: Phaser.HEADLESS, }); + tripleArrows = allMoves[Moves.TRIPLE_ARROWS]; + flinchAttr = tripleArrows.getAttrs(FlinchAttr)[0]; + defDropAttr = tripleArrows.getAttrs(StatStageChangeAttr)[0]; }); afterEach(() => { diff --git a/test/mystery-encounter/encounter-test-utils.ts b/test/mystery-encounter/encounter-test-utils.ts index 19ec364618e..8c54e0dd606 100644 --- a/test/mystery-encounter/encounter-test-utils.ts +++ b/test/mystery-encounter/encounter-test-utils.ts @@ -48,16 +48,6 @@ export async function runMysteryEncounterToEnd( ); if (isBattle) { - game.onNextPrompt( - "DamageAnimPhase", - Mode.MESSAGE, - () => { - game.setMode(Mode.MESSAGE); - game.endPhase(); - }, - () => game.isCurrentPhase(CommandPhase), - ); - game.onNextPrompt( "CheckSwitchPhase", Mode.CONFIRM, diff --git a/test/phases/phases.test.ts b/test/phases/phases.test.ts index 4aabeb55b9e..96225c9151c 100644 --- a/test/phases/phases.test.ts +++ b/test/phases/phases.test.ts @@ -31,7 +31,7 @@ describe("Phases", () => { it("should start the login phase", async () => { const loginPhase = new LoginPhase(); scene.unshiftPhase(loginPhase); - await game.phaseInterceptor.run(LoginPhase); + await game.phaseInterceptor.to(LoginPhase); expect(scene.ui.getMode()).to.equal(Mode.MESSAGE); }); }); @@ -40,7 +40,7 @@ describe("Phases", () => { it("should start the title phase", async () => { const titlePhase = new TitlePhase(); scene.unshiftPhase(titlePhase); - await game.phaseInterceptor.run(TitlePhase); + await game.phaseInterceptor.to(TitlePhase); expect(scene.ui.getMode()).to.equal(Mode.TITLE); }); }); @@ -49,7 +49,7 @@ describe("Phases", () => { it("should start the unavailable phase", async () => { const unavailablePhase = new UnavailablePhase(); scene.unshiftPhase(unavailablePhase); - await game.phaseInterceptor.run(UnavailablePhase); + await game.phaseInterceptor.to(UnavailablePhase); expect(scene.ui.getMode()).to.equal(Mode.UNAVAILABLE); }, 20000); }); diff --git a/test/phases/select-modifier-phase.test.ts b/test/phases/select-modifier-phase.test.ts index bb3d5debc7c..d352acea77a 100644 --- a/test/phases/select-modifier-phase.test.ts +++ b/test/phases/select-modifier-phase.test.ts @@ -48,8 +48,8 @@ describe("SelectModifierPhase", () => { it("should start a select modifier phase", async () => { initSceneWithoutEncounterPhase(scene, [Species.ABRA, Species.VOLCARONA]); const selectModifierPhase = new SelectModifierPhase(); - scene.pushPhase(selectModifierPhase); - await game.phaseInterceptor.run(SelectModifierPhase); + scene.unshiftPhase(selectModifierPhase); + await game.phaseInterceptor.to(SelectModifierPhase); expect(scene.ui.getMode()).to.equal(Mode.MODIFIER_SELECT); }); diff --git a/test/plugins/api/pokerogue-account-api.test.ts b/test/plugins/api/pokerogue-account-api.test.ts index 9ec98b6a59f..e7e1b2d52b0 100644 --- a/test/plugins/api/pokerogue-account-api.test.ts +++ b/test/plugins/api/pokerogue-account-api.test.ts @@ -4,11 +4,17 @@ import { PokerogueAccountApi } from "#app/plugins/api/pokerogue-account-api"; import { getApiBaseUrl } from "#test/testUtils/testUtils"; import * as Utils from "#app/utils"; import { http, HttpResponse } from "msw"; -import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { beforeAll, afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { initServerForApiTests } from "#test/testUtils/testFileInitialization"; +import type { SetupServerApi } from "msw/node"; const apiBase = getApiBaseUrl(); const accountApi = new PokerogueAccountApi(apiBase); -const { server } = global; +let server: SetupServerApi; + +beforeAll(async () => { + server = await initServerForApiTests(); +}); afterEach(() => { server.resetHandlers(); diff --git a/test/plugins/api/pokerogue-admin-api.test.ts b/test/plugins/api/pokerogue-admin-api.test.ts index 0ce727b88da..08c4cf0dc45 100644 --- a/test/plugins/api/pokerogue-admin-api.test.ts +++ b/test/plugins/api/pokerogue-admin-api.test.ts @@ -9,11 +9,17 @@ import type { import { PokerogueAdminApi } from "#app/plugins/api/pokerogue-admin-api"; import { getApiBaseUrl } from "#test/testUtils/testUtils"; import { http, HttpResponse } from "msw"; -import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { beforeAll, afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { initServerForApiTests } from "#test/testUtils/testFileInitialization"; +import type { SetupServerApi } from "msw/node"; const apiBase = getApiBaseUrl(); const adminApi = new PokerogueAdminApi(apiBase); -const { server } = global; +let server: SetupServerApi; + +beforeAll(async () => { + server = await initServerForApiTests(); +}); afterEach(() => { server.resetHandlers(); diff --git a/test/plugins/api/pokerogue-api.test.ts b/test/plugins/api/pokerogue-api.test.ts index 241453866a5..c53a38e23ab 100644 --- a/test/plugins/api/pokerogue-api.test.ts +++ b/test/plugins/api/pokerogue-api.test.ts @@ -2,10 +2,16 @@ import type { TitleStatsResponse } from "#app/@types/PokerogueApi"; import { pokerogueApi } from "#app/plugins/api/pokerogue-api"; import { getApiBaseUrl } from "#test/testUtils/testUtils"; import { http, HttpResponse } from "msw"; -import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { beforeAll, afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { initServerForApiTests } from "#test/testUtils/testFileInitialization"; +import type { SetupServerApi } from "msw/node"; const apiBase = getApiBaseUrl(); -const { server } = global; +let server: SetupServerApi; + +beforeAll(async () => { + server = await initServerForApiTests(); +}); afterEach(() => { server.resetHandlers(); diff --git a/test/plugins/api/pokerogue-daily-api.test.ts b/test/plugins/api/pokerogue-daily-api.test.ts index 95d938e6625..563e6d09009 100644 --- a/test/plugins/api/pokerogue-daily-api.test.ts +++ b/test/plugins/api/pokerogue-daily-api.test.ts @@ -3,11 +3,17 @@ import { PokerogueDailyApi } from "#app/plugins/api/pokerogue-daily-api"; import { getApiBaseUrl } from "#test/testUtils/testUtils"; import { ScoreboardCategory, type RankingEntry } from "#app/ui/daily-run-scoreboard"; import { http, HttpResponse } from "msw"; -import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { beforeAll, afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { initServerForApiTests } from "#test/testUtils/testFileInitialization"; +import type { SetupServerApi } from "msw/node"; const apiBase = getApiBaseUrl(); const dailyApi = new PokerogueDailyApi(apiBase); -const { server } = global; +let server: SetupServerApi; + +beforeAll(async () => { + server = await initServerForApiTests(); +}); afterEach(() => { server.resetHandlers(); diff --git a/test/plugins/api/pokerogue-savedata-api.test.ts b/test/plugins/api/pokerogue-savedata-api.test.ts index 47eafa0a933..481ba62c19d 100644 --- a/test/plugins/api/pokerogue-savedata-api.test.ts +++ b/test/plugins/api/pokerogue-savedata-api.test.ts @@ -2,11 +2,17 @@ import type { UpdateAllSavedataRequest } from "#app/@types/PokerogueSavedataApi" import { PokerogueSavedataApi } from "#app/plugins/api/pokerogue-savedata-api"; import { getApiBaseUrl } from "#test/testUtils/testUtils"; import { http, HttpResponse } from "msw"; -import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { beforeAll, afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { initServerForApiTests } from "#test/testUtils/testFileInitialization"; +import type { SetupServerApi } from "msw/node"; const apiBase = getApiBaseUrl(); const savedataApi = new PokerogueSavedataApi(apiBase); -const { server } = global; +let server: SetupServerApi; + +beforeAll(async () => { + server = await initServerForApiTests(); +}); afterEach(() => { server.resetHandlers(); diff --git a/test/plugins/api/pokerogue-session-savedata-api.test.ts b/test/plugins/api/pokerogue-session-savedata-api.test.ts index 67abc9c9336..d4c235ac51a 100644 --- a/test/plugins/api/pokerogue-session-savedata-api.test.ts +++ b/test/plugins/api/pokerogue-session-savedata-api.test.ts @@ -10,11 +10,17 @@ import { PokerogueSessionSavedataApi } from "#app/plugins/api/pokerogue-session- import type { SessionSaveData } from "#app/system/game-data"; import { getApiBaseUrl } from "#test/testUtils/testUtils"; import { http, HttpResponse } from "msw"; -import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { beforeAll, afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { initServerForApiTests } from "#test/testUtils/testFileInitialization"; +import type { SetupServerApi } from "msw/node"; const apiBase = getApiBaseUrl(); const sessionSavedataApi = new PokerogueSessionSavedataApi(apiBase); -const { server } = global; + +let server: SetupServerApi; +beforeAll(async () => { + server = await initServerForApiTests(); +}); afterEach(() => { server.resetHandlers(); diff --git a/test/plugins/api/pokerogue-system-savedata-api.test.ts b/test/plugins/api/pokerogue-system-savedata-api.test.ts index 81d863049f0..0c69ab8f922 100644 --- a/test/plugins/api/pokerogue-system-savedata-api.test.ts +++ b/test/plugins/api/pokerogue-system-savedata-api.test.ts @@ -6,13 +6,20 @@ import type { } from "#app/@types/PokerogueSystemSavedataApi"; import { PokerogueSystemSavedataApi } from "#app/plugins/api/pokerogue-system-savedata-api"; import type { SystemSaveData } from "#app/system/game-data"; +import { initServerForApiTests } from "#test/testUtils/testFileInitialization"; import { getApiBaseUrl } from "#test/testUtils/testUtils"; import { http, HttpResponse } from "msw"; -import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; +import type { SetupServerApi } from "msw/node"; const apiBase = getApiBaseUrl(); const systemSavedataApi = new PokerogueSystemSavedataApi(getApiBaseUrl()); -const { server } = global; + +let server: SetupServerApi; + +beforeAll(async () => { + server = await initServerForApiTests(); +}); afterEach(() => { server.resetHandlers(); diff --git a/test/pre.test.ts b/test/pre.test.ts deleted file mode 100644 index 6ed29dce481..00000000000 --- a/test/pre.test.ts +++ /dev/null @@ -1,6 +0,0 @@ -import Overrides, { defaultOverrides } from "#app/overrides"; -import { expect, test } from "vitest"; - -test("Overrides are default values", () => { - expect(Overrides).toEqual(defaultOverrides); -}); diff --git a/test/testUtils/gameManager.ts b/test/testUtils/gameManager.ts index 3289c0ade01..390e71af126 100644 --- a/test/testUtils/gameManager.ts +++ b/test/testUtils/gameManager.ts @@ -55,6 +55,9 @@ import TextInterceptor from "#test/testUtils/TextInterceptor"; import { AES, enc } from "crypto-js"; import fs from "node:fs"; import { expect, vi } from "vitest"; +import { globalScene } from "#app/global-scene"; +import type StarterSelectUiHandler from "#app/ui/starter-select-ui-handler"; +import { MockFetch } from "#test/testUtils/mocks/mockFetch"; /** * Class to manage the game state and transitions between phases. @@ -84,10 +87,34 @@ export default class GameManager { ErrorInterceptor.getInstance().clear(); BattleScene.prototype.randBattleSeedInt = (range, min = 0) => min + range - 1; // This simulates a max roll this.gameWrapper = new GameWrapper(phaserGame, bypassLogin); - this.scene = new BattleScene(); + + let firstTimeScene = false; + + if (globalScene) { + this.scene = globalScene; + } else { + this.scene = new BattleScene(); + this.gameWrapper.setScene(this.scene); + firstTimeScene = true; + } + this.phaseInterceptor = new PhaseInterceptor(this.scene); + + if (!firstTimeScene) { + this.scene.reset(false, true); + (this.scene.ui.handlers[Mode.STARTER_SELECT] as StarterSelectUiHandler).clearStarterPreferences(); + this.scene.clearAllPhases(); + + // Must be run after phase interceptor has been initialized. + + this.scene.pushPhase(new LoginPhase()); + this.scene.pushPhase(new TitlePhase()); + this.scene.shiftPhase(); + + this.gameWrapper.scene = this.scene; + } + this.textInterceptor = new TextInterceptor(this.scene); - this.gameWrapper.setScene(this.scene); this.override = new OverridesHelper(this); this.move = new MoveHelper(this); this.classicMode = new ClassicModeHelper(this); @@ -96,9 +123,12 @@ export default class GameManager { this.settings = new SettingsHelper(this); this.reload = new ReloadHelper(this); this.modifiers = new ModifierHelper(this); + this.override.sanitizeOverrides(); // Disables Mystery Encounters on all tests (can be overridden at test level) this.override.mysteryEncounterChance(0); + + global.fetch = vi.fn(MockFetch) as any; } /** diff --git a/test/testUtils/gameWrapper.ts b/test/testUtils/gameWrapper.ts index 6af36f22d24..28c7c4af80f 100644 --- a/test/testUtils/gameWrapper.ts +++ b/test/testUtils/gameWrapper.ts @@ -6,11 +6,11 @@ import Pokemon from "#app/field/pokemon"; import * as Utils from "#app/utils"; import { blobToString } from "#test/testUtils/gameManagerUtils"; import { MockClock } from "#test/testUtils/mocks/mockClock"; -import mockConsoleLog from "#test/testUtils/mocks/mockConsoleLog"; +import { MockConsoleLog } from "#test/testUtils/mocks/mockConsoleLog"; import { MockFetch } from "#test/testUtils/mocks/mockFetch"; import MockLoader from "#test/testUtils/mocks/mockLoader"; -import mockLocalStorage from "#test/testUtils/mocks/mockLocalStorage"; -import MockImage from "#test/testUtils/mocks/mocksContainer/mockImage"; +import { mockLocalStorage } from "#test/testUtils/mocks/mockLocalStorage"; +import { MockImage } from "#test/testUtils/mocks/mocksContainer/mockImage"; import MockTextureManager from "#test/testUtils/mocks/mockTextureManager"; import fs from "node:fs"; import Phaser from "phaser"; @@ -27,18 +27,6 @@ import UpdateList = Phaser.GameObjects.UpdateList; import { version } from "../../package.json"; import { MockTimedEventManager } from "./mocks/mockTimedEventManager"; -Object.defineProperty(window, "localStorage", { - value: mockLocalStorage(), -}); -Object.defineProperty(window, "console", { - value: mockConsoleLog(false), -}); - -BBCodeText.prototype.destroy = () => null; -BBCodeText.prototype.resize = () => null; -InputText.prototype.setElement = () => null; -InputText.prototype.resize = () => null; -Phaser.GameObjects.Image = MockImage; window.URL.createObjectURL = (blob: Blob) => { blobToString(blob).then((data: string) => { localStorage.setItem("toExport", data); @@ -53,25 +41,6 @@ window.matchMedia = () => ({ matches: false, }); -/** - * Sets this object's position relative to another object with a given offset - * @param guideObject {@linkcode Phaser.GameObjects.GameObject} to base the position off of - * @param x The relative x position - * @param y The relative y position - */ -const setPositionRelative = function (guideObject: any, x: number, y: number) { - const offsetX = guideObject.width * (-0.5 + (0.5 - guideObject.originX)); - const offsetY = guideObject.height * (-0.5 + (0.5 - guideObject.originY)); - this.setPosition(guideObject.x + offsetX + x, guideObject.y + offsetY + y); -}; - -Phaser.GameObjects.Container.prototype.setPositionRelative = setPositionRelative; -Phaser.GameObjects.Sprite.prototype.setPositionRelative = setPositionRelative; -Phaser.GameObjects.Image.prototype.setPositionRelative = setPositionRelative; -Phaser.GameObjects.NineSlice.prototype.setPositionRelative = setPositionRelative; -Phaser.GameObjects.Text.prototype.setPositionRelative = setPositionRelative; -Phaser.GameObjects.Rectangle.prototype.setPositionRelative = setPositionRelative; - export default class GameWrapper { public game: Phaser.Game; public scene: BattleScene; diff --git a/test/testUtils/helpers/overridesHelper.ts b/test/testUtils/helpers/overridesHelper.ts index e69e05bce45..2d56ae35fce 100644 --- a/test/testUtils/helpers/overridesHelper.ts +++ b/test/testUtils/helpers/overridesHelper.ts @@ -6,7 +6,7 @@ import type { GameModes } from "#app/game-mode"; import { getGameMode } from "#app/game-mode"; import type { ModifierOverride } from "#app/modifier/modifier-type"; import type { BattleStyle } from "#app/overrides"; -import Overrides from "#app/overrides"; +import Overrides, { defaultOverrides } from "#app/overrides"; import type { Unlockables } from "#app/system/unlockables"; import { Biome } from "#enums/biome"; import { Moves } from "#enums/moves"; @@ -15,8 +15,9 @@ import type { MysteryEncounterType } from "#enums/mystery-encounter-type"; import { Species } from "#enums/species"; import { StatusEffect } from "#enums/status-effect"; import type { WeatherType } from "#enums/weather-type"; -import { vi } from "vitest"; +import { expect, vi } from "vitest"; import { GameManagerHelper } from "./gameManagerHelper"; +import { shiftCharCodes } from "#app/utils"; /** * Helper to handle overrides in tests @@ -226,12 +227,7 @@ export class OverridesHelper extends GameManagerHelper { * @returns `this` */ public disableTrainerWaves(): this { - const realFn = getGameMode; - vi.spyOn(GameMode, "getGameMode").mockImplementation((gameMode: GameModes) => { - const mode = realFn(gameMode); - mode.hasTrainers = false; - return mode; - }); + vi.spyOn(Overrides, "DISABLE_STANDARD_TRAINERS_OVERRIDE", "get").mockReturnValue(true); this.log("Standard trainer waves are disabled!"); return this; } @@ -263,11 +259,8 @@ export class OverridesHelper extends GameManagerHelper { * @returns `this` */ public seed(seed: string): this { - vi.spyOn(this.game.scene, "resetSeed").mockImplementation(() => { - this.game.scene.waveSeed = seed; - Phaser.Math.RND.sow([seed]); - this.game.scene.rngCounter = 0; - }); + // Shift the seed here with a negative wave number, to compensate for `resetSeed()` shifting the seed itself. + this.game.scene.setSeed(shiftCharCodes(seed, (this.game.scene.currentBattle?.waveIndex ?? 0) * -1)); this.game.scene.resetSeed(); this.log(`Seed set to "${seed}"!`); return this; @@ -539,4 +532,14 @@ export class OverridesHelper extends GameManagerHelper { private log(...params: any[]) { console.log("Overrides:", ...params); } + + public sanitizeOverrides(): void { + for (const key of Object.keys(defaultOverrides)) { + if (Overrides[key] !== defaultOverrides[key]) { + vi.spyOn(Overrides, key as any, "get").mockReturnValue(defaultOverrides[key]); + } + } + expect(Overrides).toEqual(defaultOverrides); + this.log("Sanitizing all overrides!"); + } } diff --git a/test/testUtils/listenersManager.ts b/test/testUtils/listenersManager.ts new file mode 100644 index 00000000000..da624aa8a56 --- /dev/null +++ b/test/testUtils/listenersManager.ts @@ -0,0 +1,41 @@ +import { expect } from "vitest"; + +/** + * Whether or not it is currently the first time running this manager. + */ +let firstTime = true; + +/** + * The list of listeners that were present during the first time this manager is run. + * These initial listeners are needed throughout the entire test suite, so we never remove them. + */ +const initialListeners: NodeJS.MessageListener[] = []; + +/** + * The current listener that is only needed for the current test file. + * We plan to delete it during the next test file, when it is no longer needed. + */ +let currentListener: NodeJS.MessageListener | null; + +export function manageListeners() { + if (firstTime) { + initialListeners.push(...process.listeners("message")); + } else { + expect(process.listeners("message").length).toBeLessThan(7); + + // Remove the listener that was used during the previous test file + if (currentListener) { + process.removeListener("message", currentListener); + currentListener = null; + } + + // Find the new listener that is being used for the current test file + process.listeners("message").forEach(fn => { + if (!initialListeners.includes(fn)) { + currentListener = fn; + } + }); + } + + firstTime = false; +} diff --git a/test/testUtils/mocks/mockConsoleLog.ts b/test/testUtils/mocks/mockConsoleLog.ts index 808b6ea4d4d..f54d41fea3e 100644 --- a/test/testUtils/mocks/mockConsoleLog.ts +++ b/test/testUtils/mocks/mockConsoleLog.ts @@ -1,82 +1,80 @@ -const MockConsoleLog = (_logDisabled = false, _phaseText = false) => { - let logs: any[] = []; - const logDisabled: boolean = _logDisabled; - const phaseText: boolean = _phaseText; - const originalLog = console.log; - const originalError = console.error; - const originalDebug = console.debug; - const originalWarn = console.warn; - const notified: any[] = []; +const originalLog = console.log; +const originalError = console.error; +const originalDebug = console.debug; +const originalWarn = console.warn; - const blacklist = ["Phaser", "variant icon does not exist", 'Texture "%s" not found']; - const whitelist = ["Phase"]; +const blacklist = ["Phaser", "variant icon does not exist", 'Texture "%s" not found']; +const whitelist = ["Phase"]; - return { - log(...args) { - const argsStr = this.getStr(args); - logs.push(argsStr); - if (logDisabled && !phaseText) { - return; - } - if ((phaseText && !whitelist.some(b => argsStr.includes(b))) || blacklist.some(b => argsStr.includes(b))) { - return; - } - originalLog(args); - }, - error(...args) { - const argsStr = this.getStr(args); - logs.push(argsStr); - originalError(args); // Appelle le console.error originel - }, - debug(...args) { - const argsStr = this.getStr(args); - logs.push(argsStr); - if (logDisabled && !phaseText) { - return; - } - if (!whitelist.some(b => argsStr.includes(b)) || blacklist.some(b => argsStr.includes(b))) { - return; - } - originalDebug(args); - }, - warn(...args) { - const argsStr = this.getStr(args); - logs.push(args); - if (logDisabled && !phaseText) { - return; - } - if (!whitelist.some(b => argsStr.includes(b)) || blacklist.some(b => argsStr.includes(b))) { - return; - } - originalWarn(args); - }, - notify(msg) { - originalLog(msg); - notified.push(msg); - }, - getLogs() { - return logs; - }, - clearLogs() { - logs = []; - }, - getStr(...args) { - return args - .map(arg => { - if (typeof arg === "object" && arg !== null) { - // Handle objects including arrays - return JSON.stringify(arg, (_key, value) => (typeof value === "bigint" ? value.toString() : value)); - } - if (typeof arg === "bigint") { - // Handle BigInt values - return arg.toString(); - } - // Handle all other types +export class MockConsoleLog { + constructor( + private logDisabled = false, + private phaseText = false, + ) {} + private logs: any[] = []; + private notified: any[] = []; + + public log(...args) { + const argsStr = this.getStr(args); + this.logs.push(argsStr); + if (this.logDisabled && !this.phaseText) { + return; + } + if ((this.phaseText && !whitelist.some(b => argsStr.includes(b))) || blacklist.some(b => argsStr.includes(b))) { + return; + } + originalLog(args); + } + public error(...args) { + const argsStr = this.getStr(args); + this.logs.push(argsStr); + originalError(args); // Appelle le console.error originel + } + public debug(...args) { + const argsStr = this.getStr(args); + this.logs.push(argsStr); + if (this.logDisabled && !this.phaseText) { + return; + } + if (!whitelist.some(b => argsStr.includes(b)) || blacklist.some(b => argsStr.includes(b))) { + return; + } + originalDebug(args); + } + warn(...args) { + const argsStr = this.getStr(args); + this.logs.push(args); + if (this.logDisabled && !this.phaseText) { + return; + } + if (!whitelist.some(b => argsStr.includes(b)) || blacklist.some(b => argsStr.includes(b))) { + return; + } + originalWarn(args); + } + notify(msg) { + originalLog(msg); + this.notified.push(msg); + } + getLogs() { + return this.logs; + } + clearLogs() { + this.logs = []; + } + getStr(...args) { + return args + .map(arg => { + if (typeof arg === "object" && arg !== null) { + // Handle objects including arrays + return JSON.stringify(arg, (_key, value) => (typeof value === "bigint" ? value.toString() : value)); + } + if (typeof arg === "bigint") { + // Handle BigInt values return arg.toString(); - }) - .join(";"); - }, - }; -}; - -export default MockConsoleLog; + } + return arg.toString(); + }) + .join(";"); + } +} diff --git a/test/testUtils/mocks/mockContextCanvas.ts b/test/testUtils/mocks/mockContextCanvas.ts new file mode 100644 index 00000000000..a69f039c5e9 --- /dev/null +++ b/test/testUtils/mocks/mockContextCanvas.ts @@ -0,0 +1,26 @@ +/** + * A minimal stub object to mock HTMLCanvasElement + */ +export const mockCanvas: any = { + width: 0, + getContext() { + return mockContext; + }, +}; +/** + * A minimal stub object to mock CanvasRenderingContext2D + */ +export const mockContext: any = { + font: "", + measureText: () => { + return {}; + }, + save: () => {}, + scale: () => {}, + clearRect: () => {}, + fillRect: () => {}, + fillText: () => {}, + getImageData: () => {}, + canvas: mockCanvas, + restore: () => {}, +}; diff --git a/test/testUtils/mocks/mockLocalStorage.ts b/test/testUtils/mocks/mockLocalStorage.ts index 235aa76f664..6b336841ad6 100644 --- a/test/testUtils/mocks/mockLocalStorage.ts +++ b/test/testUtils/mocks/mockLocalStorage.ts @@ -1,4 +1,4 @@ -const mockLocalStorage = () => { +export const mockLocalStorage = () => { let store = {} as Storage; return { @@ -23,5 +23,3 @@ const mockLocalStorage = () => { }, }; }; - -export default mockLocalStorage; diff --git a/test/testUtils/mocks/mockTextureManager.ts b/test/testUtils/mocks/mockTextureManager.ts index fe249c5a5f7..585ee0a674a 100644 --- a/test/testUtils/mocks/mockTextureManager.ts +++ b/test/testUtils/mocks/mockTextureManager.ts @@ -1,5 +1,5 @@ import MockContainer from "#test/testUtils/mocks/mocksContainer/mockContainer"; -import MockImage from "#test/testUtils/mocks/mocksContainer/mockImage"; +import { MockImage } from "#test/testUtils/mocks/mocksContainer/mockImage"; import MockNineslice from "#test/testUtils/mocks/mocksContainer/mockNineslice"; import MockPolygon from "#test/testUtils/mocks/mocksContainer/mockPolygon"; import MockRectangle from "#test/testUtils/mocks/mocksContainer/mockRectangle"; diff --git a/test/testUtils/mocks/mocksContainer/mockContainer.ts b/test/testUtils/mocks/mocksContainer/mockContainer.ts index 0a792c6fc79..5e739fbe3cc 100644 --- a/test/testUtils/mocks/mocksContainer/mockContainer.ts +++ b/test/testUtils/mocks/mocksContainer/mockContainer.ts @@ -215,4 +215,10 @@ export default class MockContainer implements MockGameObject { } disableInteractive = () => null; + + each(method) { + for (const item of this.list) { + method(item); + } + } } diff --git a/test/testUtils/mocks/mocksContainer/mockImage.ts b/test/testUtils/mocks/mocksContainer/mockImage.ts index 768fcfeb765..d20b4663771 100644 --- a/test/testUtils/mocks/mocksContainer/mockImage.ts +++ b/test/testUtils/mocks/mocksContainer/mockImage.ts @@ -1,6 +1,6 @@ import MockContainer from "#test/testUtils/mocks/mocksContainer/mockContainer"; -export default class MockImage extends MockContainer { +export class MockImage extends MockContainer { private texture; constructor(textureManager, x, y, texture) { diff --git a/test/testUtils/mocks/mocksContainer/mockRectangle.ts b/test/testUtils/mocks/mocksContainer/mockRectangle.ts index eec431d8ada..854baed5915 100644 --- a/test/testUtils/mocks/mocksContainer/mockRectangle.ts +++ b/test/testUtils/mocks/mocksContainer/mockRectangle.ts @@ -1,3 +1,4 @@ +import { off } from "process"; import type { MockGameObject } from "../mockGameObject"; export default class MockRectangle implements MockGameObject { @@ -72,4 +73,6 @@ export default class MockRectangle implements MockGameObject { setScale(_scale) { // return this.phaserText.setScale(scale); } + + off() {} } diff --git a/test/testUtils/testFileInitialization.ts b/test/testUtils/testFileInitialization.ts new file mode 100644 index 00000000000..2b41f3aa29a --- /dev/null +++ b/test/testUtils/testFileInitialization.ts @@ -0,0 +1,117 @@ +import { SESSION_ID_COOKIE_NAME } from "#app/constants"; +import { initLoggedInUser } from "#app/account"; +import { initAbilities } from "#app/data/ability"; +import { initBiomes } from "#app/data/balance/biomes"; +import { initEggMoves } from "#app/data/balance/egg-moves"; +import { initPokemonPrevolutions } from "#app/data/balance/pokemon-evolutions"; +import { initMoves } from "#app/data/moves/move"; +import { initMysteryEncounters } from "#app/data/mystery-encounters/mystery-encounters"; +import { initPokemonForms } from "#app/data/pokemon-forms"; +import { initSpecies } from "#app/data/pokemon-species"; +import { initAchievements } from "#app/system/achv"; +import { initVouchers } from "#app/system/voucher"; +import { initStatsKeys } from "#app/ui/game-stats-ui-handler"; +import { setCookie } from "#app/utils"; +import { blobToString } from "#test/testUtils/gameManagerUtils"; +import { MockConsoleLog } from "#test/testUtils/mocks/mockConsoleLog"; +import { mockContext } from "#test/testUtils/mocks/mockContextCanvas"; +import { mockLocalStorage } from "#test/testUtils/mocks/mockLocalStorage"; +import { MockImage } from "#test/testUtils/mocks/mocksContainer/mockImage"; +import Phaser from "phaser"; +import InputText from "phaser3-rex-plugins/plugins/inputtext"; +import BBCodeText from "phaser3-rex-plugins/plugins/bbcodetext"; +import { manageListeners } from "./listenersManager"; + +let wasInitialized = false; +/** + * An initialization function that is run at the beginning of every test file (via `beforeAll()`). + */ +export function initTestFile() { + // Set the timezone to UTC for tests. + process.env.TZ = "UTC"; + + Object.defineProperty(window, "localStorage", { + value: mockLocalStorage(), + }); + Object.defineProperty(window, "console", { + value: new MockConsoleLog(false), + }); + Object.defineProperty(document, "fonts", { + writable: true, + value: { + add: () => {}, + }, + }); + + BBCodeText.prototype.destroy = () => null; + // @ts-ignore + BBCodeText.prototype.resize = () => null; + InputText.prototype.setElement = () => null as any; + InputText.prototype.resize = () => null as any; + Phaser.GameObjects.Image = MockImage as any; + window.URL.createObjectURL = (blob: Blob) => { + blobToString(blob).then((data: string) => { + localStorage.setItem("toExport", data); + }); + return null as any; + }; + navigator.getGamepads = () => []; + setCookie(SESSION_ID_COOKIE_NAME, "fake_token"); + + window.matchMedia = () => + ({ + matches: false, + }) as any; + + /** + * Sets this object's position relative to another object with a given offset + * @param guideObject {@linkcode Phaser.GameObjects.GameObject} to base the position off of + * @param x The relative x position + * @param y The relative y position + */ + const setPositionRelative = function (guideObject: any, x: number, y: number) { + const offsetX = guideObject.width * (-0.5 + (0.5 - guideObject.originX)); + const offsetY = guideObject.height * (-0.5 + (0.5 - guideObject.originY)); + this.setPosition(guideObject.x + offsetX + x, guideObject.y + offsetY + y); + }; + + Phaser.GameObjects.Container.prototype.setPositionRelative = setPositionRelative; + Phaser.GameObjects.Sprite.prototype.setPositionRelative = setPositionRelative; + Phaser.GameObjects.Image.prototype.setPositionRelative = setPositionRelative; + Phaser.GameObjects.NineSlice.prototype.setPositionRelative = setPositionRelative; + Phaser.GameObjects.Text.prototype.setPositionRelative = setPositionRelative; + Phaser.GameObjects.Rectangle.prototype.setPositionRelative = setPositionRelative; + HTMLCanvasElement.prototype.getContext = () => mockContext; + + // Initialize all of these things if and only if they have not been initialized yet + // initSpecies(); + if (!wasInitialized) { + wasInitialized = true; + initVouchers(); + initAchievements(); + initStatsKeys(); + initPokemonPrevolutions(); + initBiomes(); + initEggMoves(); + initPokemonForms(); + initSpecies(); + initMoves(); + initAbilities(); + initLoggedInUser(); + initMysteryEncounters(); + } + + manageListeners(); +} + +/** + * Closes the current mock server and initializes a new mock server. + * This is run at the beginning of every API test file. + */ +export async function initServerForApiTests() { + global.server?.close(); + const { setupServer } = await import("msw/node"); + global.server = setupServer(); + global.server.listen({ onUnhandledRequest: "error" }); + return global.server; +} diff --git a/test/vitest.setup.ts b/test/vitest.setup.ts index 44175049042..b9da0850306 100644 --- a/test/vitest.setup.ts +++ b/test/vitest.setup.ts @@ -14,8 +14,9 @@ import { initVouchers } from "#app/system/voucher"; import { initStatsKeys } from "#app/ui/game-stats-ui-handler"; import { afterAll, beforeAll, vi } from "vitest"; +import { initTestFile } from "./testUtils/testFileInitialization"; + /** Set the timezone to UTC for tests. */ -process.env.TZ = "UTC"; /** Mock the override import to always return default values, ignoring any custom overrides. */ vi.mock("#app/overrides", async importOriginal => { @@ -63,28 +64,10 @@ vi.mock("i18next", async importOriginal => { return await importOriginal(); }); -initVouchers(); -initAchievements(); -initStatsKeys(); -initPokemonPrevolutions(); -initBiomes(); -initEggMoves(); -initPokemonForms(); -initSpecies(); -initMoves(); -initAbilities(); -initLoggedInUser(); -initMysteryEncounters(); - global.testFailed = false; beforeAll(() => { - Object.defineProperty(document, "fonts", { - writable: true, - value: { - add: () => {}, - }, - }); + initTestFile(); }); afterAll(() => { diff --git a/vitest.config.ts b/vitest.config.ts index b52c16ec00c..c781bde97ed 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -1,20 +1,31 @@ import { defineProject } from "vitest/config"; import { defaultConfig } from "./vite.config"; +import { BaseSequencer, type TestSpecification } from "vitest/node"; + +function getTestOrder(testName: string): number { + if (testName.includes("battle-scene.test.ts")) { + return 1; + } + if (testName.includes("inputs.test.ts")) { + return 2; + } + return 3; +} export default defineProject(({ mode }) => ({ ...defaultConfig, test: { testTimeout: 20000, setupFiles: ["./test/fontFace.setup.ts", "./test/vitest.setup.ts"], - server: { - deps: { - inline: ["vitest-canvas-mock"], - //@ts-ignore - optimizer: { - web: { - include: ["vitest-canvas-mock"], - }, - }, + sequence: { + sequencer: class CustomSequencer extends BaseSequencer { + async sort(files: TestSpecification[]) { + // use default sorting at first. + files = await super.sort(files); + // Except, forcibly reorder + + return files.sort((a, b) => getTestOrder(a.moduleId) - getTestOrder(b.moduleId)); + } }, }, environment: "jsdom" as const, @@ -34,7 +45,6 @@ export default defineProject(({ mode }) => ({ }, name: "main", include: ["./test/**/*.{test,spec}.ts"], - exclude: ["./test/pre.test.ts"], }, esbuild: { pure: mode === "production" ? ["console.log"] : [],