11import { describe , it , expect , vi , beforeEach , afterEach } from 'vitest' ;
22
33// Set env var before importing
4- process . env . JIRA_TOKEN = 'test-token' ;
4+ process . env . JIRA_READ_AUTH = 'test-token' ;
5+ process . env . JIRA_BASE_URL = 'https://api.atlassian.com/ex/jira/cloud-123' ;
56
6- import { extractJQL , jiraAPI , searchIssues , getIssue , formatIssue , main } from '../jira-api.js' ;
7+ import { extractJQL , jiraAPI , searchIssues , getIssue , formatIssue , getJiraApiBaseUrl , main } from '../jira-api.js' ;
78import readline from 'readline' ;
89
910// Mock fetch
@@ -51,9 +52,9 @@ describe('jiraAPI', () => {
5152 process . env = originalEnv ;
5253 } ) ;
5354
54- it ( 'should throw error if JIRA_TOKEN is not set' , async ( ) => {
55- delete process . env . JIRA_TOKEN ;
56- await expect ( jiraAPI ( '/test' ) ) . rejects . toThrow ( 'JIRA_TOKEN must be set' ) ;
55+ it ( 'should throw error if JIRA_READ_AUTH is not set' , async ( ) => {
56+ delete process . env . JIRA_READ_AUTH ;
57+ await expect ( jiraAPI ( '/test' ) ) . rejects . toThrow ( 'JIRA_READ_AUTH must be set' ) ;
5758 } ) ;
5859
5960 it ( 'should handle 401 Unauthorized error' , async ( ) => {
@@ -86,11 +87,44 @@ describe('jiraAPI', () => {
8687 const result = await jiraAPI ( '/test' ) ;
8788 expect ( result ) . toEqual ( mockData ) ;
8889 } ) ;
90+
91+ it ( 'should throw error if JIRA_BASE_URL is not set' , async ( ) => {
92+ delete process . env . JIRA_BASE_URL ;
93+ global . fetch . mockResolvedValue ( {
94+ ok : true ,
95+ json : vi . fn ( ) . mockResolvedValue ( { } )
96+ } ) ;
97+
98+ await expect ( jiraAPI ( '/test' ) ) . rejects . toThrow ( 'JIRA_BASE_URL must be set' ) ;
99+ expect ( global . fetch ) . not . toHaveBeenCalled ( ) ;
100+ } ) ;
101+
102+ it ( 'should use JIRA_BASE_URL for scoped API token requests' , async ( ) => {
103+ global . fetch . mockResolvedValue ( {
104+ ok : true ,
105+ json : vi . fn ( ) . mockResolvedValue ( { } )
106+ } ) ;
107+
108+ await jiraAPI ( '/test' ) ;
109+
110+ expect ( global . fetch . mock . calls [ 0 ] [ 0 ] ) . toBe ( 'https://api.atlassian.com/ex/jira/cloud-123/rest/api/3/test' ) ;
111+ } ) ;
112+
113+ it ( 'should remove trailing slash from JIRA_BASE_URL' , ( ) => {
114+ process . env . JIRA_BASE_URL = 'https://api.atlassian.com/ex/jira/cloud-123/' ;
115+ expect ( getJiraApiBaseUrl ( ) ) . toBe ( 'https://api.atlassian.com/ex/jira/cloud-123' ) ;
116+ } ) ;
117+
118+ it ( 'should require JIRA_BASE_URL when building Jira API base URL' , ( ) => {
119+ delete process . env . JIRA_BASE_URL ;
120+ expect ( ( ) => getJiraApiBaseUrl ( ) ) . toThrow ( 'JIRA_BASE_URL must be set' ) ;
121+ } ) ;
89122} ) ;
90123
91124describe ( 'searchIssues' , ( ) => {
92125 beforeEach ( ( ) => {
93126 global . fetch . mockClear ( ) ;
127+ process . env . JIRA_BASE_URL = 'https://api.atlassian.com/ex/jira/cloud-123' ;
94128 } ) ;
95129
96130 it ( 'should call jiraAPI with correct parameters' , async ( ) => {
@@ -110,6 +144,26 @@ describe('searchIssues', () => {
110144 } ) ;
111145} ) ;
112146
147+ describe ( 'getIssue' , ( ) => {
148+ beforeEach ( ( ) => {
149+ global . fetch . mockClear ( ) ;
150+ process . env . JIRA_BASE_URL = 'https://api.atlassian.com/ex/jira/cloud-123' ;
151+ } ) ;
152+
153+ it ( 'should request only fields needed for branch suggestions' , async ( ) => {
154+ global . fetch . mockResolvedValue ( {
155+ ok : true ,
156+ json : vi . fn ( ) . mockResolvedValue ( { key : 'TT-123' } )
157+ } ) ;
158+
159+ await getIssue ( 'TT-123' ) ;
160+
161+ const url = global . fetch . mock . calls [ 0 ] [ 0 ] ;
162+ expect ( url ) . toContain ( '/issue/TT-123?' ) ;
163+ expect ( url ) . toContain ( 'fields=summary,priority,issuetype,fixVersions' ) ;
164+ } ) ;
165+ } ) ;
166+
113167describe ( 'formatIssue' , ( ) => {
114168 it ( 'should format issue correctly' , ( ) => {
115169 const issue = {
@@ -243,4 +297,4 @@ describe('main execution', () => {
243297 expect ( global . fetch ) . toHaveBeenCalledTimes ( 1 ) ;
244298 expect ( consoleLogMock ) . toHaveBeenCalledWith ( expect . stringContaining ( 'Displayed 50 of 100 total issues' ) ) ;
245299 } ) ;
246- } ) ;
300+ } ) ;
0 commit comments