Skip to content

test: add tests for gadgets helper utilities - #51

Open
mrhapile wants to merge 1 commit into
inspektor-gadget:mainfrom
mrhapile:test/gadgets-helperts
Open

test: add tests for gadgets helper utilities#51
mrhapile wants to merge 1 commit into
inspektor-gadget:mainfrom
mrhapile:test/gadgets-helperts

Conversation

@mrhapile

@mrhapile mrhapile commented Mar 7, 2026

Copy link
Copy Markdown

Add tests for gadgets helper utilities

ref #20

This PR adds a test file for the helper utilities defined in src/gadgets/helper.ts.

The tests validate the behavior of the exported helper functions by checking their expected outputs for valid inputs and ensuring that edge cases are handled safely. The tests are written following the repository's existing testing conventions and aim to improve overall test coverage while ensuring the helper functions behave as expected.

How to use

Reviewers can validate this PR by running the test suite locally and confirming that the newly added tests execute successfully.

Steps to validate:

  1. Checkout this branch.
  2. Install dependencies if necessary.
  3. Run the test suite.

Testing done

Commands executed:

npm install
npm test

Result:

Screenshot 2026-03-07 at 11 17 45 PM

The test suite runs successfully and the new test file src/gadgets/helper.test.ts executes without errors.

Signed-off-by: mrhapile <allinonegaming3456@gmail.com>
Copilot AI review requested due to automatic review settings March 7, 2026 17:52

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a Vitest test suite for src/gadgets/helper.ts helper utilities to improve coverage and validate expected behavior for common inputs.

Changes:

  • Introduces src/gadgets/helper.test.ts with unit tests for pod label detection, array de-duplication, nested property access, identifier helpers, and environment/URL helpers.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +4 to +91
IG_CONTAINER_KEY,
IG_CONTAINER_VALUE,
isIGPod,
removeDuplicates,
getProperty,
createIdentifier,
parseIdentifier,
isIdentifier,
isElectron,
isDockerDesktop,
getServerURL,
} from './helper';

