@etus/bhono-app 0.1.5 → 0.1.6

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 (269) hide show
  1. package/dist/index.js +0 -0
  2. package/package.json +5 -1
  3. package/templates/base/.husky/pre-push +26 -0
  4. package/templates/base/CLAUDE.md +5 -5
  5. package/templates/base/README.md +31 -20
  6. package/templates/base/docs/app_spec.txt +13 -10
  7. package/templates/base/docs/architecture/README.md +3 -0
  8. package/templates/base/docs/architecture/data-requirements.md +4 -3
  9. package/templates/base/docs/architecture/db-bootstrap.md +39 -0
  10. package/templates/base/docs/architecture/drizzle-migration-plan.md +125 -0
  11. package/templates/base/docs/architecture/erd.md +1 -1
  12. package/templates/base/docs/architecture/sql-standards.md +100 -0
  13. package/templates/base/docs/testing.md +36 -29
  14. package/templates/base/package.json +6 -5
  15. package/templates/base/pnpm-lock.yaml +0 -123
  16. package/templates/base/schema.sql +84 -0
  17. package/templates/base/scripts/init.sh +244 -59
  18. package/templates/base/src/client/hooks/use-auth.ts +5 -0
  19. package/templates/base/src/client/routes/_authenticated/dashboard.tsx +1 -1
  20. package/templates/base/src/client/routes/index.tsx +1 -1
  21. package/templates/base/src/server/db/client.ts +3 -5
  22. package/templates/base/src/server/db/records.ts +81 -0
  23. package/templates/base/src/server/db/seed.ts +3 -2
  24. package/templates/base/src/server/db/sql.ts +96 -0
  25. package/templates/base/src/server/index.ts +16 -2
  26. package/templates/base/src/server/lib/audit.ts +74 -26
  27. package/templates/base/src/server/lib/audited-db.ts +219 -109
  28. package/templates/base/src/server/lib/transaction.ts +10 -16
  29. package/templates/base/src/server/middleware/account.ts +8 -15
  30. package/templates/base/src/server/middleware/auth.ts +102 -38
  31. package/templates/base/src/server/middleware/rate-limit.ts +6 -1
  32. package/templates/base/src/server/routes/accounts/handlers.ts +18 -6
  33. package/templates/base/src/server/routes/audits/handlers.ts +3 -1
  34. package/templates/base/src/server/routes/auth/handlers.ts +14 -9
  35. package/templates/base/src/server/routes/auth/test-login.ts +99 -45
  36. package/templates/base/src/server/routes/health/handlers.ts +4 -4
  37. package/templates/base/src/server/routes/invitations/handlers.ts +6 -3
  38. package/templates/base/src/server/routes/users/handlers.ts +21 -14
  39. package/templates/base/src/server/services/accounts.ts +242 -217
  40. package/templates/base/src/server/services/audits.ts +114 -61
  41. package/templates/base/src/server/services/auth.ts +310 -180
  42. package/templates/base/src/server/services/invitations.ts +282 -222
  43. package/templates/base/src/server/services/users.ts +383 -293
  44. package/templates/base/src/server/types/index.ts +1 -2
  45. package/templates/base/{src/server/__tests__/fixtures.ts → tests/fixtures/server.ts} +3 -3
  46. package/templates/base/{src/client/__tests__/setup-browser.ts → tests/helpers/client-setup-browser.ts} +2 -2
  47. package/templates/base/{src/client/__tests__/setup.ts → tests/helpers/client-setup.ts} +1 -1
  48. package/templates/base/{src/client/__tests__/test-utils.tsx → tests/helpers/client-test-utils.tsx} +2 -2
  49. package/templates/base/{src/server/__tests__/setup.ts → tests/helpers/server.ts} +9 -9
  50. package/templates/base/tests/integration/accounts/crud.test.ts +2 -11
  51. package/templates/base/tests/integration/audits/list.test.ts +2 -11
  52. package/templates/base/tests/integration/auth/auth-service.test.ts +1 -10
  53. package/templates/base/tests/integration/auth/invitation-token.test.ts +2 -11
  54. package/templates/base/tests/integration/auth/logout.test.ts +2 -11
  55. package/templates/base/tests/integration/auth/oauth.test.ts +23 -42
  56. package/templates/base/tests/integration/auth/refresh-token.test.ts +1 -9
  57. package/templates/base/tests/integration/auth/session-expiry.test.ts +1 -9
  58. package/templates/base/tests/integration/auth/session.test.ts +2 -11
  59. package/templates/base/tests/integration/auth/super-admin.test.ts +1 -9
  60. package/templates/base/tests/integration/authorization/analytics-role.test.ts +2 -11
  61. package/templates/base/tests/integration/authorization/billing-role.test.ts +2 -11
  62. package/templates/base/tests/integration/authorization/guards-roles.test.ts +1 -9
  63. package/templates/base/tests/integration/authorization/multi-tenancy.test.ts +2 -11
  64. package/templates/base/tests/integration/authorization/roles.test.ts +2 -11
  65. package/templates/base/tests/integration/config/production-behavior.test.ts +2 -11
  66. package/templates/base/tests/integration/health/health.test.ts +25 -44
  67. package/templates/base/tests/integration/invitations/crud.test.ts +2 -11
  68. package/templates/base/tests/integration/invitations/email.test.ts +1 -9
  69. package/templates/base/tests/integration/middleware/auth.test.ts +3 -12
  70. package/templates/base/tests/integration/middleware/request-logger.test.ts +1 -9
  71. package/templates/base/tests/integration/performance/response-times.test.ts +1 -9
  72. package/templates/base/tests/integration/security/cookie-security.test.ts +2 -11
  73. package/templates/base/tests/integration/security/csrf-protection.test.ts +2 -11
  74. package/templates/base/tests/integration/security/log-sanitization.test.ts +1 -9
  75. package/templates/base/tests/integration/security/rate-limiting.test.ts +1 -9
  76. package/templates/base/tests/integration/security/sql-injection.test.ts +7 -18
  77. package/templates/base/tests/integration/security/xss-prevention.test.ts +2 -11
  78. package/templates/base/tests/integration/setup.ts +13 -90
  79. package/templates/base/tests/integration/smoke.test.ts +3 -2
  80. package/templates/base/tests/integration/storage/upload.test.ts +2 -11
  81. package/templates/base/tests/integration/storage/validation.test.ts +2 -11
  82. package/templates/base/tests/integration/users/crud.test.ts +2 -11
  83. package/templates/base/tests/integration/users/list.test.ts +2 -11
  84. package/templates/base/tests/integration/vitest.config.ts +2 -9
  85. package/templates/base/{src/server/__tests__ → tests}/mocks/db.ts +1 -1
  86. package/templates/base/{src/server/__tests__ → tests}/mocks/index.ts +1 -1
  87. package/templates/base/{src/server/__tests__ → tests}/mocks/kv.ts +1 -1
  88. package/templates/base/{src/server/__tests__ → tests}/mocks/r2.ts +1 -1
  89. package/templates/base/{src/client/components/__tests__ → tests/unit/client/components}/sidebar.test.tsx +1 -1
  90. package/templates/base/{src/client/components/ui/__tests__ → tests/unit/client/components/ui}/avatar.test.tsx +1 -1
  91. package/templates/base/{src/client/__tests__ → tests/unit/client/components/ui}/button.test.tsx +1 -1
  92. package/templates/base/{src/client/components/ui/__tests__ → tests/unit/client/components/ui}/card.test.tsx +1 -1
  93. package/templates/base/{src/client/components/ui/__tests__ → tests/unit/client/components/ui}/dialog.test.tsx +1 -1
  94. package/templates/base/{src/client/components/ui/__tests__ → tests/unit/client/components/ui}/input.test.tsx +1 -1
  95. package/templates/base/{src/client/components/ui/__tests__ → tests/unit/client/components/ui}/loading-skeleton.test.tsx +1 -1
  96. package/templates/base/{src/client/components/ui/__tests__ → tests/unit/client/components/ui}/skeleton.test.tsx +1 -1
  97. package/templates/base/{src/client/components/ui/__tests__ → tests/unit/client/components/ui}/sonner.test.tsx +1 -1
  98. package/templates/base/{src/client/components/ui/__tests__ → tests/unit/client/components/ui}/tabs.test.tsx +1 -1
  99. package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/account.test.tsx +1 -1
  100. package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/integrations.test.tsx +1 -1
  101. package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/settings.test.tsx +1 -1
  102. package/templates/base/{src/client/routes/_authenticated/__tests__ → tests/unit/client/routes/_authenticated}/team.test.tsx +1 -1
  103. package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/authenticated-layout.test.tsx +1 -1
  104. package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/dashboard.test.tsx +1 -1
  105. package/templates/base/{src/client/routes/__tests__ → tests/unit/client/routes}/invite.test.tsx +1 -1
  106. package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/login.test.tsx +1 -1
  107. package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/navigation.test.tsx +1 -1
  108. package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/root-layout.test.tsx +1 -1
  109. package/templates/base/{src/server/auth/__tests__ → tests/unit/server/auth}/guards.test.ts +2 -2
  110. package/templates/base/{src → tests/unit}/server/auth/permissions.test.ts +1 -1
  111. package/templates/base/{src → tests/unit}/server/auth/roles.test.ts +1 -1
  112. package/templates/base/tests/unit/server/db/sql.test.ts +68 -0
  113. package/templates/base/{src → tests/unit}/server/env.test.ts +1 -1
  114. package/templates/base/tests/unit/server/lib/audited-db.test.ts +78 -0
  115. package/templates/base/{src → tests/unit}/server/lib/email.test.ts +1 -1
  116. package/templates/base/{src → tests/unit}/server/lib/errors.test.ts +1 -1
  117. package/templates/base/{src → tests/unit}/server/lib/oauth.test.ts +1 -1
  118. package/templates/base/{src → tests/unit}/server/lib/pagination.test.ts +1 -1
  119. package/templates/base/{src → tests/unit}/server/lib/password.test.ts +1 -1
  120. package/templates/base/{src → tests/unit}/server/lib/providers.test.ts +1 -1
  121. package/templates/base/{src → tests/unit}/server/lib/r2-storage.test.ts +2 -2
  122. package/templates/base/{src → tests/unit}/server/lib/session.test.ts +2 -2
  123. package/templates/base/{src → tests/unit}/server/lib/tokens.test.ts +1 -1
  124. package/templates/base/{src → tests/unit}/server/lib/transaction.test.ts +5 -14
  125. package/templates/base/{src → tests/unit}/server/middleware/account.test.ts +16 -24
  126. package/templates/base/{src → tests/unit}/server/middleware/auth.test.ts +71 -42
  127. package/templates/base/{src → tests/unit}/server/middleware/cors.test.ts +1 -1
  128. package/templates/base/{src → tests/unit}/server/middleware/error-handler.test.ts +2 -2
  129. package/templates/base/{src → tests/unit}/server/middleware/rate-limit.test.ts +3 -2
  130. package/templates/base/{src → tests/unit}/server/middleware/request-context.test.ts +1 -1
  131. package/templates/base/{src → tests/unit}/server/middleware/request-logger.test.ts +1 -1
  132. package/templates/base/{src/server/__tests__/mocks/__tests__ → tests/unit/server/mocks}/db.test.ts +1 -1
  133. package/templates/base/{src/server/__tests__/mocks/__tests__ → tests/unit/server/mocks}/kv.test.ts +1 -1
  134. package/templates/base/{src/server/__tests__/mocks/__tests__ → tests/unit/server/mocks}/r2.test.ts +1 -1
  135. package/templates/base/{src/server/routes/accounts/__tests__ → tests/unit/server/routes/accounts}/handlers.test.ts +12 -12
  136. package/templates/base/{src/server/routes/audits/__tests__ → tests/unit/server/routes/audits}/handlers.test.ts +11 -11
  137. package/templates/base/{src/server/routes/auth/__tests__ → tests/unit/server/routes/auth}/handlers.test.ts +13 -13
  138. package/templates/base/{src/server/routes/health/__tests__ → tests/unit/server/routes/health}/handlers.test.ts +27 -23
  139. package/templates/base/{src/server/routes/invitations/__tests__ → tests/unit/server/routes/invitations}/handlers.test.ts +14 -17
  140. package/templates/base/{src/server/routes/storage/__tests__ → tests/unit/server/routes/storage}/handlers.test.ts +6 -6
  141. package/templates/base/{src/server/routes/users/__tests__ → tests/unit/server/routes/users}/handlers.test.ts +12 -12
  142. package/templates/base/tests/unit/server/services/accounts.test.ts +258 -0
  143. package/templates/base/tests/unit/server/services/audits.test.ts +141 -0
  144. package/templates/base/tests/unit/server/services/auth.test.ts +179 -0
  145. package/templates/base/tests/unit/server/services/invitations.test.ts +165 -0
  146. package/templates/base/tests/unit/server/services/users.test.ts +351 -0
  147. package/templates/base/tsconfig.json +2 -1
  148. package/templates/base/vitest.config.browser.ts +3 -2
  149. package/templates/base/vitest.config.frontend.ts +3 -2
  150. package/templates/base/vitest.config.ts +7 -14
  151. package/templates/base/.claude/settings.local.json +0 -11
  152. package/templates/base/config/drizzle.config.ts +0 -10
  153. package/templates/base/src/server/db/schema/accounts.ts +0 -20
  154. package/templates/base/src/server/db/schema/audit-logs.ts +0 -26
  155. package/templates/base/src/server/db/schema/index.ts +0 -7
  156. package/templates/base/src/server/db/schema/invitations.ts +0 -30
  157. package/templates/base/src/server/db/schema/refresh-tokens.ts +0 -22
  158. package/templates/base/src/server/db/schema/user-accounts.ts +0 -25
  159. package/templates/base/src/server/db/schema/users.ts +0 -33
  160. package/templates/base/src/server/lib/audited-db.test.ts +0 -107
  161. package/templates/base/src/server/lib/schema-helpers.ts +0 -16
  162. package/templates/base/src/server/services/__tests__/accounts.test.ts +0 -764
  163. package/templates/base/src/server/services/__tests__/audits.test.ts +0 -235
  164. package/templates/base/src/server/services/__tests__/auth.test.ts +0 -765
  165. package/templates/base/src/server/services/__tests__/invitations.test.ts +0 -704
  166. package/templates/base/src/server/services/__tests__/users.test.ts +0 -755
  167. package/templates/base/tests/integration/lib/schema-helpers.test.ts +0 -129
  168. /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
  169. /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
  170. /package/templates/base/{src/client/components/__tests__ → tests/unit/client/components}/__screenshots__/sidebar.test.tsx/Sidebar-handles-logout-button-click-1.png +0 -0
  171. /package/templates/base/{src/client/components/__tests__ → tests/unit/client/components}/__screenshots__/sidebar.test.tsx/Sidebar-handles-navigation-clicks-1.png +0 -0
  172. /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
  173. /package/templates/base/{src/client/components/__tests__ → tests/unit/client/components}/__screenshots__/sidebar.test.tsx/Sidebar-highlights-active-route-1.png +0 -0
  174. /package/templates/base/{src/client/components/__tests__ → tests/unit/client/components}/__screenshots__/sidebar.test.tsx/Sidebar-renders-sidebar-navigation-items-1.png +0 -0
  175. /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
  176. /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
  177. /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
  178. /package/templates/base/{src/client/components/__tests__ → tests/unit/client/components}/error-boundary.test.tsx +0 -0
  179. /package/templates/base/{src/client/components/ui/__tests__ → tests/unit/client/components/ui}/error-fallback.test.tsx +0 -0
  180. /package/templates/base/{src/client/hooks/__tests__ → tests/unit/client/hooks}/use-auth.test.tsx +0 -0
  181. /package/templates/base/{src/client/hooks/__tests__ → tests/unit/client/hooks}/use-theme.test.tsx +0 -0
  182. /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
  183. /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
  184. /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
  185. /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
  186. /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
  187. /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
  188. /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
  189. /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
  190. /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
  191. /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
  192. /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
  193. /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
  194. /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
  195. /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
  196. /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
  197. /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
  198. /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
  199. /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
  200. /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
  201. /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
  202. /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
  203. /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
  204. /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
  205. /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
  206. /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
  207. /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
  208. /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
  209. /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
  210. /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
  211. /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
  212. /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
  213. /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
  214. /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
  215. /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
  216. /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
  217. /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
  218. /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
  219. /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
  220. /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
  221. /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
  222. /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
  223. /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
  224. /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
  225. /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
  226. /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
  227. /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
  228. /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
  229. /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
  230. /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
  231. /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
  232. /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
  233. /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
  234. /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
  235. /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
  236. /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
  237. /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
  238. /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
  239. /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
  240. /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
  241. /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
  242. /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
  243. /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
  244. /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
  245. /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
  246. /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
  247. /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
  248. /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
  249. /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
  250. /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
  251. /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
  252. /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
  253. /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
  254. /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
  255. /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
  256. /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
  257. /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
  258. /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
  259. /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
  260. /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
  261. /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
  262. /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
  263. /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
  264. /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
  265. /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
  266. /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
  267. /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
  268. /package/templates/base/{src/client/__tests__ → tests/unit/client}/routes/error-components.test.tsx +0 -0
  269. /package/templates/base/{src/shared/schemas/__tests__ → tests/unit/shared}/schemas.test.ts +0 -0
@@ -0,0 +1,141 @@
1
+ // src/server/services/__tests__/audits.test.ts
2
+ import { describe, it, expect, vi, beforeEach, type Mock } from 'vitest'
3
+ import { auditsService, type AuditLogFilters } from '@server/services/audits'
4
+ import type { ServiceContext, AuditLog } from '@server/types'
5
+ import { createUserFixture, createSuperAdminFixture } from '@tests/fixtures/server'
6
+
7
+ vi.mock('@server/db/sql', () => ({
8
+ queryOne: vi.fn(),
9
+ queryAll: vi.fn(),
10
+ }))
11
+
12
+ import { queryOne, queryAll } from '@server/db/sql'
13
+
14
+ const db = {} as D1Database
15
+
16
+ function createMockContext(overrides: Partial<ServiceContext> = {}): ServiceContext {
17
+ const user = createUserFixture({
18
+ id: 'ctx-user-123',
19
+ email: 'user@test.com',
20
+ name: 'Test User',
21
+ })
22
+
23
+ return {
24
+ accountId: 'account-123',
25
+ user,
26
+ userRole: 'ADMIN',
27
+ transactionId: 'tx-123',
28
+ ip: '127.0.0.1',
29
+ userAgent: 'TestAgent/1.0',
30
+ ...overrides,
31
+ }
32
+ }
33
+
34
+ function createSuperAdminContext(overrides: Partial<ServiceContext> = {}): ServiceContext {
35
+ const user = createSuperAdminFixture({
36
+ id: 'super-admin-123',
37
+ email: 'superadmin@test.com',
38
+ name: 'Super Admin',
39
+ })
40
+
41
+ return {
42
+ accountId: 'account-123',
43
+ user,
44
+ userRole: 'ADMIN',
45
+ transactionId: 'tx-123',
46
+ ip: '127.0.0.1',
47
+ userAgent: 'TestAgent/1.0',
48
+ ...overrides,
49
+ }
50
+ }
51
+
52
+ function createMockAuditLog(overrides: Partial<AuditLog> = {}): AuditLog {
53
+ return {
54
+ id: 'log-1',
55
+ transactionId: 'tx-1',
56
+ accountId: 'account-123',
57
+ userId: 'user-123',
58
+ entity: 'user',
59
+ entityId: 'user-456',
60
+ action: 'INSERT',
61
+ changes: { name: 'New Name' },
62
+ ipAddress: '127.0.0.1',
63
+ userAgent: 'TestAgent',
64
+ timestamp: '2025-01-01T00:00:00Z',
65
+ ...overrides,
66
+ }
67
+ }
68
+
69
+ describe('auditsService', () => {
70
+ let ctx: ServiceContext
71
+ let superAdminCtx: ServiceContext
72
+
73
+ beforeEach(() => {
74
+ vi.clearAllMocks()
75
+ ctx = createMockContext()
76
+ superAdminCtx = createSuperAdminContext()
77
+ })
78
+
79
+ describe('findAll', () => {
80
+ const defaultFilters: AuditLogFilters = { page: 1, limit: 10 }
81
+
82
+ it('should return paginated audit logs with correct meta', async () => {
83
+ const mockLogs = [
84
+ createMockAuditLog({ id: 'log-1', entity: 'user', action: 'INSERT' }),
85
+ createMockAuditLog({ id: 'log-2', entity: 'account', action: 'UPDATE' }),
86
+ ]
87
+
88
+ ;(queryOne as Mock).mockResolvedValueOnce({ count: 2 })
89
+ ;(queryAll as Mock).mockResolvedValueOnce(mockLogs.map((log) => ({
90
+ id: log.id,
91
+ transactionId: log.transactionId,
92
+ accountId: log.accountId,
93
+ userId: log.userId,
94
+ entity: log.entity,
95
+ entityId: log.entityId,
96
+ action: log.action,
97
+ changes: JSON.stringify(log.changes),
98
+ ipAddress: log.ipAddress,
99
+ userAgent: log.userAgent,
100
+ timestamp: log.timestamp,
101
+ })))
102
+
103
+ const result = await auditsService.findAll(db, superAdminCtx, defaultFilters)
104
+
105
+ expect(result.data).toHaveLength(2)
106
+ expect(result.meta.totalItems).toBe(2)
107
+ })
108
+
109
+ it('should include account filter for non-super-admin', async () => {
110
+ ;(queryOne as Mock).mockResolvedValueOnce({ count: 0 })
111
+ ;(queryAll as Mock).mockResolvedValueOnce([])
112
+
113
+ await auditsService.findAll(db, ctx, defaultFilters)
114
+
115
+ expect((queryOne as Mock).mock.calls[0][2]).toContain(ctx.accountId)
116
+ })
117
+
118
+ it('should filter by entity when provided', async () => {
119
+ const mockLogs = [createMockAuditLog({ id: 'log-1', entity: 'user' })]
120
+
121
+ ;(queryOne as Mock).mockResolvedValueOnce({ count: 1 })
122
+ ;(queryAll as Mock).mockResolvedValueOnce(mockLogs.map((log) => ({
123
+ id: log.id,
124
+ transactionId: log.transactionId,
125
+ accountId: log.accountId,
126
+ userId: log.userId,
127
+ entity: log.entity,
128
+ entityId: log.entityId,
129
+ action: log.action,
130
+ changes: JSON.stringify(log.changes),
131
+ ipAddress: log.ipAddress,
132
+ userAgent: log.userAgent,
133
+ timestamp: log.timestamp,
134
+ })))
135
+
136
+ const result = await auditsService.findAll(db, superAdminCtx, { ...defaultFilters, entity: 'user' })
137
+
138
+ expect(result.data).toHaveLength(1)
139
+ })
140
+ })
141
+ })
@@ -0,0 +1,179 @@
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
+ }))
22
+
23
+ import { createAccessToken, generateRefreshToken, hashToken } from '@server/lib/tokens'
24
+ import { logAuthEvent } from '@server/lib/audit'
25
+ import { queryOne, execute } from '@server/db/sql'
26
+
27
+ const db = {} as D1Database
28
+
29
+ function createMockEnv() {
30
+ return {
31
+ APP_URL: 'http://localhost:8787',
32
+ JWT_SECRET: 'secret',
33
+ JWT_EXPIRY_MINUTES: '15',
34
+ GOOGLE_CLIENT_ID: 'id',
35
+ GOOGLE_CLIENT_SECRET: 'secret',
36
+ GOOGLE_REDIRECT_URI: 'http://localhost:8787',
37
+ REFRESH_TOKEN_EXPIRY_DAYS: '30',
38
+ SENDGRID_API_KEY: 'key',
39
+ SENDGRID_FROM_EMAIL: 'test@example.com',
40
+ ENVIRONMENT: 'test',
41
+ } as any
42
+ }
43
+
44
+ const mockAuthContext = {
45
+ transactionId: 'test-transaction-id',
46
+ ip: '127.0.0.1',
47
+ userAgent: 'IntegrationTest/1.0',
48
+ }
49
+
50
+ describe('authService', () => {
51
+ beforeEach(() => {
52
+ vi.clearAllMocks()
53
+ })
54
+
55
+ describe('findOrCreateUser', () => {
56
+ it('should create a new user on first OAuth login', async () => {
57
+ const env = createMockEnv()
58
+ const googleUser: GoogleUserInfo = {
59
+ sub: 'new_google_sub_123',
60
+ email: 'newuser@gmail.com',
61
+ email_verified: true,
62
+ name: 'New Google User',
63
+ picture: 'https://example.com/avatar.jpg',
64
+ given_name: 'New',
65
+ family_name: 'User',
66
+ }
67
+
68
+ const userRow = {
69
+ id: 'user-123',
70
+ googleId: googleUser.sub,
71
+ email: googleUser.email,
72
+ name: googleUser.name,
73
+ avatarUrl: googleUser.picture,
74
+ status: 'active',
75
+ providerIds: JSON.stringify(['google']),
76
+ isSuperAdmin: 0,
77
+ createdAt: new Date().toISOString(),
78
+ updatedAt: new Date().toISOString(),
79
+ deletedAt: null,
80
+ }
81
+
82
+ ;(queryOne as Mock)
83
+ .mockResolvedValueOnce(null)
84
+ .mockResolvedValueOnce(userRow)
85
+
86
+ const result = await authService.findOrCreateUser(db, env, googleUser, mockAuthContext)
87
+
88
+ expect(result.isNewUser).toBe(true)
89
+ expect(result.user.email).toBe(googleUser.email)
90
+ expect(createAccessToken).toHaveBeenCalled()
91
+ expect(generateRefreshToken).toHaveBeenCalled()
92
+ expect(hashToken).toHaveBeenCalled()
93
+ expect(execute).toHaveBeenCalled()
94
+ expect(logAuthEvent).toHaveBeenCalled()
95
+ })
96
+
97
+ it('should return existing user on subsequent OAuth login', async () => {
98
+ const env = createMockEnv()
99
+ const googleUser: GoogleUserInfo = {
100
+ sub: 'existing_sub_456',
101
+ email: 'existing@gmail.com',
102
+ email_verified: true,
103
+ name: 'Existing User',
104
+ picture: null,
105
+ given_name: 'Existing',
106
+ family_name: 'User',
107
+ }
108
+
109
+ const userRow = {
110
+ id: 'user-456',
111
+ googleId: googleUser.sub,
112
+ email: googleUser.email,
113
+ name: googleUser.name,
114
+ avatarUrl: null,
115
+ status: 'active',
116
+ providerIds: JSON.stringify(['google']),
117
+ isSuperAdmin: 0,
118
+ createdAt: new Date().toISOString(),
119
+ updatedAt: new Date().toISOString(),
120
+ deletedAt: null,
121
+ }
122
+
123
+ ;(queryOne as Mock).mockResolvedValueOnce(userRow)
124
+
125
+ const result = await authService.findOrCreateUser(db, env, googleUser, mockAuthContext)
126
+
127
+ expect(result.isNewUser).toBe(false)
128
+ expect(result.user.id).toBe('user-456')
129
+ expect(execute).toHaveBeenCalled()
130
+ })
131
+ })
132
+
133
+ describe('refreshAccessToken', () => {
134
+ it('should return a new access token when refresh token is valid', async () => {
135
+ const env = createMockEnv()
136
+
137
+ ;(queryOne as Mock)
138
+ .mockResolvedValueOnce({ id: 'token-1', userId: 'user-1', tokenHash: 'hashed-token' })
139
+ .mockResolvedValueOnce({
140
+ id: 'user-1',
141
+ googleId: 'google-1',
142
+ email: 'user@example.com',
143
+ name: 'User',
144
+ avatarUrl: null,
145
+ status: 'active',
146
+ providerIds: JSON.stringify(['google']),
147
+ isSuperAdmin: 0,
148
+ createdAt: new Date().toISOString(),
149
+ updatedAt: new Date().toISOString(),
150
+ deletedAt: null,
151
+ })
152
+
153
+ const result = await authService.refreshAccessToken(db, env, 'refresh-token', mockAuthContext)
154
+
155
+ expect(result.accessToken).toBe('mock-access-token')
156
+ expect(logAuthEvent).toHaveBeenCalled()
157
+ })
158
+
159
+ it('should throw for invalid refresh token', async () => {
160
+ const env = createMockEnv()
161
+ ;(queryOne as Mock).mockResolvedValueOnce(null)
162
+
163
+ await expect(
164
+ authService.refreshAccessToken(db, env, 'invalid-token', mockAuthContext)
165
+ ).rejects.toThrow(UnauthorizedError)
166
+ })
167
+ })
168
+
169
+ describe('revokeRefreshToken', () => {
170
+ it('should revoke refresh token and log logout', async () => {
171
+ const env = createMockEnv()
172
+
173
+ await authService.revokeRefreshToken(db, 'refresh-token', mockAuthContext, 'user-1')
174
+
175
+ expect(execute).toHaveBeenCalled()
176
+ expect(logAuthEvent).toHaveBeenCalled()
177
+ })
178
+ })
179
+ })
@@ -0,0 +1,165 @@
1
+ // src/server/services/__tests__/invitations.test.ts
2
+ import { describe, it, expect, vi, beforeEach, type Mock } from 'vitest'
3
+ import { invitationsService } from '@server/services/invitations'
4
+ import type { ServiceContext } from '@server/types'
5
+ import { createUserFixture, createAccountFixture } from '@tests/fixtures/server'
6
+ import { ConflictError, NotFoundError } from '@server/lib/errors'
7
+
8
+ vi.mock('@server/lib/email', () => ({
9
+ sendInvitationEmail: vi.fn(),
10
+ }))
11
+
12
+ vi.mock('@server/lib/audit', () => ({
13
+ logAuthEvent: vi.fn(),
14
+ }))
15
+
16
+ vi.mock('@server/db/sql', () => ({
17
+ queryOne: vi.fn(),
18
+ queryAll: vi.fn(),
19
+ execute: vi.fn(),
20
+ }))
21
+
22
+ import { sendInvitationEmail } from '@server/lib/email'
23
+ import { logAuthEvent } from '@server/lib/audit'
24
+ import { queryOne, queryAll, execute } from '@server/db/sql'
25
+
26
+ const db = {} as D1Database
27
+
28
+ function createMockContext(overrides: Partial<ServiceContext> = {}): ServiceContext {
29
+ const user = createUserFixture({
30
+ id: 'ctx-user-123',
31
+ email: 'context@example.com',
32
+ name: 'Context User',
33
+ })
34
+
35
+ return {
36
+ accountId: 'account-123',
37
+ user,
38
+ userRole: 'ADMIN',
39
+ transactionId: 'tx-123',
40
+ ip: '127.0.0.1',
41
+ userAgent: 'TestAgent/1.0',
42
+ ...overrides,
43
+ }
44
+ }
45
+
46
+ function createMockEnv() {
47
+ return {
48
+ APP_URL: 'http://localhost:8787',
49
+ SENDGRID_API_KEY: 'test',
50
+ SENDGRID_FROM_EMAIL: 'test@example.com',
51
+ } as any
52
+ }
53
+
54
+ describe('invitationsService', () => {
55
+ let ctx: ServiceContext
56
+
57
+ beforeEach(() => {
58
+ vi.clearAllMocks()
59
+ ctx = createMockContext()
60
+ })
61
+
62
+ describe('create', () => {
63
+ it('should link existing user instead of creating invitation', async () => {
64
+ const env = createMockEnv()
65
+ const existingUser = createUserFixture({ id: 'user-1', email: 'user@example.com' })
66
+
67
+ ;(queryOne as Mock)
68
+ .mockResolvedValueOnce(null)
69
+ .mockResolvedValueOnce({ id: existingUser.id, email: existingUser.email, name: existingUser.name })
70
+
71
+ const result = await invitationsService.create(db, env, ctx, {
72
+ email: existingUser.email,
73
+ role: 'VIEWER',
74
+ })
75
+
76
+ expect(result.linked).toBe(true)
77
+ expect(execute).toHaveBeenCalled()
78
+ expect(sendInvitationEmail).not.toHaveBeenCalled()
79
+ })
80
+
81
+ it('should create invitation when user not found', async () => {
82
+ const env = createMockEnv()
83
+ const account = createAccountFixture({ id: 'account-123', name: 'Account' })
84
+
85
+ ;(queryOne as Mock)
86
+ .mockResolvedValueOnce(null)
87
+ .mockResolvedValueOnce(null)
88
+ .mockResolvedValueOnce(null)
89
+ .mockResolvedValueOnce({ name: account.name })
90
+
91
+ const result = await invitationsService.create(db, env, ctx, {
92
+ email: 'invite@example.com',
93
+ role: 'VIEWER',
94
+ })
95
+
96
+ expect(result.invited).toBe(true)
97
+ expect(sendInvitationEmail).toHaveBeenCalled()
98
+ })
99
+
100
+ it('should reject when pending invitation exists', async () => {
101
+ const env = createMockEnv()
102
+
103
+ ;(queryOne as Mock)
104
+ .mockResolvedValueOnce(null)
105
+ .mockResolvedValueOnce(null)
106
+ .mockResolvedValueOnce({ id: 'inv-1' })
107
+
108
+ await expect(
109
+ invitationsService.create(db, env, ctx, {
110
+ email: 'invite@example.com',
111
+ role: 'VIEWER',
112
+ })
113
+ ).rejects.toThrow(ConflictError)
114
+ })
115
+ })
116
+
117
+ describe('list', () => {
118
+ it('should list invitations', async () => {
119
+ ;(queryAll as Mock).mockResolvedValueOnce([
120
+ {
121
+ id: 'inv-1',
122
+ email: 'invite@example.com',
123
+ role: 'VIEWER',
124
+ invitedById: 'user-1',
125
+ inviterName: 'Inviter',
126
+ expiresAt: new Date().toISOString(),
127
+ createdAt: new Date().toISOString(),
128
+ },
129
+ ])
130
+
131
+ const result = await invitationsService.list(db, ctx)
132
+
133
+ expect(result).toHaveLength(1)
134
+ })
135
+ })
136
+
137
+ describe('revoke', () => {
138
+ it('should revoke existing invitation', async () => {
139
+ ;(queryOne as Mock).mockResolvedValueOnce({ id: 'inv-1' })
140
+
141
+ await invitationsService.revoke(db, ctx, 'inv-1')
142
+
143
+ expect(execute).toHaveBeenCalled()
144
+ })
145
+
146
+ it('should throw if invitation not found', async () => {
147
+ ;(queryOne as Mock).mockResolvedValueOnce(null)
148
+
149
+ await expect(invitationsService.revoke(db, ctx, 'inv-1')).rejects.toThrow(NotFoundError)
150
+ })
151
+ })
152
+
153
+ describe('accept', () => {
154
+ it('should accept invitation and log auth event', async () => {
155
+ ;(queryOne as Mock).mockResolvedValueOnce({ id: 'inv-1', accountId: 'account-123', role: 'VIEWER' })
156
+
157
+ await invitationsService.accept(db, 'inv-1', 'user-1', {
158
+ transactionId: 'tx',
159
+ })
160
+
161
+ expect(execute).toHaveBeenCalled()
162
+ expect(logAuthEvent).toHaveBeenCalled()
163
+ })
164
+ })
165
+ })