2
0

crowi.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { Server } from 'node:http';
  2. import Crowi from '~/server/crowi';
  3. import { setupModelsDependentOnCrowi } from '~/server/crowi/setup-models';
  4. let _instance: Crowi | null = null;
  5. /**
  6. * Initialize a Crowi instance with minimal required services for integration testing.
  7. * This is the Vitest equivalent of test/integration/setup-crowi.ts
  8. */
  9. const initCrowi = async (crowi: Crowi): Promise<void> => {
  10. // Setup models that depend on Crowi instance
  11. crowi.models = await setupModelsDependentOnCrowi(crowi);
  12. // Setup config manager
  13. await crowi.setupConfigManager();
  14. // Setup Socket.IO service with dummy server
  15. await crowi.setupSocketIoService();
  16. await crowi.socketIoService.attachServer(new Server());
  17. // Setup application
  18. await crowi.setUpApp();
  19. // Setup services required for most integration tests
  20. await Promise.all([
  21. crowi.setupPassport(),
  22. crowi.setupAttachmentService(),
  23. crowi.setUpAcl(),
  24. crowi.setupPageService(),
  25. crowi.setupInAppNotificationService(),
  26. crowi.setupActivityService(),
  27. crowi.setupUserGroupService(),
  28. ]);
  29. };
  30. /**
  31. * Get a Crowi instance for integration testing.
  32. * By default, returns a singleton instance. Pass true to create a new instance.
  33. *
  34. * @returns Promise resolving to a Crowi instance
  35. */
  36. export async function getInstance(): Promise<Crowi> {
  37. // Initialize singleton instance
  38. if (_instance == null) {
  39. _instance = new Crowi();
  40. await initCrowi(_instance);
  41. }
  42. return _instance;
  43. }
  44. /**
  45. * Reset the singleton instance.
  46. * Useful for test isolation when needed.
  47. */
  48. export function resetInstance(): void {
  49. _instance = null;
  50. }