describe('gadgets helper utilities', () => {
let originalWindow: any;
let originalProcess: any;
let originalNavigator: any;

beforeEach(() => {
// Save globals for restoring
originalWindow = global.window;
originalProcess = global.process;
originalNavigator = global.navigator;
});

afterEach(() => {
// Restore globals
global.window = originalWindow;
global.process = originalProcess;
global.navigator = originalNavigator;
vi.restoreAllMocks();
});

describe('isIGPod', () => {
test('returns false if podResource has no labels', () => {
const podResource = { metadata: {} };
expect(isIGPod(podResource)).toBe(false);
});

test('returns true if podResource has matching IG container label', () => {
const podResource = {
metadata: {
labels: {
[IG_CONTAINER_KEY]: IG_CONTAINER_VALUE,
},
},
};
expect(isIGPod(podResource)).toBe(true);
});

test('returns false if podResource has labels but no matching IG label', () => {
const podResource = {
metadata: {
labels: {
'some-other-key': 'some-value',
},
},
};
expect(isIGPod(podResource)).toBe(false);
});
});

describe('removeDuplicates', () => {
test('removes duplicate objects based on "key" property', () => {
const input = [
{ key: 'a', value: 1 },
{ key: 'b', value: 2 },
{ key: 'a', value: 3 }, // Duplicate key
];
const expected = [
{ key: 'a', value: 1 },
{ key: 'b', value: 2 },
];
expect(removeDuplicates(input)).toEqual(expected);
});

test('returns an empty array when given an empty array', () => {
expect(removeDuplicates([])).toEqual([]);
});

test('returns the same array if there are no duplicates', () => {
const input = [
{ key: 'a', value: 1 },
{ key: 'b', value: 2 },
];
expect(removeDuplicates(input)).toEqual(input);
});
});

Copilot AI Mar 7, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new test file uses 4-space indentation, but the existing TS/TSX files in this repo are consistently formatted with 2-space indentation (e.g., src/gadgets/helper.ts). Please run the repo formatter (or reformat this file) so it matches project style and avoids noisy diffs in future edits.

Suggested change
IG_CONTAINER_KEY,
IG_CONTAINER_VALUE,
isIGPod,
removeDuplicates,
getProperty,
createIdentifier,
parseIdentifier,
isIdentifier,
isElectron,
isDockerDesktop,
getServerURL,
} from './helper';
describe('gadgets helper utilities', () => {
let originalWindow: any;
let originalProcess: any;
let originalNavigator: any;
beforeEach(() => {
// Save globals for restoring
originalWindow = global.window;
originalProcess = global.process;
originalNavigator = global.navigator;
});
afterEach(() => {
// Restore globals
global.window = originalWindow;
global.process = originalProcess;
global.navigator = originalNavigator;
vi.restoreAllMocks();
});
describe('isIGPod', () => {
test('returns false if podResource has no labels', () => {
const podResource = { metadata: {} };
expect(isIGPod(podResource)).toBe(false);
});
test('returns true if podResource has matching IG container label', () => {
const podResource = {
metadata: {
labels: {
[IG_CONTAINER_KEY]: IG_CONTAINER_VALUE,
},
},
};
expect(isIGPod(podResource)).toBe(true);
});
test('returns false if podResource has labels but no matching IG label', () => {
const podResource = {
metadata: {
labels: {
'some-other-key': 'some-value',
},
},
};
expect(isIGPod(podResource)).toBe(false);
});
});
describe('removeDuplicates', () => {
test('removes duplicate objects based on "key" property', () => {
const input = [
{ key: 'a', value: 1 },
{ key: 'b', value: 2 },
{ key: 'a', value: 3 }, // Duplicate key
];
const expected = [
{ key: 'a', value: 1 },
{ key: 'b', value: 2 },
];
expect(removeDuplicates(input)).toEqual(expected);
});
test('returns an empty array when given an empty array', () => {
expect(removeDuplicates([])).toEqual([]);
});
test('returns the same array if there are no duplicates', () => {
const input = [
{ key: 'a', value: 1 },
{ key: 'b', value: 2 },
];
expect(removeDuplicates(input)).toEqual(input);
});
});
IG_CONTAINER_KEY,
IG_CONTAINER_VALUE,
isIGPod,
removeDuplicates,
getProperty,
createIdentifier,
parseIdentifier,
isIdentifier,
isElectron,
isDockerDesktop,
getServerURL,
} from './helper';
describe('gadgets helper utilities', () => {
let originalWindow: any;
let originalProcess: any;
let originalNavigator: any;
beforeEach(() => {
// Save globals for restoring
originalWindow = global.window;
originalProcess = global.process;
originalNavigator = global.navigator;
});
afterEach(() => {
// Restore globals
global.window = originalWindow;
global.process = originalProcess;
global.navigator = originalNavigator;
vi.restoreAllMocks();
});
describe('isIGPod', () => {
test('returns false if podResource has no labels', () => {
const podResource = { metadata: {} };
expect(isIGPod(podResource)).toBe(false);
});
test('returns true if podResource has matching IG container label', () => {
const podResource = {
metadata: {
labels: {
[IG_CONTAINER_KEY]: IG_CONTAINER_VALUE,
},
},
};
expect(isIGPod(podResource)).toBe(true);
});
test('returns false if podResource has labels but no matching IG label', () => {
const podResource = {
metadata: {
labels: {
'some-other-key': 'some-value',
},
},
};
expect(isIGPod(podResource)).toBe(false);
});
});
describe('removeDuplicates', () => {
test('removes duplicate objects based on "key" property', () => {
const input = [
{ key: 'a', value: 1 },
{ key: 'b', value: 2 },
{ key: 'a', value: 3 }, // Duplicate key
];
const expected = [
{ key: 'a', value: 1 },
{ key: 'b', value: 2 },
];
expect(removeDuplicates(input)).toEqual(expected);
});
test('returns an empty array when given an empty array', () => {
expect(removeDuplicates([])).toEqual([]);
});
test('returns the same array if there are no duplicates', () => {
const input = [
{ key: 'a', value: 1 },
{ key: 'b', value: 2 },
];
expect(removeDuplicates(input)).toEqual(input);
});
});

Copilot uses AI. Check for mistakes.
Comment on lines +84 to +90
test('returns the same array if there are no duplicates', () => {
const input = [
{ key: 'a', value: 1 },
{ key: 'b', value: 2 },
];
expect(removeDuplicates(input)).toEqual(input);
});

Copilot AI Mar 7, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test name "returns the same array if there are no duplicates" is misleading: removeDuplicates always returns a new array instance (it just deep-equals the input when there are no duplicates). Please rename the test to reflect that it returns an equal array / preserves elements and order, rather than the same array reference.

Copilot uses AI. Check for mistakes.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +18 to +33
let originalWindow: any;
let originalProcess: any;
let originalNavigator: any;

beforeEach(() => {
// Save globals for restoring
originalWindow = global.window;
originalProcess = global.process;
originalNavigator = global.navigator;
});

afterEach(() => {
// Restore globals
global.window = originalWindow;
global.process = originalProcess;
global.navigator = originalNavigator;

Copilot AI Apr 7, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests replace global.window, global.process, and global.navigator with plain objects. Overwriting the entire window/process object can make the suite flaky (it can remove properties Vitest/jsdom or other helpers may rely on during the test). Prefer stubbing just the needed properties (e.g., window.ddClient, window.process.type, process.versions.electron, navigator.userAgent) via vi.stubGlobal/Object.defineProperty and restoring via vi.unstubAllGlobals() or by restoring individual properties.

Suggested change
let originalWindow: any;
let originalProcess: any;
let originalNavigator: any;
beforeEach(() => {
// Save globals for restoring
originalWindow = global.window;
originalProcess = global.process;
originalNavigator = global.navigator;
});
afterEach(() => {
// Restore globals
global.window = originalWindow;
global.process = originalProcess;
global.navigator = originalNavigator;
afterEach(() => {
vi.unstubAllGlobals();

Copilot uses AI. Check for mistakes.

@illume illume left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contributions!

Please let us know if you want to continue this by addressing the review comments? If not that's ok, we can take over the PR (and finish it or close it ourselves).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants