Add more tests for InvGroups

This commit is contained in:
2026-01-14 11:01:56 +01:00
parent a9db3a0972
commit 8a0726480b

View File

@@ -1,5 +1,5 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { GetInvTypes, GetInvTypesByGroupId, SearchInvTypes } from './typesense-client';
import { GetInvGroups, GetInvTypes, GetInvTypesByGroupId, SearchInvTypes } from './typesense-client';
import { resourceLimits } from 'worker_threads';
describe('Typesense Client InvTypes', () => {
@@ -100,4 +100,62 @@ describe('Typesense Client GetInvTypesByGroupId', () => {
expect(results.length).toBe(32);
expect(fetchSpy).toHaveBeenCalledTimes(1);
});
});
describe('Typesense Client GetInvGroups', () => {
let fetchSpy: ReturnType<typeof vi.spyOn>;
beforeEach(() => {
fetchSpy = vi.spyOn(globalThis, 'fetch');
});
afterEach(() => {
fetchSpy.mockRestore();
});
it('get groups by group id', async () => {
const results = await GetInvGroups([766]);
expect(results.size).toBe(1);
expect(results.get(766)).toBeDefined();
expect(fetchSpy).toHaveBeenCalledTimes(1);
});
it('get multiple groups by group id', async () => {
GetInvGroups([766]); // Cache one group
const groupIds = [766, 767, 768];
const results = await GetInvGroups(groupIds);
expect(results.size).toBe(groupIds.length);
groupIds.forEach(groupId => {
expect(results.get(groupId)).toBeDefined();
});
expect(fetchSpy).toHaveBeenCalledTimes(1);
});
it('returns null for missing group ids', async () => {
// Assuming 999999 is not a valid group id
const groupIds = [766, 999999];
const results = await GetInvGroups(groupIds);
expect(results.size).toBe(1); // 1 does exist the other not
expect(results.get(766)).toBeDefined();
expect(results.get(999999)).toBeUndefined();
expect(fetchSpy).toHaveBeenCalledTimes(1);
});
it('uses cache for already fetched group ids', async () => {
// First fetch populates the cache
await GetInvGroups([766]);
fetchSpy.mockClear();
// Second fetch should hit the cache and not re-fetch
const results = await GetInvGroups([766]);
expect(results.size).toBe(1);
expect(results.get(766)).toBeDefined();
expect(fetchSpy).not.toHaveBeenCalled();
});
it('handles empty input', async () => {
const results = await GetInvGroups([]);
expect(results.size).toBe(0);
expect(fetchSpy).not.toHaveBeenCalled();
});
});