|
4 | 4 | */ |
5 | 5 | import assert from 'assert' |
6 | 6 | import sinon from 'sinon' |
7 | | -import { SecurityIssueFilters, SecurityTreeViewFilterState } from 'aws-core-vscode/codewhisperer' |
| 7 | +import { |
| 8 | + CodeIssueGroupingStrategy, |
| 9 | + CodeIssueGroupingStrategyState, |
| 10 | + SecurityIssueFilters, |
| 11 | + SecurityTreeViewFilterState, |
| 12 | +} from 'aws-core-vscode/codewhisperer' |
8 | 13 | import { globals } from 'aws-core-vscode/shared' |
9 | 14 |
|
10 | 15 | describe('model', function () { |
@@ -70,4 +75,113 @@ describe('model', function () { |
70 | 75 | assert.deepStrictEqual(hiddenSeverities, ['High', 'Low']) |
71 | 76 | }) |
72 | 77 | }) |
| 78 | + |
| 79 | + describe('CodeIssueGroupingStrategyState', function () { |
| 80 | + let sandbox: sinon.SinonSandbox |
| 81 | + let state: CodeIssueGroupingStrategyState |
| 82 | + |
| 83 | + beforeEach(function () { |
| 84 | + sandbox = sinon.createSandbox() |
| 85 | + // Reset the singleton instance before each test |
| 86 | + // @ts-ignore - accessing private static for testing |
| 87 | + CodeIssueGroupingStrategyState['#instance'] = undefined |
| 88 | + state = CodeIssueGroupingStrategyState.instance |
| 89 | + }) |
| 90 | + |
| 91 | + afterEach(function () { |
| 92 | + sandbox.restore() |
| 93 | + }) |
| 94 | + |
| 95 | + describe('instance', function () { |
| 96 | + it('should return the same instance when called multiple times', function () { |
| 97 | + const instance1 = CodeIssueGroupingStrategyState.instance |
| 98 | + const instance2 = CodeIssueGroupingStrategyState.instance |
| 99 | + assert.strictEqual(instance1, instance2) |
| 100 | + }) |
| 101 | + }) |
| 102 | + |
| 103 | + describe('getState', function () { |
| 104 | + it('should return fallback when no state is stored', function () { |
| 105 | + const tryGetStub = sandbox.stub(globals.globalState, 'tryGet').returns(undefined) |
| 106 | + const result = state.getState() |
| 107 | + |
| 108 | + sinon.assert.calledWith(tryGetStub, 'aws.amazonq.codescan.groupingStrategy', String) |
| 109 | + assert.equal(result, CodeIssueGroupingStrategy.Severity) |
| 110 | + }) |
| 111 | + |
| 112 | + it('should return stored state when valid', function () { |
| 113 | + const validStrategy = CodeIssueGroupingStrategy.Severity |
| 114 | + const tryGetStub = sandbox.stub(globals.globalState, 'tryGet').returns(validStrategy) |
| 115 | + |
| 116 | + const result = state.getState() |
| 117 | + |
| 118 | + assert.equal(result, validStrategy) |
| 119 | + }) |
| 120 | + |
| 121 | + it('should return fallback when stored state is invalid', function () { |
| 122 | + const invalidStrategy = 'invalid' |
| 123 | + const tryGetStub = sandbox.stub(globals.globalState, 'tryGet').returns(invalidStrategy) |
| 124 | + |
| 125 | + const result = state.getState() |
| 126 | + |
| 127 | + assert.equal(result, CodeIssueGroupingStrategy.Severity) |
| 128 | + }) |
| 129 | + }) |
| 130 | + |
| 131 | + describe('setState', function () { |
| 132 | + it('should update state and fire change event for valid strategy', async function () { |
| 133 | + const validStrategy = CodeIssueGroupingStrategy.FileLocation |
| 134 | + const updateStub = sandbox.stub(globals.globalState, 'update').resolves() |
| 135 | + |
| 136 | + // Create a spy to watch for event emissions |
| 137 | + const eventSpy = sandbox.spy() |
| 138 | + state.onDidChangeState(eventSpy) |
| 139 | + |
| 140 | + await state.setState(validStrategy) |
| 141 | + |
| 142 | + sinon.assert.calledWith(updateStub, 'aws.amazonq.codescan.groupingStrategy', validStrategy) |
| 143 | + sinon.assert.calledWith(eventSpy, validStrategy) |
| 144 | + }) |
| 145 | + |
| 146 | + it('should use fallback and fire change event for invalid strategy', async function () { |
| 147 | + const invalidStrategy = 'invalid' |
| 148 | + const updateStub = sandbox.stub(globals.globalState, 'update').resolves() |
| 149 | + |
| 150 | + // Create a spy to watch for event emissions |
| 151 | + const eventSpy = sandbox.spy() |
| 152 | + state.onDidChangeState(eventSpy) |
| 153 | + |
| 154 | + await state.setState(invalidStrategy) |
| 155 | + |
| 156 | + sinon.assert.calledWith( |
| 157 | + updateStub, |
| 158 | + 'aws.amazonq.codescan.groupingStrategy', |
| 159 | + CodeIssueGroupingStrategy.Severity |
| 160 | + ) |
| 161 | + sinon.assert.calledWith(eventSpy, CodeIssueGroupingStrategy.Severity) |
| 162 | + }) |
| 163 | + }) |
| 164 | + |
| 165 | + describe('reset', function () { |
| 166 | + it('should set state to fallback value', async function () { |
| 167 | + const setStateStub = sandbox.stub(state, 'setState').resolves() |
| 168 | + |
| 169 | + await state.reset() |
| 170 | + |
| 171 | + sinon.assert.calledWith(setStateStub, CodeIssueGroupingStrategy.Severity) |
| 172 | + }) |
| 173 | + }) |
| 174 | + |
| 175 | + describe('onDidChangeState', function () { |
| 176 | + it('should allow subscribing to state changes', async function () { |
| 177 | + const listener = sandbox.spy() |
| 178 | + const disposable = state.onDidChangeState(listener) |
| 179 | + |
| 180 | + await state.setState(CodeIssueGroupingStrategy.Severity) |
| 181 | + |
| 182 | + sinon.assert.calledWith(listener, CodeIssueGroupingStrategy.Severity) |
| 183 | + disposable.dispose() |
| 184 | + }) |
| 185 | + }) |
| 186 | + }) |
73 | 187 | }) |
0 commit comments