@@ -12,6 +12,15 @@ import { RequestError } from "@octokit/request-error";
1212vi . mock ( "octokit" ) ;
1313vi . mock ( "../../../src/api/utils.js" ) ;
1414
15+ // Helper to create an async generator from an array of pages
16+ function createAsyncIterator < T > ( pages : T [ ] ) {
17+ return ( async function * ( ) {
18+ for ( const page of pages ) {
19+ yield page ;
20+ }
21+ } ) ( ) ;
22+ }
23+
1524describe ( "createGithubTeam" , ( ) => {
1625 const mockLogger = {
1726 info : vi . fn ( ( ) => { } ) ,
@@ -23,7 +32,7 @@ describe("createGithubTeam", () => {
2332 auth : {
2433 appId : 1 ,
2534 installationId : 1 ,
26- privateKey : "abc"
35+ privateKey : "abc" ,
2736 } ,
2837 orgId : "test-org" ,
2938 parentTeamId : 123 ,
@@ -39,8 +48,13 @@ describe("createGithubTeam", () => {
3948 vi . clearAllMocks ( ) ;
4049 mockOctokit = {
4150 request : vi . fn ( ( ) => { } ) ,
51+ paginate : {
52+ iterator : vi . fn ( ) ,
53+ } ,
4254 } ;
43- ( Octokit as any ) . mockImplementation ( function ( ) { return mockOctokit ; } ) ;
55+ ( Octokit as any ) . mockImplementation ( function ( ) {
56+ return mockOctokit ;
57+ } ) ;
4458 } ) ;
4559
4660 afterEach ( ( ) => {
@@ -49,26 +63,32 @@ describe("createGithubTeam", () => {
4963
5064 it ( "should return existing team ID if team already exists" , async ( ) => {
5165 const existingTeamId = 456 ;
52- mockOctokit . request . mockResolvedValueOnce ( {
53- data : [
54- { name : "Other Team" , id : 789 } ,
55- { name : "Test Team" , id : existingTeamId } ,
56- ] ,
57- } ) ;
66+ mockOctokit . paginate . iterator . mockReturnValue (
67+ createAsyncIterator ( [
68+ {
69+ data : [
70+ { name : "Other Team" , id : 789 } ,
71+ { name : "Test Team" , id : existingTeamId } ,
72+ ] ,
73+ } ,
74+ ] )
75+ ) ;
5876
5977 const result = await createGithubTeam ( defaultInputs ) ;
6078
6179 expect ( result ) . toStrictEqual ( { updated : false , id : existingTeamId } ) ;
6280 expect ( mockLogger . info ) . toHaveBeenCalledWith (
6381 `Team "Test Team" already exists with id: ${ existingTeamId } `
6482 ) ;
65- expect ( mockOctokit . request ) . toHaveBeenCalledTimes ( 1 ) ;
83+ expect ( mockOctokit . request ) . not . toHaveBeenCalled ( ) ;
6684 } ) ;
6785
6886 it ( "should create new team" , async ( ) => {
6987 const newTeamId = 999 ;
7088 // Mock getting teams (no existing team)
71- mockOctokit . request . mockResolvedValueOnce ( { data : [ ] } ) ;
89+ mockOctokit . paginate . iterator . mockReturnValue (
90+ createAsyncIterator ( [ { data : [ ] } ] )
91+ ) ;
7292
7393 // Mock creating team
7494 mockOctokit . request . mockResolvedValueOnce ( {
@@ -100,7 +120,9 @@ describe("createGithubTeam", () => {
100120 const inputsWithoutDescription = { ...defaultInputs } ;
101121 delete ( inputsWithoutDescription as any ) . description ;
102122
103- mockOctokit . request . mockResolvedValueOnce ( { data : [ ] } ) ;
123+ mockOctokit . paginate . iterator . mockReturnValue (
124+ createAsyncIterator ( [ { data : [ ] } ] )
125+ ) ;
104126 mockOctokit . request . mockResolvedValueOnce ( {
105127 status : 201 ,
106128 data : { id : newTeamId , slug : "test-team" } ,
@@ -123,7 +145,9 @@ describe("createGithubTeam", () => {
123145 const inputsWithoutPrivacy = { ...defaultInputs } ;
124146 delete ( inputsWithoutPrivacy as any ) . privacy ;
125147
126- mockOctokit . request . mockResolvedValueOnce ( { data : [ ] } ) ;
148+ mockOctokit . paginate . iterator . mockReturnValue (
149+ createAsyncIterator ( [ { data : [ ] } ] )
150+ ) ;
127151 mockOctokit . request . mockResolvedValueOnce ( {
128152 status : 201 ,
129153 data : { id : newTeamId , slug : "test-team" } ,
@@ -142,7 +166,9 @@ describe("createGithubTeam", () => {
142166 } ) ;
143167
144168 it ( "should throw GithubError if team creation fails with non-201 status" , async ( ) => {
145- mockOctokit . request . mockResolvedValueOnce ( { data : [ ] } ) ;
169+ mockOctokit . paginate . iterator . mockReturnValue (
170+ createAsyncIterator ( [ { data : [ ] } ] )
171+ ) ;
146172 mockOctokit . request . mockResolvedValueOnce ( {
147173 status : 400 ,
148174 data : { message : "Bad request" } ,
@@ -153,14 +179,26 @@ describe("createGithubTeam", () => {
153179 } ) ;
154180
155181 it ( "should rethrow BaseError instances" , async ( ) => {
156- const baseError = new GithubError ( { message : "Failed to create GitHub team." } ) ;
157- mockOctokit . request . mockRejectedValueOnce ( baseError ) ;
182+ const baseError = new GithubError ( {
183+ message : "Failed to create GitHub team." ,
184+ } ) ;
185+ mockOctokit . paginate . iterator . mockReturnValue (
186+ ( async function * ( ) {
187+ yield { data : [ ] } ;
188+ throw baseError ;
189+ } ) ( )
190+ ) ;
158191
159192 await expect ( createGithubTeam ( defaultInputs ) ) . rejects . toThrow ( baseError ) ;
160193 } ) ;
161194
162195 it ( "should wrap non-BaseError exceptions in GithubError" , async ( ) => {
163- mockOctokit . request . mockRejectedValueOnce ( new Error ( "Unknown error" ) ) ;
196+ mockOctokit . paginate . iterator . mockReturnValue (
197+ ( async function * ( ) {
198+ yield { data : [ ] } ;
199+ throw new Error ( "Unknown error" ) ;
200+ } ) ( )
201+ ) ;
164202
165203 await expect ( createGithubTeam ( defaultInputs ) ) . rejects . toThrow ( GithubError ) ;
166204 expect ( mockLogger . error ) . toHaveBeenCalledWith (
@@ -180,7 +218,7 @@ describe("assignIdpGroupsToTeam", () => {
180218 auth : {
181219 appId : 1 ,
182220 installationId : 1 ,
183- privateKey : "abc"
221+ privateKey : "abc" ,
184222 } ,
185223 teamId : 123 ,
186224 groupsToSync : [ "group-1" , "group-2" ] ,
@@ -195,8 +233,13 @@ describe("assignIdpGroupsToTeam", () => {
195233 vi . clearAllMocks ( ) ;
196234 mockOctokit = {
197235 request : vi . fn ( ( ) => { } ) ,
236+ paginate : {
237+ iterator : vi . fn ( ) ,
238+ } ,
198239 } ;
199- ( Octokit as any ) . mockImplementation ( function ( ) { return mockOctokit ; } ) ;
240+ ( Octokit as any ) . mockImplementation ( function ( ) {
241+ return mockOctokit ;
242+ } ) ;
200243 ( utils . sleep as any ) . mockResolvedValue ( undefined ) ;
201244 } ) ;
202245
@@ -349,7 +392,9 @@ describe("assignIdpGroupsToTeam", () => {
349392 } ) ;
350393
351394 it ( "should rethrow BaseError instances" , async ( ) => {
352- const baseError = new GithubError ( { message : "Failed to assign IdP groups to team 123" } ) ;
395+ const baseError = new GithubError ( {
396+ message : "Failed to assign IdP groups to team 123" ,
397+ } ) ;
353398 mockOctokit . request . mockRejectedValueOnce ( baseError ) ;
354399
355400 await expect ( assignIdpGroupsToTeam ( defaultInputs ) ) . rejects . toThrow (
@@ -392,7 +437,9 @@ describe("assignIdpGroupsToTeam", () => {
392437 expect ( mockLogger . warn ) . toHaveBeenCalledWith (
393438 expect . stringContaining ( "Team sync is not available for team 123" )
394439 ) ;
395- expect ( mockLogger . warn ) . toHaveBeenCalledWith ( "Skipping IdP group assignment" ) ;
440+ expect ( mockLogger . warn ) . toHaveBeenCalledWith (
441+ "Skipping IdP group assignment"
442+ ) ;
396443 // Should not attempt to search for groups or patch
397444 expect ( mockOctokit . request ) . toHaveBeenCalledTimes ( 1 ) ; // Only sync check
398445 } ) ;
0 commit comments