1+ import { extractPort } from '../extractPort'
2+ import { Result } from '../result'
3+
4+ describe ( 'extractPort' , ( ) => {
5+ it ( 'should extract the port number from a localhost URL with port' , ( ) => {
6+ const uri = 'http://localhost:3000'
7+ const result = extractPort ( uri )
8+
9+ expect ( Result . isSuccess ( result ) ) . toBe ( true )
10+ if ( Result . isSuccess ( result ) ) {
11+ expect ( result . value ) . toBe ( 3000 )
12+ }
13+ } )
14+
15+ it ( 'should return undefined for a localhost URL without port' , ( ) => {
16+ const uri = 'http://localhost'
17+ const result = extractPort ( uri )
18+
19+ expect ( Result . isSuccess ( result ) ) . toBe ( true )
20+ if ( Result . isSuccess ( result ) ) {
21+ expect ( result . value ) . toBeUndefined ( )
22+ }
23+ } )
24+
25+ it ( 'should fail for a non-localhost URL' , ( ) => {
26+ const uri = 'http://example.com:3000'
27+ const result = extractPort ( uri )
28+
29+ expect ( Result . isFailure ( result ) ) . toBe ( true )
30+ if ( Result . isFailure ( result ) ) {
31+ expect ( result . error . message ) . toBe ( 'Invalid URL format' )
32+ }
33+ } )
34+
35+ it ( 'should fail for a localhost URL with an invalid port number' , ( ) => {
36+ const uri = 'http://localhost:99999'
37+ const result = extractPort ( uri )
38+
39+ expect ( Result . isFailure ( result ) ) . toBe ( true )
40+ if ( Result . isFailure ( result ) ) {
41+ expect ( result . error . message ) . toBe ( 'Not a valid port: 99999' )
42+ }
43+ } )
44+
45+ it ( 'should fail for a localhost URL with a negative port number' , ( ) => {
46+ const uri = 'http://localhost:-1'
47+ const result = extractPort ( uri )
48+
49+ expect ( Result . isFailure ( result ) ) . toBe ( true )
50+ if ( Result . isFailure ( result ) ) {
51+ expect ( result . error . message ) . toBe ( 'Invalid URL format' )
52+ }
53+ } )
54+
55+ it ( 'should fail for a localhost URL with a non-numeric port' , ( ) => {
56+ const uri = 'http://localhost:abc'
57+ const result = extractPort ( uri )
58+
59+ expect ( Result . isFailure ( result ) ) . toBe ( true )
60+ if ( Result . isFailure ( result ) ) {
61+ expect ( result . error . message ) . toBe ( 'Invalid URL format' )
62+ }
63+ } )
64+
65+ it ( 'should fail for URLs with incorrect protocol' , ( ) => {
66+ const uri = 'https://localhost:3000'
67+ const result = extractPort ( uri )
68+
69+ expect ( Result . isFailure ( result ) ) . toBe ( true )
70+ if ( Result . isFailure ( result ) ) {
71+ expect ( result . error . message ) . toBe ( 'Invalid URL format' )
72+ }
73+ } )
74+
75+ it ( 'should fail for non-URL strings' , ( ) => {
76+ const uri = 'not a url'
77+ const result = extractPort ( uri )
78+
79+ expect ( Result . isFailure ( result ) ) . toBe ( true )
80+ if ( Result . isFailure ( result ) ) {
81+ expect ( result . error . message ) . toBe ( 'Invalid URL format' )
82+ }
83+ } )
84+
85+ it ( 'should handle boundary value 0 for port' , ( ) => {
86+ const uri = 'http://localhost:0'
87+ const result = extractPort ( uri )
88+
89+ expect ( Result . isSuccess ( result ) ) . toBe ( true )
90+ if ( Result . isSuccess ( result ) ) {
91+ expect ( result . value ) . toBe ( 0 )
92+ }
93+ } )
94+
95+ it ( 'should handle boundary value 65535 for port' , ( ) => {
96+ const uri = 'http://localhost:65535'
97+ const result = extractPort ( uri )
98+
99+ expect ( Result . isSuccess ( result ) ) . toBe ( true )
100+ if ( Result . isSuccess ( result ) ) {
101+ expect ( result . value ) . toBe ( 65535 )
102+ }
103+ } )
104+ } )
0 commit comments