Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

test(ui): cover api keys, taxonomy, and starters store

Josh Corbett3bd5b167d970
diff --git a/invokeai/frontend/webv2/src/features/models/core/taxonomy.test.ts b/invokeai/frontend/webv2/src/features/models/core/taxonomy.test.ts
new file mode 100644
index 0000000000..d876e91432
--- /dev/null
+++ b/invokeai/frontend/webv2/src/features/models/core/taxonomy.test.ts
@@ -0,0 +1,49 @@
+import { describe, expect, it } from 'vitest';
+
+import { formatBytes, getInstallSourceLabel, getModelFormatLabel, getModelTypeLabel } from './taxonomy';
+
+describe('formatBytes', () => {
+ it('renders a dash for missing or invalid sizes', () => {
+ expect(formatBytes(null)).toBe('—');
+ expect(formatBytes(undefined)).toBe('—');
+ expect(formatBytes(-1)).toBe('—');
+ expect(formatBytes(Number.NaN)).toBe('—');
+ });
+
+ it('scales across unit boundaries', () => {
+ expect(formatBytes(0)).toBe('0 B');
+ expect(formatBytes(1023)).toBe('1023 B');
+ expect(formatBytes(1024)).toBe('1.0 KB');
+ expect(formatBytes(1024 * 1024)).toBe('1.0 MB');
+ expect(formatBytes(1.5 * 1024 * 1024 * 1024)).toBe('1.5 GB');
+ });
+});
+
+describe('getInstallSourceLabel', () => {
+ it('passes strings through and extracts structured source fields in order', () => {
+ expect(getInstallSourceLabel('https://example.com/model.safetensors')).toBe(
+ 'https://example.com/model.safetensors'
+ );
+ expect(getInstallSourceLabel({ repo_id: 'owner/repo', url: 'https://x' })).toBe('owner/repo');
+ expect(getInstallSourceLabel({ url: 'https://x' })).toBe('https://x');
+ expect(getInstallSourceLabel({ path: '/models/x' })).toBe('/models/x');
+ });
+
+ it('falls back to a generic label for unrecognized payloads', () => {
+ expect(getInstallSourceLabel({ something: 'else' })).toBe('model');
+ expect(getInstallSourceLabel(undefined)).toBe('model');
+ expect(getInstallSourceLabel(42)).toBe('model');
+ });
+});
+
+describe('label fallbacks', () => {
+ it('title-cases unknown open-union values', () => {
+ expect(getModelTypeLabel('mistral_encoder')).toBe('Mistral Encoder');
+ expect(getModelFormatLabel('some_new-format')).toBe('Some New Format');
+ });
+
+ it('prefers curated labels for known values', () => {
+ expect(getModelTypeLabel('lora')).toBe('LoRA');
+ expect(getModelFormatLabel('gguf_quantized')).toBe('GGUF');
+ });
+});
diff --git a/invokeai/frontend/webv2/src/features/models/data/apiKeys.test.ts b/invokeai/frontend/webv2/src/features/models/data/apiKeys.test.ts
new file mode 100644
index 0000000000..093cdf833b
--- /dev/null
+++ b/invokeai/frontend/webv2/src/features/models/data/apiKeys.test.ts
@@ -0,0 +1,90 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+
+const identity = vi.hoisted(() => ({ getUserStorageScope: vi.fn(() => ':user:a') }));
+
+vi.mock('@features/identity', () => identity);
+
+import {
+ clearCivitaiApiKey,
+ getAccessTokenForSource,
+ getCivitaiApiKey,
+ isCivitaiUrl,
+ setCivitaiApiKey,
+} from './apiKeys';
+
+const storage = new Map<string, string>();
+
+beforeEach(() => {
+ identity.getUserStorageScope.mockReturnValue(':user:a');
+ vi.stubGlobal('window', {
+ localStorage: {
+ getItem: (key: string) => storage.get(key) ?? null,
+ removeItem: (key: string) => storage.delete(key),
+ setItem: (key: string, value: string) => storage.set(key, value),
+ },
+ });
+});
+
+afterEach(() => {
+ storage.clear();
+ vi.unstubAllGlobals();
+});
+
+describe('isCivitaiUrl', () => {
+ it('matches the civitai host and its subdomains only', () => {
+ expect(isCivitaiUrl('https://civitai.com/models/1')).toBe(true);
+ expect(isCivitaiUrl('https://api.civitai.com/download')).toBe(true);
+ expect(isCivitaiUrl('https://not-civitai.com/models/1')).toBe(false);
+ expect(isCivitaiUrl('https://civitai.com.evil.example/models/1')).toBe(false);
+ });
+
+ it('rejects malformed URLs and plain paths', () => {
+ expect(isCivitaiUrl('civitai.com/models/1')).toBe(false);
+ expect(isCivitaiUrl('/models/file.safetensors')).toBe(false);
+ expect(isCivitaiUrl('')).toBe(false);
+ });
+});
+
+describe('civitai key storage', () => {
+ it('stores, reads, and clears the key per user scope', () => {
+ expect(getCivitaiApiKey()).toBeNull();
+
+ setCivitaiApiKey('key-a');
+ expect(getCivitaiApiKey()).toBe('key-a');
+
+ identity.getUserStorageScope.mockReturnValue(':user:b');
+ expect(getCivitaiApiKey()).toBeNull();
+
+ identity.getUserStorageScope.mockReturnValue(':user:a');
+ clearCivitaiApiKey();
+ expect(getCivitaiApiKey()).toBeNull();
+ });
+
+ it('returns null instead of throwing when storage is unavailable', () => {
+ vi.stubGlobal('window', {
+ localStorage: {
+ getItem: () => {
+ throw new Error('denied');
+ },
+ setItem: () => {
+ throw new Error('denied');
+ },
+ },
+ });
+
+ expect(getCivitaiApiKey()).toBeNull();
+ expect(() => setCivitaiApiKey('key')).not.toThrow();
+ });
+});
+
+describe('getAccessTokenForSource', () => {
+ it('returns the saved key only for civitai URLs', () => {
+ setCivitaiApiKey('key-a');
+ expect(getAccessTokenForSource('https://civitai.com/api/download/models/1')).toBe('key-a');
+ expect(getAccessTokenForSource('https://huggingface.co/owner/repo')).toBeUndefined();
+ });
+
+ it('returns undefined when no key is saved', () => {
+ expect(getAccessTokenForSource('https://civitai.com/api/download/models/1')).toBeUndefined();
+ });
+});
diff --git a/invokeai/frontend/webv2/src/features/models/data/startersStore.test.ts b/invokeai/frontend/webv2/src/features/models/data/startersStore.test.ts
new file mode 100644
index 0000000000..6e3bda9ea0
--- /dev/null
+++ b/invokeai/frontend/webv2/src/features/models/data/startersStore.test.ts
@@ -0,0 +1,56 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+const api = vi.hoisted(() => ({ getStarterModels: vi.fn() }));
+
+vi.mock('./api', () => api);
+
+const response = { starter_bundles: {}, starter_models: [] };
+
+describe('starters store', () => {
+ beforeEach(() => {
+ vi.resetModules();
+ api.getStarterModels.mockReset();
+ });
+
+ it('dedupes concurrent refreshes and keeps the catalog on later failures', async () => {
+ api.getStarterModels.mockResolvedValueOnce(response).mockRejectedValueOnce(new Error('outage'));
+ const store = await import('./startersStore');
+
+ const first = store.refreshStarters();
+ expect(store.refreshStarters()).toBe(first);
+ await first;
+ expect(store.getStartersSnapshot()).toMatchObject({ error: null, response, status: 'loaded' });
+
+ await store.refreshStarters();
+ expect(store.getStartersSnapshot()).toMatchObject({ error: 'outage', response, status: 'loaded' });
+ expect(api.getStarterModels).toHaveBeenCalledTimes(2);
+ });
+
+ it('ensures once and revalidates only when already loaded', async () => {
+ api.getStarterModels.mockResolvedValue(response);
+ const store = await import('./startersStore');
+
+ store.refreshStartersIfLoaded();
+ expect(api.getStarterModels).not.toHaveBeenCalled();
+
+ store.ensureStartersLoaded();
+ store.ensureStartersLoaded();
+ await Promise.resolve();
+ expect(api.getStarterModels).toHaveBeenCalledTimes(1);
+ });
+
+ it('clears the catalog on account switch', async () => {
+ const account = await import('@platform/state/accountLifecycle');
+ account.accountLifecycle.activate('user-a');
+ api.getStarterModels.mockResolvedValue(response);
+ const store = await import('./startersStore');
+
+ await store.refreshStarters();
+ expect(store.getStartersSnapshot().status).toBe('loaded');
+
+ account.accountLifecycle.invalidate();
+ account.accountLifecycle.activate('user-b');
+
+ expect(store.getStartersSnapshot()).toMatchObject({ response: null, status: 'idle' });
+ });
+});
diff --git a/invokeai/frontend/webv2/src/features/models/data/startersStore.ts b/invokeai/frontend/webv2/src/features/models/data/startersStore.ts
index 061ea702c4..26bed296dc 100644
--- a/invokeai/frontend/webv2/src/features/models/data/startersStore.ts
+++ b/invokeai/frontend/webv2/src/features/models/data/startersStore.ts
@@ -83,6 +83,8 @@ export const refreshStartersIfLoaded = (): void => {
}
};
+export const getStartersSnapshot = (): StartersSnapshot => store.getSnapshot();
+
export const useStartersSelector = store.useSelector;
export const useStartersSnapshot = (): StartersSnapshot => store.useSnapshot();