-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: update logger interface and add log grouping
- Update the `CommonLogger` interface to include the `group`, `groupEnd`, and `groupCollapsed` methods. - Add the `LogLevel` type to represent different log levels. - Modify the `compileTypes` function to use the new logger methods for logging and grouping log messages.
- Loading branch information
1 parent
156d94e
commit d2b0449
Showing
15 changed files
with
236 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { parentPort } from 'node:worker_threads'; | ||
import { describe, expect, test, vi } from 'vitest'; | ||
|
||
import { sendLog, workerLogger } from '../workerLogger'; | ||
|
||
vi.mock('node:worker_threads', () => ({ | ||
parentPort: { | ||
postMessage: vi.fn(), | ||
}, | ||
})); | ||
|
||
describe('workerLogger', () => { | ||
const mockPostMessage = vi.mocked(parentPort?.postMessage); | ||
|
||
test('sendLog handles various data types correctly', () => { | ||
const testData = [ | ||
{ a: 'b', c: { d: 'e' } }, | ||
null, | ||
'c', | ||
1, | ||
['a', 'b'], | ||
undefined, | ||
0, | ||
true, | ||
false, | ||
() => 'anonymous function', | ||
/test/, | ||
]; | ||
|
||
sendLog('info', testData); | ||
|
||
expect(mockPostMessage).toHaveBeenCalledWith({ | ||
status: 'log', | ||
level: 'info', | ||
message: [ | ||
'{\n "a": "b",\n "c": {\n "d": "e"\n }\n}', | ||
'null', | ||
'c', | ||
'1', | ||
'[\n "a",\n "b"\n]', | ||
'', | ||
'0', | ||
'true', | ||
'false', | ||
'() => "anonymous function"', | ||
'/test/', | ||
].join(' '), | ||
}); | ||
}); | ||
|
||
test('workerLogger methods call sendLog with correct level and data', () => { | ||
const logMethods = ['error', 'warn', 'info', 'log', 'group', 'groupCollapsed'] as const; | ||
const testData = ['Test message', { key: 'value' }, 42, true]; | ||
|
||
logMethods.forEach(method => { | ||
workerLogger[method](...testData); | ||
expect(mockPostMessage).toHaveBeenCalledWith({ | ||
status: 'log', | ||
level: method, | ||
message: 'Test message {\n "key": "value"\n} 42 true', | ||
}); | ||
}); | ||
}); | ||
|
||
test('workerLogger.groupEnd calls sendLog with empty array', () => { | ||
workerLogger.groupEnd(); | ||
expect(mockPostMessage).toHaveBeenCalledWith({ | ||
status: 'log', | ||
level: 'groupEnd', | ||
message: '', | ||
}); | ||
}); | ||
|
||
test('sendLog throws error on circular references', () => { | ||
const circular: Dict = { a: 'circular' }; | ||
circular.self = circular; | ||
|
||
expect(() => sendLog('info', [circular])).toThrow('Converting circular structure to JSON'); | ||
|
||
expect(mockPostMessage).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('sendLog handles deep nested objects and arrays', () => { | ||
const deepNested = { | ||
a: [1, { b: { c: [2, 3, { d: 4 }] } }], | ||
e: { f: { g: { h: 5 } } }, | ||
}; | ||
|
||
sendLog('info', [deepNested]); | ||
|
||
expect(mockPostMessage).toHaveBeenCalledWith({ | ||
status: 'log', | ||
level: 'info', | ||
message: JSON.stringify(deepNested, null, 2), | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.