@jmruthers/pace-core 0.5.136 → 0.5.137

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 (289) hide show
  1. package/dist/{DataTable-CYOHOX3O.js → DataTable-6M4L6BI2.js} +10 -9
  2. package/dist/{EventLogo-801uofbR.d.ts → EventLogo-rFL_kRjk.d.ts} +73 -1
  3. package/dist/{UnifiedAuthProvider-5E5TUNMS.js → UnifiedAuthProvider-XIQQ7LVU.js} +4 -5
  4. package/dist/{chunk-YLKIDTUK.js → chunk-22WKWKRX.js} +4 -4
  5. package/dist/{chunk-TVYPTYOY.js → chunk-4C7EXCAR.js} +60 -24
  6. package/dist/chunk-4C7EXCAR.js.map +1 -0
  7. package/dist/{chunk-2TWNJ46Y.js → chunk-6LAAY47Q.js} +2 -2
  8. package/dist/{chunk-444EZN6N.js → chunk-7QCC6MCP.js} +88 -1
  9. package/dist/chunk-7QCC6MCP.js.map +1 -0
  10. package/dist/{chunk-FHWWBIHA.js → chunk-BCIBECNB.js} +5 -5
  11. package/dist/chunk-BJPBT3CU.js +21 -0
  12. package/dist/chunk-BJPBT3CU.js.map +1 -0
  13. package/dist/{chunk-L6PGMCMD.js → chunk-BLCXZEYF.js} +3 -3
  14. package/dist/{chunk-HJGGOMQ6.js → chunk-HAWZXGR2.js} +147 -103
  15. package/dist/chunk-HAWZXGR2.js.map +1 -0
  16. package/dist/{chunk-XARJS7CD.js → chunk-INQLMHPF.js} +2 -2
  17. package/dist/chunk-JISYG63F.js +70 -0
  18. package/dist/chunk-JISYG63F.js.map +1 -0
  19. package/dist/{chunk-NOHEVYVX.js → chunk-KYRHUBIU.js} +417 -319
  20. package/dist/chunk-KYRHUBIU.js.map +1 -0
  21. package/dist/{chunk-SL2YQDR6.js → chunk-MA6EPSGZ.js} +2 -2
  22. package/dist/{chunk-5DPZ5EAT.js → chunk-OWAG3GSU.js} +1 -3
  23. package/dist/{chunk-LTV3XIJJ.js → chunk-T6JN6LH6.js} +4 -4
  24. package/dist/{chunk-4MT5BGGL.js → chunk-YCWDTTUK.js} +4 -6
  25. package/dist/{chunk-4MT5BGGL.js.map → chunk-YCWDTTUK.js.map} +1 -1
  26. package/dist/components.d.ts +1 -1
  27. package/dist/components.js +12 -11
  28. package/dist/components.js.map +1 -1
  29. package/dist/hooks.js +8 -9
  30. package/dist/hooks.js.map +1 -1
  31. package/dist/index.d.ts +2 -2
  32. package/dist/index.js +15 -14
  33. package/dist/index.js.map +1 -1
  34. package/dist/providers.js +3 -4
  35. package/dist/rbac/index.js +8 -9
  36. package/dist/schema-DTDZQe2u.d.ts +28 -0
  37. package/dist/types.d.ts +152 -3
  38. package/dist/types.js +51 -16
  39. package/dist/types.js.map +1 -1
  40. package/dist/utils.d.ts +89 -4
  41. package/dist/utils.js +214 -96
  42. package/dist/utils.js.map +1 -1
  43. package/dist/validation.d.ts +1 -343
  44. package/dist/validation.js +3 -100
  45. package/docs/api/classes/ColumnFactory.md +1 -1
  46. package/docs/api/classes/ErrorBoundary.md +1 -1
  47. package/docs/api/classes/InvalidScopeError.md +1 -1
  48. package/docs/api/classes/MissingUserContextError.md +1 -1
  49. package/docs/api/classes/OrganisationContextRequiredError.md +1 -1
  50. package/docs/api/classes/PermissionDeniedError.md +1 -1
  51. package/docs/api/classes/PublicErrorBoundary.md +1 -1
  52. package/docs/api/classes/RBACAuditManager.md +1 -1
  53. package/docs/api/classes/RBACCache.md +1 -1
  54. package/docs/api/classes/RBACEngine.md +1 -1
  55. package/docs/api/classes/RBACError.md +1 -1
  56. package/docs/api/classes/RBACNotInitializedError.md +1 -1
  57. package/docs/api/classes/SecureSupabaseClient.md +1 -1
  58. package/docs/api/classes/StorageUtils.md +1 -1
  59. package/docs/api/enums/FileCategory.md +1 -1
  60. package/docs/api/interfaces/AggregateConfig.md +1 -1
  61. package/docs/api/interfaces/BadgeProps.md +27 -0
  62. package/docs/api/interfaces/ButtonProps.md +1 -1
  63. package/docs/api/interfaces/CardProps.md +1 -1
  64. package/docs/api/interfaces/ColorPalette.md +1 -1
  65. package/docs/api/interfaces/ColorShade.md +1 -1
  66. package/docs/api/interfaces/DataAccessRecord.md +1 -1
  67. package/docs/api/interfaces/DataRecord.md +1 -1
  68. package/docs/api/interfaces/DataTableAction.md +1 -1
  69. package/docs/api/interfaces/DataTableColumn.md +1 -1
  70. package/docs/api/interfaces/DataTableProps.md +1 -1
  71. package/docs/api/interfaces/DataTableToolbarButton.md +1 -1
  72. package/docs/api/interfaces/EmptyStateConfig.md +1 -1
  73. package/docs/api/interfaces/EnhancedNavigationMenuProps.md +1 -1
  74. package/docs/api/interfaces/EventAppRoleData.md +1 -1
  75. package/docs/api/interfaces/EventLogoProps.md +1 -1
  76. package/docs/api/interfaces/ExportColumn.md +1 -1
  77. package/docs/api/interfaces/ExportOptions.md +1 -1
  78. package/docs/api/interfaces/FileDisplayProps.md +1 -1
  79. package/docs/api/interfaces/FileMetadata.md +1 -1
  80. package/docs/api/interfaces/FileReference.md +1 -1
  81. package/docs/api/interfaces/FileSizeLimits.md +1 -1
  82. package/docs/api/interfaces/FileUploadOptions.md +1 -1
  83. package/docs/api/interfaces/FileUploadProps.md +1 -1
  84. package/docs/api/interfaces/FooterProps.md +1 -1
  85. package/docs/api/interfaces/GrantEventAppRoleParams.md +1 -1
  86. package/docs/api/interfaces/InactivityWarningModalProps.md +1 -1
  87. package/docs/api/interfaces/InputProps.md +1 -1
  88. package/docs/api/interfaces/LabelProps.md +1 -1
  89. package/docs/api/interfaces/LoginFormProps.md +1 -1
  90. package/docs/api/interfaces/NavigationAccessRecord.md +1 -1
  91. package/docs/api/interfaces/NavigationContextType.md +1 -1
  92. package/docs/api/interfaces/NavigationGuardProps.md +1 -1
  93. package/docs/api/interfaces/NavigationItem.md +1 -1
  94. package/docs/api/interfaces/NavigationMenuProps.md +1 -1
  95. package/docs/api/interfaces/NavigationProviderProps.md +1 -1
  96. package/docs/api/interfaces/Organisation.md +1 -1
  97. package/docs/api/interfaces/OrganisationContextType.md +1 -1
  98. package/docs/api/interfaces/OrganisationMembership.md +1 -1
  99. package/docs/api/interfaces/OrganisationProviderProps.md +1 -1
  100. package/docs/api/interfaces/OrganisationSecurityError.md +1 -1
  101. package/docs/api/interfaces/PaceAppLayoutProps.md +1 -1
  102. package/docs/api/interfaces/PaceLoginPageProps.md +1 -1
  103. package/docs/api/interfaces/PageAccessRecord.md +1 -1
  104. package/docs/api/interfaces/PagePermissionContextType.md +1 -1
  105. package/docs/api/interfaces/PagePermissionGuardProps.md +1 -1
  106. package/docs/api/interfaces/PagePermissionProviderProps.md +1 -1
  107. package/docs/api/interfaces/PaletteData.md +1 -1
  108. package/docs/api/interfaces/PermissionEnforcerProps.md +1 -1
  109. package/docs/api/interfaces/ProtectedRouteProps.md +1 -1
  110. package/docs/api/interfaces/PublicErrorBoundaryProps.md +1 -1
  111. package/docs/api/interfaces/PublicErrorBoundaryState.md +1 -1
  112. package/docs/api/interfaces/PublicLoadingSpinnerProps.md +1 -1
  113. package/docs/api/interfaces/PublicPageFooterProps.md +1 -1
  114. package/docs/api/interfaces/PublicPageHeaderProps.md +1 -1
  115. package/docs/api/interfaces/PublicPageLayoutProps.md +1 -1
  116. package/docs/api/interfaces/RBACConfig.md +1 -1
  117. package/docs/api/interfaces/RBACLogger.md +1 -1
  118. package/docs/api/interfaces/RevokeEventAppRoleParams.md +1 -1
  119. package/docs/api/interfaces/RoleBasedRouterContextType.md +1 -1
  120. package/docs/api/interfaces/RoleBasedRouterProps.md +1 -1
  121. package/docs/api/interfaces/RoleManagementResult.md +1 -1
  122. package/docs/api/interfaces/RouteAccessRecord.md +1 -1
  123. package/docs/api/interfaces/RouteConfig.md +1 -1
  124. package/docs/api/interfaces/SecureDataContextType.md +1 -1
  125. package/docs/api/interfaces/SecureDataProviderProps.md +1 -1
  126. package/docs/api/interfaces/SessionRestorationLoaderProps.md +1 -1
  127. package/docs/api/interfaces/StorageConfig.md +1 -1
  128. package/docs/api/interfaces/StorageFileInfo.md +1 -1
  129. package/docs/api/interfaces/StorageFileMetadata.md +1 -1
  130. package/docs/api/interfaces/StorageListOptions.md +1 -1
  131. package/docs/api/interfaces/StorageListResult.md +1 -1
  132. package/docs/api/interfaces/StorageUploadOptions.md +1 -1
  133. package/docs/api/interfaces/StorageUploadResult.md +1 -1
  134. package/docs/api/interfaces/StorageUrlOptions.md +1 -1
  135. package/docs/api/interfaces/StyleImport.md +1 -1
  136. package/docs/api/interfaces/SwitchProps.md +1 -1
  137. package/docs/api/interfaces/ToastActionElement.md +1 -1
  138. package/docs/api/interfaces/ToastProps.md +1 -1
  139. package/docs/api/interfaces/UnifiedAuthContextType.md +1 -1
  140. package/docs/api/interfaces/UnifiedAuthProviderProps.md +1 -1
  141. package/docs/api/interfaces/UseInactivityTrackerOptions.md +1 -1
  142. package/docs/api/interfaces/UseInactivityTrackerReturn.md +1 -1
  143. package/docs/api/interfaces/UsePublicEventOptions.md +1 -1
  144. package/docs/api/interfaces/UsePublicEventReturn.md +1 -1
  145. package/docs/api/interfaces/UsePublicFileDisplayOptions.md +1 -1
  146. package/docs/api/interfaces/UsePublicFileDisplayReturn.md +1 -1
  147. package/docs/api/interfaces/UsePublicRouteParamsReturn.md +1 -1
  148. package/docs/api/interfaces/UseResolvedScopeOptions.md +1 -1
  149. package/docs/api/interfaces/UseResolvedScopeReturn.md +1 -1
  150. package/docs/api/interfaces/UserEventAccess.md +1 -1
  151. package/docs/api/interfaces/UserMenuProps.md +1 -1
  152. package/docs/api/interfaces/UserProfile.md +1 -1
  153. package/docs/api/modules.md +79 -10
  154. package/docs/architecture/README.md +0 -1
  155. package/docs/styles/README.md +0 -2
  156. package/examples/RBAC/CompleteRBACExample.tsx +324 -0
  157. package/examples/RBAC/EventBasedApp.tsx +239 -0
  158. package/examples/RBAC/PermissionExample.tsx +151 -0
  159. package/examples/RBAC/index.ts +13 -0
  160. package/examples/public-pages/CorrectPublicPageImplementation.tsx +301 -0
  161. package/examples/public-pages/PublicEventPage.tsx +274 -0
  162. package/examples/public-pages/PublicPageApp.tsx +308 -0
  163. package/examples/public-pages/PublicPageUsageExample.tsx +216 -0
  164. package/examples/public-pages/index.ts +14 -0
  165. package/package.json +1 -10
  166. package/src/__tests__/TEST_STANDARD.md +92 -0
  167. package/src/components/Badge/Badge.test.tsx +314 -0
  168. package/src/components/Badge/Badge.tsx +304 -0
  169. package/src/components/Badge/index.ts +3 -0
  170. package/src/components/DataTable/__tests__/DataTableCore.test-setup.ts +217 -0
  171. package/src/components/DataTable/__tests__/styles.test.ts +1 -1
  172. package/src/components/DataTable/components/ColumnFilter.tsx +8 -4
  173. package/src/components/DataTable/components/DataTableBody.tsx +461 -0
  174. package/src/components/DataTable/components/DraggableColumnHeader.tsx +144 -0
  175. package/src/components/DataTable/components/FilterRow.tsx +9 -3
  176. package/src/components/DataTable/components/PaginationControls.tsx +1 -0
  177. package/src/components/DataTable/components/VirtualizedDataTable.tsx +513 -0
  178. package/src/components/DataTable/components/__tests__/AccessDeniedPage.test.tsx +14 -68
  179. package/src/components/DataTable/components/__tests__/ColumnFilter.test.tsx +62 -0
  180. package/src/components/DataTable/components/__tests__/FilterRow.test.tsx +43 -0
  181. package/src/components/DataTable/core/ActionManager.ts +235 -0
  182. package/src/components/DataTable/core/ColumnManager.ts +205 -0
  183. package/src/components/DataTable/core/DataManager.ts +188 -0
  184. package/src/components/DataTable/core/DataTableContext.tsx +181 -0
  185. package/src/components/DataTable/core/LocalDataAdapter.ts +273 -0
  186. package/src/components/DataTable/core/PluginRegistry.ts +229 -0
  187. package/src/components/DataTable/core/StateManager.ts +311 -0
  188. package/src/components/DataTable/core/interfaces.ts +338 -0
  189. package/src/components/DataTable/styles.ts +27 -6
  190. package/src/components/DataTable/utils/__tests__/columnUtils.test.ts +94 -0
  191. package/src/components/DataTable/utils/columnUtils.ts +40 -0
  192. package/src/components/DataTable/utils/debugTools.ts +609 -0
  193. package/src/components/DataTable/utils/index.ts +1 -0
  194. package/src/components/Dialog/README.md +804 -0
  195. package/src/components/Dialog/utils/__tests__/safeHtml.unit.test.ts +611 -0
  196. package/src/components/Dialog/utils/safeHtml.ts +185 -0
  197. package/src/components/Footer/Footer.test.tsx +1 -1
  198. package/src/components/Form/Form.test.tsx +1 -1
  199. package/src/components/Form/FormErrorSummary.tsx +113 -0
  200. package/src/components/Form/FormFieldset.tsx +127 -0
  201. package/src/components/Form/FormLiveRegion.tsx +198 -0
  202. package/src/components/LoginForm/LoginForm.test.tsx +1 -1
  203. package/src/components/PaceAppLayout/__tests__/PaceAppLayout.performance.test.tsx +76 -10
  204. package/src/components/PaceLoginPage/PaceLoginPage.tsx +1 -1
  205. package/src/components/PasswordReset/PasswordResetForm.test.tsx +597 -0
  206. package/src/components/PasswordReset/PasswordResetForm.tsx +201 -0
  207. package/src/components/PublicLayout/PublicPageDebugger.tsx +104 -0
  208. package/src/components/PublicLayout/PublicPageDiagnostic.tsx +162 -0
  209. package/src/components/PublicLayout/__tests__/PublicPageFooter.test.tsx +1 -1
  210. package/src/components/Select/Select.test.tsx +1 -1
  211. package/src/components/Select/Select.tsx +20 -8
  212. package/src/components/Table/__tests__/Table.test.tsx +1 -1
  213. package/src/components/index.ts +3 -0
  214. package/src/hooks/__tests__/useFileUrl.unit.test.ts +83 -85
  215. package/src/index.ts +4 -0
  216. package/src/styles/core.css +3 -0
  217. package/src/utils/appConfig.ts +47 -0
  218. package/src/utils/appIdResolver.test.ts +499 -0
  219. package/src/utils/appIdResolver.ts +130 -0
  220. package/src/utils/appNameResolver.simple.test.ts +212 -0
  221. package/src/utils/appNameResolver.test.ts +121 -0
  222. package/src/utils/appNameResolver.ts +191 -0
  223. package/src/utils/audit.ts +127 -0
  224. package/src/utils/auth-utils.ts +96 -0
  225. package/src/utils/bundleAnalysis.ts +129 -0
  226. package/src/utils/cn.ts +7 -0
  227. package/src/utils/debugLogger.ts +67 -0
  228. package/src/utils/deviceFingerprint.ts +215 -0
  229. package/src/utils/dynamicUtils.ts +105 -0
  230. package/src/utils/file-reference.test.ts +788 -0
  231. package/src/utils/file-reference.ts +519 -0
  232. package/src/utils/formatDate.test.ts +237 -0
  233. package/src/utils/formatting.ts +133 -0
  234. package/src/utils/index.ts +7 -0
  235. package/src/utils/lazyLoad.tsx +44 -0
  236. package/src/utils/logger.ts +179 -0
  237. package/src/utils/organisationContext.test.ts +322 -0
  238. package/src/utils/organisationContext.ts +153 -0
  239. package/src/utils/performanceBenchmark.ts +64 -0
  240. package/src/utils/performanceBudgets.ts +110 -0
  241. package/src/utils/permissionTypes.ts +37 -0
  242. package/src/utils/permissionUtils.test.ts +393 -0
  243. package/src/utils/permissionUtils.ts +34 -0
  244. package/src/utils/sanitization.ts +264 -0
  245. package/src/utils/schemaUtils.ts +37 -0
  246. package/src/utils/secureDataAccess.test.ts +711 -0
  247. package/src/utils/secureDataAccess.ts +377 -0
  248. package/src/utils/secureErrors.ts +79 -0
  249. package/src/utils/secureStorage.ts +244 -0
  250. package/src/utils/security.ts +156 -0
  251. package/src/utils/securityMonitor.ts +45 -0
  252. package/src/utils/sessionTracking.ts +126 -0
  253. package/src/utils/validation.ts +111 -0
  254. package/src/utils/validationUtils.ts +120 -0
  255. package/src/validation/index.ts +2 -2
  256. package/dist/chunk-444EZN6N.js.map +0 -1
  257. package/dist/chunk-APIBCTL2.js +0 -670
  258. package/dist/chunk-APIBCTL2.js.map +0 -1
  259. package/dist/chunk-HJGGOMQ6.js.map +0 -1
  260. package/dist/chunk-K2WWTH7O.js +0 -94
  261. package/dist/chunk-K2WWTH7O.js.map +0 -1
  262. package/dist/chunk-LMC26NLJ.js +0 -84
  263. package/dist/chunk-LMC26NLJ.js.map +0 -1
  264. package/dist/chunk-NOHEVYVX.js.map +0 -1
  265. package/dist/chunk-TVYPTYOY.js.map +0 -1
  266. package/dist/validation-8npbysjg.d.ts +0 -177
  267. /package/dist/{DataTable-CYOHOX3O.js.map → DataTable-6M4L6BI2.js.map} +0 -0
  268. /package/dist/{UnifiedAuthProvider-5E5TUNMS.js.map → UnifiedAuthProvider-XIQQ7LVU.js.map} +0 -0
  269. /package/dist/{chunk-YLKIDTUK.js.map → chunk-22WKWKRX.js.map} +0 -0
  270. /package/dist/{chunk-2TWNJ46Y.js.map → chunk-6LAAY47Q.js.map} +0 -0
  271. /package/dist/{chunk-FHWWBIHA.js.map → chunk-BCIBECNB.js.map} +0 -0
  272. /package/dist/{chunk-L6PGMCMD.js.map → chunk-BLCXZEYF.js.map} +0 -0
  273. /package/dist/{chunk-XARJS7CD.js.map → chunk-INQLMHPF.js.map} +0 -0
  274. /package/dist/{chunk-SL2YQDR6.js.map → chunk-MA6EPSGZ.js.map} +0 -0
  275. /package/dist/{chunk-5DPZ5EAT.js.map → chunk-OWAG3GSU.js.map} +0 -0
  276. /package/dist/{chunk-LTV3XIJJ.js.map → chunk-T6JN6LH6.js.map} +0 -0
  277. /package/examples/{components → components 2}/DataTable/HierarchicalActionsExample.tsx +0 -0
  278. /package/examples/{components → components 2}/DataTable/HierarchicalExample.tsx +0 -0
  279. /package/examples/{components → components 2}/DataTable/InitialPageSizeExample.tsx +0 -0
  280. /package/examples/{components → components 2}/DataTable/PerformanceExample.tsx +0 -0
  281. /package/examples/{components → components 2}/DataTable/index.ts +0 -0
  282. /package/examples/{components → components 2}/Dialog/BasicHtmlTest.tsx +0 -0
  283. /package/examples/{components → components 2}/Dialog/DebugHtmlExample.tsx +0 -0
  284. /package/examples/{components → components 2}/Dialog/HtmlDialogExample.tsx +0 -0
  285. /package/examples/{components → components 2}/Dialog/ScrollableDialogExample.tsx +0 -0
  286. /package/examples/{components → components 2}/Dialog/SimpleHtmlTest.tsx +0 -0
  287. /package/examples/{components → components 2}/Dialog/SmartDialogExample.tsx +0 -0
  288. /package/examples/{components → components 2}/Dialog/index.ts +0 -0
  289. /package/examples/{components → components 2}/index.ts +0 -0
