diff --git a/package-lock.json b/package-lock.json index 8d1fb29..0286f23 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,7 +18,8 @@ "@types/react-dom": "^19.0.4", "@vitejs/plugin-react": "^4.4.1", "typescript": "^5.8.2", - "vite": "^6.2.0" + "vite": "^6.2.0", + "vitest": "^3.0.8" } }, "node_modules/@babel/code-frame": { @@ -1197,6 +1198,24 @@ "@babel/types": "^7.28.2" } }, + "node_modules/@types/chai": { + "version": "5.2.3", + "resolved": "https://registry.npmjs.org/@types/chai/-/chai-5.2.3.tgz", + "integrity": "sha512-Mw558oeA9fFbv65/y4mHtXDs9bPnFMZAL/jxdPFUpOHHIXX91mcgEHbS5Lahr+pwZFR8A7GQleRWeI6cGFC2UA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/deep-eql": "*", + "assertion-error": "^2.0.1" + } + }, + "node_modules/@types/deep-eql": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/@types/deep-eql/-/deep-eql-4.0.2.tgz", + "integrity": "sha512-c9h9dVVMigMPc4bwTvC5dxqtqJZwQPePsWjPlpSOnojbor6pGqdk541lfA7AqFQr5pB1BRdq0juY9db81BwyFw==", + "dev": true, + "license": "MIT" + }, "node_modules/@types/estree": { "version": "1.0.8", "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.8.tgz", @@ -1255,6 +1274,131 @@ "vite": "^4.2.0 || ^5.0.0 || ^6.0.0 || ^7.0.0" } }, + "node_modules/@vitest/expect": { + "version": "3.2.4", + "resolved": "https://registry.npmjs.org/@vitest/expect/-/expect-3.2.4.tgz", + "integrity": "sha512-Io0yyORnB6sikFlt8QW5K7slY4OjqNX9jmJQ02QDda8lyM6B5oNgVWoSoKPac8/kgnCUzuHQKrSLtu/uOqqrig==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/chai": "^5.2.2", + "@vitest/spy": "3.2.4", + "@vitest/utils": "3.2.4", + "chai": "^5.2.0", + "tinyrainbow": "^2.0.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/@vitest/mocker": { + "version": "3.2.4", + "resolved": "https://registry.npmjs.org/@vitest/mocker/-/mocker-3.2.4.tgz", + "integrity": "sha512-46ryTE9RZO/rfDd7pEqFl7etuyzekzEhUbTW3BvmeO/BcCMEgq59BKhek3dXDWgAj4oMK6OZi+vRr1wPW6qjEQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@vitest/spy": "3.2.4", + "estree-walker": "^3.0.3", + "magic-string": "^0.30.17" + }, + "funding": { + "url": "https://opencollective.com/vitest" + }, + "peerDependencies": { + "msw": "^2.4.9", + "vite": "^5.0.0 || ^6.0.0 || ^7.0.0-0" + }, + "peerDependenciesMeta": { + "msw": { + "optional": true + }, + "vite": { + "optional": true + } + } + }, + "node_modules/@vitest/pretty-format": { + "version": "3.2.4", + "resolved": "https://registry.npmjs.org/@vitest/pretty-format/-/pretty-format-3.2.4.tgz", + "integrity": "sha512-IVNZik8IVRJRTr9fxlitMKeJeXFFFN0JaB9PHPGQ8NKQbGpfjlTx9zO4RefN8gp7eqjNy8nyK3NZmBzOPeIxtA==", + "dev": true, + "license": "MIT", + "dependencies": { + "tinyrainbow": "^2.0.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/@vitest/runner": { + "version": "3.2.4", + "resolved": "https://registry.npmjs.org/@vitest/runner/-/runner-3.2.4.tgz", + "integrity": "sha512-oukfKT9Mk41LreEW09vt45f8wx7DordoWUZMYdY/cyAk7w5TWkTRCNZYF7sX7n2wB7jyGAl74OxgwhPgKaqDMQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@vitest/utils": "3.2.4", + "pathe": "^2.0.3", + "strip-literal": "^3.0.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/@vitest/snapshot": { + "version": "3.2.4", + "resolved": "https://registry.npmjs.org/@vitest/snapshot/-/snapshot-3.2.4.tgz", + "integrity": "sha512-dEYtS7qQP2CjU27QBC5oUOxLE/v5eLkGqPE0ZKEIDGMs4vKWe7IjgLOeauHsR0D5YuuycGRO5oSRXnwnmA78fQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@vitest/pretty-format": "3.2.4", + "magic-string": "^0.30.17", + "pathe": "^2.0.3" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/@vitest/spy": { + "version": "3.2.4", + "resolved": "https://registry.npmjs.org/@vitest/spy/-/spy-3.2.4.tgz", + "integrity": "sha512-vAfasCOe6AIK70iP5UD11Ac4siNUNJ9i/9PZ3NKx07sG6sUxeag1LWdNrMWeKKYBLlzuK+Gn65Yd5nyL6ds+nw==", + "dev": true, + "license": "MIT", + "dependencies": { + "tinyspy": "^4.0.3" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/@vitest/utils": { + "version": "3.2.4", + "resolved": "https://registry.npmjs.org/@vitest/utils/-/utils-3.2.4.tgz", + "integrity": "sha512-fB2V0JFrQSMsCo9HiSq3Ezpdv4iYaXRG1Sx8edX3MwxfyNn83mKiGzOcH+Fkxt4MHxr3y42fQi1oeAInqgX2QA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@vitest/pretty-format": "3.2.4", + "loupe": "^3.1.4", + "tinyrainbow": "^2.0.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/assertion-error": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-2.0.1.tgz", + "integrity": "sha512-Izi8RQcffqCeNVgFigKli1ssklIbpHnCYc6AknXGYoB6grJqyeby7jv12JUQgmTAnIDnbck1uxksT4dzN3PWBA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + } + }, "node_modules/baseline-browser-mapping": { "version": "2.10.8", "resolved": "https://registry.npmjs.org/baseline-browser-mapping/-/baseline-browser-mapping-2.10.8.tgz", @@ -1302,6 +1446,16 @@ "node": "^6 || ^7 || ^8 || ^9 || ^10 || ^11 || ^12 || >=13.7" } }, + "node_modules/cac": { + "version": "6.7.14", + "resolved": "https://registry.npmjs.org/cac/-/cac-6.7.14.tgz", + "integrity": "sha512-b6Ilus+c3RrdDk+JhLKUAQfzzgLEPy6wcXqS7f/xe1EETvsDP6GORG7SFuOs6cID5YkqchW/LXZbX5bc8j7ZcQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=8" + } + }, "node_modules/caniuse-lite": { "version": "1.0.30001779", "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001779.tgz", @@ -1323,6 +1477,33 @@ ], "license": "CC-BY-4.0" }, + "node_modules/chai": { + "version": "5.3.3", + "resolved": "https://registry.npmjs.org/chai/-/chai-5.3.3.tgz", + "integrity": "sha512-4zNhdJD/iOjSH0A05ea+Ke6MU5mmpQcbQsSOkgdaUMJ9zTlDTD/GYlwohmIE2u0gaxHYiVHEn1Fw9mZ/ktJWgw==", + "dev": true, + "license": "MIT", + "dependencies": { + "assertion-error": "^2.0.1", + "check-error": "^2.1.1", + "deep-eql": "^5.0.1", + "loupe": "^3.1.0", + "pathval": "^2.0.0" + }, + "engines": { + "node": ">=18" + } + }, + "node_modules/check-error": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/check-error/-/check-error-2.1.3.tgz", + "integrity": "sha512-PAJdDJusoxnwm1VwW07VWwUN1sl7smmC3OKggvndJFadxxDRyFJBX/ggnu/KE4kQAB7a3Dp8f/YXC1FlUprWmA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 16" + } + }, "node_modules/convert-source-map": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/convert-source-map/-/convert-source-map-2.0.0.tgz", @@ -1355,6 +1536,16 @@ } } }, + "node_modules/deep-eql": { + "version": "5.0.2", + "resolved": "https://registry.npmjs.org/deep-eql/-/deep-eql-5.0.2.tgz", + "integrity": "sha512-h5k/5U50IJJFpzfL6nO9jaaumfjO/f2NjK/oYB2Djzm4p9L+3T9qWpZqZ2hAbLPuuYq9wrU08WQyBTL5GbPk5Q==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6" + } + }, "node_modules/electron-to-chromium": { "version": "1.5.313", "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.313.tgz", @@ -1362,6 +1553,13 @@ "dev": true, "license": "ISC" }, + "node_modules/es-module-lexer": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/es-module-lexer/-/es-module-lexer-1.7.0.tgz", + "integrity": "sha512-jEQoCwk8hyb2AZziIOLhDqpm5+2ww5uIE6lkO/6jcOCusfk6LhMHpXXfBLXTZ7Ydyt0j4VoUQv6uGNYbdW+kBA==", + "dev": true, + "license": "MIT" + }, "node_modules/esbuild": { "version": "0.25.12", "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.25.12.tgz", @@ -1414,6 +1612,26 @@ "node": ">=6" } }, + "node_modules/estree-walker": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-3.0.3.tgz", + "integrity": "sha512-7RUKfXgSMMkzt6ZuXmqapOurLGPPfgj6l9uRZ7lRGolvk0y2yocc35LdcxKC5PQZdn2DMqioAQ2NoWcrTKmm6g==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/estree": "^1.0.0" + } + }, + "node_modules/expect-type": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/expect-type/-/expect-type-1.3.0.tgz", + "integrity": "sha512-knvyeauYhqjOYvQ66MznSMs83wmHrCycNEN6Ao+2AeYEfxUIkuiVxdEa1qlGEPK+We3n0THiDciYSsCcgW/DoA==", + "dev": true, + "license": "Apache-2.0", + "engines": { + "node": ">=12.0.0" + } + }, "node_modules/fdir": { "version": "6.5.0", "resolved": "https://registry.npmjs.org/fdir/-/fdir-6.5.0.tgz", @@ -1490,6 +1708,13 @@ "node": ">=6" } }, + "node_modules/loupe": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/loupe/-/loupe-3.2.1.tgz", + "integrity": "sha512-CdzqowRJCeLU72bHvWqwRBBlLcMEtIvGrlvef74kMnV2AolS9Y8xUv1I0U/MNAWMhBlKIoyuEgoJ0t/bbwHbLQ==", + "dev": true, + "license": "MIT" + }, "node_modules/lru-cache": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-5.1.1.tgz", @@ -1500,6 +1725,16 @@ "yallist": "^3.0.2" } }, + "node_modules/magic-string": { + "version": "0.30.21", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.30.21.tgz", + "integrity": "sha512-vd2F4YUyEXKGcLHoq+TEyCjxueSeHnFxyyjNp80yg0XV4vUhnDer/lvvlqM/arB5bXQN5K2/3oinyCRyx8T2CQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/sourcemap-codec": "^1.5.5" + } + }, "node_modules/ms": { "version": "2.1.3", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", @@ -1533,6 +1768,23 @@ "dev": true, "license": "MIT" }, + "node_modules/pathe": { + "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" + }, + "node_modules/pathval": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/pathval/-/pathval-2.0.1.tgz", + "integrity": "sha512-//nshmD55c46FuFw26xV/xFAaB5HF9Xdap7HJBBnrKdAd6/GxDBaNA1870O79+9ueg61cZLSVc+OaFlfmObYVQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 14.16" + } + }, "node_modules/picocolors": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.1.tgz", @@ -1674,6 +1926,13 @@ "semver": "bin/semver.js" } }, + "node_modules/siginfo": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/siginfo/-/siginfo-2.0.0.tgz", + "integrity": "sha512-ybx0WO1/8bSBLEWXZvEd7gMW3Sn3JFlW3TvX1nREbDLRNQNaeNN8WK0meBwPdAaOI7TtRRRJn/Es1zhrrCHu7g==", + "dev": true, + "license": "ISC" + }, "node_modules/source-map-js": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.2.1.tgz", @@ -1684,6 +1943,54 @@ "node": ">=0.10.0" } }, + "node_modules/stackback": { + "version": "0.0.2", + "resolved": "https://registry.npmjs.org/stackback/-/stackback-0.0.2.tgz", + "integrity": "sha512-1XMJE5fQo1jGH6Y/7ebnwPOBEkIEnT4QF32d5R1+VXdXveM0IBMJt8zfaxX1P3QhVwrYe+576+jkANtSS2mBbw==", + "dev": true, + "license": "MIT" + }, + "node_modules/std-env": { + "version": "3.10.0", + "resolved": "https://registry.npmjs.org/std-env/-/std-env-3.10.0.tgz", + "integrity": "sha512-5GS12FdOZNliM5mAOxFRg7Ir0pWz8MdpYm6AY6VPkGpbA7ZzmbzNcBJQ0GPvvyWgcY7QAhCgf9Uy89I03faLkg==", + "dev": true, + "license": "MIT" + }, + "node_modules/strip-literal": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/strip-literal/-/strip-literal-3.1.0.tgz", + "integrity": "sha512-8r3mkIM/2+PpjHoOtiAW8Rg3jJLHaV7xPwG+YRGrv6FP0wwk/toTpATxWYOW0BKdWwl82VT2tFYi5DlROa0Mxg==", + "dev": true, + "license": "MIT", + "dependencies": { + "js-tokens": "^9.0.1" + }, + "funding": { + "url": "https://github.com/sponsors/antfu" + } + }, + "node_modules/strip-literal/node_modules/js-tokens": { + "version": "9.0.1", + "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-9.0.1.tgz", + "integrity": "sha512-mxa9E9ITFOt0ban3j6L5MpjwegGz6lBQmM1IJkWeBZGcMxto50+eWdjC/52xDbS2vy0k7vIMK0Fe2wfL9OQSpQ==", + "dev": true, + "license": "MIT" + }, + "node_modules/tinybench": { + "version": "2.9.0", + "resolved": "https://registry.npmjs.org/tinybench/-/tinybench-2.9.0.tgz", + "integrity": "sha512-0+DUvqWMValLmha6lr4kD8iAMK1HzV0/aKnCtWb9v9641TnP/MFb7Pc2bxoxQjTXAErryXVgUOfv2YqNllqGeg==", + "dev": true, + "license": "MIT" + }, + "node_modules/tinyexec": { + "version": "0.3.2", + "resolved": "https://registry.npmjs.org/tinyexec/-/tinyexec-0.3.2.tgz", + "integrity": "sha512-KQQR9yN7R5+OSwaK0XQoj22pwHoTlgYqmUscPYoknOoWCWfj/5/ABTMRi69FrKU5ffPVh5QcFikpWJI/P1ocHA==", + "dev": true, + "license": "MIT" + }, "node_modules/tinyglobby": { "version": "0.2.15", "resolved": "https://registry.npmjs.org/tinyglobby/-/tinyglobby-0.2.15.tgz", @@ -1701,6 +2008,36 @@ "url": "https://github.com/sponsors/SuperchupuDev" } }, + "node_modules/tinypool": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/tinypool/-/tinypool-1.1.1.tgz", + "integrity": "sha512-Zba82s87IFq9A9XmjiX5uZA/ARWDrB03OHlq+Vw1fSdt0I+4/Kutwy8BP4Y/y/aORMo61FQ0vIb5j44vSo5Pkg==", + "dev": true, + "license": "MIT", + "engines": { + "node": "^18.0.0 || >=20.0.0" + } + }, + "node_modules/tinyrainbow": { + "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": { + "node": ">=14.0.0" + } + }, + "node_modules/tinyspy": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/tinyspy/-/tinyspy-4.0.4.tgz", + "integrity": "sha512-azl+t0z7pw/z958Gy9svOTuzqIk6xq+NSheJzn5MMWtWTFywIacg2wUlzKFGtt3cthx0r2SxMK0yzJOR0IES7Q==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=14.0.0" + } + }, "node_modules/typescript": { "version": "5.9.3", "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz", @@ -1828,6 +2165,119 @@ } } }, + "node_modules/vite-node": { + "version": "3.2.4", + "resolved": "https://registry.npmjs.org/vite-node/-/vite-node-3.2.4.tgz", + "integrity": "sha512-EbKSKh+bh1E1IFxeO0pg1n4dvoOTt0UDiXMd/qn++r98+jPO1xtJilvXldeuQ8giIB5IkpjCgMleHMNEsGH6pg==", + "dev": true, + "license": "MIT", + "dependencies": { + "cac": "^6.7.14", + "debug": "^4.4.1", + "es-module-lexer": "^1.7.0", + "pathe": "^2.0.3", + "vite": "^5.0.0 || ^6.0.0 || ^7.0.0-0" + }, + "bin": { + "vite-node": "vite-node.mjs" + }, + "engines": { + "node": "^18.0.0 || ^20.0.0 || >=22.0.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "node_modules/vitest": { + "version": "3.2.4", + "resolved": "https://registry.npmjs.org/vitest/-/vitest-3.2.4.tgz", + "integrity": "sha512-LUCP5ev3GURDysTWiP47wRRUpLKMOfPh+yKTx3kVIEiu5KOMeqzpnYNsKyOoVrULivR8tLcks4+lga33Whn90A==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/chai": "^5.2.2", + "@vitest/expect": "3.2.4", + "@vitest/mocker": "3.2.4", + "@vitest/pretty-format": "^3.2.4", + "@vitest/runner": "3.2.4", + "@vitest/snapshot": "3.2.4", + "@vitest/spy": "3.2.4", + "@vitest/utils": "3.2.4", + "chai": "^5.2.0", + "debug": "^4.4.1", + "expect-type": "^1.2.1", + "magic-string": "^0.30.17", + "pathe": "^2.0.3", + "picomatch": "^4.0.2", + "std-env": "^3.9.0", + "tinybench": "^2.9.0", + "tinyexec": "^0.3.2", + "tinyglobby": "^0.2.14", + "tinypool": "^1.1.1", + "tinyrainbow": "^2.0.0", + "vite": "^5.0.0 || ^6.0.0 || ^7.0.0-0", + "vite-node": "3.2.4", + "why-is-node-running": "^2.3.0" + }, + "bin": { + "vitest": "vitest.mjs" + }, + "engines": { + "node": "^18.0.0 || ^20.0.0 || >=22.0.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + }, + "peerDependencies": { + "@edge-runtime/vm": "*", + "@types/debug": "^4.1.12", + "@types/node": "^18.0.0 || ^20.0.0 || >=22.0.0", + "@vitest/browser": "3.2.4", + "@vitest/ui": "3.2.4", + "happy-dom": "*", + "jsdom": "*" + }, + "peerDependenciesMeta": { + "@edge-runtime/vm": { + "optional": true + }, + "@types/debug": { + "optional": true + }, + "@types/node": { + "optional": true + }, + "@vitest/browser": { + "optional": true + }, + "@vitest/ui": { + "optional": true + }, + "happy-dom": { + "optional": true + }, + "jsdom": { + "optional": true + } + } + }, + "node_modules/why-is-node-running": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/why-is-node-running/-/why-is-node-running-2.3.0.tgz", + "integrity": "sha512-hUrmaWBdVDcxvYqnyh09zunKzROWjbZTiNy8dBEjkS7ehEDQibXJ7XvlmtbwuTclUiIyN+CyXQD4Vmko8fNm8w==", + "dev": true, + "license": "MIT", + "dependencies": { + "siginfo": "^2.0.0", + "stackback": "0.0.2" + }, + "bin": { + "why-is-node-running": "cli.js" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/yallist": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/yallist/-/yallist-3.1.1.tgz", diff --git a/package.json b/package.json index 771e202..cd5a440 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,8 @@ "dev": "vite", "build": "npm run check && vite build", "preview": "vite preview", - "check": "tsc --noEmit -p tsconfig.app.json && tsc --noEmit -p tsconfig.node.json" + "check": "tsc --noEmit -p tsconfig.app.json && tsc --noEmit -p tsconfig.node.json", + "test": "vitest run" }, "dependencies": { "react": "^19.0.0", @@ -20,6 +21,7 @@ "@types/react-dom": "^19.0.4", "@vitejs/plugin-react": "^4.4.1", "typescript": "^5.8.2", - "vite": "^6.2.0" + "vite": "^6.2.0", + "vitest": "^3.0.8" } } diff --git a/src/data/sampleContentPack.ts b/src/data/sampleContentPack.ts index 251ba26..6792dee 100644 --- a/src/data/sampleContentPack.ts +++ b/src/data/sampleContentPack.ts @@ -72,6 +72,14 @@ const samplePack = { }, ], items: [ + { + id: "item.flint-and-steel", + name: "Flint and Steel", + itemType: "gear", + stackable: false, + consumable: false, + mvp: true, + }, { id: "item.ration", name: "Ration", @@ -88,6 +96,30 @@ const samplePack = { consumable: false, mvp: true, }, + { + id: "item.pouch", + name: "Pouch", + itemType: "gear", + stackable: false, + consumable: false, + mvp: true, + }, + { + id: "item.wax-sealing-kit", + name: "Wax Sealing Kit", + itemType: "gear", + stackable: false, + consumable: false, + mvp: true, + }, + { + id: "item.backpack", + name: "Backpack", + itemType: "gear", + stackable: false, + consumable: false, + mvp: true, + }, ], potions: [ { diff --git a/src/rules/character.test.ts b/src/rules/character.test.ts new file mode 100644 index 0000000..f56aaeb --- /dev/null +++ b/src/rules/character.test.ts @@ -0,0 +1,50 @@ +import { describe, expect, it } from "vitest"; + +import { sampleContentPack } from "@/data/sampleContentPack"; + +import { createStartingAdventurer, getCharacterCreationOptions } from "./character"; + +describe("character creation", () => { + it("exposes legal starting options from the content pack", () => { + const options = getCharacterCreationOptions(sampleContentPack); + + expect(options.weapons).toHaveLength(1); + expect(options.armour).toHaveLength(1); + expect(options.scrolls).toHaveLength(1); + }); + + it("builds a valid starting adventurer from legal choices", () => { + const adventurer = createStartingAdventurer(sampleContentPack, { + name: "Mira", + weaponId: "weapon.short-sword", + armourId: "armour.leather-vest", + scrollId: "scroll.lesser-heal", + }); + + expect(adventurer.name).toBe("Mira"); + expect(adventurer.level).toBe(1); + expect(adventurer.stats.shift).toBe(2); + expect(adventurer.manoeuvreIds).toEqual([ + "manoeuvre.exact-strike", + "manoeuvre.guard-break", + ]); + expect(adventurer.inventory.rationCount).toBe(3); + expect(adventurer.inventory.carried).toEqual( + expect.arrayContaining([ + expect.objectContaining({ definitionId: "potion.healing", quantity: 1 }), + expect.objectContaining({ definitionId: "scroll.lesser-heal", quantity: 1 }), + ]), + ); + }); + + it("rejects an empty adventurer name", () => { + expect(() => + createStartingAdventurer(sampleContentPack, { + name: " ", + weaponId: "weapon.short-sword", + armourId: "armour.leather-vest", + scrollId: "scroll.lesser-heal", + }), + ).toThrow("Adventurer name is required."); + }); +}); diff --git a/src/rules/character.ts b/src/rules/character.ts new file mode 100644 index 0000000..527eaba --- /dev/null +++ b/src/rules/character.ts @@ -0,0 +1,128 @@ +import type { ContentPack } from "@/types/content"; +import type { AdventurerState, InventoryEntry } from "@/types/state"; + +export type CharacterCreationChoiceSet = { + name: string; + weaponId: string; + armourId: string; + scrollId: string; +}; + +const DEFAULT_STARTING_ITEM_IDS = [ + "item.flint-and-steel", + "item.lantern", + "item.ration", + "item.pouch", + "item.wax-sealing-kit", + "item.backpack", +]; + +function requireDefinition( + entries: T[], + id: string, + label: string, +) { + const entry = entries.find((candidate) => candidate.id === id); + + if (!entry) { + throw new Error(`Unknown ${label} id: ${id}`); + } + + return entry; +} + +function makeInventoryEntry(definitionId: string, quantity = 1): InventoryEntry { + return { + definitionId, + quantity, + }; +} + +export function getCharacterCreationOptions(content: ContentPack) { + return { + weapons: content.weapons.filter((weapon) => weapon.startingOption), + armour: content.armour.filter((armour) => armour.startingOption), + scrolls: content.scrolls.filter((scroll) => scroll.startingOption), + }; +} + +export function createStartingAdventurer( + content: ContentPack, + choices: CharacterCreationChoiceSet, +): AdventurerState { + const trimmedName = choices.name.trim(); + + if (trimmedName.length === 0) { + throw new Error("Adventurer name is required."); + } + + const selectedWeapon = requireDefinition(content.weapons, choices.weaponId, "weapon"); + const selectedArmour = requireDefinition(content.armour, choices.armourId, "armour"); + const selectedScroll = requireDefinition(content.scrolls, choices.scrollId, "scroll"); + + if (!selectedWeapon.startingOption) { + throw new Error(`Weapon ${selectedWeapon.id} is not a legal starting option.`); + } + + if (!selectedArmour.startingOption) { + throw new Error(`Armour ${selectedArmour.id} is not a legal starting option.`); + } + + if (!selectedScroll.startingOption) { + throw new Error(`Scroll ${selectedScroll.id} is not a legal starting option.`); + } + + const allowedManoeuvreIds = selectedWeapon.allowedManoeuvreIds; + + if (allowedManoeuvreIds.length === 0) { + throw new Error(`Weapon ${selectedWeapon.id} does not define starting manoeuvres.`); + } + + DEFAULT_STARTING_ITEM_IDS.forEach((itemId) => requireDefinition(content.items, itemId, "item")); + requireDefinition(content.potions, "potion.healing", "potion"); + + return { + id: "adventurer.new", + name: trimmedName, + level: 1, + xp: 0, + hp: { + current: 10, + max: 10, + base: 10, + }, + stats: { + shift: 2, + discipline: 1, + precision: 0, + }, + weaponId: selectedWeapon.id, + manoeuvreIds: allowedManoeuvreIds, + armourId: selectedArmour.id, + favour: {}, + statuses: [], + inventory: { + carried: [ + makeInventoryEntry("item.flint-and-steel"), + makeInventoryEntry("item.lantern"), + makeInventoryEntry("item.ration", 3), + makeInventoryEntry("item.pouch"), + makeInventoryEntry("item.wax-sealing-kit"), + makeInventoryEntry("item.backpack"), + makeInventoryEntry(selectedScroll.id), + makeInventoryEntry("potion.healing"), + ], + equipped: [ + makeInventoryEntry(selectedWeapon.id), + makeInventoryEntry(selectedArmour.id), + ], + stored: [], + currency: { + gold: 0, + }, + rationCount: 3, + lightSources: [makeInventoryEntry("item.lantern")], + }, + progressionFlags: [], + }; +} diff --git a/src/rules/dice.test.ts b/src/rules/dice.test.ts new file mode 100644 index 0000000..ac5fd59 --- /dev/null +++ b/src/rules/dice.test.ts @@ -0,0 +1,46 @@ +import { describe, expect, it } from "vitest"; + +import { applyRollModifier, roll2D6, rollD3, rollD66, rollDice } from "./dice"; + +function createSequenceRoller(values: number[]) { + let index = 0; + + return () => { + const next = values[index]; + index += 1; + return next; + }; +} + +describe("dice rules", () => { + it("maps d3 rolls from a d6 source", () => { + const result = rollD3(createSequenceRoller([5])); + + expect(result.rolls).toEqual([5]); + expect(result.total).toBe(3); + expect(result.modifiedTotal).toBe(3); + }); + + it("preserves primary and secondary dice for d66", () => { + const result = rollD66(createSequenceRoller([4, 2])); + + expect(result.primary).toBe(4); + expect(result.secondary).toBe(2); + expect(result.total).toBe(42); + }); + + it("applies modifiers with clamping when limits are provided", () => { + const roll = roll2D6(createSequenceRoller([6, 6])); + const modified = applyRollModifier(roll, 3, { min: 2, max: 12 }); + + expect(modified.modifiedTotal).toBe(12); + expect(modified.clamped).toBe(true); + }); + + it("routes dice kinds through the shared roller", () => { + const result = rollDice("d6", createSequenceRoller([2])); + + expect(result.total).toBe(2); + expect(result.diceKind).toBe("d6"); + }); +}); diff --git a/src/rules/dice.ts b/src/rules/dice.ts new file mode 100644 index 0000000..05a3495 --- /dev/null +++ b/src/rules/dice.ts @@ -0,0 +1,113 @@ +import type { DiceKind, RollResult } from "@/types/rules"; + +export type DiceRoller = (sides: number) => number; + +const defaultDiceRoller: DiceRoller = (sides) => + Math.floor(Math.random() * sides) + 1; + +function assertRollInRange(value: number, sides: number) { + if (!Number.isInteger(value) || value < 1 || value > sides) { + throw new Error(`Dice roller returned invalid value ${value} for d${sides}.`); + } +} + +function clampTotal(total: number, min: number, max: number) { + return Math.max(min, Math.min(max, total)); +} + +export function rollD3(roller: DiceRoller = defaultDiceRoller): RollResult { + const value = roller(6); + assertRollInRange(value, 6); + const total = Math.ceil(value / 2); + + return { + diceKind: "d3", + rolls: [value], + total, + modifiedTotal: total, + }; +} + +export function rollD6(roller: DiceRoller = defaultDiceRoller): RollResult { + const value = roller(6); + assertRollInRange(value, 6); + + return { + diceKind: "d6", + rolls: [value], + total: value, + modifiedTotal: value, + }; +} + +export function roll2D6(roller: DiceRoller = defaultDiceRoller): RollResult { + const first = roller(6); + const second = roller(6); + assertRollInRange(first, 6); + assertRollInRange(second, 6); + const total = first + second; + + return { + diceKind: "2d6", + rolls: [first, second], + primary: first, + secondary: second, + total, + modifiedTotal: total, + }; +} + +export function rollD66(roller: DiceRoller = defaultDiceRoller): RollResult { + const primary = roller(6); + const secondary = roller(6); + assertRollInRange(primary, 6); + assertRollInRange(secondary, 6); + const total = primary * 10 + secondary; + + return { + diceKind: "d66", + rolls: [primary, secondary], + primary, + secondary, + total, + modifiedTotal: total, + }; +} + +export function applyRollModifier( + roll: RollResult, + modifier = 0, + limits?: { min: number; max: number }, +): RollResult { + if (roll.total === undefined) { + throw new Error("Cannot apply a modifier to a roll without a total."); + } + + const nextTotal = roll.total + modifier; + const boundedTotal = limits + ? clampTotal(nextTotal, limits.min, limits.max) + : nextTotal; + + return { + ...roll, + modifier, + modifiedTotal: boundedTotal, + clamped: limits ? boundedTotal !== nextTotal : false, + }; +} + +export function rollDice( + diceKind: DiceKind, + roller: DiceRoller = defaultDiceRoller, +): RollResult { + switch (diceKind) { + case "d3": + return rollD3(roller); + case "d6": + return rollD6(roller); + case "2d6": + return roll2D6(roller); + case "d66": + return rollD66(roller); + } +} diff --git a/src/rules/tables.test.ts b/src/rules/tables.test.ts new file mode 100644 index 0000000..d6fd7c7 --- /dev/null +++ b/src/rules/tables.test.ts @@ -0,0 +1,62 @@ +import { describe, expect, it } from "vitest"; + +import type { TableDefinition } from "@/types/content"; + +import { lookupTable, resolveTableEntry } from "./tables"; + +function createSequenceRoller(values: number[]) { + let index = 0; + + return () => { + const next = values[index]; + index += 1; + return next; + }; +} + +const d6Table: TableDefinition = { + id: "table.sample", + code: "SAMPLE1", + name: "Sample Table", + category: "generic", + page: 1, + diceKind: "d6", + usesModifiedRangesRule: true, + entries: [ + { key: "1", exact: 1, label: "One" }, + { key: "2-5", min: 2, max: 5, label: "Two to Five" }, + { key: "6", exact: 6, label: "Six" }, + ], + mvp: true, +}; + +describe("table lookup rules", () => { + it("finds a matching exact entry", () => { + const result = lookupTable(d6Table, { roller: createSequenceRoller([6]) }); + + expect(result.entry.label).toBe("Six"); + expect(result.roll.total).toBe(6); + }); + + it("clamps modified results when the table uses modified ranges", () => { + const result = lookupTable(d6Table, { + roller: createSequenceRoller([6]), + modifier: 3, + }); + + expect(result.entry.label).toBe("Six"); + expect(result.roll.modifiedTotal).toBe(6); + expect(result.roll.clamped).toBe(true); + }); + + it("falls back to the nearest numeric entry for direct resolution", () => { + const result = resolveTableEntry(d6Table, { + diceKind: "d6", + rolls: [0], + total: 4, + modifiedTotal: 7, + }); + + expect(result.entry.label).toBe("Six"); + }); +}); diff --git a/src/rules/tables.ts b/src/rules/tables.ts new file mode 100644 index 0000000..4386a20 --- /dev/null +++ b/src/rules/tables.ts @@ -0,0 +1,110 @@ +import type { TableDefinition, TableEntry } from "@/types/content"; +import type { RollResult } from "@/types/rules"; + +import { applyRollModifier, rollDice, type DiceRoller } from "./dice"; + +export type TableLookupResult = { + tableId: string; + entryKey: string; + roll: RollResult; + entry: TableEntry; +}; + +function getNumericEntryValue(entry: TableEntry) { + if (entry.d66 !== undefined) { + return entry.d66; + } + + if (entry.exact !== undefined) { + return entry.exact; + } + + return undefined; +} + +function getEntryBounds(entries: TableEntry[]) { + const values = entries + .flatMap((entry) => { + if (entry.min !== undefined && entry.max !== undefined) { + return [entry.min, entry.max]; + } + + const exactValue = getNumericEntryValue(entry); + return exactValue !== undefined ? [exactValue] : []; + }) + .sort((a, b) => a - b); + + if (values.length === 0) { + throw new Error("Cannot compute table bounds for an empty numeric table."); + } + + return { min: values[0], max: values[values.length - 1] }; +} + +function matchesEntry(entry: TableEntry, lookupValue: number) { + if (entry.min !== undefined && entry.max !== undefined) { + return lookupValue >= entry.min && lookupValue <= entry.max; + } + + const exactValue = getNumericEntryValue(entry); + return exactValue === lookupValue; +} + +function findNearestEntry(entries: TableEntry[], lookupValue: number) { + return entries + .map((entry) => ({ entry, value: getNumericEntryValue(entry) })) + .filter((candidate): candidate is { entry: TableEntry; value: number } => { + return candidate.value !== undefined; + }) + .sort((left, right) => { + const distanceDelta = + Math.abs(left.value - lookupValue) - Math.abs(right.value - lookupValue); + + return distanceDelta !== 0 ? distanceDelta : left.value - right.value; + })[0]?.entry; +} + +export function resolveTableEntry( + table: TableDefinition, + roll: RollResult, +): TableLookupResult { + const lookupValue = roll.modifiedTotal ?? roll.total; + + if (lookupValue === undefined) { + throw new Error(`Cannot resolve table ${table.code} without a roll total.`); + } + + const matchingEntry = table.entries.find((entry) => matchesEntry(entry, lookupValue)); + const fallbackEntry = matchingEntry ?? findNearestEntry(table.entries, lookupValue); + + if (!fallbackEntry) { + throw new Error(`No table entry found for ${table.code} with value ${lookupValue}.`); + } + + return { + tableId: table.id, + entryKey: fallbackEntry.key, + roll, + entry: fallbackEntry, + }; +} + +export function lookupTable( + table: TableDefinition, + options?: { + modifier?: number; + roller?: DiceRoller; + }, +): TableLookupResult { + const rawRoll = rollDice(table.diceKind, options?.roller); + const roll = + options?.modifier !== undefined + ? applyRollModifier( + rawRoll, + options.modifier, + table.usesModifiedRangesRule ? getEntryBounds(table.entries) : undefined, + ) + : rawRoll; + + return resolveTableEntry(table, roll); +}