application-resource-attributes.spec.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { getApplicationResourceAttributes } from './application-resource-attributes';
  2. // Mock external dependencies
  3. vi.mock('~/utils/logger', () => ({
  4. default: () => ({
  5. info: vi.fn(),
  6. error: vi.fn(),
  7. }),
  8. }));
  9. // Mock growi-info service
  10. const mockGrowiInfoService = {
  11. getGrowiInfo: vi.fn(),
  12. };
  13. vi.mock('~/server/service/growi-info', () => ({
  14. growiInfoService: mockGrowiInfoService,
  15. }));
  16. describe('getApplicationResourceAttributes', () => {
  17. beforeEach(() => {
  18. vi.clearAllMocks();
  19. });
  20. it('should return complete application resource attributes when growi info is available', async() => {
  21. const mockGrowiInfo = {
  22. type: 'app',
  23. deploymentType: 'standalone',
  24. additionalInfo: {
  25. attachmentType: 'local',
  26. installedAt: new Date('2023-01-01T00:00:00.000Z'),
  27. installedAtByOldestUser: new Date('2023-01-01T00:00:00.000Z'),
  28. },
  29. };
  30. mockGrowiInfoService.getGrowiInfo.mockResolvedValue(mockGrowiInfo);
  31. const result = await getApplicationResourceAttributes();
  32. expect(result).toEqual({
  33. 'growi.service.type': 'app',
  34. 'growi.deployment.type': 'standalone',
  35. 'growi.attachment.type': 'local',
  36. 'growi.installedAt': '2023-01-01T00:00:00.000Z',
  37. 'growi.installedAt.by_oldest_user': '2023-01-01T00:00:00.000Z',
  38. });
  39. expect(mockGrowiInfoService.getGrowiInfo).toHaveBeenCalledWith({ includeInstalledInfo: true });
  40. });
  41. it('should handle missing additionalInfo gracefully', async() => {
  42. const mockGrowiInfo = {
  43. type: 'app',
  44. deploymentType: 'standalone',
  45. additionalInfo: undefined,
  46. };
  47. mockGrowiInfoService.getGrowiInfo.mockResolvedValue(mockGrowiInfo);
  48. const result = await getApplicationResourceAttributes();
  49. expect(result).toEqual({
  50. 'growi.service.type': 'app',
  51. 'growi.deployment.type': 'standalone',
  52. 'growi.attachment.type': undefined,
  53. 'growi.installedAt': undefined,
  54. 'growi.installedAt.by_oldest_user': undefined,
  55. });
  56. });
  57. it('should return empty object when growiInfoService throws error', async() => {
  58. mockGrowiInfoService.getGrowiInfo.mockRejectedValue(new Error('Service unavailable'));
  59. const result = await getApplicationResourceAttributes();
  60. expect(result).toEqual({});
  61. });
  62. it('should handle partial additionalInfo data', async() => {
  63. const mockGrowiInfo = {
  64. type: 'app',
  65. deploymentType: 'docker',
  66. additionalInfo: {
  67. attachmentType: 'gridfs',
  68. // Missing installedAt and installedAtByOldestUser
  69. },
  70. };
  71. mockGrowiInfoService.getGrowiInfo.mockResolvedValue(mockGrowiInfo);
  72. const result = await getApplicationResourceAttributes();
  73. expect(result).toEqual({
  74. 'growi.service.type': 'app',
  75. 'growi.deployment.type': 'docker',
  76. 'growi.attachment.type': 'gridfs',
  77. 'growi.installedAt': undefined,
  78. 'growi.installedAt.by_oldest_user': undefined,
  79. });
  80. });
  81. });