1+ import { jest } from '@jest/globals' ;
2+ import {
3+ gitUrlToGithubUrl ,
4+ extractPrNumber ,
5+ getLagoonInstances ,
6+ getProjectsWithDetails ,
7+ getEnvironments ,
8+ getUsers ,
9+ clearDrupalCache ,
10+ generateLoginLink ,
11+ deleteEnvironment ,
12+ deployBranch ,
13+ getGitBranches
14+ } from './lagoon-api.mjs' ;
15+
16+ // Mock the execCommand function
17+ jest . unstable_mockModule ( './command/index.mjs' , ( ) => ( {
18+ LagoonCommand : jest . fn ( ) . mockImplementation ( ( ) => ( {
19+ withInstance : jest . fn ( ) . mockReturnThis ( ) ,
20+ withProject : jest . fn ( ) . mockReturnThis ( ) ,
21+ withEnvironment : jest . fn ( ) . mockReturnThis ( ) ,
22+ withJsonOutput : jest . fn ( ) . mockReturnThis ( ) ,
23+ withForce : jest . fn ( ) . mockReturnThis ( ) ,
24+ listConfigs : jest . fn ( ) . mockReturnThis ( ) ,
25+ listProjects : jest . fn ( ) . mockReturnThis ( ) ,
26+ listEnvironments : jest . fn ( ) . mockReturnThis ( ) ,
27+ listUsers : jest . fn ( ) . mockReturnThis ( ) ,
28+ deleteEnvironment : jest . fn ( ) . mockReturnThis ( ) ,
29+ deployBranch : jest . fn ( ) . mockReturnThis ( ) ,
30+ login : jest . fn ( ) . mockReturnThis ( ) ,
31+ ssh : jest . fn ( ) . mockReturnThis ( ) ,
32+ getArgs : jest . fn ( ) . mockReturnValue ( [ ] ) ,
33+ getBaseCommand : jest . fn ( ) . mockReturnValue ( 'lagoon' ) ,
34+ getCommandArray : jest . fn ( ) . mockReturnValue ( [ 'lagoon' ] ) ,
35+ toString : jest . fn ( ) . mockReturnValue ( 'lagoon' )
36+ } ) ) ,
37+ GitCommand : jest . fn ( ) . mockImplementation ( ( ) => ( {
38+ lsRemote : jest . fn ( ) . mockReturnThis ( ) ,
39+ getArgs : jest . fn ( ) . mockReturnValue ( [ ] ) ,
40+ getBaseCommand : jest . fn ( ) . mockReturnValue ( 'git' ) ,
41+ getCommandArray : jest . fn ( ) . mockReturnValue ( [ 'git' ] ) ,
42+ toString : jest . fn ( ) . mockReturnValue ( 'git ls-remote' )
43+ } ) ) ,
44+ LagoonExecutor : jest . fn ( ) . mockImplementation ( ( ) => ( {
45+ execute : jest . fn ( )
46+ } ) )
47+ } ) ) ;
48+
49+ // Mock the execCommand function
50+ jest . mock ( './lagoon-api.mjs' , ( ) => {
51+ const originalModule = jest . requireActual ( './lagoon-api.mjs' ) ;
52+
53+ // Only mock the async API functions, keep the helper functions as is
54+ return {
55+ ...originalModule ,
56+ execCommand : jest . fn ( )
57+ } ;
58+ } , { virtual : true } ) ;
59+
60+ // Unit tests for pure helper functions
61+ describe ( 'Helper Functions' , ( ) => {
62+ describe ( 'gitUrlToGithubUrl' , ( ) => {
63+ test ( 'should convert SSH GitHub URL to HTTPS URL' , ( ) => {
64+ const sshUrl = 'git@github.com:richardgaunt/lagoon-cli-wrapper.git' ;
65+ expect ( gitUrlToGithubUrl ( sshUrl ) ) . toBe ( 'https://github.com/richardgaunt/lagoon-cli-wrapper' ) ;
66+ } ) ;
67+
68+ test ( 'should clean HTTPS GitHub URL' , ( ) => {
69+ const httpsUrl = 'https://github.com/richardgaunt/lagoon-cli-wrapper.git' ;
70+ expect ( gitUrlToGithubUrl ( httpsUrl ) ) . toBe ( 'https://github.com/richardgaunt/lagoon-cli-wrapper' ) ;
71+ } ) ;
72+
73+ test ( 'should return null for non-GitHub URLs' , ( ) => {
74+ const nonGithubUrl = 'https://gitlab.com/some/project.git' ;
75+ expect ( gitUrlToGithubUrl ( nonGithubUrl ) ) . toBeNull ( ) ;
76+ } ) ;
77+
78+ // This test uses a spied/mocked version of the function
79+ test ( 'should handle URLs with github.com in them' , ( ) => {
80+ // Create a URL that definitely contains 'github.com'
81+ const githubUrl = 'https://github.com/user/repo.git' ;
82+ expect ( gitUrlToGithubUrl ( githubUrl ) ) . toBe ( 'https://github.com/user/repo' ) ;
83+ } ) ;
84+ } ) ;
85+
86+ describe ( 'extractPrNumber' , ( ) => {
87+ test ( 'should extract PR number from environment name' , ( ) => {
88+ expect ( extractPrNumber ( 'pr-123' ) ) . toBe ( '123' ) ;
89+ } ) ;
90+
91+ test ( 'should be case insensitive' , ( ) => {
92+ expect ( extractPrNumber ( 'PR-456' ) ) . toBe ( '456' ) ;
93+ } ) ;
94+
95+ test ( 'should return null for non-PR environment names' , ( ) => {
96+ expect ( extractPrNumber ( 'develop' ) ) . toBeNull ( ) ;
97+ expect ( extractPrNumber ( 'feature-123' ) ) . toBeNull ( ) ;
98+ expect ( extractPrNumber ( 'master' ) ) . toBeNull ( ) ;
99+ expect ( extractPrNumber ( 'pr123' ) ) . toBeNull ( ) ; // Missing hyphen
100+ } ) ;
101+
102+ test ( 'should handle multi-digit PR numbers' , ( ) => {
103+ expect ( extractPrNumber ( 'pr-1' ) ) . toBe ( '1' ) ;
104+ expect ( extractPrNumber ( 'pr-9999' ) ) . toBe ( '9999' ) ;
105+ } ) ;
106+ } ) ;
107+ } ) ;
0 commit comments