-
-
Notifications
You must be signed in to change notification settings - Fork 1
🧪 testing improvement for getQueryTimeout config #135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import './vscode_mock_setup'; | ||
| import assert from 'node:assert'; | ||
| import { test, describe, beforeEach, afterEach } from 'node:test'; | ||
| import * as vscode from 'vscode'; | ||
| import { getQueryTimeout, getMaximumFileSizeBytes } from '../../src/config'; | ||
|
|
||
| describe('Configuration Retrievers', () => { | ||
| let originalGetConfiguration: any; | ||
|
|
||
| beforeEach(() => { | ||
| originalGetConfiguration = vscode.workspace.getConfiguration; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vscode.workspace.getConfiguration = originalGetConfiguration; | ||
| }); | ||
|
|
||
| describe('getQueryTimeout', () => { | ||
| test('should return default timeout (30000ms) when not configured', () => { | ||
| vscode.workspace.getConfiguration = (section) => { | ||
| assert.strictEqual(section, 'sqliteExplorer'); | ||
| return { | ||
| get: (key: string, defaultValue: any) => { | ||
| assert.strictEqual(key, 'queryTimeout'); | ||
| return defaultValue; | ||
| }, | ||
| update: () => Promise.resolve() | ||
| } as any; | ||
| }; | ||
|
|
||
| const timeout = getQueryTimeout(); | ||
| assert.strictEqual(timeout, 30000); | ||
| }); | ||
|
|
||
| test('should return configured timeout when defined', () => { | ||
| vscode.workspace.getConfiguration = (section) => { | ||
| assert.strictEqual(section, 'sqliteExplorer'); | ||
| return { | ||
| get: (key: string, defaultValue: any) => { | ||
| assert.strictEqual(key, 'queryTimeout'); | ||
| return 15000; | ||
| }, | ||
| update: () => Promise.resolve() | ||
| } as any; | ||
| }; | ||
|
|
||
| const timeout = getQueryTimeout(); | ||
| assert.strictEqual(timeout, 15000); | ||
| }); | ||
| }); | ||
|
Comment on lines
+18
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test setup for describe('getQueryTimeout', () => {
const setupMock = (returnValue: any) => {
vscode.workspace.getConfiguration = (section) => {
assert.strictEqual(section, 'sqliteExplorer');
return {
get: (key: string, defaultValue: any) => {
assert.strictEqual(key, 'queryTimeout');
if (returnValue === 'use_default') {
return defaultValue;
}
return returnValue;
},
update: () => Promise.resolve()
} as any;
};
};
test('should return default timeout (30000ms) when not configured', () => {
setupMock('use_default');
const timeout = getQueryTimeout();
assert.strictEqual(timeout, 30000);
});
test('should return configured timeout when defined', () => {
setupMock(15000);
const timeout = getQueryTimeout();
assert.strictEqual(timeout, 15000);
});
}); |
||
|
|
||
| describe('getMaximumFileSizeBytes', () => { | ||
| test('should return default size (200MB) when not configured', () => { | ||
| vscode.workspace.getConfiguration = (section) => { | ||
| assert.strictEqual(section, 'sqliteExplorer'); | ||
| return { | ||
| get: (key: string, defaultValue: any) => { | ||
| assert.strictEqual(key, 'maxFileSize'); | ||
| return undefined; // Not configured | ||
| }, | ||
| update: () => Promise.resolve() | ||
| } as any; | ||
| }; | ||
|
|
||
| const size = getMaximumFileSizeBytes(); | ||
| assert.strictEqual(size, 200 * (2 ** 20)); | ||
| }); | ||
|
|
||
| test('should return configured size in bytes', () => { | ||
| vscode.workspace.getConfiguration = (section) => { | ||
| assert.strictEqual(section, 'sqliteExplorer'); | ||
| return { | ||
| get: (key: string, defaultValue: any) => { | ||
| assert.strictEqual(key, 'maxFileSize'); | ||
| return 50; // 50MB | ||
| }, | ||
| update: () => Promise.resolve() | ||
| } as any; | ||
| }; | ||
|
|
||
| const size = getMaximumFileSizeBytes(); | ||
| assert.strictEqual(size, 50 * (2 ** 20)); | ||
| }); | ||
| }); | ||
|
Comment on lines
+52
to
+84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing edge-case test for The docstring on test('should return 0 (unlimited) when maxFileSize is configured as 0', () => {
vscode.workspace.getConfiguration = (_section) => ({
get: (_key: string, _defaultValue: any) => 0,
update: () => Promise.resolve()
} as any);
const size = getMaximumFileSizeBytes();
assert.strictEqual(size, 0);
}); |
||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,3 +12,18 @@ Module._load = function (request, parent, isMain) { | |
| } | ||
| return originalLoad(request, parent, isMain); | ||
| }; | ||
|
|
||
| mockVscode.extensions = { | ||
| getExtension: () => ({ packageJSON: { version: '1.0.0' }, extensionUri: mockVscode.Uri.file('/fake/path') }) | ||
| }; | ||
|
|
||
| // Polyfill import.meta.env for tsx runner | ||
| // @ts-ignore | ||
| if (typeof process !== 'undefined') { | ||
| // @ts-ignore | ||
| globalThis.import = globalThis.import || {}; | ||
| // @ts-ignore | ||
| globalThis.import.meta = globalThis.import.meta || {}; | ||
| // @ts-ignore | ||
| globalThis.import.meta.env = globalThis.import.meta.env || { VSCODE_BROWSER_EXT: false }; | ||
| } | ||
|
Comment on lines
+22
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To improve code clarity and reduce the number of if (typeof process !== 'undefined') {
const g = globalThis as any;
g.import = g.import || {};
g.import.meta = g.import.meta || {};
g.import.meta.env = g.import.meta.env || { VSCODE_BROWSER_EXT: false };
}
Comment on lines
+21
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This polyfill attempts to simulate In practice, the current tests pass only because The PR description correctly identifies that the architectural fix (moving the config functions into |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Import placed at the bottom of the file
The
import * as vsc from 'vscode'statement is placed after all the constant exports, which is unconventional and inconsistent with standard TypeScript/JavaScript practice. Imports should always be at the top of the file before any module-level declarations. While ES moduleimportstatements are hoisted and technically work regardless of position, this layout makes the file harder to read and the dependency onvscodeless obvious.Move the import to the very top of the file.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!