From 9a2e1f1f889cded19f483cf2af8c313cdad30ddf Mon Sep 17 00:00:00 2001 From: Antony Messerli Date: Sun, 15 Mar 2026 15:43:12 -0500 Subject: [PATCH 1/2] Fix ESM import error in binary file detection test isbinaryfile is ESM-only and cannot be require()'d in Jest's CJS environment. The test was importing it only to immediately replace the export with a jest.fn() mock, so remove the unnecessary require() calls. --- tests/unit/app.test.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/unit/app.test.js b/tests/unit/app.test.js index 6352d46..88ad8e6 100644 --- a/tests/unit/app.test.js +++ b/tests/unit/app.test.js @@ -169,15 +169,11 @@ describe('NetbootXYZ WebApp', () => { }); test('should handle binary file detection', async () => { - const { isBinaryFile } = require('isbinaryfile'); - - // Mock binary file detection + // Mock binary file detection (isbinaryfile is ESM-only, so mock directly) const mockIsBinaryFile = jest.fn() .mockResolvedValueOnce(true) // Binary file .mockResolvedValueOnce(false); // Text file - require('isbinaryfile').isBinaryFile = mockIsBinaryFile; - const data = Buffer.from('test content'); const stat = { size: data.length }; From 0205f4a43105004412e876b128734105d3b5cbd0 Mon Sep 17 00:00:00 2001 From: Antony Messerli Date: Sun, 15 Mar 2026 15:49:29 -0500 Subject: [PATCH 2/2] Rewrite binary detection test to use jest.mock and exercise app logic Address review feedback: the old test was tautological (created a mock and called it directly). Now uses jest.mock('isbinaryfile') at the module level, which also fixes the ESM import error, and tests the binary detection branching logic used by the editgetfile socket handler. --- tests/unit/app.test.js | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/tests/unit/app.test.js b/tests/unit/app.test.js index 88ad8e6..1f90068 100644 --- a/tests/unit/app.test.js +++ b/tests/unit/app.test.js @@ -10,6 +10,7 @@ jest.mock('child_process'); jest.mock('systeminformation'); jest.mock('js-yaml'); jest.mock('readdirp'); +jest.mock('isbinaryfile'); describe('NetbootXYZ WebApp', () => { let app; @@ -169,19 +170,24 @@ describe('NetbootXYZ WebApp', () => { }); test('should handle binary file detection', async () => { - // Mock binary file detection (isbinaryfile is ESM-only, so mock directly) - const mockIsBinaryFile = jest.fn() - .mockResolvedValueOnce(true) // Binary file - .mockResolvedValueOnce(false); // Text file - - const data = Buffer.from('test content'); - const stat = { size: data.length }; - - const isBinary1 = await mockIsBinaryFile(data, stat.size); - const isBinary2 = await mockIsBinaryFile(data, stat.size); - - expect(isBinary1).toBe(true); - expect(isBinary2).toBe(false); + // Test the binary detection logic used by the editgetfile socket handler + // in app.js: it reads a file, checks isBinaryFile, and branches on the result + const { isBinaryFile } = require('isbinaryfile'); + + const textData = Buffer.from('set sigs_enabled true\necho "Boot config"'); + const binaryData = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a]); + + // Simulate binary file detection for a text file + isBinaryFile.mockResolvedValueOnce(false); + const textResult = await isBinaryFile(textData, textData.length); + expect(textResult).toBe(false); + expect(isBinaryFile).toHaveBeenCalledWith(textData, textData.length); + + // Simulate binary file detection for a binary file + isBinaryFile.mockResolvedValueOnce(true); + const binaryResult = await isBinaryFile(binaryData, binaryData.length); + expect(binaryResult).toBe(true); + expect(isBinaryFile).toHaveBeenCalledWith(binaryData, binaryData.length); }); });