forked from databricks/databricks-sql-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlz4.test.ts
More file actions
156 lines (127 loc) · 4.89 KB
/
lz4.test.ts
File metadata and controls
156 lines (127 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { expect } from 'chai';
import sinon from 'sinon';
describe('lz4 module loader', () => {
let moduleLoadStub: sinon.SinonStub | undefined;
let consoleWarnStub: sinon.SinonStub;
beforeEach(() => {
consoleWarnStub = sinon.stub(console, 'warn');
});
afterEach(() => {
consoleWarnStub.restore();
if (moduleLoadStub) {
moduleLoadStub.restore();
}
// Clear module cache
Object.keys(require.cache).forEach((key) => {
if (key.includes('lz4')) {
delete require.cache[key];
}
});
});
const mockModuleLoad = (lz4MockOrError: unknown): { restore: () => void; wasLz4LoadAttempted: () => boolean } => {
// eslint-disable-next-line global-require
const Module = require('module');
const originalLoad = Module._load;
let lz4LoadAttempted = false;
Module._load = (request: string, parent: unknown, isMain: boolean) => {
if (request === 'lz4') {
lz4LoadAttempted = true;
if (lz4MockOrError instanceof Error) {
throw lz4MockOrError;
}
return lz4MockOrError;
}
return originalLoad.call(Module, request, parent, isMain);
};
return {
restore: () => {
Module._load = originalLoad;
},
wasLz4LoadAttempted: () => lz4LoadAttempted,
};
};
const loadLz4Module = () => {
delete require.cache[require.resolve('../../../lib/utils/lz4')];
// eslint-disable-next-line global-require
return require('../../../lib/utils/lz4');
};
it('should successfully load and use lz4 module when available', () => {
const fakeLz4 = {
encode: (buf: Buffer) => {
const compressed = Buffer.from(`compressed:${buf.toString()}`);
return compressed;
},
decode: (buf: Buffer) => {
const decompressed = buf.toString().replace('compressed:', '');
return Buffer.from(decompressed);
},
};
const { restore } = mockModuleLoad(fakeLz4);
const moduleExports = loadLz4Module();
const lz4Module = moduleExports.default();
restore();
expect(lz4Module).to.not.be.undefined;
expect(lz4Module.encode).to.be.a('function');
expect(lz4Module.decode).to.be.a('function');
const testData = Buffer.from('Hello, World!');
const compressed = lz4Module.encode(testData);
const decompressed = lz4Module.decode(compressed);
expect(decompressed.toString()).to.equal('Hello, World!');
expect(consoleWarnStub.called).to.be.false;
});
it('should return undefined when lz4 module fails to load with MODULE_NOT_FOUND', () => {
const err: NodeJS.ErrnoException = new Error("Cannot find module 'lz4'");
err.code = 'MODULE_NOT_FOUND';
const { restore } = mockModuleLoad(err);
const moduleExports = loadLz4Module();
const lz4Module = moduleExports.default();
restore();
expect(lz4Module).to.be.undefined;
expect(consoleWarnStub.called).to.be.false;
});
it('should return undefined and log warning when lz4 fails with ERR_DLOPEN_FAILED', () => {
const err: NodeJS.ErrnoException = new Error('Module did not self-register');
err.code = 'ERR_DLOPEN_FAILED';
const { restore } = mockModuleLoad(err);
const moduleExports = loadLz4Module();
const lz4Module = moduleExports.default();
restore();
expect(lz4Module).to.be.undefined;
expect(consoleWarnStub.calledOnce).to.be.true;
expect(consoleWarnStub.firstCall.args[0]).to.include('Architecture or version mismatch');
});
it('should return undefined and log warning when lz4 fails with unknown error code', () => {
const err: NodeJS.ErrnoException = new Error('Some unknown error');
err.code = 'UNKNOWN_ERROR';
const { restore } = mockModuleLoad(err);
const moduleExports = loadLz4Module();
const lz4Module = moduleExports.default();
restore();
expect(lz4Module).to.be.undefined;
expect(consoleWarnStub.calledOnce).to.be.true;
expect(consoleWarnStub.firstCall.args[0]).to.include('Unhandled error code');
});
it('should return undefined and log warning when error has no code property', () => {
const err = new Error('Error without code');
const { restore } = mockModuleLoad(err);
const moduleExports = loadLz4Module();
const lz4Module = moduleExports.default();
restore();
expect(lz4Module).to.be.undefined;
expect(consoleWarnStub.calledOnce).to.be.true;
expect(consoleWarnStub.firstCall.args[0]).to.include('Invalid error object');
});
it('should not attempt to load lz4 module when getResolvedModule is not called', () => {
const fakeLz4 = {
encode: () => Buffer.from(''),
decode: () => Buffer.from(''),
};
const { restore, wasLz4LoadAttempted } = mockModuleLoad(fakeLz4);
// Load the module but don't call getResolvedModule
loadLz4Module();
// Note: we're NOT calling .default() here
restore();
expect(wasLz4LoadAttempted()).to.be.false;
expect(consoleWarnStub.called).to.be.false;
});
});