|
5 | 5 | */ |
6 | 6 |
|
7 | 7 | import assert from 'node:assert'; |
8 | | -import {describe, it} from 'node:test'; |
| 8 | +import {afterEach, describe, it} from 'node:test'; |
9 | 9 |
|
| 10 | +import sinon from 'sinon'; |
| 11 | + |
| 12 | +import {AggregatedIssue} from '../node_modules/chrome-devtools-frontend/mcp/mcp.js'; |
10 | 13 | import { |
11 | 14 | extractUrlLikeFromDevToolsTitle, |
12 | 15 | urlsEqual, |
| 16 | + mapIssueToMessageObject, |
13 | 17 | } from '../src/DevtoolsUtils.js'; |
| 18 | +import {ISSUE_UTILS} from '../src/issue-descriptions.js'; |
14 | 19 |
|
15 | 20 | describe('extractUrlFromDevToolsTitle', () => { |
16 | 21 | it('deals with no trailing /', () => { |
@@ -70,3 +75,104 @@ describe('urlsEqual', () => { |
70 | 75 | ); |
71 | 76 | }); |
72 | 77 | }); |
| 78 | + |
| 79 | +describe('mapIssueToMessageObject', () => { |
| 80 | + const mockDescription = { |
| 81 | + file: 'mock-issue.md', |
| 82 | + substitutions: new Map([['PLACEHOLDER_VALUE', 'substitution value']]), |
| 83 | + links: [ |
| 84 | + {link: 'http://example.com/learnmore', linkTitle: 'Learn more'}, |
| 85 | + { |
| 86 | + link: 'http://example.com/another-learnmore', |
| 87 | + linkTitle: 'Learn more 2', |
| 88 | + }, |
| 89 | + ], |
| 90 | + }; |
| 91 | + |
| 92 | + afterEach(() => { |
| 93 | + sinon.restore(); |
| 94 | + }); |
| 95 | + |
| 96 | + it('maps aggregated issue with substituted description', () => { |
| 97 | + const mockAggregatedIssue = sinon.createStubInstance(AggregatedIssue); |
| 98 | + mockAggregatedIssue.getDescription.returns(mockDescription); |
| 99 | + mockAggregatedIssue.getAggregatedIssuesCount.returns(1); |
| 100 | + |
| 101 | + const getIssueDescriptionStub = sinon.stub( |
| 102 | + ISSUE_UTILS, |
| 103 | + 'getIssueDescription', |
| 104 | + ); |
| 105 | + |
| 106 | + getIssueDescriptionStub |
| 107 | + .withArgs('mock-issue.md') |
| 108 | + .returns( |
| 109 | + '# Mock Issue Title\n\nThis is a mock issue description with a {PLACEHOLDER_VALUE}.', |
| 110 | + ); |
| 111 | + |
| 112 | + const result = mapIssueToMessageObject(mockAggregatedIssue); |
| 113 | + const expected = { |
| 114 | + type: 'issue', |
| 115 | + item: mockAggregatedIssue, |
| 116 | + message: 'Mock Issue Title', |
| 117 | + count: 1, |
| 118 | + description: |
| 119 | + '# Mock Issue Title\n\nThis is a mock issue description with a substitution value.', |
| 120 | + }; |
| 121 | + assert.deepStrictEqual(result, expected); |
| 122 | + }); |
| 123 | + |
| 124 | + it('returns null for the issue with no description', () => { |
| 125 | + const mockAggregatedIssue = sinon.createStubInstance(AggregatedIssue); |
| 126 | + mockAggregatedIssue.getDescription.returns(null); |
| 127 | + |
| 128 | + const result = mapIssueToMessageObject(mockAggregatedIssue); |
| 129 | + assert.equal(result, null); |
| 130 | + }); |
| 131 | + |
| 132 | + it('returns null if there is no desciption file', () => { |
| 133 | + const mockAggregatedIssue = sinon.createStubInstance(AggregatedIssue); |
| 134 | + mockAggregatedIssue.getDescription.returns(mockDescription); |
| 135 | + mockAggregatedIssue.getAggregatedIssuesCount.returns(1); |
| 136 | + |
| 137 | + const getIssueDescriptionStub = sinon.stub( |
| 138 | + ISSUE_UTILS, |
| 139 | + 'getIssueDescription', |
| 140 | + ); |
| 141 | + |
| 142 | + getIssueDescriptionStub.withArgs('mock-issue.md').returns(null); |
| 143 | + const result = mapIssueToMessageObject(mockAggregatedIssue); |
| 144 | + assert.equal(result, null); |
| 145 | + }); |
| 146 | + |
| 147 | + it("returns null if can't parse the title", () => { |
| 148 | + const mockAggregatedIssue = sinon.createStubInstance(AggregatedIssue); |
| 149 | + mockAggregatedIssue.getDescription.returns(mockDescription); |
| 150 | + mockAggregatedIssue.getAggregatedIssuesCount.returns(1); |
| 151 | + |
| 152 | + const getIssueDescriptionStub = sinon.stub( |
| 153 | + ISSUE_UTILS, |
| 154 | + 'getIssueDescription', |
| 155 | + ); |
| 156 | + |
| 157 | + getIssueDescriptionStub |
| 158 | + .withArgs('mock-issue.md') |
| 159 | + .returns('No title test {PLACEHOLDER_VALUE}'); |
| 160 | + assert.deepStrictEqual(mapIssueToMessageObject(mockAggregatedIssue), null); |
| 161 | + }); |
| 162 | + |
| 163 | + it('returns null if devtools utill function throws an error', () => { |
| 164 | + const mockAggregatedIssue = sinon.createStubInstance(AggregatedIssue); |
| 165 | + mockAggregatedIssue.getDescription.returns(mockDescription); |
| 166 | + mockAggregatedIssue.getAggregatedIssuesCount.returns(1); |
| 167 | + |
| 168 | + const getIssueDescriptionStub = sinon.stub( |
| 169 | + ISSUE_UTILS, |
| 170 | + 'getIssueDescription', |
| 171 | + ); |
| 172 | + // An error will be thrown if placeholder doesn't start from PLACEHOLDER_ |
| 173 | + getIssueDescriptionStub |
| 174 | + .withArgs('mock-issue.md') |
| 175 | + .returns('No title test {WRONG_PLACEHOLDER}'); |
| 176 | + assert.deepStrictEqual(mapIssueToMessageObject(mockAggregatedIssue), null); |
| 177 | + }); |
| 178 | +}); |
0 commit comments