@etus/bhono-app 0.1.5 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (300) hide show
  1. package/dist/index.js +0 -0
  2. package/package.json +7 -2
  3. package/templates/base/.claude/commands/check-skill-rules.md +112 -29
  4. package/templates/base/.claude/commands/linear/implement-issue.md +383 -55
  5. package/templates/base/.claude/commands/ship.md +77 -13
  6. package/templates/base/.claude/hooks/package-lock.json +0 -419
  7. package/templates/base/.claude/hooks/skill-activation-prompt.ts +185 -113
  8. package/templates/base/.claude/hooks/skill-tool-guard.sh +6 -0
  9. package/templates/base/.claude/hooks/skill-tool-guard.ts +198 -0
  10. package/templates/base/.claude/scripts/validate-skill-rules.sh +55 -32
  11. package/templates/base/.claude/settings.json +18 -11
  12. package/templates/base/.claude/skills/skill-rules.json +326 -173
  13. package/templates/base/.env.example +3 -0
  14. package/templates/base/CLAUDE.md +5 -5
  15. package/templates/base/README.md +40 -27
  16. package/templates/base/config/eslint.config.js +1 -0
  17. package/templates/base/config/wrangler.json +16 -17
  18. package/templates/base/docs/SETUP-GUIDE.md +566 -0
  19. package/templates/base/docs/app_spec.txt +13 -10
  20. package/templates/base/docs/architecture/README.md +162 -5
  21. package/templates/base/docs/architecture/api-catalog.md +575 -0
  22. package/templates/base/docs/architecture/c4-component.md +309 -0
  23. package/templates/base/docs/architecture/c4-container.md +183 -0
  24. package/templates/base/docs/architecture/c4-context.md +106 -0
  25. package/templates/base/docs/architecture/data-requirements.md +4 -3
  26. package/templates/base/docs/architecture/db-bootstrap.md +39 -0
  27. package/templates/base/docs/architecture/dependencies.md +327 -0
  28. package/templates/base/docs/architecture/drizzle-migration-plan.md +125 -0
  29. package/templates/base/docs/architecture/erd.md +1 -1
  30. package/templates/base/docs/architecture/sql-standards.md +100 -0
  31. package/templates/base/docs/architecture/tech-debt.md +184 -0
  32. package/templates/base/docs/testing.md +36 -29
  33. package/templates/base/package.json +26 -20
  34. package/templates/base/schema.sql +84 -0
  35. package/templates/base/scripts/capture-prod-session.ts +2 -2
  36. package/templates/base/scripts/init.sh +244 -59
  37. package/templates/base/scripts/sync-template.sh +104 -0
  38. package/templates/base/src/client/hooks/use-auth.ts +5 -0
  39. package/templates/base/src/client/routes/_authenticated/dashboard.tsx +1 -1
  40. package/templates/base/src/client/routes/index.tsx +1 -1
  41. package/templates/base/src/server/db/client.ts +3 -5
  42. package/templates/base/src/server/db/records.ts +81 -0
  43. package/templates/base/src/server/db/seed.ts +3 -2
  44. package/templates/base/src/server/db/sql.ts +116 -0
  45. package/templates/base/src/server/index.ts +17 -2
  46. package/templates/base/src/server/lib/audit.ts +74 -26
  47. package/templates/base/src/server/lib/audited-db.ts +219 -109
  48. package/templates/base/src/server/lib/transaction.ts +10 -16
  49. package/templates/base/src/server/middleware/account.ts +9 -16
  50. package/templates/base/src/server/middleware/auth.ts +102 -38
  51. package/templates/base/src/server/middleware/rate-limit.ts +8 -6
  52. package/templates/base/src/server/routes/accounts/handlers.ts +18 -6
  53. package/templates/base/src/server/routes/audits/handlers.ts +3 -1
  54. package/templates/base/src/server/routes/auth/handlers.ts +15 -10
  55. package/templates/base/src/server/routes/auth/test-login.ts +99 -45
  56. package/templates/base/src/server/routes/health/handlers.ts +4 -4
  57. package/templates/base/src/server/routes/index.ts +9 -0
  58. package/templates/base/src/server/routes/invitations/handlers.ts +9 -6
  59. package/templates/base/src/server/routes/openapi.ts +1 -1
  60. package/templates/base/src/server/routes/users/handlers.ts +21 -14
  61. package/templates/base/src/server/services/accounts.ts +242 -217
  62. package/templates/base/src/server/services/audits.ts +114 -61
  63. package/templates/base/src/server/services/auth.ts +310 -180
  64. package/templates/base/src/server/services/invitations.ts +282 -222
  65. package/templates/base/src/server/services/users.ts +383 -293
  66. package/templates/base/src/server/types/index.ts +1 -2
  67. package/templates/base/src/shared/types/api.ts +66 -198
  68. package/templates/base/tests/e2e/auth.setup.ts +1 -1
  69. package/templates/base/{src/server/__tests__/fixtures.ts → tests/fixtures/server.ts} +3 -3
  70. package/templates/base/{src/client/__tests__/setup-browser.ts → tests/helpers/client-setup-browser.ts} +2 -2
  71. package/templates/base/{src/client/__tests__/setup.ts → tests/helpers/client-setup.ts} +1 -1
  72. package/templates/base/{src/client/__tests__/test-utils.tsx → tests/helpers/client-test-utils.tsx} +2 -2
  73. package/templates/base/{src/server/__tests__/setup.ts → tests/helpers/server.ts} +9 -9
  74. package/templates/base/tests/integration/accounts/crud.test.ts +2 -11
  75. package/templates/base/tests/integration/audits/list.test.ts +2 -11
  76. package/templates/base/tests/integration/auth/auth-service.test.ts +1 -10
  77. package/templates/base/tests/integration/auth/invitation-token.test.ts +2 -11
  78. package/templates/base/tests/integration/auth/logout.test.ts +2 -11
  79. package/templates/base/tests/integration/auth/oauth.test.ts +23 -42
  80. package/templates/base/tests/integration/auth/refresh-token.test.ts +1 -9
  81. package/templates/base/tests/integration/auth/session-expiry.test.ts +1 -9
  82. package/templates/base/tests/integration/auth/session.test.ts +2 -11
  83. package/templates/base/tests/integration/auth/super-admin.test.ts +1 -9
  84. package/templates/base/tests/integration/authorization/analytics-role.test.ts +2 -11
  85. package/templates/base/tests/integration/authorization/billing-role.test.ts +2 -11
  86. package/templates/base/tests/integration/authorization/guards-roles.test.ts +1 -9
  87. package/templates/base/tests/integration/authorization/multi-tenancy.test.ts +2 -11
  88. package/templates/base/tests/integration/authorization/roles.test.ts +2 -11
  89. package/templates/base/tests/integration/config/production-behavior.test.ts +2 -11
  90. package/templates/base/tests/integration/health/health.test.ts +25 -44
  91. package/templates/base/tests/integration/invitations/crud.test.ts +2 -11
  92. package/templates/base/tests/integration/invitations/email.test.ts +1 -9
  93. package/templates/base/tests/integration/middleware/auth.test.ts +3 -12
  94. package/templates/base/tests/integration/middleware/request-logger.test.ts +1 -9
  95. package/templates/base/tests/integration/performance/response-times.test.ts +1 -9
  96. package/templates/base/tests/integration/security/cookie-security.test.ts +2 -11
  97. package/templates/base/tests/integration/security/csrf-protection.test.ts +2 -11
  98. package/templates/base/tests/integration/security/log-sanitization.test.ts +1 -9
  99. package/templates/base/tests/integration/security/rate-limiting.test.ts +1 -9
  100. package/templates/base/tests/integration/security/sql-injection.test.ts +7 -18
  101. package/templates/base/tests/integration/security/xss-prevention.test.ts +2 -11
  102. package/templates/base/tests/integration/setup.ts +13 -90
  103. package/templates/base/tests/integration/smoke.test.ts +3 -2
  104. package/templates/base/tests/integration/storage/upload.test.ts +2 -11
  105. package/templates/base/tests/integration/storage/validation.test.ts +2 -11
  106. package/templates/base/tests/integration/users/crud.test.ts +2 -11
  107. package/templates/base/tests/integration/users/list.test.ts +2 -11
  108. package/templates/base/tests/integration/vitest.config.ts +2 -9
  109. package/templates/base/{src/server/__tests__ → tests}/mocks/db.ts +1 -1
  110. package/templates/base/{src/server/__tests__ → tests}/mocks/index.ts +1 -1
  111. package/templates/base/{src/server/__tests__ → tests}/mocks/kv.ts +1 -1
  112. package/templates/base/{src/server/__tests__ → tests}/mocks/r2.ts +1 -1
  113. package/templates/base/{src/client/components/__tests__ → tests/unit/client/components}/sidebar.test.tsx +1 -1
  114. package/templates/base/{src/client/components/ui/__tests__ → tests/unit/client/components/ui}/avatar.test.tsx +1 -1
  115. package/templates/base/{src/client/__tests__ → tests/unit/client/components/ui}/button.test.tsx +1 -1
  116. package/templates/base/{src/client/components/ui/__tests__ → tests/unit/client/components/ui}/card.test.tsx +1 -1
  117. package/templates/base/{src/client/components/ui/__tests__ → tests/unit/client/components/ui}/dialog.test.tsx +1 -1
  118. package/templates/base/{src/client/components/ui/__tests__ → tests/unit/client/components/ui}/input.test.tsx +1 -1
  119. package/templates/base/{src/client/components/ui/__tests__ → tests/unit/client/components/ui}/loading-skeleton.test.tsx +1 -1
  120. package/templates/base/{src/client/components/ui/__tests__ → tests/unit/client/components/ui}/skeleton.test.tsx +1 -1
  121. package/templates/base/{src/client/components/ui/__tests__ → tests/unit/client/components/ui}/sonner.test.tsx +1 -1
  122. package/templates/base/{src/client/components/ui/__tests__ → tests/unit/client/components/ui}/tabs.test.tsx +1 -1
  123. package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/account.test.tsx +1 -1
  124. package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/integrations.test.tsx +1 -1
  125. package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/settings.test.tsx +1 -1
  126. package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/team.test.tsx +1 -1
  127. package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/authenticated-layout.test.tsx +1 -1
  128. package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/dashboard.test.tsx +1 -1
  129. package/templates/base/{src/client/routes/__tests__ → tests/unit/client/routes}/invite.test.tsx +1 -1
  130. package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/login.test.tsx +1 -1
  131. package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/navigation.test.tsx +1 -1
  132. package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/root-layout.test.tsx +1 -1
  133. package/templates/base/{src/server/auth/__tests__ → tests/unit/server/auth}/guards.test.ts +2 -2
  134. package/templates/base/{src → tests/unit}/server/auth/permissions.test.ts +1 -1
  135. package/templates/base/{src → tests/unit}/server/auth/roles.test.ts +1 -1
  136. package/templates/base/tests/unit/server/db/sql.test.ts +68 -0
  137. package/templates/base/{src → tests/unit}/server/env.test.ts +1 -1
  138. package/templates/base/tests/unit/server/lib/audited-db.test.ts +78 -0
  139. package/templates/base/{src → tests/unit}/server/lib/email.test.ts +1 -1
  140. package/templates/base/{src → tests/unit}/server/lib/errors.test.ts +1 -1
  141. package/templates/base/{src → tests/unit}/server/lib/oauth.test.ts +1 -1
  142. package/templates/base/{src → tests/unit}/server/lib/pagination.test.ts +1 -1
  143. package/templates/base/{src → tests/unit}/server/lib/password.test.ts +1 -1
  144. package/templates/base/{src → tests/unit}/server/lib/providers.test.ts +1 -1
  145. package/templates/base/{src → tests/unit}/server/lib/r2-storage.test.ts +2 -2
  146. package/templates/base/{src → tests/unit}/server/lib/session.test.ts +2 -2
  147. package/templates/base/{src → tests/unit}/server/lib/tokens.test.ts +1 -1
  148. package/templates/base/{src → tests/unit}/server/lib/transaction.test.ts +5 -14
  149. package/templates/base/{src → tests/unit}/server/middleware/account.test.ts +16 -24
  150. package/templates/base/tests/unit/server/middleware/auth.test.ts +647 -0
  151. package/templates/base/{src → tests/unit}/server/middleware/cors.test.ts +1 -1
  152. package/templates/base/{src → tests/unit}/server/middleware/error-handler.test.ts +2 -2
  153. package/templates/base/{src → tests/unit}/server/middleware/rate-limit.test.ts +3 -2
  154. package/templates/base/{src → tests/unit}/server/middleware/request-context.test.ts +1 -1
  155. package/templates/base/{src → tests/unit}/server/middleware/request-logger.test.ts +1 -1
  156. package/templates/base/{src/server/__tests__/mocks/__tests__ → tests/unit/server/mocks}/db.test.ts +1 -1
  157. package/templates/base/{src/server/__tests__/mocks/__tests__ → tests/unit/server/mocks}/kv.test.ts +1 -1
  158. package/templates/base/{src/server/__tests__/mocks/__tests__ → tests/unit/server/mocks}/r2.test.ts +1 -1
  159. package/templates/base/{src/server/routes/accounts/__tests__ → tests/unit/server/routes/accounts}/handlers.test.ts +12 -12
  160. package/templates/base/{src/server/routes/audits/__tests__ → tests/unit/server/routes/audits}/handlers.test.ts +11 -11
  161. package/templates/base/{src/server/routes/auth/__tests__ → tests/unit/server/routes/auth}/handlers.test.ts +124 -13
  162. package/templates/base/{src/server/routes/health/__tests__ → tests/unit/server/routes/health}/handlers.test.ts +27 -23
  163. package/templates/base/{src/server/routes/invitations/__tests__ → tests/unit/server/routes/invitations}/handlers.test.ts +14 -17
  164. package/templates/base/{src/server/routes/storage/__tests__ → tests/unit/server/routes/storage}/handlers.test.ts +6 -6
  165. package/templates/base/{src/server/routes/users/__tests__ → tests/unit/server/routes/users}/handlers.test.ts +81 -17
  166. package/templates/base/tests/unit/server/services/accounts.test.ts +406 -0
  167. package/templates/base/tests/unit/server/services/audits.test.ts +360 -0
  168. package/templates/base/tests/unit/server/services/auth.test.ts +656 -0
  169. package/templates/base/tests/unit/server/services/invitations.test.ts +343 -0
  170. package/templates/base/tests/unit/server/services/users.test.ts +706 -0
  171. package/templates/base/{src/shared/schemas/__tests__ → tests/unit/shared}/schemas.test.ts +1 -1
  172. package/templates/base/tsconfig.json +2 -1
  173. package/templates/base/vite.config.ts +3 -1
  174. package/templates/base/vitest.config.browser.ts +3 -2
  175. package/templates/base/vitest.config.frontend.ts +3 -2
  176. package/templates/base/vitest.config.ts +7 -14
  177. package/templates/base/.claude/settings.local.json +0 -11
  178. package/templates/base/.github/workflows/test.yml +0 -127
  179. package/templates/base/auth-setup-error.png +0 -0
  180. package/templates/base/config/drizzle.config.ts +0 -10
  181. package/templates/base/pnpm-lock.yaml +0 -8175
  182. package/templates/base/src/server/db/schema/accounts.ts +0 -20
  183. package/templates/base/src/server/db/schema/audit-logs.ts +0 -26
  184. package/templates/base/src/server/db/schema/index.ts +0 -7
  185. package/templates/base/src/server/db/schema/invitations.ts +0 -30
  186. package/templates/base/src/server/db/schema/refresh-tokens.ts +0 -22
  187. package/templates/base/src/server/db/schema/user-accounts.ts +0 -25
  188. package/templates/base/src/server/db/schema/users.ts +0 -33
  189. package/templates/base/src/server/lib/audited-db.test.ts +0 -107
  190. package/templates/base/src/server/lib/schema-helpers.ts +0 -16
  191. package/templates/base/src/server/middleware/auth.test.ts +0 -345
  192. package/templates/base/src/server/services/__tests__/accounts.test.ts +0 -764
  193. package/templates/base/src/server/services/__tests__/audits.test.ts +0 -235
  194. package/templates/base/src/server/services/__tests__/auth.test.ts +0 -765
  195. package/templates/base/src/server/services/__tests__/invitations.test.ts +0 -704
  196. package/templates/base/src/server/services/__tests__/users.test.ts +0 -755
  197. package/templates/base/tests/e2e/_auth/.gitkeep +0 -0
  198. package/templates/base/tests/integration/lib/schema-helpers.test.ts +0 -129
  199. package/templates/base/tsconfig.tsbuildinfo +0 -1
  200. /package/templates/base/{src/client/components/__tests__ → tests/unit/client/components}/__screenshots__/sidebar.test.tsx/Sidebar-can-be-collapsed-by-default-1.png +0 -0
  201. /package/templates/base/{src/client/components/__tests__ → tests/unit/client/components}/__screenshots__/sidebar.test.tsx/Sidebar-expands-when-collapsed-and-expand-button-is-clicked-1.png +0 -0
  202. /package/templates/base/{src/client/components/__tests__ → tests/unit/client/components}/__screenshots__/sidebar.test.tsx/Sidebar-handles-logout-button-click-1.png +0 -0
  203. /package/templates/base/{src/client/components/__tests__ → tests/unit/client/components}/__screenshots__/sidebar.test.tsx/Sidebar-handles-navigation-clicks-1.png +0 -0
  204. /package/templates/base/{src/client/components/__tests__ → tests/unit/client/components}/__screenshots__/sidebar.test.tsx/Sidebar-hides-navigation-labels-when-collapsed-1.png +0 -0
  205. /package/templates/base/{src/client/components/__tests__ → tests/unit/client/components}/__screenshots__/sidebar.test.tsx/Sidebar-highlights-active-route-1.png +0 -0
  206. /package/templates/base/{src/client/components/__tests__ → tests/unit/client/components}/__screenshots__/sidebar.test.tsx/Sidebar-renders-sidebar-navigation-items-1.png +0 -0
  207. /package/templates/base/{src/client/components/__tests__ → tests/unit/client/components}/__screenshots__/sidebar.test.tsx/Sidebar-shows-keyboard-shortcut-hint-when-expanded-1.png +0 -0
  208. /package/templates/base/{src/client/components/__tests__ → tests/unit/client/components}/__screenshots__/sidebar.test.tsx/Sidebar-shows-user-info-when-authenticated-1.png +0 -0
  209. /package/templates/base/{src/client/components/__tests__ → tests/unit/client/components}/__screenshots__/sidebar.test.tsx/Sidebar-shows-user-initials-in-avatar-fallback-1.png +0 -0
  210. /package/templates/base/{src/client/components/__tests__ → tests/unit/client/components}/error-boundary.test.tsx +0 -0
  211. /package/templates/base/{src/client/components/ui/__tests__ → tests/unit/client/components/ui}/error-fallback.test.tsx +0 -0
  212. /package/templates/base/{src/client/hooks/__tests__ → tests/unit/client/hooks}/use-auth.test.tsx +0 -0
  213. /package/templates/base/{src/client/hooks/__tests__ → tests/unit/client/hooks}/use-theme.test.tsx +0 -0
  214. /package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/__screenshots__/dashboard.test.tsx/Dashboard-Page-when-authenticated-should-display-dashboard-stats-cards-1.png +0 -0
  215. /package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/__screenshots__/dashboard.test.tsx/Dashboard-Page-when-authenticated-should-display-quick-action-cards-1.png +0 -0
  216. /package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/__screenshots__/dashboard.test.tsx/Dashboard-Page-when-authenticated-should-display-recent-activity-section-1.png +0 -0
  217. /package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/__screenshots__/dashboard.test.tsx/Dashboard-Page-when-authenticated-should-display-user-first-name-in-welcome-message-1.png +0 -0
  218. /package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/__screenshots__/dashboard.test.tsx/Dashboard-Page-when-authenticated-should-display-user-information-in-sidebar-1.png +0 -0
  219. /package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/__screenshots__/dashboard.test.tsx/Dashboard-Page-when-authenticated-should-render-dashboard-when-authenticated-1.png +0 -0
  220. /package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/__screenshots__/dashboard.test.tsx/Dashboard-Page-when-authenticated-should-show-navigation-sidebar-1.png +0 -0
  221. /package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/__screenshots__/dashboard.test.tsx/Dashboard-Page-when-unauthenticated-should-redirect-to-login-when-not-authenticated-1.png +0 -0
  222. /package/templates/base/{src/client/routes/__tests__ → tests/unit/client/routes}/__screenshots__/invite.test.tsx/Invite-Token-Page-pending-invitation-state-should-display-accept-invitation-button-1.png +0 -0
  223. /package/templates/base/{src/client/routes/__tests__ → tests/unit/client/routes}/__screenshots__/invite.test.tsx/Invite-Token-Page-pending-invitation-state-should-display-decline-button-linking-to-homepage-1.png +0 -0
  224. /package/templates/base/{src/client/routes/__tests__ → tests/unit/client/routes}/__screenshots__/invite.test.tsx/Invite-Token-Page-pending-invitation-state-should-display-invitation-details--email--workspace--role--1.png +0 -0
  225. /package/templates/base/{src/client/routes/__tests__ → tests/unit/client/routes}/__screenshots__/invite.test.tsx/Invite-Token-Page-pending-invitation-state-should-have-a-logo-link-to-homepage-1.png +0 -0
  226. /package/templates/base/{src/client/routes/__tests__ → tests/unit/client/routes}/__screenshots__/invite.test.tsx/Invite-Token-Page-pending-invitation-state-should-render-invitation-page-with-inviter-name-and-workspace-1.png +0 -0
  227. /package/templates/base/{src/client/routes/__tests__ → tests/unit/client/routes}/__screenshots__/invite.test.tsx/Invite-Token-Page-pending-invitation-state-should-show-terms-of-service-and-privacy-policy-links-1.png +0 -0
  228. /package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/__screenshots__/login.test.tsx/Login-Page-should-display-Google-OAuth-login-button-1.png +0 -0
  229. /package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/__screenshots__/login.test.tsx/Login-Page-should-have-a-link-back-to-home-page-1.png +0 -0
  230. /package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/__screenshots__/login.test.tsx/Login-Page-should-render-login-content-without-waiting-for-authentication-1.png +0 -0
  231. /package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/__screenshots__/login.test.tsx/Login-Page-should-render-login-page-at--login-route-1.png +0 -0
  232. /package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/__screenshots__/login.test.tsx/Login-Page-should-show-Terms-of-Service-and-Privacy-Policy-links-1.png +0 -0
  233. /package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/__screenshots__/login.test.tsx/Login-Page-should-trigger-OAuth-flow-when-clicking-login-button-1.png +0 -0
  234. /package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/__screenshots__/navigation.test.tsx/Navigation-404-handling-should-display-404-text-on-not-found-page-1.png +0 -0
  235. /package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/__screenshots__/navigation.test.tsx/Navigation-404-handling-should-have-navigation-options-on-404-page-1.png +0 -0
  236. /package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/__screenshots__/navigation.test.tsx/Navigation-404-handling-should-render-404-page-for-unknown-routes-1.png +0 -0
  237. /package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/__screenshots__/navigation.test.tsx/Navigation-authenticated-navigation-should-navigate-from-dashboard-to-account-page-1.png +0 -0
  238. /package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/__screenshots__/navigation.test.tsx/Navigation-authenticated-navigation-should-navigate-from-dashboard-to-integrations-page-1.png +0 -0
  239. /package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/__screenshots__/navigation.test.tsx/Navigation-authenticated-navigation-should-navigate-from-dashboard-to-settings-page-1.png +0 -0
  240. /package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/__screenshots__/navigation.test.tsx/Navigation-authenticated-navigation-should-navigate-from-dashboard-to-team-page-1.png +0 -0
  241. /package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/__screenshots__/navigation.test.tsx/Navigation-home-page-navigation-should-display-navigation-links-on-home-page-1.png +0 -0
  242. /package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/__screenshots__/navigation.test.tsx/Navigation-home-page-navigation-should-have-correct-link-destinations-on-home-page-1.png +0 -0
  243. /package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/__screenshots__/navigation.test.tsx/Navigation-unauthenticated-navigation-should-allow-access-to-home-page-without-authentication-1.png +0 -0
  244. /package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/__screenshots__/navigation.test.tsx/Navigation-unauthenticated-navigation-should-allow-access-to-login-page-without-authentication-1.png +0 -0
  245. /package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/__screenshots__/navigation.test.tsx/Navigation-unauthenticated-navigation-should-redirect-unauthenticated-users-from-dashboard-to-login-1.png +0 -0
  246. /package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/__screenshots__/navigation.test.tsx/Navigation-unauthenticated-navigation-should-redirect-unauthenticated-users-from-settings-to-login-1.png +0 -0
  247. /package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/__screenshots__/navigation.test.tsx/Navigation-unauthenticated-navigation-should-redirect-unauthenticated-users-from-team-to-login-1.png +0 -0
  248. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/account.test.tsx/Account-Page-should-render-Active-Sessions-section-with-session-cards-1.png +0 -0
  249. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/account.test.tsx/Account-Page-should-render-page-with-correct-title--Account--1.png +0 -0
  250. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/account.test.tsx/Account-Page-should-show-API-Access-section-1.png +0 -0
  251. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/account.test.tsx/Account-Page-should-show-Connected-Accounts-section-with-Google-connected-1.png +0 -0
  252. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/account.test.tsx/Account-Page-should-show-Danger-Zone-section-with-delete-button-1.png +0 -0
  253. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/account.test.tsx/Account-Page-should-show-Security-section-with-Two-Factor-Authentication-1.png +0 -0
  254. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/integrations.test.tsx/Integrations-Page-API-documentation-section-should-display-API-documentation-link-section-1.png +0 -0
  255. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/integrations.test.tsx/Integrations-Page-Create-Webhook-Dialog-should-have-Add-Webhook-trigger-button-1.png +0 -0
  256. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/integrations.test.tsx/Integrations-Page-category-filters-should-display-all-category-buttons-1.png +0 -0
  257. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/integrations.test.tsx/Integrations-Page-integration-cards-should-display-all-integration-cards-with-names-1.png +0 -0
  258. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/integrations.test.tsx/Integrations-Page-integration-cards-should-display-category-badges-on-cards-1.png +0 -0
  259. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/integrations.test.tsx/Integrations-Page-integration-cards-should-show-Configure-button-for-connected-integrations-1.png +0 -0
  260. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/integrations.test.tsx/Integrations-Page-integration-cards-should-show-Connect-button-for-not-connected-integrations-1.png +0 -0
  261. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/integrations.test.tsx/Integrations-Page-integration-cards-should-show-Connected-badge-for-connected-integrations-1.png +0 -0
  262. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/integrations.test.tsx/Integrations-Page-page-rendering-should-display-Available-Integrations-section-1.png +0 -0
  263. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/integrations.test.tsx/Integrations-Page-page-rendering-should-display-Webhooks-section-1.png +0 -0
  264. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/integrations.test.tsx/Integrations-Page-page-rendering-should-display-connected-count-1.png +0 -0
  265. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/integrations.test.tsx/Integrations-Page-page-rendering-should-display-page-description-1.png +0 -0
  266. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/integrations.test.tsx/Integrations-Page-page-rendering-should-display-search-input-1.png +0 -0
  267. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/integrations.test.tsx/Integrations-Page-page-rendering-should-render-with-correct-title--Integrations--1.png +0 -0
  268. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/integrations.test.tsx/Integrations-Page-search-functionality-should-filter-integrations-based-on-search-query-1.png +0 -0
  269. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/integrations.test.tsx/Integrations-Page-search-functionality-should-search-by-description-1.png +0 -0
  270. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/integrations.test.tsx/Integrations-Page-search-functionality-should-show-no-results-message-when-search-has-no-matches-1.png +0 -0
  271. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/integrations.test.tsx/Integrations-Page-webhooks-section-should-display-existing-webhook-1.png +0 -0
  272. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/integrations.test.tsx/Integrations-Page-webhooks-section-should-display-webhook-events-1.png +0 -0
  273. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/integrations.test.tsx/Integrations-Page-webhooks-section-should-display-webhook-last-delivery-info-1.png +0 -0
  274. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/integrations.test.tsx/Integrations-Page-webhooks-section-should-display-webhook-success-status-1.png +0 -0
  275. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/integrations.test.tsx/Integrations-Page-webhooks-section-should-have-Add-Webhook-button-1.png +0 -0
  276. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/settings.test.tsx/Settings-Page-Account-tab-should-display-connected-accounts-section-with-Google-provider-1.png +0 -0
  277. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/settings.test.tsx/Settings-Page-Account-tab-should-display-sessions-and-danger-zone-sections-1.png +0 -0
  278. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/settings.test.tsx/Settings-Page-Notifications-tab-should-display-all-notification-toggle-options-1.png +0 -0
  279. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/settings.test.tsx/Settings-Page-Notifications-tab-should-display-email-notifications-section-1.png +0 -0
  280. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/settings.test.tsx/Settings-Page-Notifications-tab-should-display-toggle-switches-for-notification-options-1.png +0 -0
  281. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/settings.test.tsx/Settings-Page-Notifications-tab-should-have-toggles-checked-by-default-1.png +0 -0
  282. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/settings.test.tsx/Settings-Page-Profile-tab-should-display--Save-Changes--button-1.png +0 -0
  283. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/settings.test.tsx/Settings-Page-Profile-tab-should-display-personal-information-form-1.png +0 -0
  284. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/settings.test.tsx/Settings-Page-Profile-tab-should-display-profile-picture-section-1.png +0 -0
  285. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/settings.test.tsx/Settings-Page-Profile-tab-should-display-user-email-in-disabled-email-input-1.png +0 -0
  286. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/settings.test.tsx/Settings-Page-Profile-tab-should-display-user-initials-in-avatar-1.png +0 -0
  287. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/settings.test.tsx/Settings-Page-Profile-tab-should-display-user-name-in-the-name-input-1.png +0 -0
  288. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/settings.test.tsx/Settings-Page-page-rendering-should-display-all-three-tabs-1.png +0 -0
  289. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/settings.test.tsx/Settings-Page-page-rendering-should-display-page-description-1.png +0 -0
  290. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/settings.test.tsx/Settings-Page-page-rendering-should-render-with-correct-title--Settings--1.png +0 -0
  291. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/settings.test.tsx/Settings-Page-tab-navigation-should-show-Profile-tab-as-default-active-tab-1.png +0 -0
  292. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/settings.test.tsx/Settings-Page-tab-navigation-should-switch-to-Account-tab-when-clicked-1.png +0 -0
  293. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/settings.test.tsx/Settings-Page-tab-navigation-should-switch-to-Notifications-tab-when-clicked-1.png +0 -0
  294. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/team.test.tsx/Team-Page-should-display-Active-Members-section-with-member-count-1.png +0 -0
  295. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/team.test.tsx/Team-Page-should-display-Pending-Invitations-section-with-invitation-details-1.png +0 -0
  296. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/team.test.tsx/Team-Page-should-have-invite-member-button-that-can-be-clicked-1.png +0 -0
  297. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/team.test.tsx/Team-Page-should-render-page-with-correct-title-and-description-1.png +0 -0
  298. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/team.test.tsx/Team-Page-should-render-search-input-that-filters-team-members-1.png +0 -0
  299. /package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/__screenshots__/team.test.tsx/Team-Page-should-show-current-user-with---you---indicator-and-role-badge-1.png +0 -0
  300. /package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/error-components.test.tsx +0 -0
