|
1 | 1 | import { expect } from 'chai'; |
2 | | -import { describe, it } from 'mocha'; |
| 2 | +import { afterEach, describe, it } from 'mocha'; |
3 | 3 |
|
4 | | -import { |
| 4 | +import $ from '@/core/app'; |
| 5 | +import { SETTINGS_KEY } from '@/constants'; |
| 6 | +import registerSettingsRoutes, { |
5 | 7 | getGithubAvatarApiUrl, |
6 | 8 | shouldRefreshArtifactStoreForSettingsPatch, |
7 | 9 | } from '@/restful/settings'; |
8 | 10 |
|
| 11 | +function createRouteApp() { |
| 12 | + const handlers = new Map(); |
| 13 | + const app = { |
| 14 | + handlers, |
| 15 | + route(pattern) { |
| 16 | + const chain = {}; |
| 17 | + chain.get = (handler) => { |
| 18 | + handlers.set(`GET ${pattern}`, handler); |
| 19 | + return chain; |
| 20 | + }; |
| 21 | + chain.patch = (handler) => { |
| 22 | + handlers.set(`PATCH ${pattern}`, handler); |
| 23 | + return chain; |
| 24 | + }; |
| 25 | + return chain; |
| 26 | + }, |
| 27 | + }; |
| 28 | + |
| 29 | + return app; |
| 30 | +} |
| 31 | + |
| 32 | +function createResponse(routePath) { |
| 33 | + return { |
| 34 | + body: null, |
| 35 | + req: { |
| 36 | + route: { |
| 37 | + path: routePath, |
| 38 | + }, |
| 39 | + }, |
| 40 | + statusCode: 200, |
| 41 | + status(code) { |
| 42 | + this.statusCode = code; |
| 43 | + return this; |
| 44 | + }, |
| 45 | + json(payload) { |
| 46 | + this.body = payload; |
| 47 | + return this; |
| 48 | + }, |
| 49 | + }; |
| 50 | +} |
| 51 | + |
9 | 52 | describe('settings routes', function () { |
10 | 53 | describe('artifact store refresh detection', function () { |
11 | 54 | it('refreshes when GitHub API URL changes', function () { |
@@ -71,4 +114,105 @@ describe('settings routes', function () { |
71 | 114 | ).to.equal('https://litegist.example.com/api/users/xream'); |
72 | 115 | }); |
73 | 116 | }); |
| 117 | + |
| 118 | + describe('backend request concurrency settings', function () { |
| 119 | + const originalRead = $.read.bind($); |
| 120 | + const originalWrite = $.write.bind($); |
| 121 | + |
| 122 | + afterEach(function () { |
| 123 | + $.read = originalRead; |
| 124 | + $.write = originalWrite; |
| 125 | + }); |
| 126 | + |
| 127 | + async function patchSettings(initialSettings, body) { |
| 128 | + const state = { |
| 129 | + [SETTINGS_KEY]: initialSettings, |
| 130 | + }; |
| 131 | + $.read = (key) => state[key]; |
| 132 | + $.write = (data, key) => { |
| 133 | + state[key] = data; |
| 134 | + return true; |
| 135 | + }; |
| 136 | + |
| 137 | + const app = createRouteApp(); |
| 138 | + registerSettingsRoutes(app); |
| 139 | + const patchHandler = app.handlers.get('PATCH /api/settings'); |
| 140 | + const res = createResponse('/api/settings'); |
| 141 | + |
| 142 | + await patchHandler({ body }, res); |
| 143 | + |
| 144 | + expect(res.body.status).to.equal('success'); |
| 145 | + return state[SETTINGS_KEY]; |
| 146 | + } |
| 147 | + |
| 148 | + it('persists positive integer backend request concurrency', async function () { |
| 149 | + const settings = await patchSettings( |
| 150 | + {}, |
| 151 | + { backendRequestConcurrency: '15' }, |
| 152 | + ); |
| 153 | + |
| 154 | + expect(settings).to.deep.include({ |
| 155 | + backendRequestConcurrency: '15', |
| 156 | + }); |
| 157 | + }); |
| 158 | + |
| 159 | + it('allows backend request concurrency above proxy app guidance', async function () { |
| 160 | + const settings = await patchSettings( |
| 161 | + {}, |
| 162 | + { backendRequestConcurrency: 21 }, |
| 163 | + ); |
| 164 | + |
| 165 | + expect(settings).to.deep.include({ |
| 166 | + backendRequestConcurrency: 21, |
| 167 | + }); |
| 168 | + }); |
| 169 | + |
| 170 | + it('clears invalid backend request concurrency values', async function () { |
| 171 | + const invalidValues = ['', 'abc', '1.5', 0, -1, null]; |
| 172 | + |
| 173 | + for (const value of invalidValues) { |
| 174 | + const settings = await patchSettings( |
| 175 | + { backendRequestConcurrency: 8 }, |
| 176 | + { backendRequestConcurrency: value }, |
| 177 | + ); |
| 178 | + |
| 179 | + expect(settings).to.not.have.property( |
| 180 | + 'backendRequestConcurrency', |
| 181 | + ); |
| 182 | + } |
| 183 | + }); |
| 184 | + |
| 185 | + it('persists backend request concurrency wait time values', async function () { |
| 186 | + const zeroWaitSettings = await patchSettings( |
| 187 | + {}, |
| 188 | + { backendRequestConcurrencyWaitTime: 0 }, |
| 189 | + ); |
| 190 | + const positiveWaitSettings = await patchSettings( |
| 191 | + {}, |
| 192 | + { backendRequestConcurrencyWaitTime: '50' }, |
| 193 | + ); |
| 194 | + |
| 195 | + expect(zeroWaitSettings).to.deep.include({ |
| 196 | + backendRequestConcurrencyWaitTime: 0, |
| 197 | + }); |
| 198 | + expect(positiveWaitSettings).to.deep.include({ |
| 199 | + backendRequestConcurrencyWaitTime: '50', |
| 200 | + }); |
| 201 | + }); |
| 202 | + |
| 203 | + it('clears invalid backend request concurrency wait time values', async function () { |
| 204 | + const invalidValues = ['', 'abc', '1.5', -1, null]; |
| 205 | + |
| 206 | + for (const value of invalidValues) { |
| 207 | + const settings = await patchSettings( |
| 208 | + { backendRequestConcurrencyWaitTime: 8 }, |
| 209 | + { backendRequestConcurrencyWaitTime: value }, |
| 210 | + ); |
| 211 | + |
| 212 | + expect(settings).to.not.have.property( |
| 213 | + 'backendRequestConcurrencyWaitTime', |
| 214 | + ); |
| 215 | + } |
| 216 | + }); |
| 217 | + }); |
74 | 218 | }); |
0 commit comments