@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
@@ -1,755 +0,0 @@
1
- // src/server/services/__tests__/users.test.ts
2
- import { describe, it, expect, vi, beforeEach, type Mock } from 'vitest'
3
- import { usersService } from '../users'
4
- import { NotFoundError } from '../../lib/errors'
5
- import type { ServiceContext, User, PaginationQuery } from '../../types'
6
- import {
7
- createUserFixture,
8
- createSuperAdminFixture,
9
- createDeletedUserFixture,
10
- createAccountFixture,
11
- } from '../../__tests__/fixtures'
12
-
13
- // Mock audited-db module
14
- vi.mock('../../lib/audited-db', () => ({
15
- auditedUpdate: vi.fn(),
16
- auditedDelete: vi.fn(),
17
- }))
18
-
19
- // Mock audit module
20
- vi.mock('../../lib/audit', () => ({
21
- logAudit: vi.fn(),
22
- }))
23
-
24
- import { auditedUpdate, auditedDelete } from '../../lib/audited-db'
25
- import { logAudit } from '../../lib/audit'
26
-
27
- /**
28
- * Creates a mock Drizzle database instance with chainable methods
29
- */
30
- function createMockDb() {
31
- const mockSelectResult: any[] = []
32
- const mockCountResult = [{ count: 0 }]
33
-
34
- const createChainable = (result: any) => {
35
- const chainable: any = {
36
- from: vi.fn().mockReturnThis(),
37
- where: vi.fn().mockReturnThis(),
38
- limit: vi.fn().mockReturnThis(),
39
- offset: vi.fn().mockReturnThis(),
40
- orderBy: vi.fn().mockImplementation(() => Promise.resolve(result)),
41
- }
42
- // Make where also resolve directly when called at the end
43
- chainable.where.mockImplementation(() => {
44
- const next = { ...chainable }
45
- next.limit = vi.fn().mockResolvedValue(result)
46
- return next
47
- })
48
- chainable.from.mockImplementation(() => {
49
- const next = { ...chainable }
50
- next.where.mockImplementation(() => {
51
- const whereNext = { ...chainable }
52
- whereNext.limit = vi.fn().mockResolvedValue(result)
53
- whereNext.offset = vi.fn().mockReturnThis()
54
- whereNext.orderBy = vi.fn().mockResolvedValue(result)
55
- return whereNext
56
- })
57
- return next
58
- })
59
- return chainable
60
- }
61
-
62
- const db = {
63
- select: vi.fn().mockImplementation((fields?: any) => {
64
- // If selecting count, return count result
65
- if (fields && 'count' in fields) {
66
- return createChainable(mockCountResult)
67
- }
68
- return createChainable(mockSelectResult)
69
- }),
70
- insert: vi.fn().mockReturnValue({
71
- values: vi.fn().mockReturnValue({
72
- returning: vi.fn().mockResolvedValue([]),
73
- }),
74
- }),
75
- update: vi.fn().mockReturnValue({
76
- set: vi.fn().mockReturnValue({
77
- where: vi.fn().mockReturnValue({
78
- returning: vi.fn().mockResolvedValue([]),
79
- }),
80
- }),
81
- }),
82
- delete: vi.fn().mockReturnValue({
83
- where: vi.fn().mockResolvedValue(),
84
- }),
85
- // Helper to set mock results
86
- _setSelectResult: (result: any[]) => {
87
- mockSelectResult.length = 0
88
- mockSelectResult.push(...result)
89
- },
90
- _setCountResult: (count: number) => {
91
- mockCountResult[0] = { count }
92
- },
93
- _mockSelectResult: mockSelectResult,
94
- _mockCountResult: mockCountResult,
95
- }
96
-
97
- return db as any
98
- }
99
-
100
- /**
101
- * Creates a standard service context for testing
102
- */
103
- function createMockContext(overrides: Partial<ServiceContext> = {}): ServiceContext {
104
- const user = createUserFixture({
105
- id: 'ctx-user-123',
106
- email: 'context@example.com',
107
- name: 'Context User',
108
- })
109
-
110
- return {
111
- accountId: 'account-123',
112
- user,
113
- userRole: 'ADMIN',
114
- transactionId: 'tx-123',
115
- ip: '127.0.0.1',
116
- userAgent: 'TestAgent/1.0',
117
- ...overrides,
118
- }
119
- }
120
-
121
- /**
122
- * Creates a super admin context for testing
123
- */
124
- function createSuperAdminContext(overrides: Partial<ServiceContext> = {}): ServiceContext {
125
- const user = createSuperAdminFixture({
126
- id: 'super-admin-123',
127
- email: 'superadmin@example.com',
128
- name: 'Super Admin',
129
- })
130
-
131
- return {
132
- accountId: 'account-123',
133
- user,
134
- userRole: 'ADMIN',
135
- transactionId: 'tx-123',
136
- ip: '127.0.0.1',
137
- userAgent: 'TestAgent/1.0',
138
- ...overrides,
139
- }
140
- }
141
-
142
- describe('usersService', () => {
143
- let mockDb: ReturnType<typeof createMockDb>
144
- let ctx: ServiceContext
145
- let superAdminCtx: ServiceContext
146
-
147
- beforeEach(() => {
148
- vi.clearAllMocks()
149
- mockDb = createMockDb()
150
- ctx = createMockContext()
151
- superAdminCtx = createSuperAdminContext()
152
- })
153
-
154
- describe('findAll', () => {
155
- const defaultPagination: PaginationQuery = {
156
- page: 1,
157
- limit: 10,
158
- }
159
-
160
- it('should return paginated users for account', async () => {
161
- const testUsers = [
162
- createUserFixture({ id: 'user-1', name: 'User One' }),
163
- createUserFixture({ id: 'user-2', name: 'User Two' }),
164
- ]
165
-
166
- // Mock count query
167
- const countChain = {
168
- from: vi.fn().mockReturnValue({
169
- where: vi.fn().mockResolvedValue([{ count: 2 }]),
170
- }),
171
- }
172
-
173
- // Mock data query
174
- const dataChain = {
175
- from: vi.fn().mockReturnValue({
176
- where: vi.fn().mockReturnValue({
177
- limit: vi.fn().mockReturnValue({
178
- offset: vi.fn().mockReturnValue({
179
- orderBy: vi.fn().mockResolvedValue(testUsers.map((u) => ({
180
- ...u,
181
- providerIds: u.providerIds,
182
- }))),
183
- }),
184
- }),
185
- }),
186
- }),
187
- }
188
-
189
- let selectCallCount = 0
190
- mockDb.select = vi.fn().mockImplementation((fields?: any) => {
191
- selectCallCount++
192
- // First call is count, second is data
193
- if (selectCallCount === 1 || (fields && 'count' in fields)) {
194
- return countChain
195
- }
196
- return dataChain
197
- })
198
-
199
- const result = await usersService.findAll(mockDb, ctx, defaultPagination)
200
-
201
- expect(result.data).toHaveLength(2)
202
- expect(result.meta.totalItems).toBe(2)
203
- expect(result.meta.currentPage).toBe(1)
204
- expect(result.meta.limit).toBe(10)
205
- })
206
-
207
- it('should return empty array when no users found', async () => {
208
- // Mock empty results
209
- const countChain = {
210
- from: vi.fn().mockReturnValue({
211
- where: vi.fn().mockResolvedValue([{ count: 0 }]),
212
- }),
213
- }
214
- const dataChain = {
215
- from: vi.fn().mockReturnValue({
216
- where: vi.fn().mockReturnValue({
217
- limit: vi.fn().mockReturnValue({
218
- offset: vi.fn().mockReturnValue({
219
- orderBy: vi.fn().mockResolvedValue([]),
220
- }),
221
- }),
222
- }),
223
- }),
224
- }
225
-
226
- let selectCallCount = 0
227
- mockDb.select = vi.fn().mockImplementation((fields?: any) => {
228
- selectCallCount++
229
- if (selectCallCount === 1 || (fields && 'count' in fields)) {
230
- return countChain
231
- }
232
- return dataChain
233
- })
234
-
235
- const result = await usersService.findAll(mockDb, ctx, defaultPagination)
236
-
237
- expect(result.data).toHaveLength(0)
238
- expect(result.meta.totalItems).toBe(0)
239
- })
240
-
241
- it('should filter by search query', async () => {
242
- const searchUser = createUserFixture({ id: 'user-1', name: 'John Doe', email: 'john@example.com' })
243
-
244
- const countChain = {
245
- from: vi.fn().mockReturnValue({
246
- where: vi.fn().mockResolvedValue([{ count: 1 }]),
247
- }),
248
- }
249
- const dataChain = {
250
- from: vi.fn().mockReturnValue({
251
- where: vi.fn().mockReturnValue({
252
- limit: vi.fn().mockReturnValue({
253
- offset: vi.fn().mockReturnValue({
254
- orderBy: vi.fn().mockResolvedValue([searchUser]),
255
- }),
256
- }),
257
- }),
258
- }),
259
- }
260
-
261
- let selectCallCount = 0
262
- mockDb.select = vi.fn().mockImplementation((fields?: any) => {
263
- selectCallCount++
264
- if (selectCallCount === 1 || (fields && 'count' in fields)) {
265
- return countChain
266
- }
267
- return dataChain
268
- })
269
-
270
- const paginationWithQuery: PaginationQuery = {
271
- ...defaultPagination,
272
- query: 'John',
273
- }
274
-
275
- const result = await usersService.findAll(mockDb, ctx, paginationWithQuery)
276
-
277
- expect(result.data).toHaveLength(1)
278
- expect(result.data[0].name).toBe('John Doe')
279
- })
280
-
281
- it('should allow super admin to see all users', async () => {
282
- const allUsers = [
283
- createUserFixture({ id: 'user-1', name: 'User One' }),
284
- createUserFixture({ id: 'user-2', name: 'User Two' }),
285
- createUserFixture({ id: 'user-3', name: 'User Three' }),
286
- ]
287
-
288
- const countChain = {
289
- from: vi.fn().mockReturnValue({
290
- where: vi.fn().mockResolvedValue([{ count: 3 }]),
291
- }),
292
- }
293
- const dataChain = {
294
- from: vi.fn().mockReturnValue({
295
- where: vi.fn().mockReturnValue({
296
- limit: vi.fn().mockReturnValue({
297
- offset: vi.fn().mockReturnValue({
298
- orderBy: vi.fn().mockResolvedValue(allUsers),
299
- }),
300
- }),
301
- }),
302
- }),
303
- }
304
-
305
- let selectCallCount = 0
306
- mockDb.select = vi.fn().mockImplementation((fields?: any) => {
307
- selectCallCount++
308
- if (selectCallCount === 1 || (fields && 'count' in fields)) {
309
- return countChain
310
- }
311
- return dataChain
312
- })
313
-
314
- const result = await usersService.findAll(mockDb, superAdminCtx, defaultPagination)
315
-
316
- expect(result.data).toHaveLength(3)
317
- expect(result.meta.totalItems).toBe(3)
318
- })
319
-
320
- it('should handle pagination correctly', async () => {
321
- const testUsers = [createUserFixture({ id: 'user-3', name: 'User Three' })]
322
-
323
- const countChain = {
324
- from: vi.fn().mockReturnValue({
325
- where: vi.fn().mockResolvedValue([{ count: 25 }]),
326
- }),
327
- }
328
- const dataChain = {
329
- from: vi.fn().mockReturnValue({
330
- where: vi.fn().mockReturnValue({
331
- limit: vi.fn().mockReturnValue({
332
- offset: vi.fn().mockReturnValue({
333
- orderBy: vi.fn().mockResolvedValue(testUsers),
334
- }),
335
- }),
336
- }),
337
- }),
338
- }
339
-
340
- let selectCallCount = 0
341
- mockDb.select = vi.fn().mockImplementation((fields?: any) => {
342
- selectCallCount++
343
- if (selectCallCount === 1 || (fields && 'count' in fields)) {
344
- return countChain
345
- }
346
- return dataChain
347
- })
348
-
349
- const pagination: PaginationQuery = { page: 3, limit: 10 }
350
- const result = await usersService.findAll(mockDb, ctx, pagination)
351
-
352
- expect(result.meta.currentPage).toBe(3)
353
- expect(result.meta.totalPages).toBe(3)
354
- expect(result.meta.hasPreviousPage).toBe(true)
355
- expect(result.meta.hasNextPage).toBe(false)
356
- })
357
- })
358
-
359
- describe('findById', () => {
360
- it('should return user by ID', async () => {
361
- const testUser = createUserFixture({ id: 'user-123', name: 'Test User' })
362
-
363
- // Mock finding user
364
- const userChain = {
365
- from: vi.fn().mockReturnValue({
366
- where: vi.fn().mockReturnValue({
367
- limit: vi.fn().mockResolvedValue([testUser]),
368
- }),
369
- }),
370
- }
371
-
372
- // Mock membership check for non-super-admin
373
- const membershipChain = {
374
- from: vi.fn().mockReturnValue({
375
- where: vi.fn().mockReturnValue({
376
- limit: vi.fn().mockResolvedValue([{ userId: 'user-123', accountId: 'account-123' }]),
377
- }),
378
- }),
379
- }
380
-
381
- let selectCallCount = 0
382
- mockDb.select = vi.fn().mockImplementation(() => {
383
- selectCallCount++
384
- if (selectCallCount === 1) {
385
- return userChain
386
- }
387
- return membershipChain
388
- })
389
-
390
- const result = await usersService.findById(mockDb, ctx, 'user-123')
391
-
392
- expect(result.id).toBe('user-123')
393
- expect(result.name).toBe('Test User')
394
- })
395
-
396
- it('should throw NotFoundError when user does not exist', async () => {
397
- const emptyChain = {
398
- from: vi.fn().mockReturnValue({
399
- where: vi.fn().mockReturnValue({
400
- limit: vi.fn().mockResolvedValue([]),
401
- }),
402
- }),
403
- }
404
-
405
- mockDb.select = vi.fn().mockReturnValue(emptyChain)
406
-
407
- await expect(usersService.findById(mockDb, ctx, 'nonexistent')).rejects.toThrow(
408
- NotFoundError
409
- )
410
- })
411
-
412
- it('should throw NotFoundError for soft-deleted user', async () => {
413
- // When user is soft-deleted, the query with isNull(deletedAt) returns empty
414
- const emptyChain = {
415
- from: vi.fn().mockReturnValue({
416
- where: vi.fn().mockReturnValue({
417
- limit: vi.fn().mockResolvedValue([]),
418
- }),
419
- }),
420
- }
421
-
422
- mockDb.select = vi.fn().mockReturnValue(emptyChain)
423
-
424
- await expect(usersService.findById(mockDb, ctx, 'deleted-user')).rejects.toThrow(
425
- NotFoundError
426
- )
427
- })
428
-
429
- it('should throw NotFoundError when non-super-admin accesses user from different account', async () => {
430
- const testUser = createUserFixture({ id: 'user-123', name: 'Test User' })
431
-
432
- // Mock finding user (user exists)
433
- const userChain = {
434
- from: vi.fn().mockReturnValue({
435
- where: vi.fn().mockReturnValue({
436
- limit: vi.fn().mockResolvedValue([testUser]),
437
- }),
438
- }),
439
- }
440
-
441
- // Mock membership check (no membership found)
442
- const emptyMembershipChain = {
443
- from: vi.fn().mockReturnValue({
444
- where: vi.fn().mockReturnValue({
445
- limit: vi.fn().mockResolvedValue([]),
446
- }),
447
- }),
448
- }
449
-
450
- let selectCallCount = 0
451
- mockDb.select = vi.fn().mockImplementation(() => {
452
- selectCallCount++
453
- if (selectCallCount === 1) {
454
- return userChain
455
- }
456
- return emptyMembershipChain
457
- })
458
-
459
- await expect(usersService.findById(mockDb, ctx, 'user-123')).rejects.toThrow(NotFoundError)
460
- })
461
-
462
- it('should allow super admin to access any user', async () => {
463
- const testUser = createUserFixture({ id: 'any-user', name: 'Any User' })
464
-
465
- const userChain = {
466
- from: vi.fn().mockReturnValue({
467
- where: vi.fn().mockReturnValue({
468
- limit: vi.fn().mockResolvedValue([testUser]),
469
- }),
470
- }),
471
- }
472
-
473
- mockDb.select = vi.fn().mockReturnValue(userChain)
474
-
475
- const result = await usersService.findById(mockDb, superAdminCtx, 'any-user')
476
-
477
- expect(result.id).toBe('any-user')
478
- // Super admin should not need membership check
479
- expect(mockDb.select).toHaveBeenCalledTimes(1)
480
- })
481
- })
482
-
483
- describe('update', () => {
484
- it('should update user fields', async () => {
485
- const existingUser = createUserFixture({ id: 'user-123', name: 'Old Name' })
486
- const updatedUser = { ...existingUser, name: 'New Name', status: 'active' as const }
487
-
488
- // Mock findById (first select for user, second for membership)
489
- const userChain = {
490
- from: vi.fn().mockReturnValue({
491
- where: vi.fn().mockReturnValue({
492
- limit: vi.fn().mockResolvedValue([existingUser]),
493
- }),
494
- }),
495
- }
496
- const membershipChain = {
497
- from: vi.fn().mockReturnValue({
498
- where: vi.fn().mockReturnValue({
499
- limit: vi.fn().mockResolvedValue([{ userId: 'user-123', accountId: 'account-123' }]),
500
- }),
501
- }),
502
- }
503
-
504
- let selectCallCount = 0
505
- mockDb.select = vi.fn().mockImplementation(() => {
506
- selectCallCount++
507
- if (selectCallCount === 1) {
508
- return userChain
509
- }
510
- return membershipChain
511
- })
512
-
513
- // Mock auditedUpdate
514
- ;(auditedUpdate as Mock).mockResolvedValue([updatedUser])
515
-
516
- const result = await usersService.update(mockDb, ctx, 'user-123', { name: 'New Name' })
517
-
518
- expect(result.name).toBe('New Name')
519
- expect(auditedUpdate).toHaveBeenCalled()
520
- })
521
-
522
- it('should throw NotFoundError for non-existent user', async () => {
523
- const emptyChain = {
524
- from: vi.fn().mockReturnValue({
525
- where: vi.fn().mockReturnValue({
526
- limit: vi.fn().mockResolvedValue([]),
527
- }),
528
- }),
529
- }
530
-
531
- mockDb.select = vi.fn().mockReturnValue(emptyChain)
532
-
533
- await expect(
534
- usersService.update(mockDb, ctx, 'nonexistent', { name: 'New Name' })
535
- ).rejects.toThrow(NotFoundError)
536
- })
537
-
538
- it('should update user status', async () => {
539
- const existingUser = createUserFixture({ id: 'user-123', status: 'active' })
540
- const updatedUser = { ...existingUser, status: 'inactive' as const }
541
-
542
- const userChain = {
543
- from: vi.fn().mockReturnValue({
544
- where: vi.fn().mockReturnValue({
545
- limit: vi.fn().mockResolvedValue([existingUser]),
546
- }),
547
- }),
548
- }
549
- const membershipChain = {
550
- from: vi.fn().mockReturnValue({
551
- where: vi.fn().mockReturnValue({
552
- limit: vi.fn().mockResolvedValue([{ userId: 'user-123', accountId: 'account-123' }]),
553
- }),
554
- }),
555
- }
556
-
557
- let selectCallCount = 0
558
- mockDb.select = vi.fn().mockImplementation(() => {
559
- selectCallCount++
560
- if (selectCallCount === 1) {
561
- return userChain
562
- }
563
- return membershipChain
564
- })
565
-
566
- ;(auditedUpdate as Mock).mockResolvedValue([updatedUser])
567
-
568
- const result = await usersService.update(mockDb, ctx, 'user-123', { status: 'inactive' })
569
-
570
- expect(result.status).toBe('inactive')
571
- })
572
- })
573
-
574
- describe('delete', () => {
575
- it('should soft delete user', async () => {
576
- const existingUser = createUserFixture({ id: 'user-123' })
577
-
578
- const userChain = {
579
- from: vi.fn().mockReturnValue({
580
- where: vi.fn().mockReturnValue({
581
- limit: vi.fn().mockResolvedValue([existingUser]),
582
- }),
583
- }),
584
- }
585
- const membershipChain = {
586
- from: vi.fn().mockReturnValue({
587
- where: vi.fn().mockReturnValue({
588
- limit: vi.fn().mockResolvedValue([{ userId: 'user-123', accountId: 'account-123' }]),
589
- }),
590
- }),
591
- }
592
-
593
- let selectCallCount = 0
594
- mockDb.select = vi.fn().mockImplementation(() => {
595
- selectCallCount++
596
- if (selectCallCount === 1) {
597
- return userChain
598
- }
599
- return membershipChain
600
- })
601
-
602
- ;(auditedDelete as Mock).mockResolvedValue()
603
-
604
- await usersService.delete(mockDb, ctx, 'user-123')
605
-
606
- expect(auditedDelete).toHaveBeenCalled()
607
- })
608
-
609
- it('should throw NotFoundError for non-existent user', async () => {
610
- const emptyChain = {
611
- from: vi.fn().mockReturnValue({
612
- where: vi.fn().mockReturnValue({
613
- limit: vi.fn().mockResolvedValue([]),
614
- }),
615
- }),
616
- }
617
-
618
- mockDb.select = vi.fn().mockReturnValue(emptyChain)
619
-
620
- await expect(usersService.delete(mockDb, ctx, 'nonexistent')).rejects.toThrow(NotFoundError)
621
- })
622
- })
623
-
624
- describe('restore', () => {
625
- it('should restore soft-deleted user', async () => {
626
- const deletedUser = createDeletedUserFixture({ id: 'deleted-user-123' })
627
- const restoredUser = { ...deletedUser, deletedAt: null }
628
-
629
- // Mock finding deleted user
630
- const deletedUserChain = {
631
- from: vi.fn().mockReturnValue({
632
- where: vi.fn().mockReturnValue({
633
- limit: vi.fn().mockResolvedValue([deletedUser]),
634
- }),
635
- }),
636
- }
637
-
638
- mockDb.select = vi.fn().mockReturnValue(deletedUserChain)
639
-
640
- // Mock auditedUpdate for restore (returns array)
641
- ;(auditedUpdate as Mock).mockResolvedValue([restoredUser])
642
-
643
- const result = await usersService.restore(mockDb, ctx, 'deleted-user-123')
644
-
645
- expect(result.id).toBe('deleted-user-123')
646
- expect(result.deletedAt).toBeNull()
647
- expect(auditedUpdate).toHaveBeenCalled()
648
- })
649
-
650
- it('should throw NotFoundError for non-deleted user', async () => {
651
- // When querying for deletedAt is NOT NULL, an active user won't be found
652
- const emptyChain = {
653
- from: vi.fn().mockReturnValue({
654
- where: vi.fn().mockReturnValue({
655
- limit: vi.fn().mockResolvedValue([]),
656
- }),
657
- }),
658
- }
659
-
660
- mockDb.select = vi.fn().mockReturnValue(emptyChain)
661
-
662
- await expect(usersService.restore(mockDb, ctx, 'active-user')).rejects.toThrow(NotFoundError)
663
- })
664
-
665
- it('should throw NotFoundError for non-existent user', async () => {
666
- const emptyChain = {
667
- from: vi.fn().mockReturnValue({
668
- where: vi.fn().mockReturnValue({
669
- limit: vi.fn().mockResolvedValue([]),
670
- }),
671
- }),
672
- }
673
-
674
- mockDb.select = vi.fn().mockReturnValue(emptyChain)
675
-
676
- await expect(usersService.restore(mockDb, ctx, 'nonexistent')).rejects.toThrow(NotFoundError)
677
- })
678
- })
679
-
680
- describe('createUserAccounts', () => {
681
- it('should create user-account relationships', async () => {
682
- // Mock checking existing relationship (not found)
683
- const emptyChain = {
684
- from: vi.fn().mockReturnValue({
685
- where: vi.fn().mockReturnValue({
686
- limit: vi.fn().mockResolvedValue([]),
687
- }),
688
- }),
689
- }
690
-
691
- mockDb.select = vi.fn().mockReturnValue(emptyChain)
692
-
693
- // Mock insert
694
- mockDb.insert = vi.fn().mockReturnValue({
695
- values: vi.fn().mockResolvedValue(),
696
- })
697
-
698
- const items = [
699
- { userId: 'user-1', accountId: 'account-1', role: 'VIEWER' as const },
700
- { userId: 'user-2', accountId: 'account-1', role: 'ADMIN' as const },
701
- ]
702
-
703
- const result = await usersService.createUserAccounts(mockDb, ctx, items)
704
-
705
- expect(result.success).toBe(true)
706
- expect(result.count).toBe(2)
707
- })
708
-
709
- it('should update existing relationship role', async () => {
710
- // Mock finding existing relationship
711
- const existingChain = {
712
- from: vi.fn().mockReturnValue({
713
- where: vi.fn().mockReturnValue({
714
- limit: vi.fn().mockResolvedValue([{ userId: 'user-1', accountId: 'account-1', role: 'VIEWER' }]),
715
- }),
716
- }),
717
- }
718
-
719
- mockDb.select = vi.fn().mockReturnValue(existingChain)
720
-
721
- // Mock update
722
- mockDb.update = vi.fn().mockReturnValue({
723
- set: vi.fn().mockReturnValue({
724
- where: vi.fn().mockResolvedValue(),
725
- }),
726
- })
727
-
728
- const items = [{ userId: 'user-1', accountId: 'account-1', role: 'ADMIN' as const }]
729
-
730
- const result = await usersService.createUserAccounts(mockDb, ctx, items)
731
-
732
- expect(result.success).toBe(true)
733
- expect(result.count).toBe(1)
734
- expect(mockDb.update).toHaveBeenCalled()
735
- })
736
- })
737
-
738
- describe('deleteUserAccounts', () => {
739
- it('should delete user-account relationships', async () => {
740
- mockDb.delete = vi.fn().mockReturnValue({
741
- where: vi.fn().mockResolvedValue(),
742
- })
743
-
744
- const items = [
745
- { userId: 'user-1', accountId: 'account-1', role: 'VIEWER' as const },
746
- ]
747
-
748
- const result = await usersService.deleteUserAccounts(mockDb, ctx, items)
749
-
750
- expect(result.success).toBe(true)
751
- expect(result.count).toBe(1)
752
- expect(mockDb.delete).toHaveBeenCalled()
753
- })
754
- })
755
- })