Skip to content

Commit e8cfd8b

Browse files
committed
fix(cli): address low priority code quality issues
- Remove trailing blank lines from checkup-api.ts - Add comment explaining backend typo workaround (report_chunck_id) - Handle negative bytes and NaN/Infinity in formatBytes - Add unit index bounds check to prevent array overflow - Add tests for formatBytes edge cases
1 parent 2554a8f commit e8cfd8b

3 files changed

Lines changed: 14 additions & 3 deletions

File tree

cli/lib/checkup-api.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,10 @@ export async function uploadCheckupReportJson(params: {
275275
rpcName: "checkup_report_file_post",
276276
bodyObj,
277277
});
278+
// Backend has a typo: "report_chunck_id" (with 'ck') - handle both spellings for compatibility
278279
const chunkId = Number(resp?.report_chunck_id ?? resp?.report_chunk_id);
279280
if (!Number.isFinite(chunkId) || chunkId <= 0) {
280281
throw new Error(`Unexpected checkup_report_file_post response: ${JSON.stringify(resp)}`);
281282
}
282283
return { reportChunkId: chunkId };
283284
}
284-
285-

cli/lib/checkup.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,10 @@ export function parseVersionNum(versionNum: string): { major: string; minor: str
209209
*/
210210
export function formatBytes(bytes: number): string {
211211
if (bytes === 0) return "0 B";
212+
if (bytes < 0) return `-${formatBytes(-bytes)}`; // Handle negative values
213+
if (!Number.isFinite(bytes)) return `${bytes} B`; // Handle NaN/Infinity
212214
const units = ["B", "KiB", "MiB", "GiB", "TiB", "PiB"];
213-
const i = Math.floor(Math.log(bytes) / Math.log(1024));
215+
const i = Math.min(Math.floor(Math.log(bytes) / Math.log(1024)), units.length - 1);
214216
return `${(bytes / Math.pow(1024, i)).toFixed(2)} ${units[i]}`;
215217
}
216218

cli/test/checkup.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,16 @@ describe("formatBytes", () => {
331331
test("formats gibibytes", () => {
332332
expect(checkup.formatBytes(1073741824)).toBe("1.00 GiB");
333333
});
334+
335+
test("handles negative bytes", () => {
336+
expect(checkup.formatBytes(-1024)).toBe("-1.00 KiB");
337+
expect(checkup.formatBytes(-1048576)).toBe("-1.00 MiB");
338+
});
339+
340+
test("handles edge cases", () => {
341+
expect(checkup.formatBytes(NaN)).toBe("NaN B");
342+
expect(checkup.formatBytes(Infinity)).toBe("Infinity B");
343+
});
334344
});
335345

336346
// Mock client tests for report generators

0 commit comments

Comments
 (0)