@@ -262,4 +262,153 @@ describe('ConnectDBComponent', () => {
262262 } ,
263263 } ) ;
264264 } ) ;
265+
266+ describe ( 'Connection string functionality' , ( ) => {
267+ it ( 'should parse a PostgreSQL connection string and populate db fields' , ( ) => {
268+ component . connectionString = 'postgresql://myuser:mypass@db.example.com:5432/mydb' ;
269+ component . onConnectionStringChange ( null ) ;
270+
271+ expect ( component . db . type ) . toBe ( DBtype . Postgres ) ;
272+ expect ( component . db . host ) . toBe ( 'db.example.com' ) ;
273+ expect ( component . db . port ) . toBe ( '5432' ) ;
274+ expect ( component . db . username ) . toBe ( 'myuser' ) ;
275+ expect ( component . db . password ) . toBe ( 'mypass' ) ;
276+ expect ( component . db . database ) . toBe ( 'mydb' ) ;
277+ } ) ;
278+
279+ it ( 'should parse a MySQL connection string and populate db fields' , ( ) => {
280+ component . connectionString = 'mysql://root:secret@localhost:3306/app_db' ;
281+ component . onConnectionStringChange ( null ) ;
282+
283+ expect ( component . db . type ) . toBe ( DBtype . MySQL ) ;
284+ expect ( component . db . host ) . toBe ( 'localhost' ) ;
285+ expect ( component . db . port ) . toBe ( '3306' ) ;
286+ expect ( component . db . username ) . toBe ( 'root' ) ;
287+ expect ( component . db . password ) . toBe ( 'secret' ) ;
288+ expect ( component . db . database ) . toBe ( 'app_db' ) ;
289+ } ) ;
290+
291+ it ( 'should parse a MongoDB connection string with authSource option' , ( ) => {
292+ component . connectionString = 'mongodb://admin:pass123@mongo.host.com:27017/mydb?authSource=admin' ;
293+ component . onConnectionStringChange ( null ) ;
294+
295+ expect ( component . db . type ) . toBe ( DBtype . Mongo ) ;
296+ expect ( component . db . host ) . toBe ( 'mongo.host.com' ) ;
297+ expect ( component . db . port ) . toBe ( '27017' ) ;
298+ expect ( component . db . username ) . toBe ( 'admin' ) ;
299+ expect ( component . db . password ) . toBe ( 'pass123' ) ;
300+ expect ( component . db . database ) . toBe ( 'mydb' ) ;
301+ expect ( component . db . authSource ) . toBe ( 'admin' ) ;
302+ expect ( component . autoFilledFields . has ( 'authSource' ) ) . toBe ( true ) ;
303+ } ) ;
304+
305+ it ( 'should set autoFilledFields for all parsed fields' , ( ) => {
306+ component . connectionString = 'postgresql://user:pass@host:5432/db' ;
307+ component . onConnectionStringChange ( null ) ;
308+
309+ expect ( component . autoFilledFields . has ( 'host' ) ) . toBe ( true ) ;
310+ expect ( component . autoFilledFields . has ( 'port' ) ) . toBe ( true ) ;
311+ expect ( component . autoFilledFields . has ( 'username' ) ) . toBe ( true ) ;
312+ expect ( component . autoFilledFields . has ( 'password' ) ) . toBe ( true ) ;
313+ expect ( component . autoFilledFields . has ( 'database' ) ) . toBe ( true ) ;
314+ } ) ;
315+
316+ it ( 'should set ssl to true when sslmode=require is in connection string' , ( ) => {
317+ component . connectionString = 'postgresql://user:pass@host:5432/db?sslmode=require' ;
318+ component . onConnectionStringChange ( null ) ;
319+
320+ expect ( component . db . ssl ) . toBe ( true ) ;
321+ } ) ;
322+
323+ it ( 'should set schema when schema option is present' , ( ) => {
324+ component . connectionString = 'postgresql://user:pass@host:5432/db?schema=my_schema' ;
325+ component . onConnectionStringChange ( null ) ;
326+
327+ expect ( component . db . schema ) . toBe ( 'my_schema' ) ;
328+ expect ( component . autoFilledFields . has ( 'schema' ) ) . toBe ( true ) ;
329+ } ) ;
330+
331+ it ( 'should reset fieldsOverridden to false after parsing' , ( ) => {
332+ component . fieldsOverridden = true ;
333+ component . connectionString = 'postgresql://user:pass@host:5432/db' ;
334+ component . onConnectionStringChange ( null ) ;
335+
336+ expect ( component . fieldsOverridden ) . toBe ( false ) ;
337+ } ) ;
338+
339+ it ( 'should not modify db fields when connection string is empty' , ( ) => {
340+ const originalType = component . db . type ;
341+ const originalHost = component . db . host ;
342+
343+ component . connectionString = ' ' ;
344+ component . onConnectionStringChange ( null ) ;
345+
346+ expect ( component . db . type ) . toBe ( originalType ) ;
347+ expect ( component . db . host ) . toBe ( originalHost ) ;
348+ } ) ;
349+
350+ it ( 'should not modify db fields when connection string is invalid' , ( ) => {
351+ const originalType = component . db . type ;
352+ const originalHost = component . db . host ;
353+
354+ component . connectionString = 'not-a-valid-connection-string' ;
355+ component . onConnectionStringChange ( null ) ;
356+
357+ expect ( component . db . type ) . toBe ( originalType ) ;
358+ expect ( component . db . host ) . toBe ( originalHost ) ;
359+ } ) ;
360+
361+ it ( 'should use default port when port is not specified in connection string' , ( ) => {
362+ component . connectionString = 'postgresql://user:pass@host/db' ;
363+ component . onConnectionStringChange ( null ) ;
364+
365+ expect ( component . db . port ) . toBe ( '5432' ) ;
366+ } ) ;
367+
368+ it ( 'should handle URL-encoded username and password' , ( ) => {
369+ component . connectionString = 'postgresql://my%40user:p%40ss%23word@host:5432/db' ;
370+ component . onConnectionStringChange ( null ) ;
371+
372+ expect ( component . db . username ) . toBe ( 'my@user' ) ;
373+ expect ( component . db . password ) . toBe ( 'p@ss#word' ) ;
374+ } ) ;
375+
376+ it ( 'should clear the connection string after successful parsing' , ( ) => {
377+ vi . useFakeTimers ( ) ;
378+ component . connectionString = 'postgresql://user:pass@host:5432/db' ;
379+ component . onConnectionStringChange ( null ) ;
380+
381+ vi . advanceTimersByTime ( 300 ) ;
382+ expect ( component . connectionString ) . toBe ( '' ) ;
383+ vi . useRealTimers ( ) ;
384+ } ) ;
385+
386+ it ( 'should store parsed connection string in parsedConnectionString' , ( ) => {
387+ const connStr = 'postgresql://user:pass@host:5432/db' ;
388+ component . connectionString = connStr ;
389+ component . onConnectionStringChange ( null ) ;
390+
391+ expect ( component . parsedConnectionString ) . toBe ( connStr ) ;
392+ } ) ;
393+ } ) ;
394+
395+ describe ( 'clearAutoFilledField' , ( ) => {
396+ it ( 'should remove the field from autoFilledFields' , ( ) => {
397+ component . autoFilledFields = new Set ( [ 'host' , 'port' , 'username' ] ) ;
398+ component . clearAutoFilledField ( 'host' ) ;
399+
400+ expect ( component . autoFilledFields . has ( 'host' ) ) . toBe ( false ) ;
401+ expect ( component . autoFilledFields . has ( 'port' ) ) . toBe ( true ) ;
402+ expect ( component . autoFilledFields . has ( 'username' ) ) . toBe ( true ) ;
403+ } ) ;
404+
405+ it ( 'should set fieldsOverridden to true' , ( ) => {
406+ component . autoFilledFields = new Set ( [ 'host' ] ) ;
407+ component . fieldsOverridden = false ;
408+
409+ component . clearAutoFilledField ( 'host' ) ;
410+
411+ expect ( component . fieldsOverridden ) . toBe ( true ) ;
412+ } ) ;
413+ } ) ;
265414} ) ;
0 commit comments