From d2ddaef0d4d355720d56fdc9f3a9ffe8c91ab7dd Mon Sep 17 00:00:00 2001 From: Benjamin Toby Date: Sat, 21 Mar 2026 10:14:38 +0100 Subject: [PATCH] Update tests --- .gitignore | 4 +- bun.lock | 51 +++++++++-- dist/__tests__/e2e/e2e.test.d.ts | 1 + dist/__tests__/e2e/e2e.test.js | 52 +++++++++++ .../server/bunext-req-handler.test.d.ts | 1 + .../server/bunext-req-handler.test.js | 73 ++++++++++++++++ .../functions/server/handle-hmr.test.d.ts | 1 + .../functions/server/handle-hmr.test.js | 37 ++++++++ .../functions/server/handle-routes.test.d.ts | 1 + .../functions/server/handle-routes.test.js | 65 ++++++++++++++ .../functions/server/start-server.test.d.ts | 1 + .../functions/server/start-server.test.js | 36 ++++++++ dist/__tests__/hydration/hydration.test.d.ts | 1 + dist/__tests__/hydration/hydration.test.js | 62 +++++++++++++ dist/functions/server/bunext-req-handler.js | 7 +- .../web-pages/grab-page-error-component.js | 10 +++ package.json | 8 +- src/__tests__/e2e/e2e.test.ts | 63 ++++++++++++++ .../server/bunext-req-handler.test.ts | 86 +++++++++++++++++++ .../functions/server/handle-hmr.test.ts | 42 +++++++++ .../functions/server/handle-routes.test.ts | 76 ++++++++++++++++ .../functions/server/start-server.test.ts | 44 ++++++++++ src/__tests__/hydration/hydration.test.tsx | 79 +++++++++++++++++ src/functions/server/bunext-req-handler.ts | 6 +- src/functions/server/handle-hmr-update.ts | 82 ------------------ .../web-pages/grab-page-error-component.tsx | 10 +++ 26 files changed, 798 insertions(+), 101 deletions(-) create mode 100644 dist/__tests__/e2e/e2e.test.d.ts create mode 100644 dist/__tests__/e2e/e2e.test.js create mode 100644 dist/__tests__/functions/server/bunext-req-handler.test.d.ts create mode 100644 dist/__tests__/functions/server/bunext-req-handler.test.js create mode 100644 dist/__tests__/functions/server/handle-hmr.test.d.ts create mode 100644 dist/__tests__/functions/server/handle-hmr.test.js create mode 100644 dist/__tests__/functions/server/handle-routes.test.d.ts create mode 100644 dist/__tests__/functions/server/handle-routes.test.js create mode 100644 dist/__tests__/functions/server/start-server.test.d.ts create mode 100644 dist/__tests__/functions/server/start-server.test.js create mode 100644 dist/__tests__/hydration/hydration.test.d.ts create mode 100644 dist/__tests__/hydration/hydration.test.js create mode 100644 src/__tests__/e2e/e2e.test.ts create mode 100644 src/__tests__/functions/server/bunext-req-handler.test.ts create mode 100644 src/__tests__/functions/server/handle-hmr.test.ts create mode 100644 src/__tests__/functions/server/handle-routes.test.ts create mode 100644 src/__tests__/functions/server/start-server.test.ts create mode 100644 src/__tests__/hydration/hydration.test.tsx delete mode 100644 src/functions/server/handle-hmr-update.ts diff --git a/.gitignore b/.gitignore index 6b7eed9..f785dfc 100644 --- a/.gitignore +++ b/.gitignore @@ -175,4 +175,6 @@ out /test .bunext /bin -/build \ No newline at end of file +/build +__fixtures__ +/public \ No newline at end of file diff --git a/bun.lock b/bun.lock index 6690c4d..c31e1bc 100644 --- a/bun.lock +++ b/bun.lock @@ -16,12 +16,15 @@ "postcss": "^8.5.8", }, "devDependencies": { + "@testing-library/dom": "^10.4.1", + "@testing-library/react": "^16.3.2", "@types/bun": "latest", "@types/lodash": "^4.17.24", "@types/micromatch": "^4.0.10", "@types/node": "^24.10.0", "@types/react": "^19.2.2", "@types/react-dom": "^19.2.2", + "happy-dom": "^20.8.4", "react": "^19.2.4", "react-dom": "^19.2.4", }, @@ -35,6 +38,12 @@ "packages": { "@alloc/quick-lru": ["@alloc/quick-lru@5.2.0", "", {}, "sha512-UrcABB+4bUrFABwbluTIBErXwvbsU/V7TZWfmbgJfbkwiBuziS9gxdODUyuiecfdGQ85jglMW6juS3+z5TsKLw=="], + "@babel/code-frame": ["@babel/code-frame@7.29.0", "", { "dependencies": { "@babel/helper-validator-identifier": "^7.28.5", "js-tokens": "^4.0.0", "picocolors": "^1.1.1" } }, "sha512-9NhCeYjq9+3uxgdtp20LSiJXJvN0FeCtNGpJxuMFZ1Kv3cWUNb6DOhJwUvcVCzKGR66cw4njwM6hrJLqgOwbcw=="], + + "@babel/helper-validator-identifier": ["@babel/helper-validator-identifier@7.28.5", "", {}, "sha512-qSs4ifwzKJSV39ucNjsvc6WVHs6b7S03sOh2OcHF9UHfVPqWWALUsNUVzhSBiItjRZoLHx7nIarVjqKVusUZ1Q=="], + + "@babel/runtime": ["@babel/runtime@7.29.2", "", {}, "sha512-JiDShH45zKHWyGe4ZNVRrCjBz8Nh9TMmZG1kh4QTK8hCBTWBi8Da+i7s1fJw7/lYpM4ccepSNfqzZ/QvABBi5g=="], + "@esbuild/aix-ppc64": ["@esbuild/aix-ppc64@0.27.4", "", { "os": "aix", "cpu": "ppc64" }, "sha512-cQPwL2mp2nSmHHJlCyoXgHGhbEPMrEEU5xhkcy3Hs/O7nGZqEpZ2sUtLaL9MORLtDfRvVl2/3PAuEkYZH0Ty8Q=="], "@esbuild/android-arm": ["@esbuild/android-arm@0.27.4", "", { "os": "android", "cpu": "arm" }, "sha512-X9bUgvxiC8CHAGKYufLIHGXPJWnr0OCdR0anD2e21vdvgCI8lIfqFbnoeOz7lBjdrAGUhqLZLcQo6MLhTO2DKQ=="], @@ -151,6 +160,12 @@ "@tailwindcss/postcss": ["@tailwindcss/postcss@4.2.2", "", { "dependencies": { "@alloc/quick-lru": "^5.2.0", "@tailwindcss/node": "4.2.2", "@tailwindcss/oxide": "4.2.2", "postcss": "^8.5.6", "tailwindcss": "4.2.2" } }, "sha512-n4goKQbW8RVXIbNKRB/45LzyUqN451deQK0nzIeauVEqjlI49slUlgKYJM2QyUzap/PcpnS7kzSUmPb1sCRvYQ=="], + "@testing-library/dom": ["@testing-library/dom@10.4.1", "", { "dependencies": { "@babel/code-frame": "^7.10.4", "@babel/runtime": "^7.12.5", "@types/aria-query": "^5.0.1", "aria-query": "5.3.0", "dom-accessibility-api": "^0.5.9", "lz-string": "^1.5.0", "picocolors": "1.1.1", "pretty-format": "^27.0.2" } }, "sha512-o4PXJQidqJl82ckFaXUeoAW+XysPLauYI43Abki5hABd853iMhitooc6znOnczgbTYmEP6U6/y1ZyKAIsvMKGg=="], + + "@testing-library/react": ["@testing-library/react@16.3.2", "", { "dependencies": { "@babel/runtime": "^7.12.5" }, "peerDependencies": { "@testing-library/dom": "^10.0.0", "@types/react": "^18.0.0 || ^19.0.0", "@types/react-dom": "^18.0.0 || ^19.0.0", "react": "^18.0.0 || ^19.0.0", "react-dom": "^18.0.0 || ^19.0.0" }, "optionalPeers": ["@types/react", "@types/react-dom"] }, "sha512-XU5/SytQM+ykqMnAnvB2umaJNIOsLF3PVv//1Ew4CTcpz0/BRyy/af40qqrt7SjKpDdT1saBMc42CUok5gaw+g=="], + + "@types/aria-query": ["@types/aria-query@5.0.4", "", {}, "sha512-rfT93uj5s0PRL7EzccGMs3brplhcrghnDoV26NqKhCAS1hVo+WdNsPvE/yb6ilfr5hi2MEk6d5EWJTKdxg8jVw=="], + "@types/braces": ["@types/braces@3.0.5", "", {}, "sha512-SQFof9H+LXeWNz8wDe7oN5zu7ket0qwMu5vZubW4GCJ8Kkeh6nBWUz87+KTz/G3Kqsrp0j/W253XJb3KMEeg3w=="], "@types/bun": ["@types/bun@1.2.3", "", { "dependencies": { "bun-types": "1.2.3" } }, "sha512-054h79ipETRfjtsCW9qJK8Ipof67Pw9bodFWmkfkaUaRiIQ1dIV2VTlheshlBx3mpKr0KeK8VqnMMCtgN9rQtw=="], @@ -165,9 +180,15 @@ "@types/react-dom": ["@types/react-dom@19.2.3", "", { "peerDependencies": { "@types/react": "^19.2.0" } }, "sha512-jp2L/eY6fn+KgVVQAOqYItbF0VY/YApe5Mz2F0aykSO8gx31bYCZyvSeYxCHKvzHG5eZjc+zyaS5BrBWya2+kQ=="], - "@types/ws": ["@types/ws@8.5.14", "", { "dependencies": { "@types/node": "*" } }, "sha512-bd/YFLW+URhBzMXurx7lWByOu+xzU9+kb3RboOteXYDfW+tr+JZa99OyNmPINEGB/ahzKrEuc8rcv4gnpJmxTw=="], + "@types/whatwg-mimetype": ["@types/whatwg-mimetype@3.0.2", "", {}, "sha512-c2AKvDT8ToxLIOUlN51gTiHXflsfIFisS4pO7pDPoKouJCESkhZnEy623gwP9laCy5lnLDAw1vAzu2vM2YLOrA=="], - "ansi-regex": ["ansi-regex@6.2.2", "", {}, "sha512-Bq3SmSpyFHaWjPk8If9yc6svM8c56dB5BAtW4Qbw5jHTwwXXcTLoRMkpDJp6VL0XzlWaCHTXrkFURMYmD0sLqg=="], + "@types/ws": ["@types/ws@8.18.1", "", { "dependencies": { "@types/node": "*" } }, "sha512-ThVF6DCVhA8kUGy+aazFQ4kXQ7E1Ty7A3ypFOe0IcJV8O/M511G99AW24irKrW56Wt44yG9+ij8FaqoBGkuBXg=="], + + "ansi-regex": ["ansi-regex@5.0.1", "", {}, "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ=="], + + "ansi-styles": ["ansi-styles@5.2.0", "", {}, "sha512-Cxwpt2SfTzTtXcfOlzGEee8O+c+MmUgGrNiBcXnuWxuFJHe6a5Hz7qwhwe5OgaSYI0IJvkLqWX1ASG+cJOkEiA=="], + + "aria-query": ["aria-query@5.3.0", "", { "dependencies": { "dequal": "^2.0.3" } }, "sha512-b0P0sZPKtyu8HkeRAfCq0IfURZK+SuwMjY1UXGBU27wpAiTwQAIlq56IbIO+ytk/JjS1fMR14ee5WBBfKi5J6A=="], "braces": ["braces@3.0.3", "", { "dependencies": { "fill-range": "^7.1.1" } }, "sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA=="], @@ -187,10 +208,16 @@ "csstype": ["csstype@3.2.3", "", {}, "sha512-z1HGKcYy2xA8AGQfwrn0PAy+PB7X/GSj3UVJW9qKyn43xWa+gl5nXmU4qqLMRzWVLFC8KusUX8T/0kCiOYpAIQ=="], + "dequal": ["dequal@2.0.3", "", {}, "sha512-0je+qPKHEMohvfRTCEo3CrPG6cAzAYgmzKyxRiYSSDkS6eGJdyVJm7WaYA5ECaAD9wLB2T4EEeymA5aFVcYXCA=="], + "detect-libc": ["detect-libc@2.1.2", "", {}, "sha512-Btj2BOOO83o3WyH59e8MgXsxEQVcarkUOpEYrubB0urwnN10yQ364rsiByU11nZlqWYZm05i/of7io4mzihBtQ=="], + "dom-accessibility-api": ["dom-accessibility-api@0.5.16", "", {}, "sha512-X7BJ2yElsnOJ30pZF4uIIDfBEVgF4XEBxL9Bxhy6dnrm5hkzqmsWHGTiHqRiITNhMyFLyAiWndIJP7Z1NTteDg=="], + "enhanced-resolve": ["enhanced-resolve@5.20.1", "", { "dependencies": { "graceful-fs": "^4.2.4", "tapable": "^2.3.0" } }, "sha512-Qohcme7V1inbAfvjItgw0EaxVX5q2rdVEZHRBrEQdRZTssLDGsL8Lwrznl8oQ/6kuTJONLaDcGjkNP247XEhcA=="], + "entities": ["entities@7.0.1", "", {}, "sha512-TWrgLOFUQTH994YUyl1yT4uyavY5nNB5muff+RtWaqNVCAK408b5ZnnbNAUEWLTCpum9w6arT70i1XdQ4UeOPA=="], + "esbuild": ["esbuild@0.27.4", "", { "optionalDependencies": { "@esbuild/aix-ppc64": "0.27.4", "@esbuild/android-arm": "0.27.4", "@esbuild/android-arm64": "0.27.4", "@esbuild/android-x64": "0.27.4", "@esbuild/darwin-arm64": "0.27.4", "@esbuild/darwin-x64": "0.27.4", "@esbuild/freebsd-arm64": "0.27.4", "@esbuild/freebsd-x64": "0.27.4", "@esbuild/linux-arm": "0.27.4", "@esbuild/linux-arm64": "0.27.4", "@esbuild/linux-ia32": "0.27.4", "@esbuild/linux-loong64": "0.27.4", "@esbuild/linux-mips64el": "0.27.4", "@esbuild/linux-ppc64": "0.27.4", "@esbuild/linux-riscv64": "0.27.4", "@esbuild/linux-s390x": "0.27.4", "@esbuild/linux-x64": "0.27.4", "@esbuild/netbsd-arm64": "0.27.4", "@esbuild/netbsd-x64": "0.27.4", "@esbuild/openbsd-arm64": "0.27.4", "@esbuild/openbsd-x64": "0.27.4", "@esbuild/openharmony-arm64": "0.27.4", "@esbuild/sunos-x64": "0.27.4", "@esbuild/win32-arm64": "0.27.4", "@esbuild/win32-ia32": "0.27.4", "@esbuild/win32-x64": "0.27.4" }, "bin": { "esbuild": "bin/esbuild" } }, "sha512-Rq4vbHnYkK5fws5NF7MYTU68FPRE1ajX7heQ/8QXXWqNgqqJ/GkmmyxIzUnf2Sr/bakf8l54716CcMGHYhMrrQ=="], "fill-range": ["fill-range@7.1.1", "", { "dependencies": { "to-regex-range": "^5.0.1" } }, "sha512-YsGpe3WHLK8ZYi4tWDg2Jy3ebRz2rXowDxnld4bkQB00cc/1Zw9AWnC0i9ztDJitivtQvaI9KaLyKrc+hBW0yg=="], @@ -199,6 +226,8 @@ "graceful-fs": ["graceful-fs@4.2.11", "", {}, "sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ=="], + "happy-dom": ["happy-dom@20.8.4", "", { "dependencies": { "@types/node": ">=20.0.0", "@types/whatwg-mimetype": "^3.0.2", "@types/ws": "^8.18.1", "entities": "^7.0.1", "whatwg-mimetype": "^3.0.0", "ws": "^8.18.3" } }, "sha512-GKhjq4OQCYB4VLFBzv8mmccUadwlAusOZOI7hC1D9xDIT5HhzkJK17c4el2f6R6C715P9xB4uiMxeKUa2nHMwQ=="], + "is-interactive": ["is-interactive@2.0.0", "", {}, "sha512-qP1vozQRI+BMOPcjFzrjXuQvdak2pHNUMZoeG2eRbiSqyvbEf/wQtEOTOX1guk6E3t36RkaqiSt8A/6YElNxLQ=="], "is-number": ["is-number@7.0.0", "", {}, "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng=="], @@ -207,6 +236,8 @@ "jiti": ["jiti@2.6.1", "", { "bin": { "jiti": "lib/jiti-cli.mjs" } }, "sha512-ekilCSN1jwRvIbgeg/57YFh8qQDNbwDb9xT/qu2DAHbFFZUicIl4ygVaAvzveMhMVr3LnpSKTNnwt8PoOfmKhQ=="], + "js-tokens": ["js-tokens@4.0.0", "", {}, "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ=="], + "lightningcss": ["lightningcss@1.32.0", "", { "dependencies": { "detect-libc": "^2.0.3" }, "optionalDependencies": { "lightningcss-android-arm64": "1.32.0", "lightningcss-darwin-arm64": "1.32.0", "lightningcss-darwin-x64": "1.32.0", "lightningcss-freebsd-x64": "1.32.0", "lightningcss-linux-arm-gnueabihf": "1.32.0", "lightningcss-linux-arm64-gnu": "1.32.0", "lightningcss-linux-arm64-musl": "1.32.0", "lightningcss-linux-x64-gnu": "1.32.0", "lightningcss-linux-x64-musl": "1.32.0", "lightningcss-win32-arm64-msvc": "1.32.0", "lightningcss-win32-x64-msvc": "1.32.0" } }, "sha512-NXYBzinNrblfraPGyrbPoD19C1h9lfI/1mzgWYvXUTe414Gz/X1FD2XBZSZM7rRTrMA8JL3OtAaGifrIKhQ5yQ=="], "lightningcss-android-arm64": ["lightningcss-android-arm64@1.32.0", "", { "os": "android", "cpu": "arm64" }, "sha512-YK7/ClTt4kAK0vo6w3X+Pnm0D2cf2vPHbhOXdoNti1Ga0al1P4TBZhwjATvjNwLEBCnKvjJc2jQgHXH0NEwlAg=="], @@ -237,6 +268,8 @@ "log-symbols": ["log-symbols@7.0.1", "", { "dependencies": { "is-unicode-supported": "^2.0.0", "yoctocolors": "^2.1.1" } }, "sha512-ja1E3yCr9i/0hmBVaM0bfwDjnGy8I/s6PP4DFp+yP+a+mrHO4Rm7DtmnqROTUkHIkqffC84YY7AeqX6oFk0WFg=="], + "lz-string": ["lz-string@1.5.0", "", { "bin": { "lz-string": "bin/bin.js" } }, "sha512-h5bgJWpxJNswbU7qCrV0tIKQCaS3blPDrqKWx+QxzuzL1zGUzij9XCWLrSLsJPu5t+eWA/ycetzYAO5IOMcWAQ=="], + "magic-string": ["magic-string@0.30.21", "", { "dependencies": { "@jridgewell/sourcemap-codec": "^1.5.5" } }, "sha512-vd2F4YUyEXKGcLHoq+TEyCjxueSeHnFxyyjNp80yg0XV4vUhnDer/lvvlqM/arB5bXQN5K2/3oinyCRyx8T2CQ=="], "micromatch": ["micromatch@4.0.8", "", { "dependencies": { "braces": "^3.0.3", "picomatch": "^2.3.1" } }, "sha512-PXwfBhYu0hBCPw8Dn0E+WDYb7af3dSLVWKi3HGv84IdF4TyFoC0ysxFd0Goxw7nSv4T/PzEJQxsYsEiFCKo2BA=="], @@ -255,10 +288,14 @@ "postcss": ["postcss@8.5.8", "", { "dependencies": { "nanoid": "^3.3.11", "picocolors": "^1.1.1", "source-map-js": "^1.2.1" } }, "sha512-OW/rX8O/jXnm82Ey1k44pObPtdblfiuWnrd8X7GJ7emImCOstunGbXUpp7HdBrFQX6rJzn3sPT397Wp5aCwCHg=="], + "pretty-format": ["pretty-format@27.5.1", "", { "dependencies": { "ansi-regex": "^5.0.1", "ansi-styles": "^5.0.0", "react-is": "^17.0.1" } }, "sha512-Qb1gy5OrP5+zDf2Bvnzdl3jsTf1qXVMazbvCoKhtKqVs4/YK4ozX4gKQJJVyNe+cajNPn0KoC0MC3FUmaHWEmQ=="], + "react": ["react@19.2.4", "", {}, "sha512-9nfp2hYpCwOjAN+8TZFGhtWEwgvWHXqESH8qT89AT/lWklpLON22Lc8pEtnpsZz7VmawabSU0gCjnj8aC0euHQ=="], "react-dom": ["react-dom@19.2.4", "", { "dependencies": { "scheduler": "^0.27.0" }, "peerDependencies": { "react": "^19.2.4" } }, "sha512-AXJdLo8kgMbimY95O2aKQqsz2iWi9jMgKJhRBAxECE4IFxfcazB2LmzloIoibJI3C12IlY20+KFaLv+71bUJeQ=="], + "react-is": ["react-is@17.0.2", "", {}, "sha512-w2GsyukL62IJnlaff/nRegPQR94C/XXamvMWmSHRJ4y7Ts/4ocGRmTHvOs8PSE6pB3dWOrD/nueuU5sduBsQ4w=="], + "restore-cursor": ["restore-cursor@5.1.0", "", { "dependencies": { "onetime": "^7.0.0", "signal-exit": "^4.1.0" } }, "sha512-oMA2dcrw6u0YfxJQXm342bFKX/E4sG9rbTzO9ptUcR/e8A33cHuvStiYOwH7fszkZlZ1z/ta9AAoPk2F4qIOHA=="], "scheduler": ["scheduler@0.27.0", "", {}, "sha512-eNv+WrVbKu1f3vbYJT/xtiF5syA5HPIMtf9IgY/nKg0sWqzAUEvqY/xm7OcZc/qafLx/iO9FgOmeSAp4v5ti/Q=="], @@ -283,6 +320,10 @@ "undici-types": ["undici-types@7.16.0", "", {}, "sha512-Zz+aZWSj8LE6zoxD+xrjh4VfkIG8Ya6LvYkZqtUQGJPZjYl53ypCaUwWqo7eI0x66KBGeRo+mlBEkMSeSZ38Nw=="], + "whatwg-mimetype": ["whatwg-mimetype@3.0.0", "", {}, "sha512-nt+N2dzIutVRxARx1nghPKGv1xHikU7HKdfafKkLNLindmPU/ch3U31NOCGGA/dmPcmb1VlofO0vnKAcsm0o/Q=="], + + "ws": ["ws@8.19.0", "", { "peerDependencies": { "bufferutil": "^4.0.1", "utf-8-validate": ">=5.0.2" }, "optionalPeers": ["bufferutil", "utf-8-validate"] }, "sha512-blAT2mjOEIi0ZzruJfIhb3nps74PRWTCz1IjglWEEpQl5XS/UNama6u2/rjFkDDouqr4L67ry+1aGIALViWjDg=="], + "yoctocolors": ["yoctocolors@2.1.2", "", {}, "sha512-CzhO+pFNo8ajLM2d2IW/R93ipy99LWjtwblvC1RsoSUMZgyLbYFr221TnSNT7GjGdYui6P459mw9JH/g/zW2ug=="], "@tailwindcss/oxide-wasm32-wasi/@emnapi/core": ["@emnapi/core@1.9.1", "", { "dependencies": { "@emnapi/wasi-threads": "1.2.0", "tslib": "^2.4.0" }, "bundled": true }, "sha512-mukuNALVsoix/w1BJwFzwXBN/dHeejQtuVzcDsfOEsdpCumXb/E9j8w11h5S54tT1xhifGfbbSm/ICrObRb3KA=="], @@ -297,13 +338,13 @@ "@tailwindcss/oxide-wasm32-wasi/tslib": ["tslib@2.8.1", "", { "bundled": true }, "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w=="], - "@types/ws/@types/node": ["@types/node@22.13.5", "", { "dependencies": { "undici-types": "~6.20.0" } }, "sha512-+lTU0PxZXn0Dr1NBtC7Y8cR21AJr87dLLU953CWA6pMxxv/UDc7jYAY90upcrie1nRcD6XNG5HOYEDtgW5TxAg=="], - "bun-types/@types/node": ["@types/node@22.13.5", "", { "dependencies": { "undici-types": "~6.20.0" } }, "sha512-+lTU0PxZXn0Dr1NBtC7Y8cR21AJr87dLLU953CWA6pMxxv/UDc7jYAY90upcrie1nRcD6XNG5HOYEDtgW5TxAg=="], + "bun-types/@types/ws": ["@types/ws@8.5.14", "", { "dependencies": { "@types/node": "*" } }, "sha512-bd/YFLW+URhBzMXurx7lWByOu+xzU9+kb3RboOteXYDfW+tr+JZa99OyNmPINEGB/ahzKrEuc8rcv4gnpJmxTw=="], + "lightningcss-wasm/napi-wasm": ["napi-wasm@1.1.3", "", { "bundled": true }, "sha512-h/4nMGsHjZDCYmQVNODIrYACVJ+I9KItbG+0si6W/jSjdA9JbWDoU4LLeMXVcEQGHjttI2tuXqDrbGF7qkUHHg=="], - "@types/ws/@types/node/undici-types": ["undici-types@6.20.0", "", {}, "sha512-Ny6QZ2Nju20vw1SRHe3d9jVu6gJ+4e3+MMpqu7pqE5HT6WsTSlce++GQmK5UXS8mzV8DSYHrQH+Xrf2jVcuKNg=="], + "strip-ansi/ansi-regex": ["ansi-regex@6.2.2", "", {}, "sha512-Bq3SmSpyFHaWjPk8If9yc6svM8c56dB5BAtW4Qbw5jHTwwXXcTLoRMkpDJp6VL0XzlWaCHTXrkFURMYmD0sLqg=="], "bun-types/@types/node/undici-types": ["undici-types@6.20.0", "", {}, "sha512-Ny6QZ2Nju20vw1SRHe3d9jVu6gJ+4e3+MMpqu7pqE5HT6WsTSlce++GQmK5UXS8mzV8DSYHrQH+Xrf2jVcuKNg=="], } diff --git a/dist/__tests__/e2e/e2e.test.d.ts b/dist/__tests__/e2e/e2e.test.d.ts new file mode 100644 index 0000000..cb0ff5c --- /dev/null +++ b/dist/__tests__/e2e/e2e.test.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/dist/__tests__/e2e/e2e.test.js b/dist/__tests__/e2e/e2e.test.js new file mode 100644 index 0000000..daeaec4 --- /dev/null +++ b/dist/__tests__/e2e/e2e.test.js @@ -0,0 +1,52 @@ +import { describe, expect, test, beforeAll, afterAll } from "bun:test"; +import startServer from "../../../src/functions/server/start-server"; +import bunextInit from "../../../src/functions/bunext-init"; +import path from "path"; +import fs from "fs"; +let originalCwd = process.cwd(); +describe("E2E Integration", () => { + let server; + beforeAll(async () => { + // Change to the fixture directory to simulate actual user repo + const fixtureDir = path.resolve(__dirname, "../__fixtures__/app"); + process.chdir(fixtureDir); + // Mock grabAppPort to assign dynamically to avoid port conflicts + global.CONFIG = { development: true }; + }); + afterAll(async () => { + if (server) { + server.stop(true); + } + process.chdir(originalCwd); + // Ensure to remove the dummy generated .bunext folder + const dotBunext = path.resolve(__dirname, "../__fixtures__/app/.bunext"); + if (fs.existsSync(dotBunext)) { + fs.rmSync(dotBunext, { recursive: true, force: true }); + } + const pubBunext = path.resolve(__dirname, "../__fixtures__/app/public/__bunext"); + if (fs.existsSync(pubBunext)) { + fs.rmSync(pubBunext, { recursive: true, force: true }); + } + }); + test("boots up the server and correctly routes to index.tsx page", async () => { + // Mock to randomize port + // Note: Bun test runs modules in isolation but startServer imports grab-app-port + // If we can't easily mock we can set PORT env + process.env.PORT = "0"; // Let Bun.serve pick port + await bunextInit(); + server = await startServer(); + expect(server).toBeDefined(); + // Fetch the index page + const response = await fetch(`http://localhost:${server.port}/`); + expect(response.status).toBe(200); + const html = await response.text(); + expect(html).toContain("Hello E2E"); + }); + test("returns 404 for unknown route", async () => { + const response = await fetch(`http://localhost:${server.port}/unknown-foo-bar123`); + expect(response.status).toBe(404); + const text = await response.text(); + // Assume default 404 preset component is rendered + expect(text).toContain("404"); + }); +}); diff --git a/dist/__tests__/functions/server/bunext-req-handler.test.d.ts b/dist/__tests__/functions/server/bunext-req-handler.test.d.ts new file mode 100644 index 0000000..cb0ff5c --- /dev/null +++ b/dist/__tests__/functions/server/bunext-req-handler.test.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/dist/__tests__/functions/server/bunext-req-handler.test.js b/dist/__tests__/functions/server/bunext-req-handler.test.js new file mode 100644 index 0000000..6269047 --- /dev/null +++ b/dist/__tests__/functions/server/bunext-req-handler.test.js @@ -0,0 +1,73 @@ +import { describe, expect, test, mock, afterAll } from "bun:test"; +import bunextRequestHandler from "../../../../src/functions/server/bunext-req-handler"; +mock.module("../../../../src/utils/is-development", () => ({ + default: () => true +})); +mock.module("../../../../src/utils/grab-constants", () => ({ + default: () => ({ + config: { + middleware: async ({ url }) => { + if (url.pathname === "/blocked") { + return new Response("Blocked by middleware", { status: 403 }); + } + return undefined; + } + } + }) +})); +mock.module("../../../../src/functions/server/handle-routes", () => ({ + default: async () => new Response("api-routes") +})); +mock.module("../../../../src/functions/server/handle-public", () => ({ + default: async () => new Response("public") +})); +mock.module("../../../../src/functions/server/handle-files", () => ({ + default: async () => new Response("files") +})); +mock.module("../../../../src/functions/server/web-pages/handle-web-pages", () => ({ + default: async () => new Response("web-pages") +})); +/** + * Tests for the `bunext-req-handler` module. + * Ensures that requests are correctly routed to the proper subsystem. + */ +describe("bunext-req-handler", () => { + afterAll(() => { + mock.restore(); + }); + test("middleware is caught", async () => { + const req = new Request("http://localhost/blocked"); + const res = await bunextRequestHandler({ req }); + expect(res.status).toBe(403); + expect(await res.text()).toBe("Blocked by middleware"); + }); + test("routes /__hmr to handleHmr in dev", async () => { + global.ROUTER = { match: () => ({}) }; + global.HMR_CONTROLLERS = []; + const req = new Request("http://localhost/__hmr", { + headers: { referer: "http://localhost/" } + }); + const res = await bunextRequestHandler({ req }); + expect(res.headers.get("Content-Type")).toBe("text/event-stream"); + }); + test("routes /api/ to handleRoutes", async () => { + const req = new Request("http://localhost/api/users"); + const res = await bunextRequestHandler({ req }); + expect(await res.text()).toBe("api-routes"); + }); + test("routes /public/ to handlePublic", async () => { + const req = new Request("http://localhost/public/image.png"); + const res = await bunextRequestHandler({ req }); + expect(await res.text()).toBe("public"); + }); + test("routes files like .js to handleFiles", async () => { + const req = new Request("http://localhost/script.js"); + const res = await bunextRequestHandler({ req }); + expect(await res.text()).toBe("files"); + }); + test("routes anything else to handleWebPages", async () => { + const req = new Request("http://localhost/about"); + const res = await bunextRequestHandler({ req }); + expect(await res.text()).toBe("web-pages"); + }); +}); diff --git a/dist/__tests__/functions/server/handle-hmr.test.d.ts b/dist/__tests__/functions/server/handle-hmr.test.d.ts new file mode 100644 index 0000000..cb0ff5c --- /dev/null +++ b/dist/__tests__/functions/server/handle-hmr.test.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/dist/__tests__/functions/server/handle-hmr.test.js b/dist/__tests__/functions/server/handle-hmr.test.js new file mode 100644 index 0000000..5c6b60f --- /dev/null +++ b/dist/__tests__/functions/server/handle-hmr.test.js @@ -0,0 +1,37 @@ +import { describe, expect, test, mock, beforeEach, afterEach } from "bun:test"; +import handleHmr from "../../../../src/functions/server/handle-hmr"; +describe("handle-hmr", () => { + beforeEach(() => { + global.ROUTER = { + match: (path) => { + if (path === "/test") + return { filePath: "/test-file" }; + return null; + } + }; + global.HMR_CONTROLLERS = []; + global.BUNDLER_CTX_MAP = [ + { local_path: "/test-file" } + ]; + }); + afterEach(() => { + global.ROUTER = undefined; + global.HMR_CONTROLLERS = []; + global.BUNDLER_CTX_MAP = undefined; + }); + test("sets up SSE stream and pushes to HMR_CONTROLLERS", async () => { + const req = new Request("http://localhost/hmr", { + headers: { + "referer": "http://localhost/test" + } + }); + const res = await handleHmr({ req }); + expect(res.status).toBe(200); + expect(res.headers.get("Content-Type")).toBe("text/event-stream"); + expect(res.headers.get("Connection")).toBe("keep-alive"); + expect(global.HMR_CONTROLLERS.length).toBe(1); + const controller = global.HMR_CONTROLLERS[0]; + expect(controller.page_url).toBe("http://localhost/test"); + expect(controller.target_map?.local_path).toBe("/test-file"); + }); +}); diff --git a/dist/__tests__/functions/server/handle-routes.test.d.ts b/dist/__tests__/functions/server/handle-routes.test.d.ts new file mode 100644 index 0000000..cb0ff5c --- /dev/null +++ b/dist/__tests__/functions/server/handle-routes.test.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/dist/__tests__/functions/server/handle-routes.test.js b/dist/__tests__/functions/server/handle-routes.test.js new file mode 100644 index 0000000..2bd7610 --- /dev/null +++ b/dist/__tests__/functions/server/handle-routes.test.js @@ -0,0 +1,65 @@ +import { describe, expect, test, mock, afterAll } from "bun:test"; +import handleRoutes from "../../../../src/functions/server/handle-routes"; +mock.module("../../../../src/utils/is-development", () => ({ + default: () => false +})); +mock.module("../../../../src/utils/grab-constants", () => ({ + default: () => ({ MBInBytes: 1048576, ServerDefaultRequestBodyLimitBytes: 5242880 }) +})); +mock.module("../../../../src/utils/grab-router", () => ({ + default: () => ({ + match: (path) => { + if (path === "/api/test") + return { filePath: "/test-path" }; + if (path === "/api/large") + return { filePath: "/large-path" }; + return null; + } + }) +})); +mock.module("../../../../src/utils/grab-route-params", () => ({ + default: async () => ({ params: {}, searchParams: {} }) +})); +mock.module("/test-path", () => ({ + default: async () => new Response("OK", { status: 200 }) +})); +mock.module("/large-path", () => ({ + default: async () => new Response("Large OK", { status: 200 }), + config: { maxRequestBodyMB: 1 } +})); +/** + * Tests for routing logic within `handle-routes`. + */ +describe("handle-routes", () => { + afterAll(() => { + mock.restore(); + }); + test("returns 401 for unknown route", async () => { + const req = new Request("http://localhost/api/unknown"); + const res = await handleRoutes({ req }); + expect(res.status).toBe(401); + const json = await res.json(); + expect(json.success).toBe(false); + expect(json.msg).toContain("not found"); + }); + test("calls matched module default export", async () => { + const req = new Request("http://localhost/api/test"); + const res = await handleRoutes({ req }); + expect(res.status).toBe(200); + expect(await res.text()).toBe("OK"); + }); + test("enforces request body size limits", async () => { + // limit is 1MB from mock config + const req = new Request("http://localhost/api/large", { + method: "POST", + headers: { + "content-length": "2000000" // ~2MB + }, + body: "x".repeat(10) // the actual body doesn't matter since handleRoutes only checks the header + }); + const res = await handleRoutes({ req }); + expect(res.status).toBe(413); + const json = await res.json(); + expect(json.success).toBe(false); + }); +}); diff --git a/dist/__tests__/functions/server/start-server.test.d.ts b/dist/__tests__/functions/server/start-server.test.d.ts new file mode 100644 index 0000000..cb0ff5c --- /dev/null +++ b/dist/__tests__/functions/server/start-server.test.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/dist/__tests__/functions/server/start-server.test.js b/dist/__tests__/functions/server/start-server.test.js new file mode 100644 index 0000000..fb359fb --- /dev/null +++ b/dist/__tests__/functions/server/start-server.test.js @@ -0,0 +1,36 @@ +import { describe, expect, test, mock, afterEach } from "bun:test"; +import startServer from "../../../../src/functions/server/start-server"; +import { log } from "../../../../src/utils/log"; +// Mock log so we don't spam terminal during tests +mock.module("../../../../src/utils/log", () => ({ + log: { + server: mock((msg) => { }), + info: mock((msg) => { }), + error: mock((msg) => { }), + } +})); +// Mock grabConfig so it doesn't try to look for bunext.config.ts and exit process +mock.module("../../../../src/functions/grab-config", () => ({ + default: async () => ({}) +})); +// Mock grabAppPort to return 0 so Bun.serve picks a random port +mock.module("../../../../src/utils/grab-app-port", () => ({ + default: () => 0 +})); +describe("startServer", () => { + afterEach(() => { + if (global.SERVER) { + global.SERVER.stop(true); + global.SERVER = undefined; + } + }); + test("starts the server and assigns to global.SERVER", async () => { + global.CONFIG = { development: true }; + const server = await startServer(); + expect(server).toBeDefined(); + expect(server.port).toBeGreaterThan(0); + expect(global.SERVER).toBe(server); + expect(log.server).toHaveBeenCalled(); + server.stop(true); + }); +}); diff --git a/dist/__tests__/hydration/hydration.test.d.ts b/dist/__tests__/hydration/hydration.test.d.ts new file mode 100644 index 0000000..cb0ff5c --- /dev/null +++ b/dist/__tests__/hydration/hydration.test.d.ts @@ -0,0 +1 @@ +export {}; diff --git a/dist/__tests__/hydration/hydration.test.js b/dist/__tests__/hydration/hydration.test.js new file mode 100644 index 0000000..60b9b38 --- /dev/null +++ b/dist/__tests__/hydration/hydration.test.js @@ -0,0 +1,62 @@ +import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; +import { describe, expect, test, beforeEach, afterEach } from "bun:test"; +import React, { useState } from "react"; +import { renderToString } from "react-dom/server"; +import { hydrateRoot } from "react-dom/client"; +import { GlobalWindow } from "happy-dom"; +// A mock application component to test hydration +function App() { + const [count, setCount] = useState(0); + return (_jsxs("div", { id: "app-root", children: [_jsx("h1", { children: "Test Hydration" }), _jsxs("p", { "data-testid": "count", children: ["Count: ", count] }), _jsx("button", { "data-testid": "btn", onClick: () => setCount(c => c + 1), children: "Increment" })] })); +} +describe("React Hydration", () => { + let window; + let document; + beforeEach(() => { + window = new GlobalWindow(); + document = window.document; + global.window = window; + global.document = document; + global.navigator = { userAgent: "node.js" }; + }); + afterEach(() => { + // Clean up global mocks + delete global.window; + delete global.document; + delete global.navigator; + window.close(); + }); + test("hydrates a server-rendered component and binds events", async () => { + // 1. Server-side render + const html = renderToString(_jsx(App, {})); + // 2. Setup DOM as it would be delivered to the client + document.body.innerHTML = `
${html}
`; + const rootNode = document.getElementById("root"); + // 3. Hydrate + let hydrateError = null; + try { + await new Promise((resolve) => { + hydrateRoot(rootNode, _jsx(App, {}), { + onRecoverableError: (err) => { + hydrateError = err; + } + }); + setTimeout(resolve, 50); // let React finish hydration + }); + } + catch (e) { + hydrateError = e; + } + // Verify no hydration errors + expect(hydrateError).toBeNull(); + // 4. Verify client-side interactivity + const button = document.querySelector('[data-testid="btn"]'); + const countText = document.querySelector('[data-testid="count"]'); + expect(countText.textContent).toBe("Count: 0"); + // Simulate click + button.dispatchEvent(new window.Event("click", { bubbles: true })); + // Let async state updates process + await new Promise(r => setTimeout(r, 50)); + expect(countText.textContent).toBe("Count: 1"); + }); +}); diff --git a/dist/functions/server/bunext-req-handler.js b/dist/functions/server/bunext-req-handler.js index 306a4af..6062703 100644 --- a/dist/functions/server/bunext-req-handler.js +++ b/dist/functions/server/bunext-req-handler.js @@ -2,9 +2,7 @@ import handleWebPages from "./web-pages/handle-web-pages"; import handleRoutes from "./handle-routes"; import isDevelopment from "../../utils/is-development"; import grabConstants from "../../utils/grab-constants"; -import { AppData } from "../../data/app-data"; import handleHmr from "./handle-hmr"; -import handleHmrUpdate from "./handle-hmr-update"; import handlePublic from "./handle-public"; import handleFiles from "./handle-files"; export default async function bunextRequestHandler({ req: initial_req, }) { @@ -26,10 +24,7 @@ export default async function bunextRequestHandler({ req: initial_req, }) { req = middleware_res; } } - if (url.pathname == `/${AppData["ClientHMRPath"]}`) { - response = await handleHmrUpdate({ req }); - } - else if (url.pathname === "/__hmr" && is_dev) { + if (url.pathname === "/__hmr" && is_dev) { response = await handleHmr({ req }); } else if (url.pathname.startsWith("/api/")) { diff --git a/dist/functions/server/web-pages/grab-page-error-component.js b/dist/functions/server/web-pages/grab-page-error-component.js index 60311aa..f0e2714 100644 --- a/dist/functions/server/web-pages/grab-page-error-component.js +++ b/dist/functions/server/web-pages/grab-page-error-component.js @@ -21,6 +21,11 @@ export default async function grabPageErrorComponent({ error, routeParams, is404 routeParams, module, bundledMap, + serverRes: { + responseOptions: { + status: is404 ? 404 : 500 + } + } }; } catch { @@ -37,6 +42,11 @@ export default async function grabPageErrorComponent({ error, routeParams, is404 routeParams, module: { default: DefaultNotFound }, bundledMap: {}, + serverRes: { + responseOptions: { + status: is404 ? 404 : 500 + } + } }; } } diff --git a/package.json b/package.json index ed2ada7..a19dbce 100644 --- a/package.json +++ b/package.json @@ -14,17 +14,21 @@ ], "scripts": { "dev": "tsc --watch", - "publish": "tsc --noEmit && tsc && git add . && git commit -m 'Update watcher function. Add tests' && git push", + "publish": "tsc --noEmit && tsc && git add . && git commit -m 'Update tests' && git push", "compile": "bun build ./src/commands/index.ts --compile --outfile bin/bunext", - "build": "tsc" + "build": "tsc", + "test": "bun test --max-concurrency=1" }, "devDependencies": { + "@testing-library/dom": "^10.4.1", + "@testing-library/react": "^16.3.2", "@types/bun": "latest", "@types/lodash": "^4.17.24", "@types/micromatch": "^4.0.10", "@types/node": "^24.10.0", "@types/react": "^19.2.2", "@types/react-dom": "^19.2.2", + "happy-dom": "^20.8.4", "react": "^19.2.4", "react-dom": "^19.2.4" }, diff --git a/src/__tests__/e2e/e2e.test.ts b/src/__tests__/e2e/e2e.test.ts new file mode 100644 index 0000000..c8f0969 --- /dev/null +++ b/src/__tests__/e2e/e2e.test.ts @@ -0,0 +1,63 @@ +import { describe, expect, test, beforeAll, afterAll } from "bun:test"; +import startServer from "../../../src/functions/server/start-server"; +import bunextInit from "../../../src/functions/bunext-init"; +import path from "path"; +import fs from "fs"; + +let originalCwd = process.cwd(); + +describe("E2E Integration", () => { + let server: any; + + beforeAll(async () => { + // Change to the fixture directory to simulate actual user repo + const fixtureDir = path.resolve(__dirname, "../__fixtures__/app"); + process.chdir(fixtureDir); + + // Mock grabAppPort to assign dynamically to avoid port conflicts + global.CONFIG = { development: true }; + }); + + afterAll(async () => { + if (server) { + server.stop(true); + } + process.chdir(originalCwd); + + // Ensure to remove the dummy generated .bunext folder + const dotBunext = path.resolve(__dirname, "../__fixtures__/app/.bunext"); + if (fs.existsSync(dotBunext)) { + fs.rmSync(dotBunext, { recursive: true, force: true }); + } + const pubBunext = path.resolve(__dirname, "../__fixtures__/app/public/__bunext"); + if (fs.existsSync(pubBunext)) { + fs.rmSync(pubBunext, { recursive: true, force: true }); + } + }); + + test("boots up the server and correctly routes to index.tsx page", async () => { + // Mock to randomize port + // Note: Bun test runs modules in isolation but startServer imports grab-app-port + // If we can't easily mock we can set PORT env + process.env.PORT = "0"; // Let Bun.serve pick port + + await bunextInit(); + server = await startServer(); + expect(server).toBeDefined(); + + // Fetch the index page + const response = await fetch(`http://localhost:${server.port}/`); + expect(response.status).toBe(200); + + const html = await response.text(); + expect(html).toContain("Hello E2E"); + }); + + test("returns 404 for unknown route", async () => { + const response = await fetch(`http://localhost:${server.port}/unknown-foo-bar123`); + expect(response.status).toBe(404); + const text = await response.text(); + // Assume default 404 preset component is rendered + expect(text).toContain("404"); + }); +}); diff --git a/src/__tests__/functions/server/bunext-req-handler.test.ts b/src/__tests__/functions/server/bunext-req-handler.test.ts new file mode 100644 index 0000000..e7c78da --- /dev/null +++ b/src/__tests__/functions/server/bunext-req-handler.test.ts @@ -0,0 +1,86 @@ +import { describe, expect, test, mock, afterAll } from "bun:test"; +import bunextRequestHandler from "../../../../src/functions/server/bunext-req-handler"; + +mock.module("../../../../src/utils/is-development", () => ({ + default: () => true +})); + +mock.module("../../../../src/utils/grab-constants", () => ({ + default: () => ({ + config: { + middleware: async ({ url }: any) => { + if (url.pathname === "/blocked") { + return new Response("Blocked by middleware", { status: 403 }); + } + return undefined; + } + } + }) +})); + +mock.module("../../../../src/functions/server/handle-routes", () => ({ + default: async () => new Response("api-routes") +})); + +mock.module("../../../../src/functions/server/handle-public", () => ({ + default: async () => new Response("public") +})); + +mock.module("../../../../src/functions/server/handle-files", () => ({ + default: async () => new Response("files") +})); + +mock.module("../../../../src/functions/server/web-pages/handle-web-pages", () => ({ + default: async () => new Response("web-pages") +})); + +/** + * Tests for the `bunext-req-handler` module. + * Ensures that requests are correctly routed to the proper subsystem. + */ +describe("bunext-req-handler", () => { + afterAll(() => { + mock.restore(); + }); + + test("middleware is caught", async () => { + const req = new Request("http://localhost/blocked"); + const res = await bunextRequestHandler({ req }); + expect(res.status).toBe(403); + expect(await res.text()).toBe("Blocked by middleware"); + }); + + test("routes /__hmr to handleHmr in dev", async () => { + global.ROUTER = { match: () => ({}) } as any; + global.HMR_CONTROLLERS = []; + const req = new Request("http://localhost/__hmr", { + headers: { referer: "http://localhost/" } + }); + const res = await bunextRequestHandler({ req }); + expect(res.headers.get("Content-Type")).toBe("text/event-stream"); + }); + + test("routes /api/ to handleRoutes", async () => { + const req = new Request("http://localhost/api/users"); + const res = await bunextRequestHandler({ req }); + expect(await res.text()).toBe("api-routes"); + }); + + test("routes /public/ to handlePublic", async () => { + const req = new Request("http://localhost/public/image.png"); + const res = await bunextRequestHandler({ req }); + expect(await res.text()).toBe("public"); + }); + + test("routes files like .js to handleFiles", async () => { + const req = new Request("http://localhost/script.js"); + const res = await bunextRequestHandler({ req }); + expect(await res.text()).toBe("files"); + }); + + test("routes anything else to handleWebPages", async () => { + const req = new Request("http://localhost/about"); + const res = await bunextRequestHandler({ req }); + expect(await res.text()).toBe("web-pages"); + }); +}); diff --git a/src/__tests__/functions/server/handle-hmr.test.ts b/src/__tests__/functions/server/handle-hmr.test.ts new file mode 100644 index 0000000..a2ec590 --- /dev/null +++ b/src/__tests__/functions/server/handle-hmr.test.ts @@ -0,0 +1,42 @@ +import { describe, expect, test, mock, beforeEach, afterEach } from "bun:test"; +import handleHmr from "../../../../src/functions/server/handle-hmr"; + +describe("handle-hmr", () => { + beforeEach(() => { + global.ROUTER = { + match: (path: string) => { + if (path === "/test") return { filePath: "/test-file" }; + return null; + } + } as any; + global.HMR_CONTROLLERS = []; + global.BUNDLER_CTX_MAP = [ + { local_path: "/test-file" } as any + ]; + }); + + afterEach(() => { + global.ROUTER = undefined as any; + global.HMR_CONTROLLERS = []; + global.BUNDLER_CTX_MAP = undefined as any; + }); + + test("sets up SSE stream and pushes to HMR_CONTROLLERS", async () => { + const req = new Request("http://localhost/hmr", { + headers: { + "referer": "http://localhost/test" + } + }); + + const res = await handleHmr({ req }); + + expect(res.status).toBe(200); + expect(res.headers.get("Content-Type")).toBe("text/event-stream"); + expect(res.headers.get("Connection")).toBe("keep-alive"); + + expect(global.HMR_CONTROLLERS.length).toBe(1); + const controller = global.HMR_CONTROLLERS[0]; + expect(controller.page_url).toBe("http://localhost/test"); + expect(controller.target_map?.local_path).toBe("/test-file"); + }); +}); diff --git a/src/__tests__/functions/server/handle-routes.test.ts b/src/__tests__/functions/server/handle-routes.test.ts new file mode 100644 index 0000000..c813c23 --- /dev/null +++ b/src/__tests__/functions/server/handle-routes.test.ts @@ -0,0 +1,76 @@ +import { describe, expect, test, mock, afterAll } from "bun:test"; +import handleRoutes from "../../../../src/functions/server/handle-routes"; + +mock.module("../../../../src/utils/is-development", () => ({ + default: () => false +})); + +mock.module("../../../../src/utils/grab-constants", () => ({ + default: () => ({ MBInBytes: 1048576, ServerDefaultRequestBodyLimitBytes: 5242880 }) +})); + +mock.module("../../../../src/utils/grab-router", () => ({ + default: () => ({ + match: (path: string) => { + if (path === "/api/test") return { filePath: "/test-path" }; + if (path === "/api/large") return { filePath: "/large-path" }; + return null; + } + }) +})); + +mock.module("../../../../src/utils/grab-route-params", () => ({ + default: async () => ({ params: {}, searchParams: {} }) +})); + +mock.module("/test-path", () => ({ + default: async () => new Response("OK", { status: 200 }) +})); + +mock.module("/large-path", () => ({ + default: async () => new Response("Large OK", { status: 200 }), + config: { maxRequestBodyMB: 1 } +})); + +/** + * Tests for routing logic within `handle-routes`. + */ +describe("handle-routes", () => { + afterAll(() => { + mock.restore(); + }); + + test("returns 401 for unknown route", async () => { + const req = new Request("http://localhost/api/unknown"); + const res = await handleRoutes({ req }); + + expect(res.status).toBe(401); + const json = await res.json(); + expect(json.success).toBe(false); + expect(json.msg).toContain("not found"); + }); + + test("calls matched module default export", async () => { + const req = new Request("http://localhost/api/test"); + const res = await handleRoutes({ req }); + + expect(res.status).toBe(200); + expect(await res.text()).toBe("OK"); + }); + + test("enforces request body size limits", async () => { + // limit is 1MB from mock config + const req = new Request("http://localhost/api/large", { + method: "POST", + headers: { + "content-length": "2000000" // ~2MB + }, + body: "x".repeat(10) // the actual body doesn't matter since handleRoutes only checks the header + }); + const res = await handleRoutes({ req }); + + expect(res.status).toBe(413); + const json = await res.json(); + expect(json.success).toBe(false); + }); +}); diff --git a/src/__tests__/functions/server/start-server.test.ts b/src/__tests__/functions/server/start-server.test.ts new file mode 100644 index 0000000..e100680 --- /dev/null +++ b/src/__tests__/functions/server/start-server.test.ts @@ -0,0 +1,44 @@ +import { describe, expect, test, mock, afterEach } from "bun:test"; +import startServer from "../../../../src/functions/server/start-server"; +import { log } from "../../../../src/utils/log"; + +// Mock log so we don't spam terminal during tests +mock.module("../../../../src/utils/log", () => ({ + log: { + server: mock((msg: string) => {}), + info: mock((msg: string) => {}), + error: mock((msg: string) => {}), + } +})); + +// Mock grabConfig so it doesn't try to look for bunext.config.ts and exit process +mock.module("../../../../src/functions/grab-config", () => ({ + default: async () => ({}) +})); + +// Mock grabAppPort to return 0 so Bun.serve picks a random port +mock.module("../../../../src/utils/grab-app-port", () => ({ + default: () => 0 +})); + +describe("startServer", () => { + afterEach(() => { + if (global.SERVER) { + global.SERVER.stop(true); + global.SERVER = undefined; + } + }); + + test("starts the server and assigns to global.SERVER", async () => { + global.CONFIG = { development: true }; + + const server = await startServer(); + + expect(server).toBeDefined(); + expect(server.port).toBeGreaterThan(0); + expect(global.SERVER).toBe(server); + expect(log.server).toHaveBeenCalled(); + + server.stop(true); + }); +}); diff --git a/src/__tests__/hydration/hydration.test.tsx b/src/__tests__/hydration/hydration.test.tsx new file mode 100644 index 0000000..6507bf2 --- /dev/null +++ b/src/__tests__/hydration/hydration.test.tsx @@ -0,0 +1,79 @@ +import { describe, expect, test, beforeEach, afterEach } from "bun:test"; +import React, { useState } from "react"; +import { renderToString } from "react-dom/server"; +import { hydrateRoot } from "react-dom/client"; +import { GlobalWindow } from "happy-dom"; + +// A mock application component to test hydration +function App() { + const [count, setCount] = useState(0); + return ( +
+

Test Hydration

+

Count: {count}

+ +
+ ); +} + +describe("React Hydration", () => { + let window: GlobalWindow; + let document: any; + + beforeEach(() => { + window = new GlobalWindow(); + document = window.document; + global.window = window as any; + global.document = document as any; + global.navigator = { userAgent: "node.js" } as any; + }); + + afterEach(() => { + // Clean up global mocks + delete (global as any).window; + delete (global as any).document; + delete (global as any).navigator; + window.close(); + }); + + test("hydrates a server-rendered component and binds events", async () => { + // 1. Server-side render + const html = renderToString(); + + // 2. Setup DOM as it would be delivered to the client + document.body.innerHTML = `
${html}
`; + const rootNode = document.getElementById("root"); + + // 3. Hydrate + let hydrateError = null; + try { + await new Promise((resolve) => { + hydrateRoot(rootNode, , { + onRecoverableError: (err) => { + hydrateError = err; + } + }); + setTimeout(resolve, 50); // let React finish hydration + }); + } catch (e) { + hydrateError = e; + } + + // Verify no hydration errors + expect(hydrateError).toBeNull(); + + // 4. Verify client-side interactivity + const button = document.querySelector('[data-testid="btn"]'); + const countText = document.querySelector('[data-testid="count"]'); + + expect(countText.textContent).toBe("Count: 0"); + + // Simulate click + button.dispatchEvent(new window.Event("click", { bubbles: true })); + + // Let async state updates process + await new Promise(r => setTimeout(r, 50)); + + expect(countText.textContent).toBe("Count: 1"); + }); +}); diff --git a/src/functions/server/bunext-req-handler.ts b/src/functions/server/bunext-req-handler.ts index 5272d44..ace3b76 100644 --- a/src/functions/server/bunext-req-handler.ts +++ b/src/functions/server/bunext-req-handler.ts @@ -2,9 +2,7 @@ import handleWebPages from "./web-pages/handle-web-pages"; import handleRoutes from "./handle-routes"; import isDevelopment from "../../utils/is-development"; import grabConstants from "../../utils/grab-constants"; -import { AppData } from "../../data/app-data"; import handleHmr from "./handle-hmr"; -import handleHmrUpdate from "./handle-hmr-update"; import handlePublic from "./handle-public"; import handleFiles from "./handle-files"; type Params = { @@ -39,9 +37,7 @@ export default async function bunextRequestHandler({ } } - if (url.pathname == `/${AppData["ClientHMRPath"]}`) { - response = await handleHmrUpdate({ req }); - } else if (url.pathname === "/__hmr" && is_dev) { + if (url.pathname === "/__hmr" && is_dev) { response = await handleHmr({ req }); } else if (url.pathname.startsWith("/api/")) { response = await handleRoutes({ req }); diff --git a/src/functions/server/handle-hmr-update.ts b/src/functions/server/handle-hmr-update.ts deleted file mode 100644 index 3f46fc0..0000000 --- a/src/functions/server/handle-hmr-update.ts +++ /dev/null @@ -1,82 +0,0 @@ -import grabDirNames from "../../utils/grab-dir-names"; -import { AppData } from "../../data/app-data"; -import path from "path"; -import grabRootFile from "./web-pages/grab-root-file"; -import grabPageBundledReactComponent from "./web-pages/grab-page-bundled-react-component"; -import writeHMRTsxModule from "./web-pages/write-hmr-tsx-module"; - -const { BUNX_HYDRATION_SRC_DIR } = grabDirNames(); - -type Params = { - req: Request; -}; - -export default async function ({ req }: Params): Promise { - try { - const url = new URL(req.url); - - const target_href = url.searchParams.get("href"); - - if (!target_href) { - return new Response( - `No HREF passed to /${AppData["ClientHMRPath"]}`, - { status: 404 }, - ); - } - - const target_href_url = new URL(target_href); - - const match = global.ROUTER.match(target_href_url.pathname); - - if (!match?.filePath) { - return new Response(`No pages file matched for this path`, { - status: 404, - }); - } - - const out_file = path.join( - BUNX_HYDRATION_SRC_DIR, - target_href_url.pathname, - "index.js", - ); - - const { root_file } = grabRootFile(); - - const { tsx } = - (await grabPageBundledReactComponent({ - file_path: match.filePath, - root_file, - })) || {}; - - if (!tsx) { - throw new Error(`Couldn't grab txt string`); - } - - const artifact = await writeHMRTsxModule({ - tsx, - out_file, - }); - - const file = Bun.file(out_file); - - if (await file.exists()) { - return new Response(file, { - headers: { - "Content-Type": "text/javascript", - }, - }); - } - - return new Response("Not found", { - status: 404, - }); - } catch (error: any) { - const error_msg = error.message; - - console.error(error_msg); - - return new Response(error_msg || "HMR Error", { - status: 404, - }); - } -} diff --git a/src/functions/server/web-pages/grab-page-error-component.tsx b/src/functions/server/web-pages/grab-page-error-component.tsx index d466af2..fd441d2 100644 --- a/src/functions/server/web-pages/grab-page-error-component.tsx +++ b/src/functions/server/web-pages/grab-page-error-component.tsx @@ -47,6 +47,11 @@ export default async function grabPageErrorComponent({ routeParams, module, bundledMap, + serverRes: { + responseOptions: { + status: is404 ? 404 : 500 + } + } as any }; } catch { const DefaultNotFound: FC = () => ( @@ -70,6 +75,11 @@ export default async function grabPageErrorComponent({ routeParams, module: { default: DefaultNotFound }, bundledMap: {} as BundlerCTXMap, + serverRes: { + responseOptions: { + status: is404 ? 404 : 500 + } + } as any }; } }