@@ -0,0 +1,656 @@
1
+ // src/server/services/__tests__/auth.test.ts
2
+ import { describe, it, expect, vi, beforeEach, type Mock } from 'vitest'
3
+ import { authService } from '@server/services/auth'
4
+ import type { GoogleUserInfo } from '@server/types/auth'
5
+ import { UnauthorizedError } from '@server/lib/errors'
6
+
7
+ vi.mock('@server/lib/tokens', () => ({
8
+ createAccessToken: vi.fn().mockResolvedValue('mock-access-token'),
9
+ generateRefreshToken: vi.fn().mockReturnValue('mock-refresh-token'),
10
+ hashToken: vi.fn().mockResolvedValue('hashed-token'),
11
+ getRefreshTokenExpiry: vi.fn().mockReturnValue(new Date('2025-01-07T00:00:00Z')),
12
+ }))
13
+
14
+ vi.mock('@server/lib/audit', () => ({
15
+ logAuthEvent: vi.fn().mockResolvedValue(),
16
+ }))
17
+
18
+ vi.mock('@server/db/sql', () => ({
19
+ queryOne: vi.fn(),
20
+ execute: vi.fn(),
21
+ toStringValue: (value: unknown) => {
22
+ if (typeof value === 'string') return value
23
+ if (typeof value === 'number' || typeof value === 'bigint') return String(value)
24
+ return ''
25
+ },
26
+ toNullableString: (value: unknown) => {
27
+ if (value === null || value === undefined) return null
28
+ if (typeof value === 'string') return value
29
+ if (typeof value === 'number' || typeof value === 'bigint') return String(value)
30
+ return null
31
+ },
32
+ }))
33
+
34
+ import { createAccessToken, generateRefreshToken, hashToken } from '@server/lib/tokens'
35
+ import { logAuthEvent } from '@server/lib/audit'
36
+ import { queryOne, execute } from '@server/db/sql'
37
+
38
+ const db = {} as D1Database
39
+
40
+ function createMockEnv() {
41
+ return {
42
+ APP_URL: 'http://localhost:8787',
43
+ JWT_SECRET: 'secret',
44
+ JWT_EXPIRY_MINUTES: '15',
45
+ GOOGLE_CLIENT_ID: 'id',
46
+ GOOGLE_CLIENT_SECRET: 'secret',
47
+ GOOGLE_REDIRECT_URI: 'http://localhost:8787',
48
+ REFRESH_TOKEN_EXPIRY_DAYS: '30',
49
+ SENDGRID_API_KEY: 'key',
50
+ SENDGRID_FROM_EMAIL: 'test@example.com',
51
+ ENVIRONMENT: 'test',
52
+ } as any
53
+ }
54
+
55
+ const mockAuthContext = {
56
+ transactionId: 'test-transaction-id',
57
+ ip: '127.0.0.1',
58
+ userAgent: 'IntegrationTest/1.0',
59
+ }
60
+
61
+ describe('authService', () => {
62
+ beforeEach(() => {
63
+ vi.clearAllMocks()
64
+ })
65
+
66
+ describe('findOrCreateUser', () => {
67
+ it('should create a new user on first OAuth login', async () => {
68
+ const env = createMockEnv()
69
+ const googleUser: GoogleUserInfo = {
70
+ sub: 'new_google_sub_123',
71
+ email: 'newuser@gmail.com',
72
+ email_verified: true,
73
+ name: 'New Google User',
74
+ picture: 'https://example.com/avatar.jpg',
75
+ given_name: 'New',
76
+ family_name: 'User',
77
+ }
78
+
79
+ const userRow = {
80
+ id: 'user-123',
81
+ googleId: googleUser.sub,
82
+ email: googleUser.email,
83
+ name: googleUser.name,
84
+ avatarUrl: googleUser.picture,
85
+ status: 'active',
86
+ providerIds: JSON.stringify(['google']),
87
+ isSuperAdmin: 0,
88
+ createdAt: new Date().toISOString(),
89
+ updatedAt: new Date().toISOString(),
90
+ deletedAt: null,
91
+ }
92
+
93
+ ;(queryOne as Mock)
94
+ .mockResolvedValueOnce(null)
95
+ .mockResolvedValueOnce(userRow)
96
+
97
+ const result = await authService.findOrCreateUser(db, env, googleUser, mockAuthContext)
98
+
99
+ expect(result.isNewUser).toBe(true)
100
+ expect(result.user.email).toBe(googleUser.email)
101
+ expect(createAccessToken).toHaveBeenCalled()
102
+ expect(generateRefreshToken).toHaveBeenCalled()
103
+ expect(hashToken).toHaveBeenCalled()
104
+ expect(execute).toHaveBeenCalled()
105
+ expect(logAuthEvent).toHaveBeenCalled()
106
+ })
107
+
108
+ it('should return existing user on subsequent OAuth login', async () => {
109
+ const env = createMockEnv()
110
+ const googleUser: GoogleUserInfo = {
111
+ sub: 'existing_sub_456',
112
+ email: 'existing@gmail.com',
113
+ email_verified: true,
114
+ name: 'Existing User',
115
+ picture: undefined,
116
+ given_name: 'Existing',
117
+ family_name: 'User',
118
+ }
119
+
120
+ const userRow = {
121
+ id: 'user-456',
122
+ googleId: googleUser.sub,
123
+ email: googleUser.email,
124
+ name: googleUser.name,
125
+ avatarUrl: null,
126
+ status: 'active',
127
+ providerIds: JSON.stringify(['google']),
128
+ isSuperAdmin: 0,
129
+ createdAt: new Date().toISOString(),
130
+ updatedAt: new Date().toISOString(),
131
+ deletedAt: null,
132
+ }
133
+
134
+ ;(queryOne as Mock).mockResolvedValueOnce(userRow)
135
+
136
+ const result = await authService.findOrCreateUser(db, env, googleUser, mockAuthContext)
137
+
138
+ expect(result.isNewUser).toBe(false)
139
+ expect(result.user.id).toBe('user-456')
140
+ expect(execute).toHaveBeenCalled()
141
+ })
142
+ })
143
+
144
+ describe('refreshAccessToken', () => {
145
+ it('should return a new access token when refresh token is valid', async () => {
146
+ const env = createMockEnv()
147
+
148
+ ;(queryOne as Mock)
149
+ .mockResolvedValueOnce({ id: 'token-1', userId: 'user-1', tokenHash: 'hashed-token' })
150
+ .mockResolvedValueOnce({
151
+ id: 'user-1',
152
+ googleId: 'google-1',
153
+ email: 'user@example.com',
154
+ name: 'User',
155
+ avatarUrl: null,
156
+ status: 'active',
157
+ providerIds: JSON.stringify(['google']),
158
+ isSuperAdmin: 0,
159
+ createdAt: new Date().toISOString(),
160
+ updatedAt: new Date().toISOString(),
161
+ deletedAt: null,
162
+ })
163
+
164
+ const result = await authService.refreshAccessToken(db, env, 'refresh-token', mockAuthContext)
165
+
166
+ expect(result.accessToken).toBe('mock-access-token')
167
+ expect(logAuthEvent).toHaveBeenCalled()
168
+ })
169
+
170
+ it('should throw for invalid refresh token', async () => {
171
+ const env = createMockEnv()
172
+ ;(queryOne as Mock).mockResolvedValueOnce(null)
173
+
174
+ await expect(
175
+ authService.refreshAccessToken(db, env, 'invalid-token', mockAuthContext)
176
+ ).rejects.toThrow(UnauthorizedError)
177
+ })
178
+ })
179
+
180
+ describe('revokeRefreshToken', () => {
181
+ it('should revoke refresh token and log logout', async () => {
182
+ await authService.revokeRefreshToken(db, 'refresh-token', mockAuthContext, 'user-1')
183
+
184
+ expect(execute).toHaveBeenCalled()
185
+ expect(logAuthEvent).toHaveBeenCalled()
186
+ })
187
+
188
+ it('should revoke token without logging when userId is null', async () => {
189
+ await authService.revokeRefreshToken(db, 'refresh-token', mockAuthContext, null)
190
+
191
+ expect(execute).toHaveBeenCalled()
192
+ expect(logAuthEvent).not.toHaveBeenCalled()
193
+ })
194
+ })
195
+
196
+ describe('revokeAllUserTokens', () => {
197
+ it('should revoke all tokens for a user', async () => {
198
+ await authService.revokeAllUserTokens(db, 'user-1')
199
+
200
+ expect(execute).toHaveBeenCalled()
201
+ })
202
+ })
203
+
204
+ describe('getCurrentUser', () => {
205
+ it('should return user when found', async () => {
206
+ ;(queryOne as Mock).mockResolvedValueOnce({
207
+ id: 'user-1',
208
+ googleId: 'google-1',
209
+ email: 'user@example.com',
210
+ name: 'User',
211
+ avatarUrl: null,
212
+ status: 'active',
213
+ providerIds: JSON.stringify(['google']),
214
+ isSuperAdmin: 0,
215
+ createdAt: new Date().toISOString(),
216
+ updatedAt: new Date().toISOString(),
217
+ deletedAt: null,
218
+ })
219
+
220
+ const result = await authService.getCurrentUser(db, 'user-1')
221
+
222
+ expect(result.email).toBe('user@example.com')
223
+ })
224
+
225
+ it('should throw UnauthorizedError when user not found', async () => {
226
+ ;(queryOne as Mock).mockResolvedValueOnce(null)
227
+
228
+ await expect(authService.getCurrentUser(db, 'user-999')).rejects.toThrow(UnauthorizedError)
229
+ })
230
+ })
231
+
232
+ describe('findOrCreateUser edge cases', () => {
233
+ it('should not update user when info is unchanged', async () => {
234
+ const env = createMockEnv()
235
+ const googleUser: GoogleUserInfo = {
236
+ sub: 'existing_sub',
237
+ email: 'same@example.com',
238
+ email_verified: true,
239
+ name: 'Same Name',
240
+ picture: 'https://same-picture.jpg',
241
+ given_name: 'Same',
242
+ family_name: 'Name',
243
+ }
244
+
245
+ const userRow = {
246
+ id: 'user-1',
247
+ googleId: googleUser.sub,
248
+ email: googleUser.email,
249
+ name: googleUser.name,
250
+ avatarUrl: googleUser.picture,
251
+ status: 'active',
252
+ providerIds: JSON.stringify(['google']),
253
+ isSuperAdmin: 0,
254
+ createdAt: new Date().toISOString(),
255
+ updatedAt: new Date().toISOString(),
256
+ deletedAt: null,
257
+ }
258
+
259
+ ;(queryOne as Mock).mockResolvedValueOnce(userRow)
260
+
261
+ const result = await authService.findOrCreateUser(db, env, googleUser, mockAuthContext)
262
+
263
+ expect(result.isNewUser).toBe(false)
264
+ // Should only call execute for refresh token insert and login log
265
+ expect(execute).toHaveBeenCalled()
266
+ })
267
+
268
+ it('should update user when email changes', async () => {
269
+ const env = createMockEnv()
270
+ const googleUser: GoogleUserInfo = {
271
+ sub: 'existing_sub',
272
+ email: 'new-email@example.com',
273
+ email_verified: true,
274
+ name: 'Same Name',
275
+ picture: 'https://same-picture.jpg',
276
+ given_name: 'Same',
277
+ family_name: 'Name',
278
+ }
279
+
280
+ const userRow = {
281
+ id: 'user-1',
282
+ googleId: googleUser.sub,
283
+ email: 'old@example.com',
284
+ name: googleUser.name,
285
+ avatarUrl: googleUser.picture,
286
+ status: 'active',
287
+ providerIds: JSON.stringify(['google']),
288
+ isSuperAdmin: 0,
289
+ createdAt: new Date().toISOString(),
290
+ updatedAt: new Date().toISOString(),
291
+ deletedAt: null,
292
+ }
293
+
294
+ ;(queryOne as Mock)
295
+ .mockResolvedValueOnce(userRow)
296
+ .mockResolvedValueOnce({ ...userRow, email: googleUser.email })
297
+
298
+ const result = await authService.findOrCreateUser(db, env, googleUser, mockAuthContext)
299
+
300
+ expect(result.isNewUser).toBe(false)
301
+ expect(execute).toHaveBeenCalled()
302
+ })
303
+
304
+ it('should handle null picture', async () => {
305
+ const env = createMockEnv()
306
+ const googleUser: GoogleUserInfo = {
307
+ sub: 'new_google_sub',
308
+ email: 'user@example.com',
309
+ email_verified: true,
310
+ name: 'User',
311
+ picture: undefined,
312
+ given_name: 'User',
313
+ family_name: '',
314
+ }
315
+
316
+ const userRow = {
317
+ id: 'user-new',
318
+ googleId: googleUser.sub,
319
+ email: googleUser.email,
320
+ name: googleUser.name,
321
+ avatarUrl: null,
322
+ status: 'active',
323
+ providerIds: JSON.stringify(['google']),
324
+ isSuperAdmin: 0,
325
+ createdAt: new Date().toISOString(),
326
+ updatedAt: new Date().toISOString(),
327
+ deletedAt: null,
328
+ }
329
+
330
+ ;(queryOne as Mock)
331
+ .mockResolvedValueOnce(null)
332
+ .mockResolvedValueOnce(userRow)
333
+
334
+ const result = await authService.findOrCreateUser(db, env, googleUser, mockAuthContext)
335
+
336
+ expect(result.isNewUser).toBe(true)
337
+ })
338
+ })
339
+
340
+ describe('refreshAccessToken edge cases', () => {
341
+ it('should throw when user is inactive', async () => {
342
+ const env = createMockEnv()
343
+
344
+ ;(queryOne as Mock)
345
+ .mockResolvedValueOnce({ id: 'token-1', userId: 'user-1', tokenHash: 'hashed-token' })
346
+ .mockResolvedValueOnce({
347
+ id: 'user-1',
348
+ googleId: 'google-1',
349
+ email: 'user@example.com',
350
+ name: 'User',
351
+ avatarUrl: null,
352
+ status: 'inactive',
353
+ providerIds: JSON.stringify(['google']),
354
+ isSuperAdmin: 0,
355
+ createdAt: new Date().toISOString(),
356
+ updatedAt: new Date().toISOString(),
357
+ deletedAt: null,
358
+ })
359
+
360
+ await expect(
361
+ authService.refreshAccessToken(db, env, 'refresh-token', mockAuthContext)
362
+ ).rejects.toThrow('User not found or inactive')
363
+ })
364
+
365
+ it('should throw when user not found after token found', async () => {
366
+ const env = createMockEnv()
367
+
368
+ ;(queryOne as Mock)
369
+ .mockResolvedValueOnce({ id: 'token-1', userId: 'user-1', tokenHash: 'hashed-token' })
370
+ .mockResolvedValueOnce(null)
371
+
372
+ await expect(
373
+ authService.refreshAccessToken(db, env, 'refresh-token', mockAuthContext)
374
+ ).rejects.toThrow('User not found or inactive')
375
+ })
376
+ })
377
+
378
+ describe('mapUserRow edge cases', () => {
379
+ it('should handle isSuperAdmin as string "1"', async () => {
380
+ ;(queryOne as Mock).mockResolvedValueOnce({
381
+ id: 'user-1',
382
+ googleId: 'google-1',
383
+ email: 'user@example.com',
384
+ name: 'User',
385
+ avatarUrl: null,
386
+ status: 'active',
387
+ providerIds: JSON.stringify(['google']),
388
+ isSuperAdmin: '1',
389
+ createdAt: new Date().toISOString(),
390
+ updatedAt: new Date().toISOString(),
391
+ deletedAt: null,
392
+ })
393
+
394
+ const result = await authService.getCurrentUser(db, 'user-1')
395
+
396
+ expect(result.isSuperAdmin).toBe(true)
397
+ })
398
+
399
+ it('should handle isSuperAdmin as string "true"', async () => {
400
+ ;(queryOne as Mock).mockResolvedValueOnce({
401
+ id: 'user-1',
402
+ googleId: 'google-1',
403
+ email: 'user@example.com',
404
+ name: 'User',
405
+ avatarUrl: null,
406
+ status: 'active',
407
+ providerIds: JSON.stringify(['google']),
408
+ isSuperAdmin: 'true',
409
+ createdAt: new Date().toISOString(),
410
+ updatedAt: new Date().toISOString(),
411
+ deletedAt: null,
412
+ })
413
+
414
+ const result = await authService.getCurrentUser(db, 'user-1')
415
+
416
+ expect(result.isSuperAdmin).toBe(true)
417
+ })
418
+
419
+ it('should handle isSuperAdmin as boolean true', async () => {
420
+ ;(queryOne as Mock).mockResolvedValueOnce({
421
+ id: 'user-1',
422
+ googleId: 'google-1',
423
+ email: 'user@example.com',
424
+ name: 'User',
425
+ avatarUrl: null,
426
+ status: 'active',
427
+ providerIds: JSON.stringify(['google']),
428
+ isSuperAdmin: true,
429
+ createdAt: new Date().toISOString(),
430
+ updatedAt: new Date().toISOString(),
431
+ deletedAt: null,
432
+ })
433
+
434
+ const result = await authService.getCurrentUser(db, 'user-1')
435
+
436
+ expect(result.isSuperAdmin).toBe(true)
437
+ })
438
+
439
+ it('should handle providerIds as array', async () => {
440
+ ;(queryOne as Mock).mockResolvedValueOnce({
441
+ id: 'user-1',
442
+ googleId: 'google-1',
443
+ email: 'user@example.com',
444
+ name: 'User',
445
+ avatarUrl: null,
446
+ status: 'active',
447
+ providerIds: ['google', 'github'],
448
+ isSuperAdmin: 0,
449
+ createdAt: new Date().toISOString(),
450
+ updatedAt: new Date().toISOString(),
451
+ deletedAt: null,
452
+ })
453
+
454
+ const result = await authService.getCurrentUser(db, 'user-1')
455
+
456
+ expect(result.providerIds).toEqual(['google', 'github'])
457
+ })
458
+
459
+ it('should handle invalid JSON in providerIds', async () => {
460
+ ;(queryOne as Mock).mockResolvedValueOnce({
461
+ id: 'user-1',
462
+ googleId: 'google-1',
463
+ email: 'user@example.com',
464
+ name: 'User',
465
+ avatarUrl: null,
466
+ status: 'active',
467
+ providerIds: 'not-valid-json',
468
+ isSuperAdmin: 0,
469
+ createdAt: new Date().toISOString(),
470
+ updatedAt: new Date().toISOString(),
471
+ deletedAt: null,
472
+ })
473
+
474
+ const result = await authService.getCurrentUser(db, 'user-1')
475
+
476
+ expect(result.providerIds).toEqual([])
477
+ })
478
+
479
+ it('should handle non-array JSON in providerIds', async () => {
480
+ ;(queryOne as Mock).mockResolvedValueOnce({
481
+ id: 'user-1',
482
+ googleId: 'google-1',
483
+ email: 'user@example.com',
484
+ name: 'User',
485
+ avatarUrl: null,
486
+ status: 'active',
487
+ providerIds: JSON.stringify({ type: 'object' }),
488
+ isSuperAdmin: 0,
489
+ createdAt: new Date().toISOString(),
490
+ updatedAt: new Date().toISOString(),
491
+ deletedAt: null,
492
+ })
493
+
494
+ const result = await authService.getCurrentUser(db, 'user-1')
495
+
496
+ expect(result.providerIds).toEqual([])
497
+ })
498
+
499
+ it('should handle null providerIds', async () => {
500
+ ;(queryOne as Mock).mockResolvedValueOnce({
501
+ id: 'user-1',
502
+ googleId: 'google-1',
503
+ email: 'user@example.com',
504
+ name: 'User',
505
+ avatarUrl: null,
506
+ status: 'active',
507
+ providerIds: null,
508
+ isSuperAdmin: 0,
509
+ createdAt: new Date().toISOString(),
510
+ updatedAt: new Date().toISOString(),
511
+ deletedAt: null,
512
+ })
513
+
514
+ const result = await authService.getCurrentUser(db, 'user-1')
515
+
516
+ expect(result.providerIds).toEqual([])
517
+ })
518
+
519
+ it('should handle avatarUrl present', async () => {
520
+ ;(queryOne as Mock).mockResolvedValueOnce({
521
+ id: 'user-1',
522
+ googleId: 'google-1',
523
+ email: 'user@example.com',
524
+ name: 'User',
525
+ avatarUrl: 'https://example.com/avatar.jpg',
526
+ status: 'active',
527
+ providerIds: JSON.stringify(['google']),
528
+ isSuperAdmin: 0,
529
+ createdAt: new Date().toISOString(),
530
+ updatedAt: new Date().toISOString(),
531
+ deletedAt: null,
532
+ })
533
+
534
+ const result = await authService.getCurrentUser(db, 'user-1')
535
+
536
+ expect(result).toBeDefined()
537
+ })
538
+
539
+ it('should handle deletedAt present', async () => {
540
+ ;(queryOne as Mock).mockResolvedValueOnce({
541
+ id: 'user-1',
542
+ googleId: 'google-1',
543
+ email: 'user@example.com',
544
+ name: 'User',
545
+ avatarUrl: null,
546
+ status: 'active',
547
+ providerIds: JSON.stringify(['google']),
548
+ isSuperAdmin: 0,
549
+ createdAt: new Date().toISOString(),
550
+ updatedAt: new Date().toISOString(),
551
+ deletedAt: new Date().toISOString(),
552
+ })
553
+
554
+ const result = await authService.getCurrentUser(db, 'user-1')
555
+
556
+ expect(result.deletedAt).not.toBeNull()
557
+ })
558
+ })
559
+
560
+ describe('findOrCreateUser failure cases', () => {
561
+ it('should throw when user creation fails', async () => {
562
+ const env = createMockEnv()
563
+ const googleUser: GoogleUserInfo = {
564
+ sub: 'new_google_sub',
565
+ email: 'user@example.com',
566
+ email_verified: true,
567
+ name: 'User',
568
+ picture: undefined,
569
+ given_name: 'User',
570
+ family_name: '',
571
+ }
572
+
573
+ // First call: no existing user found
574
+ // Second call: insertUserSql returns null (user creation fails)
575
+ ;(queryOne as Mock)
576
+ .mockResolvedValueOnce(null)
577
+ .mockResolvedValueOnce(null)
578
+
579
+ await expect(
580
+ authService.findOrCreateUser(db, env, googleUser, mockAuthContext)
581
+ ).rejects.toThrow('Failed to create user')
582
+ })
583
+
584
+ it('should update user when name changes', async () => {
585
+ const env = createMockEnv()
586
+ const googleUser: GoogleUserInfo = {
587
+ sub: 'existing_sub',
588
+ email: 'same@example.com',
589
+ email_verified: true,
590
+ name: 'New Name',
591
+ picture: 'https://same-picture.jpg',
592
+ given_name: 'New',
593
+ family_name: 'Name',
594
+ }
595
+
596
+ const userRow = {
597
+ id: 'user-1',
598
+ googleId: googleUser.sub,
599
+ email: googleUser.email,
600
+ name: 'Old Name',
601
+ avatarUrl: googleUser.picture,
602
+ status: 'active',
603
+ providerIds: JSON.stringify(['google']),
604
+ isSuperAdmin: 0,
605
+ createdAt: new Date().toISOString(),
606
+ updatedAt: new Date().toISOString(),
607
+ deletedAt: null,
608
+ }
609
+
610
+ ;(queryOne as Mock)
611
+ .mockResolvedValueOnce(userRow)
612
+ .mockResolvedValueOnce({ ...userRow, name: googleUser.name })
613
+
614
+ const result = await authService.findOrCreateUser(db, env, googleUser, mockAuthContext)
615
+
616
+ expect(result.isNewUser).toBe(false)
617
+ expect(execute).toHaveBeenCalled()
618
+ })
619
+
620
+ it('should update user when avatar changes', async () => {
621
+ const env = createMockEnv()
622
+ const googleUser: GoogleUserInfo = {
623
+ sub: 'existing_sub',
624
+ email: 'same@example.com',
625
+ email_verified: true,
626
+ name: 'Same Name',
627
+ picture: 'https://new-picture.jpg',
628
+ given_name: 'Same',
629
+ family_name: 'Name',
630
+ }
631
+
632
+ const userRow = {
633
+ id: 'user-1',
634
+ googleId: googleUser.sub,
635
+ email: googleUser.email,
636
+ name: googleUser.name,
637
+ avatarUrl: 'https://old-picture.jpg',
638
+ status: 'active',
639
+ providerIds: JSON.stringify(['google']),
640
+ isSuperAdmin: 0,
641
+ createdAt: new Date().toISOString(),
642
+ updatedAt: new Date().toISOString(),
643
+ deletedAt: null,
644
+ }
645
+
646
+ ;(queryOne as Mock)
647
+ .mockResolvedValueOnce(userRow)
648
+ .mockResolvedValueOnce({ ...userRow, avatarUrl: googleUser.picture })
649
+
650
+ const result = await authService.findOrCreateUser(db, env, googleUser, mockAuthContext)
651
+
652
+ expect(result.isNewUser).toBe(false)
653
+ expect(execute).toHaveBeenCalled()
654
+ })
655
+ })
656
+ })