utils.spec.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { describe, expect, it } from 'vitest';
  2. import { replaceMongoDbName } from './utils';
  3. describe('replaceMongoDbName', () => {
  4. describe('single-host URIs', () => {
  5. it('should replace database name in basic URI', () => {
  6. const result = replaceMongoDbName(
  7. 'mongodb://localhost:27017/growi_test',
  8. 'new_db',
  9. );
  10. expect(result).toBe('mongodb://localhost:27017/new_db');
  11. });
  12. it('should add database name when URI has no database', () => {
  13. const result = replaceMongoDbName('mongodb://localhost:27017', 'new_db');
  14. expect(result).toBe('mongodb://localhost:27017/new_db');
  15. });
  16. it('should add database name when URI ends with slash', () => {
  17. const result = replaceMongoDbName('mongodb://localhost:27017/', 'new_db');
  18. expect(result).toBe('mongodb://localhost:27017/new_db');
  19. });
  20. it('should preserve query parameters', () => {
  21. const result = replaceMongoDbName(
  22. 'mongodb://localhost:27017?param=value',
  23. 'new_db',
  24. );
  25. expect(result).toBe('mongodb://localhost:27017/new_db?param=value');
  26. });
  27. it('should replace database name and preserve query parameters', () => {
  28. const result = replaceMongoDbName(
  29. 'mongodb://localhost:27017/growi_test?param=value',
  30. 'new_db',
  31. );
  32. expect(result).toBe('mongodb://localhost:27017/new_db?param=value');
  33. });
  34. it('should handle authentication credentials', () => {
  35. const result = replaceMongoDbName(
  36. 'mongodb://user:pass@localhost:27017/growi_test',
  37. 'new_db',
  38. );
  39. expect(result).toBe('mongodb://user:pass@localhost:27017/new_db');
  40. });
  41. it('should handle authentication credentials with query parameters', () => {
  42. const result = replaceMongoDbName(
  43. 'mongodb://user:pass@localhost:27017/growi_test?authSource=admin',
  44. 'new_db',
  45. );
  46. expect(result).toBe(
  47. 'mongodb://user:pass@localhost:27017/new_db?authSource=admin',
  48. );
  49. });
  50. it('should handle URL-encoded credentials', () => {
  51. const result = replaceMongoDbName(
  52. 'mongodb://user%40name:p%40ss@localhost:27017/growi_test',
  53. 'new_db',
  54. );
  55. expect(result).toBe(
  56. 'mongodb://user%40name:p%40ss@localhost:27017/new_db',
  57. );
  58. });
  59. });
  60. describe('replica set URIs (multiple hosts)', () => {
  61. it('should replace database name in replica set URI', () => {
  62. const result = replaceMongoDbName(
  63. 'mongodb://host1:27017,host2:27017/growi_test?replicaSet=rs0',
  64. 'new_db',
  65. );
  66. expect(result).toBe(
  67. 'mongodb://host1:27017,host2:27017/new_db?replicaSet=rs0',
  68. );
  69. });
  70. it('should add database name to replica set URI without database', () => {
  71. const result = replaceMongoDbName(
  72. 'mongodb://host1:27017,host2:27017,host3:27017?replicaSet=rs0',
  73. 'new_db',
  74. );
  75. expect(result).toBe(
  76. 'mongodb://host1:27017,host2:27017,host3:27017/new_db?replicaSet=rs0',
  77. );
  78. });
  79. it('should handle replica set URI with authentication', () => {
  80. const result = replaceMongoDbName(
  81. 'mongodb://user:pass@host1:27017,host2:27017/growi_test?replicaSet=rs0',
  82. 'new_db',
  83. );
  84. expect(result).toBe(
  85. 'mongodb://user:pass@host1:27017,host2:27017/new_db?replicaSet=rs0',
  86. );
  87. });
  88. it('should handle replica set URI without query parameters', () => {
  89. const result = replaceMongoDbName(
  90. 'mongodb://host1:27017,host2:27017/growi_test',
  91. 'new_db',
  92. );
  93. expect(result).toBe('mongodb://host1:27017,host2:27017/new_db');
  94. });
  95. });
  96. describe('edge cases', () => {
  97. it('should handle different database names', () => {
  98. const result = replaceMongoDbName(
  99. 'mongodb://localhost:27017/growi_test',
  100. 'growi_test_1',
  101. );
  102. expect(result).toBe('mongodb://localhost:27017/growi_test_1');
  103. });
  104. it('should handle database names with underscores and numbers', () => {
  105. const result = replaceMongoDbName(
  106. 'mongodb://localhost:27017/old_db_123',
  107. 'new_db_456',
  108. );
  109. expect(result).toBe('mongodb://localhost:27017/new_db_456');
  110. });
  111. it('should preserve all query parameters', () => {
  112. const result = replaceMongoDbName(
  113. 'mongodb://localhost:27017/growi_test?authSource=admin&retryWrites=true&w=majority',
  114. 'new_db',
  115. );
  116. expect(result).toBe(
  117. 'mongodb://localhost:27017/new_db?authSource=admin&retryWrites=true&w=majority',
  118. );
  119. });
  120. });
  121. describe('error handling', () => {
  122. it('should throw error for invalid URI protocol', () => {
  123. // mongodb-connection-string-url validates protocol
  124. expect(() =>
  125. replaceMongoDbName('http://localhost:27017/db', 'new_db'),
  126. ).toThrow();
  127. });
  128. it('should throw error for malformed URI', () => {
  129. expect(() => replaceMongoDbName('not-a-uri', 'new_db')).toThrow();
  130. });
  131. });
  132. });