Skip to content

Commit bea2fd4

Browse files
committed
test: add tests for size limit checks
1 parent 6e5e287 commit bea2fd4

File tree

2 files changed

+72
-10
lines changed

2 files changed

+72
-10
lines changed

packages/core/src/codewhisperer/util/zipUtil.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ export class ZipUtil {
7171
return CodeWhispererConstants.projectScanPayloadSizeLimitBytes
7272
}
7373

74-
public reachSizeLimit(size: number, scope: CodeWhispererConstants.CodeAnalysisScope): boolean {
74+
public aboveByteLimit(size: number, scope: CodeWhispererConstants.CodeAnalysisScope): boolean {
7575
return CodeWhispererConstants.isFileAnalysisScope(scope)
7676
? size > this.getFileScanPayloadSizeLimitInBytes()
7777
: size > this.getProjectScanPayloadSizeLimitInBytes()
7878
}
7979

80-
public willReachSizeLimit(current: number, adding: number): boolean {
80+
public willReachProjectByteLimit(current: number, adding: number): boolean {
8181
const willReachLimit = current + adding > this.getProjectScanPayloadSizeLimitInBytes()
8282
return willReachLimit
8383
}
@@ -112,7 +112,7 @@ export class ZipUtil {
112112
this._totalSize += (await fs.stat(uri.fsPath)).size
113113
this._totalLines += content.split(ZipConstants.newlineRegex).length
114114

115-
if (this.reachSizeLimit(this._totalSize, scope)) {
115+
if (this.aboveByteLimit(this._totalSize, scope)) {
116116
throw new FileSizeExceededError()
117117
}
118118
const zipFilePath = this.getZipDirPath(FeatureUseCase.CODE_SCAN) + CodeWhispererConstants.codeScanZipExt
@@ -305,8 +305,8 @@ export class ZipUtil {
305305
const fileSize = Buffer.from(fileContent).length
306306

307307
if (
308-
this.reachSizeLimit(this._totalSize, CodeWhispererConstants.CodeAnalysisScope.PROJECT) ||
309-
this.willReachSizeLimit(this._totalSize, fileSize)
308+
this.aboveByteLimit(this._totalSize, CodeWhispererConstants.CodeAnalysisScope.PROJECT) ||
309+
this.willReachProjectByteLimit(this._totalSize, fileSize)
310310
) {
311311
throw new ProjectSizeExceededError()
312312
}
@@ -322,8 +322,8 @@ export class ZipUtil {
322322
const fileSize = (await fs.stat(uri.fsPath)).size
323323

324324
if (
325-
this.reachSizeLimit(this._totalSize, CodeWhispererConstants.CodeAnalysisScope.PROJECT) ||
326-
this.willReachSizeLimit(this._totalSize, fileSize)
325+
this.aboveByteLimit(this._totalSize, CodeWhispererConstants.CodeAnalysisScope.PROJECT) ||
326+
this.willReachProjectByteLimit(this._totalSize, fileSize)
327327
) {
328328
throw new ProjectSizeExceededError()
329329
}

packages/core/src/test/codewhisperer/zipUtil.test.ts

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,68 @@ describe('zipUtil', function () {
3333
sinon.restore()
3434
})
3535

36+
it('returns the proper size limit for zip', function () {
37+
assert.strictEqual(
38+
zipUtil.aboveByteLimit(
39+
CodeWhispererConstants.fileScanPayloadSizeLimitBytes + 1,
40+
CodeAnalysisScope.FILE_ON_DEMAND
41+
),
42+
true
43+
)
44+
45+
assert.strictEqual(
46+
zipUtil.aboveByteLimit(
47+
CodeWhispererConstants.fileScanPayloadSizeLimitBytes + 1,
48+
CodeAnalysisScope.FILE_AUTO
49+
),
50+
true
51+
)
52+
53+
assert.strictEqual(
54+
zipUtil.aboveByteLimit(
55+
CodeWhispererConstants.projectScanPayloadSizeLimitBytes + 1,
56+
CodeAnalysisScope.PROJECT
57+
),
58+
true
59+
)
60+
61+
assert.strictEqual(
62+
zipUtil.aboveByteLimit(
63+
CodeWhispererConstants.fileScanPayloadSizeLimitBytes - 1,
64+
CodeAnalysisScope.FILE_ON_DEMAND
65+
),
66+
false
67+
)
68+
69+
assert.strictEqual(
70+
zipUtil.aboveByteLimit(
71+
CodeWhispererConstants.fileScanPayloadSizeLimitBytes - 1,
72+
CodeAnalysisScope.FILE_AUTO
73+
),
74+
false
75+
)
76+
77+
assert.strictEqual(
78+
zipUtil.aboveByteLimit(
79+
CodeWhispererConstants.projectScanPayloadSizeLimitBytes - 1,
80+
CodeAnalysisScope.PROJECT
81+
),
82+
false
83+
)
84+
})
85+
86+
it('determines if adding file will exceed project byte limit', function () {
87+
assert.strictEqual(
88+
zipUtil.willReachProjectByteLimit(CodeWhispererConstants.projectScanPayloadSizeLimitBytes, 1),
89+
true
90+
)
91+
92+
assert.strictEqual(
93+
zipUtil.willReachProjectByteLimit(CodeWhispererConstants.projectScanPayloadSizeLimitBytes - 10, 9),
94+
false
95+
)
96+
})
97+
3698
it('Should generate zip for file scan and return expected metadata', async function () {
3799
const zipMetadata = await zipUtil.generateZip(vscode.Uri.file(appCodePath), CodeAnalysisScope.FILE_AUTO)
38100
assert.strictEqual(zipMetadata.lines, 49)
@@ -45,7 +107,7 @@ describe('zipUtil', function () {
45107
})
46108

47109
it('Should throw error if payload size limit is reached for file scan', async function () {
48-
sinon.stub(zipUtil, 'reachSizeLimit').returns(true)
110+
sinon.stub(zipUtil, 'aboveByteLimit').returns(true)
49111

50112
await assert.rejects(
51113
() => zipUtil.generateZip(vscode.Uri.file(appCodePath), CodeAnalysisScope.FILE_AUTO),
@@ -65,7 +127,7 @@ describe('zipUtil', function () {
65127
})
66128

67129
it('Should throw error if payload size limit is reached for project scan', async function () {
68-
sinon.stub(zipUtil, 'reachSizeLimit').returns(true)
130+
sinon.stub(zipUtil, 'aboveByteLimit').returns(true)
69131

70132
await assert.rejects(
71133
() => zipUtil.generateZip(vscode.Uri.file(appCodePath), CodeAnalysisScope.PROJECT),
@@ -74,7 +136,7 @@ describe('zipUtil', function () {
74136
})
75137

76138
it('Should throw error if payload size limit will be reached for project scan', async function () {
77-
sinon.stub(zipUtil, 'willReachSizeLimit').returns(true)
139+
sinon.stub(zipUtil, 'aboveByteLimit').returns(true)
78140

79141
await assert.rejects(
80142
() => zipUtil.generateZip(vscode.Uri.file(appCodePath), CodeAnalysisScope.PROJECT),

0 commit comments

Comments
 (0)