crowi.ts 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. * @param isNewInstance - If true, creates a new Crowi instance instead of returning singleton
  35. * @returns Promise resolving to a Crowi instance
  36. */
  37. export async function getInstance(isNewInstance?: boolean): Promise<Crowi> {
  38. if (isNewInstance) {
  39. const crowi = new Crowi();
  40. await initCrowi(crowi);
  41. return crowi;
  42. }
  43. // Initialize singleton instance
  44. if (_instance == null) {
  45. _instance = new Crowi();
  46. await initCrowi(_instance);
  47. }
  48. return _instance;
  49. }
  50. /**
  51. * Reset the singleton instance.
  52. * Useful for test isolation when needed.
  53. */
  54. export function resetInstance(): void {
  55. _instance = null;
  56. }