1
+ import { getFailuresInLastRun } from '../../src/tools/observability' ;
2
+ import { getLatestO11YBuildInfo } from '../../src/lib/api' ;
3
+ import { beforeEach , it , expect , describe , vi , Mock } from 'vitest'
4
+
5
+ // Mock the API module
6
+ vi . mock ( '../../src/lib/api' , ( ) => ( {
7
+ getLatestO11YBuildInfo : vi . fn ( ) ,
8
+ } ) ) ;
9
+
10
+ vi . mock ( '../../src/lib/instrumentation' , ( ) => ( {
11
+ trackMCP : vi . fn ( )
12
+ } ) ) ;
13
+
14
+ describe ( 'getFailuresInLastRun' , ( ) => {
15
+ beforeEach ( ( ) => {
16
+ vi . clearAllMocks ( ) ;
17
+ } ) ;
18
+
19
+ const validBuildData = {
20
+ observability_url : 'https://observability.browserstack.com/123' ,
21
+ unique_errors : {
22
+ overview : {
23
+ insight : 'Test insight message'
24
+ } ,
25
+ top_unique_errors : [
26
+ { error : 'Error 1' } ,
27
+ { error : 'Error 2' }
28
+ ]
29
+ }
30
+ } ;
31
+
32
+ it ( 'should successfully retrieve failures for a valid build' , async ( ) => {
33
+ ( getLatestO11YBuildInfo as Mock ) . mockResolvedValue ( validBuildData ) ;
34
+
35
+ const result = await getFailuresInLastRun ( 'test-build' , 'test-project' ) ;
36
+
37
+ expect ( getLatestO11YBuildInfo ) . toHaveBeenCalledWith ( 'test-build' , 'test-project' ) ;
38
+ expect ( result . content [ 0 ] . text ) . toContain ( 'https://observability.browserstack.com/123' ) ;
39
+ expect ( result . content [ 0 ] . text ) . toContain ( 'Test insight message' ) ;
40
+ expect ( result . content [ 0 ] . text ) . toContain ( 'Error 1' ) ;
41
+ expect ( result . content [ 0 ] . text ) . toContain ( 'Error 2' ) ;
42
+ } ) ;
43
+
44
+ it ( 'should handle missing observability URL' , async ( ) => {
45
+ ( getLatestO11YBuildInfo as Mock ) . mockResolvedValue ( {
46
+ ...validBuildData ,
47
+ observability_url : null
48
+ } ) ;
49
+
50
+ await expect ( getFailuresInLastRun ( 'test-build' , 'test-project' ) )
51
+ . rejects . toThrow ( 'No observability URL found in build data' ) ;
52
+ } ) ;
53
+
54
+ it ( 'should handle missing overview insight' , async ( ) => {
55
+ ( getLatestO11YBuildInfo as Mock ) . mockResolvedValue ( {
56
+ ...validBuildData ,
57
+ unique_errors : {
58
+ ...validBuildData . unique_errors ,
59
+ overview : { }
60
+ }
61
+ } ) ;
62
+
63
+ const result = await getFailuresInLastRun ( 'test-build' , 'test-project' ) ;
64
+ expect ( result . content [ 0 ] . text ) . toContain ( 'No overview available' ) ;
65
+ } ) ;
66
+
67
+ it ( 'should handle missing error details' , async ( ) => {
68
+ ( getLatestO11YBuildInfo as Mock ) . mockResolvedValue ( {
69
+ ...validBuildData ,
70
+ unique_errors : {
71
+ ...validBuildData . unique_errors ,
72
+ top_unique_errors : [ ]
73
+ }
74
+ } ) ;
75
+
76
+ const result = await getFailuresInLastRun ( 'test-build' , 'test-project' ) ;
77
+ expect ( result . content [ 0 ] . text ) . toContain ( 'No error details available' ) ;
78
+ } ) ;
79
+
80
+ it ( 'should handle API errors' , async ( ) => {
81
+ ( getLatestO11YBuildInfo as Mock ) . mockRejectedValue ( new Error ( 'API Error' ) ) ;
82
+
83
+ await expect ( getFailuresInLastRun ( 'test-build' , 'test-project' ) )
84
+ . rejects . toThrow ( 'API Error' ) ;
85
+ } ) ;
86
+
87
+ it ( 'should handle empty build data' , async ( ) => {
88
+ ( getLatestO11YBuildInfo as Mock ) . mockResolvedValue ( { } ) ;
89
+
90
+ await expect ( getFailuresInLastRun ( 'test-build' , 'test-project' ) )
91
+ . rejects . toThrow ( 'No observability URL found in build data' ) ;
92
+ } ) ;
93
+
94
+ it ( 'should handle partial build data' , async ( ) => {
95
+ ( getLatestO11YBuildInfo as Mock ) . mockResolvedValue ( {
96
+ observability_url : 'https://observability.browserstack.com/123' ,
97
+ unique_errors : { }
98
+ } ) ;
99
+
100
+ const result = await getFailuresInLastRun ( 'test-build' , 'test-project' ) ;
101
+ expect ( result . content [ 0 ] . text ) . toContain ( 'No overview available' ) ;
102
+ expect ( result . content [ 0 ] . text ) . toContain ( 'No error details available' ) ;
103
+ } ) ;
104
+ } ) ;
0 commit comments