-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathmain.spec.ts
More file actions
136 lines (116 loc) · 3.98 KB
/
main.spec.ts
File metadata and controls
136 lines (116 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import {Octokit} from '@octokit/rest';
import * as core from '@actions/core';
import {context} from '@actions/github';
import {GoogleGenAI} from '@google/genai';
import {IssueLabeling} from './issue-labeling.js';
describe('IssueLabeling', () => {
let mockGit: {
paginate: jasmine.Spy;
issues: {
listLabelsForRepo: jasmine.Spy;
addLabels: jasmine.Spy;
get: jasmine.Spy;
};
};
let mockAI: any;
let mockCore: jasmine.SpyObj<typeof core>;
let issueLabeling: IssueLabeling;
beforeEach(() => {
mockGit = {
paginate: jasmine.createSpy('paginate'),
issues: {
listLabelsForRepo: jasmine.createSpy('listLabelsForRepo'),
addLabels: jasmine.createSpy('addLabels'),
get: jasmine.createSpy('get'),
},
};
mockGit.issues.addLabels.and.returnValue(Promise.resolve({}));
mockGit.issues.get.and.returnValue(
Promise.resolve({
data: {
title: 'Tough Issue',
body: 'Complex Body',
labels: [],
},
}),
);
mockGit.paginate.and.callFake((fn: any, opts: any) => {
// Return value matching listLabelsForRepo signature
return Promise.resolve([{name: 'area: core'}, {name: 'area: router'}, {name: 'bug'}]);
});
mockAI = {
models: jasmine.createSpyObj('models', ['generateContent']),
};
mockCore = jasmine.createSpyObj<typeof core>('core', [
'getInput',
'info',
'error',
'warning',
'debug',
'setFailed',
]);
mockCore.getInput.and.returnValue('mock-ai-key');
mockCore.error.and.callFake(console.error);
mockCore.info.and.callFake(console.info);
mockCore.warning.and.callFake(console.warn);
mockCore.debug.and.callFake(console.debug);
mockCore.setFailed.and.callFake(console.error);
// We must cast the mock to Octokit because the mock only implements the subset used by the class.
// This is standard for mocking large interfaces like Octokit.
issueLabeling = new IssueLabeling(mockGit as unknown as Octokit, mockCore);
spyOn(issueLabeling, 'getGenerativeAI').and.returnValue(mockAI);
});
it('should initialize labels correctly', async () => {
await issueLabeling.initialize();
expect(issueLabeling.repoAreaLabels.has('area: core')).toBe(true);
expect(issueLabeling.repoAreaLabels.has('area: router')).toBe(true);
expect(issueLabeling.repoAreaLabels.has('bug')).toBe(false);
});
it('should apply a label when Gemini is confident', async () => {
mockAI.models.generateContent.and.returnValue(
Promise.resolve({
text: 'area: core',
}),
);
await issueLabeling.run();
expect(mockGit.issues.addLabels).toHaveBeenCalledWith(
jasmine.objectContaining({
labels: ['area: core'],
}),
);
});
it('should NOT apply a label when Gemini returns "ambiguous"', async () => {
mockAI.models.generateContent.and.returnValue(
Promise.resolve({
text: 'ambiguous',
}),
);
await issueLabeling.run();
expect(mockGit.issues.addLabels).not.toHaveBeenCalled();
});
it('should NOT apply a label when Gemini returns an invalid label', async () => {
mockAI.models.generateContent.and.returnValue(
Promise.resolve({
text: 'area: invalid',
}),
);
await issueLabeling.run();
expect(mockGit.issues.addLabels).not.toHaveBeenCalled();
});
it('should initialize and run with manual instantiation check', () => {
expect(issueLabeling).toBeDefined();
expect(mockCore.getInput).not.toHaveBeenCalled(); // until run is called
});
it('should skip labeling when issue already has an area label', async () => {
mockGit.issues.get.and.resolveTo({
data: {
title: 'Tough Issue',
body: 'Complex Body',
labels: [{name: 'area: core'}],
},
});
await issueLabeling.run();
expect(mockGit.issues.addLabels).not.toHaveBeenCalled();
expect(mockCore.info).toHaveBeenCalledWith('Issue already has an area label. Skipping.');
});
});