@@ -0,0 +1,322 @@
1
+ /**
2
+ * @file Organisation Context Tests
3
+ * @package @jmruthers/pace-core
4
+ * @module Utils/OrganisationContext
5
+ * @since 0.4.0
6
+ *
7
+ * Comprehensive tests for organisation context utility functions covering all critical functionality.
8
+ */
9
+
10
+ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
11
+ import {
12
+ setOrganisationContext,
13
+ clearOrganisationContext,
14
+ getOrganisationContext
15
+ } from './organisationContext';
16
+ import type { SupabaseClient } from '@supabase/supabase-js';
17
+
18
+ // Mock Supabase client
19
+ const createMockSupabaseClient = () => ({
20
+ rpc: vi.fn().mockResolvedValue({ data: null, error: null })
21
+ });
22
+
23
+ describe('Organisation Context', () => {
24
+ let mockSupabase: any;
25
+
26
+ beforeEach(() => {
27
+ vi.clearAllMocks();
28
+ mockSupabase = createMockSupabaseClient();
29
+ });
30
+
31
+ describe('setOrganisationContext', () => {
32
+ it('sets organisation context successfully', async () => {
33
+ mockSupabase.rpc.mockResolvedValue({
34
+ data: null,
35
+ error: null
36
+ });
37
+
38
+ await setOrganisationContext(mockSupabase, 'org-123');
39
+
40
+ expect(mockSupabase.rpc).toHaveBeenCalledWith('set_organisation_context', {
41
+ org_id: 'org-123'
42
+ });
43
+ });
44
+
45
+ it('handles missing supabase client gracefully', async () => {
46
+ await expect(setOrganisationContext(null as any, 'org-123')).resolves.not.toThrow();
47
+ await expect(setOrganisationContext(undefined as any, 'org-123')).resolves.not.toThrow();
48
+ });
49
+
50
+ it('handles missing organisation ID gracefully', async () => {
51
+ await expect(setOrganisationContext(mockSupabase, null as any)).resolves.not.toThrow();
52
+ await expect(setOrganisationContext(mockSupabase, undefined as any)).resolves.not.toThrow();
53
+ await expect(setOrganisationContext(mockSupabase, '')).resolves.not.toThrow();
54
+ });
55
+
56
+ it('handles database function errors gracefully', async () => {
57
+ mockSupabase.rpc.mockResolvedValue({
58
+ data: null,
59
+ error: { message: 'Function not found' }
60
+ });
61
+
62
+ // Should not throw error
63
+ await expect(setOrganisationContext(mockSupabase, 'org-123')).resolves.not.toThrow();
64
+ });
65
+
66
+ it('handles RPC exceptions gracefully', async () => {
67
+ mockSupabase.rpc.mockRejectedValue(new Error('Network error'));
68
+
69
+ // Should not throw error
70
+ await expect(setOrganisationContext(mockSupabase, 'org-123')).resolves.not.toThrow();
71
+ });
72
+
73
+ it('handles invalid organisation ID format', async () => {
74
+ await expect(setOrganisationContext(mockSupabase, 'invalid-id')).resolves.not.toThrow();
75
+ await expect(setOrganisationContext(mockSupabase, '123')).resolves.not.toThrow();
76
+ });
77
+
78
+ it('handles very long organisation ID', async () => {
79
+ const longOrgId = 'a'.repeat(1000);
80
+ await expect(setOrganisationContext(mockSupabase, longOrgId)).resolves.not.toThrow();
81
+ });
82
+ });
83
+
84
+ describe('clearOrganisationContext', () => {
85
+ it('clears organisation context successfully', async () => {
86
+ mockSupabase.rpc.mockResolvedValue({
87
+ data: null,
88
+ error: null
89
+ });
90
+
91
+ await clearOrganisationContext(mockSupabase);
92
+
93
+ expect(mockSupabase.rpc).toHaveBeenCalledWith('rbac_audit_log', {
94
+ p_event_type: 'organisation_switched',
95
+ p_metadata: { action: 'clear_context' }
96
+ });
97
+ });
98
+
99
+ it('handles missing supabase client gracefully', async () => {
100
+ await expect(clearOrganisationContext(null as any)).resolves.not.toThrow();
101
+ await expect(clearOrganisationContext(undefined as any)).resolves.not.toThrow();
102
+ });
103
+
104
+ it('handles database function errors gracefully', async () => {
105
+ mockSupabase.rpc.mockResolvedValue({
106
+ data: null,
107
+ error: { message: 'Function not found' }
108
+ });
109
+
110
+ // Should not throw error
111
+ await expect(clearOrganisationContext(mockSupabase)).resolves.not.toThrow();
112
+ });
113
+
114
+ it('handles RPC exceptions gracefully', async () => {
115
+ mockSupabase.rpc.mockRejectedValue(new Error('Network error'));
116
+
117
+ // Should not throw error
118
+ await expect(clearOrganisationContext(mockSupabase)).resolves.not.toThrow();
119
+ });
120
+ });
121
+
122
+ describe('getOrganisationContext', () => {
123
+ it('gets organisation context successfully', async () => {
124
+ const result = await getOrganisationContext(mockSupabase);
125
+
126
+ expect(result).toBeNull();
127
+ });
128
+
129
+ it('returns null when no context is set', async () => {
130
+ const result = await getOrganisationContext(mockSupabase);
131
+
132
+ expect(result).toBeNull();
133
+ });
134
+
135
+ it('handles missing supabase client gracefully', async () => {
136
+ const result1 = await getOrganisationContext(null as any);
137
+ const result2 = await getOrganisationContext(undefined as any);
138
+
139
+ expect(result1).toBeNull();
140
+ expect(result2).toBeNull();
141
+ });
142
+
143
+ it('handles database function errors gracefully', async () => {
144
+ const result = await getOrganisationContext(mockSupabase);
145
+
146
+ expect(result).toBeNull();
147
+ });
148
+
149
+ it('handles RPC exceptions gracefully', async () => {
150
+ const result = await getOrganisationContext(mockSupabase);
151
+
152
+ expect(result).toBeNull();
153
+ });
154
+
155
+ it('handles invalid data format gracefully', async () => {
156
+ const result = await getOrganisationContext(mockSupabase);
157
+
158
+ expect(result).toBeNull();
159
+ });
160
+
161
+ it('handles empty string data', async () => {
162
+ const result = await getOrganisationContext(mockSupabase);
163
+
164
+ expect(result).toBeNull();
165
+ });
166
+ });
167
+
168
+ describe('Integration Tests', () => {
169
+ it('works with real Supabase client structure', async () => {
170
+ const mockClient = {
171
+ rpc: vi.fn().mockResolvedValue({
172
+ data: 'org-123',
173
+ error: null
174
+ })
175
+ };
176
+
177
+ const result = await getOrganisationContext(mockClient as any);
178
+
179
+ expect(result).toBeNull();
180
+ });
181
+
182
+ it('handles complete workflow', async () => {
183
+ // Set context
184
+ mockSupabase.rpc.mockResolvedValueOnce({
185
+ data: null,
186
+ error: null
187
+ });
188
+
189
+ await setOrganisationContext(mockSupabase, 'org-123');
190
+
191
+ // Get context
192
+ const result = await getOrganisationContext(mockSupabase);
193
+
194
+ expect(result).toBeNull();
195
+
196
+ // Clear context
197
+ mockSupabase.rpc.mockResolvedValueOnce({
198
+ data: null,
199
+ error: null
200
+ });
201
+
202
+ await clearOrganisationContext(mockSupabase);
203
+
204
+ expect(mockSupabase.rpc).toHaveBeenCalledTimes(2);
205
+ });
206
+
207
+ it('handles multiple organisation switches', async () => {
208
+ const organisations = ['org-123', 'org-456', 'org-789'];
209
+
210
+ for (const orgId of organisations) {
211
+ mockSupabase.rpc.mockResolvedValue({
212
+ data: null,
213
+ error: null
214
+ });
215
+
216
+ await setOrganisationContext(mockSupabase, orgId);
217
+ }
218
+
219
+ expect(mockSupabase.rpc).toHaveBeenCalledTimes(3);
220
+ expect(mockSupabase.rpc).toHaveBeenCalledWith('set_organisation_context', { org_id: 'org-123' });
221
+ expect(mockSupabase.rpc).toHaveBeenCalledWith('set_organisation_context', { org_id: 'org-456' });
222
+ expect(mockSupabase.rpc).toHaveBeenCalledWith('set_organisation_context', { org_id: 'org-789' });
223
+ });
224
+ });
225
+
226
+ describe('Error Handling', () => {
227
+ it('handles network timeouts gracefully', async () => {
228
+ mockSupabase.rpc.mockRejectedValue(new Error('Request timeout'));
229
+
230
+ await expect(setOrganisationContext(mockSupabase, 'org-123')).resolves.not.toThrow();
231
+ await expect(clearOrganisationContext(mockSupabase)).resolves.not.toThrow();
232
+ await expect(getOrganisationContext(mockSupabase)).resolves.toBeNull();
233
+ });
234
+
235
+ it('handles malformed responses gracefully', async () => {
236
+ const result = await getOrganisationContext(mockSupabase);
237
+
238
+ expect(result).toBeNull();
239
+ });
240
+
241
+ it('handles null responses gracefully', async () => {
242
+ const result = await getOrganisationContext(mockSupabase);
243
+
244
+ expect(result).toBeNull();
245
+ });
246
+
247
+ it('handles undefined responses gracefully', async () => {
248
+ const result = await getOrganisationContext(mockSupabase);
249
+
250
+ expect(result).toBeNull();
251
+ });
252
+ });
253
+
254
+ describe('Performance', () => {
255
+ it('handles rapid context changes efficiently', async () => {
256
+ const startTime = Date.now();
257
+
258
+ // Rapidly switch contexts
259
+ for (let i = 0; i < 100; i++) {
260
+ mockSupabase.rpc.mockResolvedValue({
261
+ data: null,
262
+ error: null
263
+ });
264
+
265
+ await setOrganisationContext(mockSupabase, `org-${i}`);
266
+ }
267
+
268
+ const endTime = Date.now();
269
+
270
+ expect(endTime - startTime).toBeLessThan(1000); // Should complete within 1 second
271
+ });
272
+
273
+ it('handles concurrent operations gracefully', async () => {
274
+ const operations = [
275
+ setOrganisationContext(mockSupabase, 'org-1'),
276
+ setOrganisationContext(mockSupabase, 'org-2'),
277
+ clearOrganisationContext(mockSupabase),
278
+ getOrganisationContext(mockSupabase)
279
+ ];
280
+
281
+ await expect(Promise.all(operations)).resolves.not.toThrow();
282
+ });
283
+ });
284
+
285
+ describe('Edge Cases', () => {
286
+ it('handles special characters in organisation ID', async () => {
287
+ const specialOrgId = 'org-123@domain.com';
288
+ await expect(setOrganisationContext(mockSupabase, specialOrgId)).resolves.not.toThrow();
289
+ });
290
+
291
+ it('handles unicode characters in organisation ID', async () => {
292
+ const unicodeOrgId = 'org-用户-123';
293
+ await expect(setOrganisationContext(mockSupabase, unicodeOrgId)).resolves.not.toThrow();
294
+ });
295
+
296
+ it('handles very long organisation ID', async () => {
297
+ const longOrgId = 'a'.repeat(10000);
298
+ await expect(setOrganisationContext(mockSupabase, longOrgId)).resolves.not.toThrow();
299
+ });
300
+
301
+ it('handles numeric organisation ID', async () => {
302
+ await expect(setOrganisationContext(mockSupabase, '123')).resolves.not.toThrow();
303
+ });
304
+
305
+ it('handles boolean organisation ID', async () => {
306
+ await expect(setOrganisationContext(mockSupabase, 'true' as any)).resolves.not.toThrow();
307
+ });
308
+ });
309
+
310
+ describe('Type Safety', () => {
311
+ it('maintains type safety with TypeScript', async () => {
312
+ const result = await getOrganisationContext(mockSupabase);
313
+ expect(result).toBeNull();
314
+ });
315
+
316
+ it('handles mixed input types gracefully', async () => {
317
+ await expect(setOrganisationContext(mockSupabase, 123 as any)).resolves.not.toThrow();
318
+ await expect(setOrganisationContext(mockSupabase, true as any)).resolves.not.toThrow();
319
+ await expect(setOrganisationContext(mockSupabase, {} as any)).resolves.not.toThrow();
320
+ });
321
+ });
322
+ });
@@ -0,0 +1,153 @@
1
+ /**
2
+ * @file Organisation Context Utility
3
+ * @package @jmruthers/pace-core
4
+ * @module Utils/OrganisationContext
5
+ * @since 0.4.0
6
+ *
7
+ * Utility functions for managing organisation context in database sessions.
8
+ * Provides fallback mechanisms for when database functions are not available.
9
+ */
10
+
11
+ import type { SupabaseClient } from '@supabase/supabase-js';
12
+
13
+ /**
14
+ * Set organisation context in the database session
15
+ *
16
+ * This function attempts to set the organisation context using a database function.
17
+ * If the function is not available, it falls back gracefully without throwing errors.
18
+ *
19
+ * @param supabase - Supabase client instance
20
+ * @param organisationId - The organisation ID to set as context
21
+ * @returns Promise that resolves when context is set (or falls back gracefully)
22
+ */
23
+ export async function setOrganisationContext(
24
+ supabase: SupabaseClient,
25
+ organisationId: string
26
+ ): Promise<void> {
27
+ if (!supabase || !organisationId) {
28
+ // TODO: Replace with proper logging service integration
29
+ return;
30
+ }
31
+
32
+ try {
33
+ // Add timeout to prevent hanging RPC calls
34
+ const timeoutPromise = new Promise((_, reject) => {
35
+ setTimeout(() => reject(new Error('RPC timeout after 3 seconds')), 3000);
36
+ });
37
+
38
+ // Call the database function to set organisation context
39
+ const rpcPromise = supabase.rpc('set_organisation_context', {
40
+ org_id: organisationId
41
+ });
42
+
43
+ const { error } = await Promise.race([rpcPromise, timeoutPromise]) as any;
44
+
45
+ if (error) {
46
+ // Function might not exist yet - this is expected during migration
47
+ // Silent fail - will fall back to client-side filtering
48
+ console.log('[organisationContext] RPC function not available or failed, continuing without database context');
49
+ } else {
50
+ console.log('[organisationContext] Organisation context set in database successfully');
51
+ }
52
+ } catch (error) {
53
+ // Handle any other errors gracefully
54
+ // Silent fail - will fall back to client-side filtering
55
+ console.log('[organisationContext] Failed to set database context, continuing without it:', error);
56
+ }
57
+ }
58
+
59
+ /**
60
+ * Clear organisation context from the database session
61
+ *
62
+ * @param supabase - Supabase client instance
63
+ * @returns Promise that resolves when context is cleared
64
+ */
65
+ export async function clearOrganisationContext(
66
+ supabase: SupabaseClient
67
+ ): Promise<void> {
68
+ if (!supabase) {
69
+ // TODO: Replace with proper logging service integration
70
+ return;
71
+ }
72
+
73
+ try {
74
+ const { error } = await supabase.rpc('rbac_audit_log', {
75
+ p_event_type: 'organisation_switched',
76
+ p_metadata: { action: 'clear_context' }
77
+ });
78
+
79
+ if (error) {
80
+ // Silent fail - function not available
81
+ // TODO: Replace with proper logging service integration
82
+ } else {
83
+ // TODO: Replace with proper logging service integration
84
+ }
85
+ } catch (error) {
86
+ // Silent fail - error occurred
87
+ // TODO: Replace with proper logging service integration
88
+ }
89
+ }
90
+
91
+ /**
92
+ * Get current organisation context from the database session
93
+ *
94
+ * @param supabase - Supabase client instance
95
+ * @returns Promise that resolves to the current organisation ID or null
96
+ */
97
+ export async function getOrganisationContext(
98
+ supabase: SupabaseClient
99
+ ): Promise<string | null> {
100
+ if (!supabase) {
101
+ // TODO: Replace with proper logging service integration
102
+ return null;
103
+ }
104
+
105
+ try {
106
+ // For now, return null since we're not using database context
107
+ // This will be replaced with proper context management
108
+ const data = null;
109
+ const error = null;
110
+
111
+ if (error) {
112
+ // TODO: Replace with proper logging service integration
113
+ return null;
114
+ }
115
+
116
+ // Validate that data is a string (allow empty strings)
117
+ if (typeof data === 'string') {
118
+ return data;
119
+ }
120
+
121
+ // Return null for invalid data formats
122
+ return null;
123
+ } catch (error) {
124
+ // TODO: Replace with proper logging service integration
125
+ return null;
126
+ }
127
+ }
128
+
129
+ /**
130
+ * Check if organisation context functions are available in the database
131
+ *
132
+ * @param supabase - Supabase client instance
133
+ * @returns Promise that resolves to true if functions are available
134
+ */
135
+ export async function isOrganisationContextAvailable(
136
+ supabase: SupabaseClient
137
+ ): Promise<boolean> {
138
+ if (!supabase) {
139
+ return false;
140
+ }
141
+
142
+ try {
143
+ const { error } = await supabase.rpc('get_organisation_context');
144
+
145
+ if (error) {
146
+ return false;
147
+ }
148
+
149
+ return true;
150
+ } catch (error) {
151
+ return false;
152
+ }
153
+ }
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Performance benchmarking utilities
3
+ */
4
+
5
+ export interface PerformanceMetrics {
6
+ renderTime: number;
7
+ interactionTime: number;
8
+ memoryUsage: number;
9
+ bundleSize: number;
10
+ }
11
+
12
+ export function createPerformanceBenchmark(name: string) {
13
+ const startTime = performance.now();
14
+ const startMemory = (performance as any).memory?.usedJSHeapSize || 0;
15
+
16
+ return {
17
+ end: () => {
18
+ const endTime = performance.now();
19
+ const endMemory = (performance as any).memory?.usedJSHeapSize || 0;
20
+
21
+ const metrics: PerformanceMetrics = {
22
+ renderTime: endTime - startTime,
23
+ interactionTime: 0, // Would be measured separately
24
+ memoryUsage: endMemory - startMemory,
25
+ bundleSize: 0, // Would be measured at build time
26
+ };
27
+
28
+ // TODO: Replace with proper logging service integration
29
+ // For now, we'll use a no-op to prevent console pollution
30
+ const _unused = {
31
+ benchmark: `Performance Benchmark [${name}]`,
32
+ metrics
33
+ };
34
+
35
+ return metrics;
36
+ }
37
+ };
38
+ }
39
+
40
+ export function measureRenderPerformance(componentName: string, renderFn: () => void): PerformanceMetrics {
41
+ const startTime = performance.now();
42
+ const startMemory = (performance as any).memory?.usedJSHeapSize || 0;
43
+
44
+ renderFn();
45
+
46
+ const endTime = performance.now();
47
+ const endMemory = (performance as any).memory?.usedJSHeapSize || 0;
48
+
49
+ const metrics: PerformanceMetrics = {
50
+ renderTime: endTime - startTime,
51
+ interactionTime: 0,
52
+ memoryUsage: endMemory - startMemory,
53
+ bundleSize: 0,
54
+ };
55
+
56
+ // TODO: Replace with proper logging service integration
57
+ // For now, we'll use a no-op to prevent console pollution
58
+ const _unused = {
59
+ benchmark: `Render Performance [${componentName}]`,
60
+ metrics
61
+ };
62
+
63
+ return metrics;
64
+ }
@@ -0,0 +1,110 @@
1
+
2
+ interface PerformanceBudget {
3
+ metric: string;
4
+ budget: number;
5
+ actual: number;
6
+ threshold: 'error' | 'warning' | 'info';
7
+ }
8
+
9
+ interface PerformanceMetrics {
10
+ bundleSize: number;
11
+ chunkCount: number;
12
+ treeshakingEffectiveness: number;
13
+ dynamicImportUsage: number;
14
+ }
15
+
16
+ interface MeasurementResult {
17
+ passed: boolean;
18
+ value: number;
19
+ threshold: number;
20
+ }
21
+
22
+ // Performance budget thresholds with index signature
23
+ export const PERFORMANCE_BUDGETS: { [key: string]: { threshold: number } } = {
24
+ COMPONENT_RENDER: { threshold: 50 },
25
+ BUNDLE_SIZE: { threshold: 150000 },
26
+ CHUNK_COUNT: { threshold: 10 },
27
+ TREESHAKING_SCORE: { threshold: 70 },
28
+ ERROR_BOUNDARY_TRIGGER: { threshold: 5 },
29
+ MEMORY_INCREASE: { threshold: 1000 },
30
+ LARGE_LIST_RENDER: { threshold: 500 },
31
+ } as const;
32
+
33
+ class PerformanceBudgetMonitor {
34
+ private metrics: Map<string, number> = new Map();
35
+ private budgets: PerformanceBudget[] = [];
36
+
37
+ measure(metric: string, value: number, metadata?: Record<string, unknown>): MeasurementResult {
38
+ this.metrics.set(metric, value);
39
+
40
+ // In production, this would send to a proper logging service
41
+ // For now, we'll log to console for testing purposes
42
+ if (import.meta.env.MODE === 'development' || import.meta.env.MODE === 'test' || process.env.NODE_ENV === 'test') {
43
+ console.log('📊 Performance Metric: ' + metric + ' = ' + value, metadata);
44
+ }
45
+
46
+ // Get threshold for this metric
47
+ const budgetConfig = PERFORMANCE_BUDGETS[metric];
48
+ const threshold = budgetConfig?.threshold || 100;
49
+ const passed = value <= threshold;
50
+
51
+ return {
52
+ passed,
53
+ value,
54
+ threshold
55
+ };
56
+ }
57
+
58
+ setBudget(metric: string, budget: number, threshold: 'error' | 'warning' | 'info' = 'warning'): void {
59
+ this.budgets.push({ metric, budget, actual: 0, threshold });
60
+ }
61
+
62
+ checkBudgets(): PerformanceBudget[] {
63
+ const violations: PerformanceBudget[] = [];
64
+
65
+ for (const budget of this.budgets) {
66
+ const actual = this.metrics.get(budget.metric) || 0;
67
+ budget.actual = actual;
68
+
69
+ if (actual > budget.budget) {
70
+ violations.push(budget);
71
+
72
+ // In production, this would send to a proper logging service
73
+ // For now, we'll log to console for testing purposes
74
+ if (import.meta.env.MODE === 'development' || import.meta.env.MODE === 'test' || process.env.NODE_ENV === 'test') {
75
+ if (budget.threshold === 'error') {
76
+ console.error('❌ Performance budget exceeded: ' + budget.metric + ' (' + actual + ' > ' + budget.budget + ')');
77
+ } else if (budget.threshold === 'warning') {
78
+ console.warn('⚠️ Performance budget exceeded: ' + budget.metric + ' (' + actual + ' > ' + budget.budget + ')');
79
+ } else {
80
+ console.info('ℹ️ Performance budget exceeded: ' + budget.metric + ' (' + actual + ' > ' + budget.budget + ')');
81
+ }
82
+ }
83
+ }
84
+ }
85
+
86
+ return violations;
87
+ }
88
+
89
+ getMetrics(): PerformanceMetrics {
90
+ return {
91
+ bundleSize: this.metrics.get('BUNDLE_SIZE') || 0,
92
+ chunkCount: this.metrics.get('CHUNK_COUNT') || 0,
93
+ treeshakingEffectiveness: this.metrics.get('TREESHAKING_SCORE') || 0,
94
+ dynamicImportUsage: this.metrics.get('DYNAMIC_IMPORTS') || 0,
95
+ };
96
+ }
97
+
98
+ reset(): void {
99
+ this.metrics.clear();
100
+ this.budgets.length = 0;
101
+ }
102
+ }
103
+
104
+ export const performanceBudgetMonitor = new PerformanceBudgetMonitor();
105
+
106
+ // Set default performance budgets
107
+ performanceBudgetMonitor.setBudget('BUNDLE_SIZE', 150000, 'error'); // 150KB
108
+ performanceBudgetMonitor.setBudget('CHUNK_COUNT', 10, 'warning');
109
+ performanceBudgetMonitor.setBudget('TREESHAKING_SCORE', 70, 'warning');
110
+ performanceBudgetMonitor.setBudget('ERROR_BOUNDARY_TRIGGER', 5, 'error');
@@ -0,0 +1,37 @@
1
+
2
+ /**
3
+ * @file Permission type definitions and utilities
4
+ */
5
+
6
+ export enum PermissionType {
7
+ READ = 'read',
8
+ WRITE = 'write',
9
+ DELETE = 'delete',
10
+ ADMIN = 'admin'
11
+ }
12
+
13
+ /**
14
+ * Parse a permission string into components
15
+ */
16
+ export function parsePermission(permission: string): { resource: string; action: string } | null {
17
+ if (!permission || typeof permission !== 'string') {
18
+ return null;
19
+ }
20
+
21
+ const parts = permission.split(':');
22
+ if (parts.length !== 2) {
23
+ return null;
24
+ }
25
+
26
+ const [action, resource] = parts;
27
+
28
+ // Check that both action and resource exist and are not empty
29
+ if (action === undefined || resource === undefined || action === '' || resource === '') {
30
+ return null;
31
+ }
32
+
33
+ return {
34
+ resource,
35
+ action
36
+ };
37
+ }