11import { beforeEach , describe , expect , it , vi } from "vitest" ;
22import { sdkLabels } from "../../shared/src/sdk-types.js" ;
3+ import { createMockSpecGenSdkArtifactInfo } from "../../shared/test/sdk-types.js" ;
34import { LabelAction } from "../src/label.js" ;
45import {
56 getLabelAndActionImpl ,
@@ -12,9 +13,11 @@ vi.mock("../src/context.js", () => ({
1213 extractInputs : vi . fn ( ) ,
1314} ) ) ;
1415
16+ /** @type {import('vitest').Mock<(url: string) => Promise<Partial<Response>>> } */
1517const mockFetch = vi . fn ( ) ;
18+
1619// Mock global fetch
17- global . fetch = mockFetch ;
20+ global . fetch = /** @type { import('vitest').MockedFunction<typeof fetch> } */ ( mockFetch ) ;
1821
1922const mockGithub = createMockGithub ( ) ;
2023const mockContext = createMockContext ( ) ;
@@ -64,20 +67,22 @@ describe("sdk-breaking-change-labels", () => {
6467 const mockContentResponse = {
6568 ok : true ,
6669 text : vi . fn ( ) . mockResolvedValue (
67- JSON . stringify ( {
68- labelAction : true ,
69- language,
70- prNumber : "123" ,
71- } ) ,
70+ JSON . stringify (
71+ createMockSpecGenSdkArtifactInfo ( {
72+ labelAction : true ,
73+ language,
74+ prNumber : "123" ,
75+ } ) ,
76+ ) ,
7277 ) ,
7378 } ;
7479
7580 // Setup fetch to return different responses for each call
7681 mockFetch . mockImplementation ( ( url ) => {
7782 if ( url . includes ( "artifacts?artifactName=" ) ) {
78- return mockArtifactResponse ;
83+ return Promise . resolve ( mockArtifactResponse ) ;
7984 } else {
80- return mockContentResponse ;
85+ return Promise . resolve ( mockContentResponse ) ;
8186 }
8287 } ) ;
8388
@@ -90,6 +95,7 @@ describe("sdk-breaking-change-labels", () => {
9095
9196 // Verify result
9297 expect ( result ) . toEqual ( {
98+ headSha : "abc123" ,
9399 labelName : sdkLabels [ language ] . breakingChange ,
94100 labelAction : LabelAction . Add ,
95101 issueNumber : 123 ,
@@ -119,21 +125,21 @@ describe("sdk-breaking-change-labels", () => {
119125 const language = "azure-sdk-for-js" ;
120126 const mockContentResponse = {
121127 ok : true ,
122- text : vi . fn ( ) . mockResolvedValue (
123- JSON . stringify ( {
124- labelAction : false ,
125- language ,
126- prNumber : "123" ,
127- } ) ,
128- ) ,
128+ text : vi
129+ . fn ( )
130+ . mockResolvedValue (
131+ JSON . stringify (
132+ createMockSpecGenSdkArtifactInfo ( { labelAction : false , language , prNumber : "123" } ) ,
133+ ) ,
134+ ) ,
129135 } ;
130136
131137 // Setup fetch to return different responses for each call
132138 mockFetch . mockImplementation ( ( url ) => {
133139 if ( url . includes ( "artifacts?artifactName=" ) ) {
134- return mockArtifactResponse ;
140+ return Promise . resolve ( mockArtifactResponse ) ;
135141 } else {
136- return mockContentResponse ;
142+ return Promise . resolve ( mockContentResponse ) ;
137143 }
138144 } ) ;
139145
@@ -146,6 +152,7 @@ describe("sdk-breaking-change-labels", () => {
146152
147153 // Verify result has Remove action
148154 expect ( result ) . toEqual ( {
155+ headSha : "abc123" ,
149156 labelName : sdkLabels [ language ] . breakingChange ,
150157 labelAction : LabelAction . Remove ,
151158 issueNumber : 123 ,
@@ -176,20 +183,22 @@ describe("sdk-breaking-change-labels", () => {
176183 const mockContentResponse = {
177184 ok : true ,
178185 text : vi . fn ( ) . mockResolvedValue (
179- JSON . stringify ( {
180- labelAction : false ,
181- language,
182- prNumber : "123" ,
183- } ) ,
186+ JSON . stringify (
187+ createMockSpecGenSdkArtifactInfo ( {
188+ labelAction : false ,
189+ language,
190+ prNumber : "123" ,
191+ } ) ,
192+ ) ,
184193 ) ,
185194 } ;
186195
187196 // Setup fetch to return different responses for each call
188197 mockFetch . mockImplementation ( ( url ) => {
189198 if ( url . includes ( "artifacts?artifactName=" ) ) {
190- return mockArtifactResponse ;
199+ return Promise . resolve ( mockArtifactResponse ) ;
191200 } else {
192- return mockContentResponse ;
201+ return Promise . resolve ( mockContentResponse ) ;
193202 }
194203 } ) ;
195204
@@ -202,6 +211,7 @@ describe("sdk-breaking-change-labels", () => {
202211
203212 // Verify result has none action
204213 expect ( result ) . toEqual ( {
214+ headSha : "abc123" ,
205215 labelName : sdkLabels [ language ] . breakingChange ,
206216 labelAction : LabelAction . None ,
207217 issueNumber : 123 ,
@@ -343,11 +353,7 @@ describe("sdk-breaking-change-labels", () => {
343353 } ;
344354
345355 // Setup fetch to return different responses for each call
346- mockFetch . mockImplementation ( ( url ) => {
347- if ( url . includes ( "artifacts?artifactName=" ) ) {
348- return mockArtifactResponse ;
349- }
350- } ) ;
356+ mockFetch . mockResolvedValue ( mockArtifactResponse ) ;
351357
352358 // Call function and expect it to throw
353359 await expect (
@@ -374,11 +380,7 @@ describe("sdk-breaking-change-labels", () => {
374380 } ;
375381
376382 // Setup fetch to return different responses for each call
377- mockFetch . mockImplementation ( ( url ) => {
378- if ( url . includes ( "artifacts?artifactName=" ) ) {
379- return mockArtifactResponse ;
380- }
381- } ) ;
383+ mockFetch . mockResolvedValue ( mockArtifactResponse ) ;
382384
383385 // Call function and expect it to throw
384386 await expect (
@@ -419,9 +421,9 @@ describe("sdk-breaking-change-labels", () => {
419421 // Setup fetch to return different responses for each call
420422 mockFetch . mockImplementation ( ( url ) => {
421423 if ( url . includes ( "artifacts?artifactName=" ) ) {
422- return mockArtifactResponse ;
424+ return Promise . resolve ( mockArtifactResponse ) ;
423425 } else {
424- return mockContentResponse ;
426+ return Promise . resolve ( mockContentResponse ) ;
425427 }
426428 } ) ;
427429
0 commit comments